Skip to content

Commit 0877b96

Browse files
committed
[clang][ASTImporter] Fix a possible assertion failure `NeedsInjectedClassNameType(Decl)'.
The assertion can happen if ASTImporter imports a CXXRecordDecl in a template and then imports another redeclaration of this declaration, while the first import is in progress. The process of first import did not set the "described template" yet and the second import finds the first declaration at setting the injected types. Setting the injected type requires in the assertion that the described template is set. The exact assertion was: clang/lib/AST/ASTContext.cpp:4411: clang::QualType clang::ASTContext::getInjectedClassNameType(clang::CXXRecordDecl*, clang::QualType) const: Assertion `NeedsInjectedClassNameType(Decl)' failed. Reviewed By: shafik Differential Revision: https://reviews.llvm.org/D94067
1 parent 67a4c67 commit 0877b96

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5393,16 +5393,16 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
53935393

53945394
CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
53955395

5396+
auto TemplateParamsOrErr = import(D->getTemplateParameters());
5397+
if (!TemplateParamsOrErr)
5398+
return TemplateParamsOrErr.takeError();
5399+
53965400
// Create the declaration that is being templated.
53975401
CXXRecordDecl *ToTemplated;
53985402
if (Error Err = importInto(ToTemplated, FromTemplated))
53995403
return std::move(Err);
54005404

54015405
// Create the class template declaration itself.
5402-
auto TemplateParamsOrErr = import(D->getTemplateParameters());
5403-
if (!TemplateParamsOrErr)
5404-
return TemplateParamsOrErr.takeError();
5405-
54065406
ClassTemplateDecl *D2;
54075407
if (GetImportedOrCreateDecl(D2, D, Importer.getToContext(), DC, Loc, Name,
54085408
*TemplateParamsOrErr, ToTemplated))

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,6 +6141,41 @@ TEST_P(ASTImporterOptionSpecificTestBase, TypedefWithAttribute) {
61416141
EXPECT_EQ(ToAttr->getAnnotation(), "A");
61426142
}
61436143

6144+
TEST_P(ASTImporterOptionSpecificTestBase,
6145+
ImportOfTemplatedDeclWhenPreviousDeclHasNoDescribedTemplateSet) {
6146+
Decl *FromTU = getTuDecl(
6147+
R"(
6148+
6149+
namespace std {
6150+
template<typename T>
6151+
class basic_stringbuf;
6152+
}
6153+
namespace std {
6154+
class char_traits;
6155+
template<typename T = char_traits>
6156+
class basic_stringbuf;
6157+
}
6158+
namespace std {
6159+
template<typename T>
6160+
class basic_stringbuf {};
6161+
}
6162+
6163+
)",
6164+
Lang_CXX11);
6165+
6166+
auto *From1 = FirstDeclMatcher<ClassTemplateDecl>().match(
6167+
FromTU,
6168+
classTemplateDecl(hasName("basic_stringbuf"), unless(isImplicit())));
6169+
auto *To1 = cast_or_null<ClassTemplateDecl>(Import(From1, Lang_CXX11));
6170+
EXPECT_TRUE(To1);
6171+
6172+
auto *From2 = LastDeclMatcher<ClassTemplateDecl>().match(
6173+
FromTU,
6174+
classTemplateDecl(hasName("basic_stringbuf"), unless(isImplicit())));
6175+
auto *To2 = cast_or_null<ClassTemplateDecl>(Import(From2, Lang_CXX11));
6176+
EXPECT_TRUE(To2);
6177+
}
6178+
61446179
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
61456180
DefaultTestValuesForRunOptions, );
61466181

0 commit comments

Comments
 (0)