Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2638,11 +2638,12 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
if (!Name) {
// This is an anonymous namespace. Adopt an existing anonymous
// namespace if we can.
// FIXME: Not testable.
if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
DeclContext *EnclosingDC = DC->getEnclosingNamespaceContext();
if (auto *TU = dyn_cast<TranslationUnitDecl>(EnclosingDC))
MergeWithNamespace = TU->getAnonymousNamespace();
else
MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
MergeWithNamespace =
cast<NamespaceDecl>(EnclosingDC)->getAnonymousNamespace();
} else {
SmallVector<NamedDecl *, 4> ConflictingDecls;
auto FoundDecls = Importer.findDeclsInToCtx(DC, Name);
Expand Down
76 changes: 76 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10492,6 +10492,79 @@ TEST_P(ASTImporterOptionSpecificTestBase,
EXPECT_EQ(ToFr1Imp, ToFr1);
}

struct ImportAndMergeAnonymousNamespace
: public ASTImporterOptionSpecificTestBase {
protected:
void test(const char *ToCode, const char *FromCode) {
Decl *ToTU = getToTuDecl(ToCode, Lang_CXX11);
Decl *FromTU = getTuDecl(FromCode, Lang_CXX11);
auto *FromNS = FirstDeclMatcher<NamespaceDecl>().match(
FromTU, namespaceDecl(isAnonymous()));
auto *ToNS = FirstDeclMatcher<NamespaceDecl>().match(
ToTU, namespaceDecl(isAnonymous()));
auto *FromF = FirstDeclMatcher<FunctionDecl>().match(
FromTU, functionDecl(hasName("f")));
auto *ImportedF = Import(FromF, Lang_CXX11);
EXPECT_TRUE(ImportedF);
EXPECT_EQ(ImportedF->getDeclContext(), ToNS);
auto *ImportedNS = Import(FromNS, Lang_CXX11);
EXPECT_EQ(ImportedNS, ToNS);
}
};

TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInTU) {
const char *ToCode =
R"(
namespace {
}
)";
const char *FromCode =
R"(
namespace {
void f();
}
)";
test(ToCode, FromCode);
}

TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInLinkageSpec) {
const char *ToCode =
R"(
extern "C" {
namespace {
}
}
)";
const char *FromCode =
R"(
extern "C" {
namespace {
void f();
}
}
)";
test(ToCode, FromCode);
}

TEST_P(ImportAndMergeAnonymousNamespace, NamespaceInNamespace) {
const char *ToCode =
R"(
namespace X {
namespace {
}
}
)";
const char *FromCode =
R"(
namespace X {
namespace {
void f();
}
}
)";
test(ToCode, FromCode);
}

INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions);

Expand Down Expand Up @@ -10578,6 +10651,9 @@ INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportMatrixType,
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportTemplateParmDeclDefaultValue,
DefaultTestValuesForRunOptions);

ISTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportAndMergeAnonymousNamespace,
DefaultTestValuesForRunOptions);

// FIXME: Make ImportOpenCLPipe test work.
// INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportOpenCLPipe,
// DefaultTestValuesForRunOptions);
Expand Down
Loading