Skip to content

Commit b620f43

Browse files
Vipul-CariappaSterling-Augustine
authored andcommitted
[clang] return first Decl for CanonicalDecl in TranslationUnitDecl (llvm#110101)
Return the first `Decl` when using `TranslationUnitDecl::getCanonicalDecl`
1 parent fdb714b commit b620f43

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class TranslationUnitDecl : public Decl,
133133
static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
134134
return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
135135
}
136+
137+
/// Retrieves the canonical declaration of this translation unit.
138+
TranslationUnitDecl *getCanonicalDecl() override { return getFirstDecl(); }
139+
const TranslationUnitDecl *getCanonicalDecl() const { return getFirstDecl(); }
136140
};
137141

138142
/// Represents a `#pragma comment` line. Always a child of

clang/unittests/Interpreter/InterpreterTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,26 @@ TEST_F(InterpreterTest, Value) {
381381
EXPECT_TRUE(V9.isManuallyAlloc());
382382
}
383383

384+
TEST_F(InterpreterTest, TranslationUnit_CanonicalDecl) {
385+
std::vector<const char *> Args;
386+
std::unique_ptr<Interpreter> Interp = createInterpreter(Args);
387+
388+
Sema &sema = Interp->getCompilerInstance()->getSema();
389+
390+
llvm::cantFail(Interp->ParseAndExecute("int x = 42;"));
391+
392+
TranslationUnitDecl *TU =
393+
sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl();
394+
395+
llvm::cantFail(Interp->ParseAndExecute("long y = 84;"));
396+
397+
EXPECT_EQ(TU,
398+
sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
399+
400+
llvm::cantFail(Interp->ParseAndExecute("char z = 'z';"));
401+
402+
EXPECT_EQ(TU,
403+
sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
404+
}
405+
384406
} // end anonymous namespace

0 commit comments

Comments
 (0)