Author: Kadir Cetinkaya Date: 2021-10-25T13:16:14+02:00 New Revision: ffa96f022c3ff03888afca8fdda766fe556eb9c5
URL: https://github.com/llvm/llvm-project/commit/ffa96f022c3ff03888afca8fdda766fe556eb9c5 DIFF: https://github.com/llvm/llvm-project/commit/ffa96f022c3ff03888afca8fdda766fe556eb9c5.diff LOG: [clang] Fix range for forward-declared enums This used to span just the `[[enum foo]] : bar;` in the absence of a body. This patch expands the range to cover the base specifier, so that the various consumers can detect the full range of the decl. Differential Revision: https://reviews.llvm.org/D111259 Added: Modified: clang/include/clang/AST/Decl.h clang/lib/AST/Decl.cpp clang/unittests/AST/DeclTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index b46499203b0bc..85a3a8ab69708 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -3701,6 +3701,10 @@ class EnumDecl : public TagDecl { bool IsFixed); static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); + /// Overrides to provide correct range when there's an enum-base specifier + /// with forward declarations. + SourceRange getSourceRange() const override LLVM_READONLY; + /// When created, the EnumDecl corresponds to a /// forward-declared enum. This method is used to mark the /// declaration as being defined; its enumerators have already been diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 6a837430924f3..32dae5ccad3a9 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -4524,6 +4524,17 @@ unsigned EnumDecl::getODRHash() { return ODRHash; } +SourceRange EnumDecl::getSourceRange() const { + auto Res = TagDecl::getSourceRange(); + // Set end-point to enum-base, e.g. enum foo : ^bar + if (auto *TSI = getIntegerTypeSourceInfo()) { + // TagDecl doesn't know about the enum base. + if (!getBraceRange().getEnd().isValid()) + Res.setEnd(TSI->getTypeLoc().getEndLoc()); + } + return Res; +} + //===----------------------------------------------------------------------===// // RecordDecl Implementation //===----------------------------------------------------------------------===// diff --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp index 0964080c7a258..588ef859a181e 100644 --- a/clang/unittests/AST/DeclTest.cpp +++ b/clang/unittests/AST/DeclTest.cpp @@ -10,14 +10,17 @@ // //===----------------------------------------------------------------------===// +#include "clang/AST/Decl.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Mangle.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/TargetInfo.h" +#include "clang/Lex/Lexer.h" #include "clang/Tooling/Tooling.h" #include "llvm/IR/DataLayout.h" +#include "llvm/Testing/Support/Annotations.h" #include "gtest/gtest.h" using namespace clang::ast_matchers; @@ -138,3 +141,19 @@ TEST(Decl, MangleDependentSizedArray) { ASSERT_TRUE(0 == MangleA.compare("_ZTSA_i")); ASSERT_TRUE(0 == MangleB.compare("_ZTSAT0__T_")); } + +TEST(Decl, EnumDeclRange) { + llvm::Annotations Code(R"( + typedef int Foo; + [[enum Bar : Foo]];)"); + auto AST = tooling::buildASTFromCodeWithArgs(Code.code(), /*Args=*/{}); + ASTContext &Ctx = AST->getASTContext(); + const auto &SM = Ctx.getSourceManager(); + + const auto *Bar = + selectFirst<TagDecl>("Bar", match(enumDecl().bind("Bar"), Ctx)); + auto BarRange = + Lexer::getAsCharRange(Bar->getSourceRange(), SM, Ctx.getLangOpts()); + EXPECT_EQ(SM.getFileOffset(BarRange.getBegin()), Code.range().Begin); + EXPECT_EQ(SM.getFileOffset(BarRange.getEnd()), Code.range().End); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits