aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBalázs Kéri <balazs.keri@ericsson.com>2025-05-06 10:17:26 +0200
committerGitHub <noreply@github.com>2025-05-06 10:17:26 +0200
commit4b30b3f901b00da1bd1f70e9ca9086e349c7cca0 (patch)
tree1cb7d56f4330d32df1a92940ad2e64d090d2b9ab
parent6d85de88c426a40b8e5d3cfcb0ed6564c7df12be (diff)
downloadllvm-4b30b3f901b00da1bd1f70e9ca9086e349c7cca0.zip
llvm-4b30b3f901b00da1bd1f70e9ca9086e349c7cca0.tar.gz
llvm-4b30b3f901b00da1bd1f70e9ca9086e349c7cca0.tar.bz2
[clang][ASTImporter] Fix AST import if anonymous namespaces are merged (#128735)
Fix of a faulty case that is shown in the second of the added tests (an anonymous namespace is imported that resides in a `extern "C"` block).
-rw-r--r--clang/lib/AST/ASTImporter.cpp7
-rw-r--r--clang/unittests/AST/ASTImporterTest.cpp76
2 files changed, 80 insertions, 3 deletions
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index eb0e686..b481ad5 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2628,11 +2628,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);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 4192fae..cddd301e 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -10456,6 +10456,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);
@@ -10542,6 +10615,9 @@ INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportMatrixType,
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportTemplateParmDeclDefaultValue,
DefaultTestValuesForRunOptions);
+INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportAndMergeAnonymousNamespace,
+ DefaultTestValuesForRunOptions);
+
// FIXME: Make ImportOpenCLPipe test work.
// INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ImportOpenCLPipe,
// DefaultTestValuesForRunOptions);