Skip to content

Commit

Permalink
[clang] Fix range for forward-declared enums
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kadircet committed Oct 25, 2021
1 parent 025f6ca commit ffa96f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Decl.h
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/AST/Decl.cpp
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down
19 changes: 19 additions & 0 deletions clang/unittests/AST/DeclTest.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit ffa96f0

Please sign in to comment.