Skip to content

Commit 3fae6f2

Browse files
committed
[clang][ASTImporter] Fix AST import if anonymous namespaces are merged
1 parent f10e0f7 commit 3fae6f2

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,11 +2638,12 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
26382638
if (!Name) {
26392639
// This is an anonymous namespace. Adopt an existing anonymous
26402640
// namespace if we can.
2641-
// FIXME: Not testable.
2642-
if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
2641+
DeclContext *EnclosingDC = DC->getEnclosingNamespaceContext();
2642+
if (auto *TU = dyn_cast<TranslationUnitDecl>(EnclosingDC))
26432643
MergeWithNamespace = TU->getAnonymousNamespace();
26442644
else
2645-
MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2645+
MergeWithNamespace =
2646+
cast<NamespaceDecl>(EnclosingDC)->getAnonymousNamespace();
26462647
} else {
26472648
SmallVector<NamedDecl *, 4> ConflictingDecls;
26482649
auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10492,6 +10492,79 @@ TEST_P(ASTImporterOptionSpecificTestBase,
1049210492
EXPECT_EQ(ToFr1Imp, ToFr1);
1049310493
}
1049410494

10495+
struct ImportAndMergeAnonymousNamespace
10496+
: public ASTImporterOptionSpecificTestBase {
10497+
protected:
10498+
void test(const char *ToCode, const char *FromCode) {
10499+
Decl *ToTU = getToTuDecl(ToCode, Lang_CXX11);
10500+
Decl *FromTU = getTuDecl(FromCode, Lang_CXX11);
10501+
auto *FromNS = FirstDeclMatcher<NamespaceDecl>().match(
10502+
FromTU, namespaceDecl(isAnonymous()));
10503+
auto *ToNS = FirstDeclMatcher<NamespaceDecl>().match(
10504+
ToTU, namespaceDecl(isAnonymous()));
10505+
auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
10506+
FromTU, functionDecl(hasName("f")));
10507+
auto *ImportedF = Import(FromF, Lang_CXX11);
10508+
EXPECT_TRUE(ImportedF);
10509+
EXPECT_EQ(ImportedF->getDeclContext(), ToNS);
10510+
auto *ImportedNS = Import(FromNS, Lang_CXX11);
10511+
EXPECT_EQ(ImportedNS, ToNS);
10512+
}
10513+
};
10514+
10515+
TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInTU) {
10516+
const char *ToCode =
10517+
R"(
10518+
namespace {
10519+
}
10520+
)";
10521+
const char *FromCode =
10522+
R"(
10523+
namespace {
10524+
void f();
10525+
}
10526+
)";
10527+
test(ToCode, FromCode);
10528+
}
10529+
10530+
TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInLinkageSpec) {
10531+
const char *ToCode =
10532+
R"(
10533+
extern "C" {
10534+
namespace {
10535+
}
10536+
}
10537+
)";
10538+
const char *FromCode =
10539+
R"(
10540+
extern "C" {
10541+
namespace {
10542+
void f();
10543+
}
10544+
}
10545+
)";
10546+
test(ToCode, FromCode);
10547+
}
10548+
10549+
TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInNamespace) {
10550+
const char *ToCode =
10551+
R"(
10552+
namespace X {
10553+
namespace {
10554+
}
10555+
}
10556+
)";
10557+
const char *FromCode =
10558+
R"(
10559+
namespace X {
10560+
namespace {
10561+
void f();
10562+
}
10563+
}
10564+
)";
10565+
test(ToCode, FromCode);
10566+
}
10567+
1049510568
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
1049610569
DefaultTestValuesForRunOptions);
1049710570

@@ -10578,6 +10651,9 @@ INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportMatrixType,
1057810651
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportTemplateParmDeclDefaultValue,
1057910652
DefaultTestValuesForRunOptions);
1058010653

10654+
ISTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportAndMergeAnonymousNamespace,
10655+
DefaultTestValuesForRunOptions);
10656+
1058110657
// FIXME: Make ImportOpenCLPipe test work.
1058210658
// INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportOpenCLPipe,
1058310659
// DefaultTestValuesForRunOptions);

0 commit comments

Comments
 (0)