[clang] [clang] return first Decl for CanonicalDecl in TranslationUnitDecl (PR #110101)
Vipul Cariappa via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 26 21:45:08 PDT 2024
https://github.com/Vipul-Cariappa updated https://github.com/llvm/llvm-project/pull/110101
>From 43d0e353e6e5870cf601ea83d072d8c05dbe94eb Mon Sep 17 00:00:00 2001
From: Vipul Cariappa <vipulcariappa at gmail.com>
Date: Thu, 26 Sep 2024 10:28:02 +0000
Subject: [PATCH 1/2] [clang] return first Decl for CanonicalDecl in
TranslationUnitDecl
---
clang/include/clang/AST/Decl.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 0600ecc4d14a18..7ff35d73df5997 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -133,6 +133,10 @@ class TranslationUnitDecl : public Decl,
static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
}
+
+ /// Retrieves the canonical declaration of this translation unit.
+ TranslationUnitDecl *getCanonicalDecl() override { return getFirstDecl(); }
+ const TranslationUnitDecl *getCanonicalDecl() const { return getFirstDecl(); }
};
/// Represents a `#pragma comment` line. Always a child of
>From bdb96340fb8cce8342d2cc74325dad89a224f572 Mon Sep 17 00:00:00 2001
From: Vipul Cariappa <vipulcariappa at gmail.com>
Date: Fri, 27 Sep 2024 04:44:32 +0000
Subject: [PATCH 2/2] add test for `TranslationUnitDecl::getCanonicalDecl`
---
.../unittests/Interpreter/InterpreterTest.cpp | 22 +++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index a2e960f1431117..30b051e747f928 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -381,4 +381,26 @@ TEST_F(InterpreterTest, Value) {
EXPECT_TRUE(V9.isManuallyAlloc());
}
+TEST_F(InterpreterTest, TranslationUnit_CanonicalDecl) {
+ std::vector<const char *> Args;
+ std::unique_ptr<Interpreter> Interp = createInterpreter(Args);
+
+ Sema &sema = Interp->getCompilerInstance()->getSema();
+
+ llvm::cantFail(Interp->ParseAndExecute("int x = 42;"));
+
+ TranslationUnitDecl *TU =
+ sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl();
+
+ llvm::cantFail(Interp->ParseAndExecute("long y = 84;"));
+
+ EXPECT_EQ(TU,
+ sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
+
+ llvm::cantFail(Interp->ParseAndExecute("char z = 'z';"));
+
+ EXPECT_EQ(TU,
+ sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
+}
+
} // end anonymous namespace
More information about the cfe-commits
mailing list