aboutsummaryrefslogtreecommitdiff
path: root/clang
diff options
context:
space:
mode:
authorVipul Cariappa <vipulcariappa@gmail.com>2024-09-27 12:33:32 +0530
committerGitHub <noreply@github.com>2024-09-27 10:03:32 +0300
commit61c8b7159a740d43a6a0fa52756eb479e1a9c1c3 (patch)
treeb08cdd1127842f249d6ab86aed392d1228078b97 /clang
parentff8a9921ec9425e31aa1da273c2e4836f9e4069e (diff)
downloadllvm-61c8b7159a740d43a6a0fa52756eb479e1a9c1c3.zip
llvm-61c8b7159a740d43a6a0fa52756eb479e1a9c1c3.tar.gz
llvm-61c8b7159a740d43a6a0fa52756eb479e1a9c1c3.tar.bz2
[clang] return first Decl for CanonicalDecl in TranslationUnitDecl (#110101)
Return the first `Decl` when using `TranslationUnitDecl::getCanonicalDecl`
Diffstat (limited to 'clang')
-rw-r--r--clang/include/clang/AST/Decl.h4
-rw-r--r--clang/unittests/Interpreter/InterpreterTest.cpp22
2 files changed, 26 insertions, 0 deletions
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 0600ecc..7ff35d7 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -133,6 +133,10 @@ public:
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
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index a2e960f..30b051e 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