diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index c662301da344..d68924c7c049 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -243,7 +243,10 @@ public: /// The primitive diagnostic helpers. DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) { DiagnosticBuilder DB = Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID); - if (!Diags.isBuiltinNote(DiagID) && !ActiveTemplateInstantiations.empty()) + if (!Diags.isBuiltinNote(DiagID) && + !ActiveTemplateInstantiations.empty() && + ActiveTemplateInstantiations.back().Entity + != LastTemplateInstantiationErrorContext) DB << PostDiagnosticHook(PrintInstantiationStackHook, this); return DB; } @@ -1691,6 +1694,15 @@ public: llvm::SmallVector ActiveTemplateInstantiations; + /// \brief The last template from which a template instantiation + /// error or warning was produced. + /// + /// This value is used to suppress printing of redundant template + /// instantiation backtraces when there are multiple errors in the + /// same instantiation. FIXME: Does this belong in Sema? It's tough + /// to implement it anywhere else. + ClassTemplateSpecializationDecl *LastTemplateInstantiationErrorContext; + /// \brief A stack object to be created when performing template /// instantiation. /// diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 4a12b077666e..057b256ddd4a 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -56,7 +56,10 @@ Sema::InstantiatingTemplate::~InstantiatingTemplate() { /// \brief Post-diagnostic hook for printing the instantiation stack. void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) { - static_cast(Cookie)->PrintInstantiationStack(); + Sema &SemaRef = *static_cast(Cookie); + SemaRef.PrintInstantiationStack(); + SemaRef.LastTemplateInstantiationErrorContext + = SemaRef.ActiveTemplateInstantiations.back().Entity; } /// \brief Prints the current instantiation stack through a series of @@ -503,7 +506,7 @@ Sema::InstantiateBaseSpecifiers( for (ClassTemplateSpecializationDecl::base_class_iterator Base = ClassTemplate->getTemplatedDecl()->bases_begin(), BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end(); - Base != BaseEnd && !Invalid; ++Base) { + Base != BaseEnd; ++Base) { if (!Base->getType()->isDependentType()) { // FIXME: Allocate via ASTContext InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); @@ -533,7 +536,8 @@ Sema::InstantiateBaseSpecifiers( Invalid = true; } - if (AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], + if (!Invalid && + AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], InstantiatedBases.size())) Invalid = true; diff --git a/clang/test/SemaTemplate/instantiation-backtrace.cpp b/clang/test/SemaTemplate/instantiation-backtrace.cpp index f8aabff0973f..10c2de038148 100644 --- a/clang/test/SemaTemplate/instantiation-backtrace.cpp +++ b/clang/test/SemaTemplate/instantiation-backtrace.cpp @@ -1,5 +1,5 @@ // RUN: clang -fsyntax-only -verify %s -template struct A; // expected-note 2{{template is declared here}} +template struct A; // expected-note 4{{template is declared here}} template struct B : A { }; // expected-error{{implicit instantiation of undefined template}} \ // expected-error{{implicit instantiation of undefined template 'struct A'}} @@ -21,3 +21,12 @@ typedef struct { } X; void g() { (void)sizeof(B); // expected-note{{in instantiation of template class 'struct B' requested here}} } + +template +struct G : A, // expected-error{{implicit instantiation of undefined template 'struct A'}} + A // expected-error{{implicit instantiation of undefined template 'struct A'}} + { }; + +void h() { + (void)sizeof(G); // expected-note{{in instantiation of template class 'struct G' requested here}} +}