Skip to content

Commit 9b99a30

Browse files
authored
[clang][ASTImporter] skip TemplateTypeParmDecl in VisitTypeAliasTemplateDecl (#74919)
Skip checking `TemplateTypeParmDecl ` in `VisitTypeAliasTemplateDecl`. [Fix this crash](llvm/llvm-project#74765) Co-authored-by: huqizhi <[email protected]>
1 parent 34727b0 commit 9b99a30

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,9 +2771,11 @@ ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
27712771
for (auto *FoundDecl : FoundDecls) {
27722772
if (!FoundDecl->isInIdentifierNamespace(IDNS))
27732773
continue;
2774-
if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
2775-
return Importer.MapImported(D, FoundAlias);
2776-
ConflictingDecls.push_back(FoundDecl);
2774+
if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl)) {
2775+
if (IsStructuralMatch(D, FoundAlias))
2776+
return Importer.MapImported(D, FoundAlias);
2777+
ConflictingDecls.push_back(FoundDecl);
2778+
}
27772779
}
27782780

27792781
if (!ConflictingDecls.empty()) {
@@ -9402,7 +9404,6 @@ Expected<Decl *> ASTImporter::Import(Decl *FromD) {
94029404
setImportDeclError(FromD, *Error);
94039405
return make_error<ASTImportError>(*Error);
94049406
}
9405-
94069407
// Make sure that ImportImpl registered the imported decl.
94079408
assert(ImportedDecls.count(FromD) != 0 && "Missing call to MapImported?");
94089409
if (auto Error = ImportAttrs(ToD, FromD))

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,18 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19771977
D2->getTemplatedDecl()->getType());
19781978
}
19791979

1980+
static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1981+
TypeAliasTemplateDecl *D1,
1982+
TypeAliasTemplateDecl *D2) {
1983+
// Check template parameters.
1984+
if (!IsTemplateDeclCommonStructurallyEquivalent(Context, D1, D2))
1985+
return false;
1986+
1987+
// Check the templated declaration.
1988+
return IsStructurallyEquivalent(Context, D1->getTemplatedDecl(),
1989+
D2->getTemplatedDecl());
1990+
}
1991+
19801992
static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
19811993
ConceptDecl *D1,
19821994
ConceptDecl *D2) {

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9295,6 +9295,53 @@ TEST_P(ASTImporterOptionSpecificTestBase,
92959295
// EXPECT_EQ(ToF1Imported->getPreviousDecl(), ToF1);
92969296
}
92979297

9298+
TEST_P(ASTImporterOptionSpecificTestBase,
9299+
ImportTypeAliasTemplateAfterSimilarCalledTemplateTypeParm) {
9300+
const char *Code =
9301+
R"(
9302+
struct S;
9303+
template <typename>
9304+
using Callable = S;
9305+
template <typename Callable>
9306+
int bindingFunctionVTable;
9307+
)";
9308+
Decl *FromTU = getTuDecl(Code, Lang_CXX17);
9309+
9310+
auto *FromCallable = FirstDeclMatcher<TypeAliasTemplateDecl>().match(
9311+
FromTU, typeAliasTemplateDecl(hasName("Callable")));
9312+
9313+
auto *FromCallableParm = FirstDeclMatcher<TemplateTypeParmDecl>().match(
9314+
FromTU, templateTypeParmDecl(hasName("Callable")));
9315+
9316+
auto *ToFromCallableParm = Import(FromCallableParm, Lang_CXX17);
9317+
auto *ToCallable = Import(FromCallable, Lang_CXX17);
9318+
EXPECT_TRUE(ToFromCallableParm);
9319+
EXPECT_TRUE(ToCallable);
9320+
}
9321+
9322+
TEST_P(ASTImporterOptionSpecificTestBase, ImportConflictTypeAliasTemplate) {
9323+
const char *ToCode =
9324+
R"(
9325+
struct S;
9326+
template <typename, typename>
9327+
using Callable = S;
9328+
)";
9329+
const char *Code =
9330+
R"(
9331+
struct S;
9332+
template <typename>
9333+
using Callable = S;
9334+
)";
9335+
(void)getToTuDecl(ToCode, Lang_CXX17);
9336+
Decl *FromTU = getTuDecl(Code, Lang_CXX17);
9337+
9338+
auto *FromCallable = FirstDeclMatcher<TypeAliasTemplateDecl>().match(
9339+
FromTU, typeAliasTemplateDecl(hasName("Callable")));
9340+
9341+
auto *ImportedCallable = Import(FromCallable, Lang_CXX17);
9342+
EXPECT_FALSE(ImportedCallable);
9343+
}
9344+
92989345
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
92999346
DefaultTestValuesForRunOptions);
93009347

0 commit comments

Comments
 (0)