Remove the existing `Rng` field.
From the review comment: https://reviews.llvm.org/D147034
Reviewed By: kadircet
Differential Revision: https://reviews.llvm.org/D153259
#include "CollectMacros.h"
#include "AST.h"
+#include "Protocol.h"
+#include "SourceCode.h"
#include "clang/Basic/SourceLocation.h"
+#include "clang/Tooling/Syntax/Tokens.h"
#include "llvm/ADT/STLExtras.h"
+#include <cstddef>
namespace clang {
namespace clangd {
+Range MacroOccurrence::toRange(const SourceManager &SM) const {
+ auto MainFile = SM.getMainFileID();
+ return halfOpenToRange(
+ SM, syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM));
+}
+
void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,
bool IsDefinition, bool InIfCondition) {
if (!InMainFile)
auto Name = MacroNameTok.getIdentifierInfo()->getName();
Out.Names.insert(Name);
- auto Range = halfOpenToRange(
- SM, CharSourceRange::getCharRange(Loc, MacroNameTok.getEndLoc()));
+ size_t Start = SM.getFileOffset(Loc);
+ size_t End = SM.getFileOffset(MacroNameTok.getEndLoc());
if (auto SID = getSymbolID(Name, MI, SM))
- Out.MacroRefs[SID].push_back({Range, IsDefinition, InIfCondition});
+ Out.MacroRefs[SID].push_back({Start, End, IsDefinition, InIfCondition});
else
- Out.UnknownMacros.push_back({Range, IsDefinition, InIfCondition});
+ Out.UnknownMacros.push_back({Start, End, IsDefinition, InIfCondition});
}
void CollectMainFileMacros::FileChanged(SourceLocation Loc, FileChangeReason,
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/DenseMap.h"
+#include <cstddef>
#include <string>
namespace clang {
namespace clangd {
struct MacroOccurrence {
- // Instead of storing SourceLocation, we have to store the token range because
- // SourceManager from preamble is not available when we build the AST.
- Range Rng;
+ // Half-open range (end offset is exclusive) inside the main file.
+ size_t StartOffset;
+ size_t EndOffset;
+
bool IsDefinition;
// True if the occurence is used in a conditional directive, e.g. #ifdef MACRO
bool InConditionalDirective;
+
+ Range toRange(const SourceManager &SM) const;
};
struct MainFileMacros {
AST.getHeuristicResolver());
// Add highlightings for macro references.
auto AddMacro = [&](const MacroOccurrence &M) {
- auto &T = Builder.addToken(M.Rng, HighlightingKind::Macro);
+ auto &T = Builder.addToken(M.toRange(C.getSourceManager()),
+ HighlightingKind::Macro);
T.addModifier(HighlightingModifier::GlobalScope);
if (M.IsDefinition)
T.addModifier(HighlightingModifier::Declaration);
if (Refs != IDToRefs.end()) {
for (const auto &Ref : Refs->second) {
ReferencesResult::Reference Result;
- Result.Loc.range = Ref.Rng;
+ Result.Loc.range = Ref.toRange(SM);
Result.Loc.uri = URIMainFile;
if (Ref.IsDefinition) {
Result.Attributes |= ReferencesResult::Declaration;
// Add macro references.
for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
for (const auto &MacroRef : IDToRefs.second) {
- const auto &Range = MacroRef.Rng;
+ const auto &Range = MacroRef.toRange(SM);
bool IsDefinition = MacroRef.IsDefinition;
Ref R;
R.Location.Start.setLine(Range.start.line);
using testing::UnorderedElementsAreArray;
-MATCHER_P(rangeIs, R, "") { return arg.Rng == R; }
+MATCHER_P(rangeIs, R, "") {
+ return arg.StartOffset == R.Begin && arg.EndOffset == R.End;
+}
MATCHER(isDef, "") { return arg.IsDefinition; }
MATCHER(inConditionalDirective, "") { return arg.InConditionalDirective; }
#define $2(def)[[FOO]] $3[[BAR]]
int A = $2[[FOO]];
)cpp"};
- auto ExpectedResults = [](const Annotations &T, StringRef Name) {
+ auto ExpectedResults = [](const llvm::Annotations &T, StringRef Name) {
std::vector<Matcher<MacroOccurrence>> ExpectedLocations;
for (const auto &[R, Bits] : T.rangesWithPayload(Name)) {
if (Bits == "def")
};
for (const char *Test : Tests) {
- Annotations T(Test);
+ llvm::Annotations T(Test);
auto Inputs = TestTU::withCode(T.code());
Inputs.ExtraArgs.push_back("-std=c++2b");
auto AST = Inputs.build();
#include "../../clang-tidy/ClangTidyCheck.h"
#include "AST.h"
-#include "Annotations.h"
#include "Compiler.h"
#include "Config.h"
#include "Diagnostics.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Tooling/Syntax/Tokens.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Testing/Annotations/Annotations.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock.h"
}
TEST(ParsedASTTest, CollectsMainFileMacroExpansions) {
- Annotations TestCase(R"cpp(
+ llvm::Annotations TestCase(R"cpp(
#define ^MACRO_ARGS(X, Y) X Y
// - preamble ends
^ID(int A);
int D = DEF;
)cpp";
ParsedAST AST = TU.build();
- std::vector<Position> MacroExpansionPositions;
+ std::vector<size_t> MacroExpansionPositions;
for (const auto &SIDToRefs : AST.getMacros().MacroRefs) {
for (const auto &R : SIDToRefs.second)
- MacroExpansionPositions.push_back(R.Rng.start);
+ MacroExpansionPositions.push_back(R.StartOffset);
}
for (const auto &R : AST.getMacros().UnknownMacros)
- MacroExpansionPositions.push_back(R.Rng.start);
- EXPECT_THAT(MacroExpansionPositions,
- testing::UnorderedElementsAreArray(TestCase.points()));
+ MacroExpansionPositions.push_back(R.StartOffset);
+ EXPECT_THAT(
+ MacroExpansionPositions,
+ testing::UnorderedElementsAreArray(TestCase.points()));
}
MATCHER_P(withFileName, Inc, "") { return arg.FileName == Inc; }
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/Testing/Annotations/Annotations.h"
#include "gmock/gmock.h"
#include "gtest/gtest-matchers.h"
#include "gtest/gtest.h"
for (const auto &Case : Cases) {
SCOPED_TRACE(Case.Contents);
- Annotations Modified(Case.Contents);
+ llvm::Annotations Modified(Case.Contents);
EXPECT_THAT(getPreamblePatch("", Modified.code()),
MatchesRegex(Case.ExpectedPatch));
auto AST = createPatchedAST("", Modified.code());
ASSERT_TRUE(AST);
- std::vector<Range> MacroRefRanges;
+ std::vector<llvm::Annotations::Range> MacroRefRanges;
for (auto &M : AST->getMacros().MacroRefs) {
for (auto &O : M.getSecond())
- MacroRefRanges.push_back(O.Rng);
+ MacroRefRanges.push_back({O.StartOffset, O.EndOffset});
}
EXPECT_THAT(MacroRefRanges, Contains(Modified.range()));
}