Skip to content

Commit c0ee022

Browse files
committed
[clangd] NFC, add getLangOpts helper to ParsedAST
The addition of the helper is split out from https://reviews.llvm.org/D69543 as suggested by Kadir. I also updated the existing uses to use the new API.
1 parent acda2bc commit c0ee022

13 files changed

+35
-39
lines changed

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
312312
const auto &SM = AST.getSourceManager();
313313
SourceLocation Loc =
314314
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
315-
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
316-
auto Range = getTokenRange(SM, AST.getASTContext().getLangOpts(), Loc);
315+
Pos, AST.getSourceManager(), AST.getLangOpts()));
316+
auto Range = getTokenRange(SM, AST.getLangOpts(), Loc);
317317
if (!Range)
318318
return CB(llvm::None); // "rename" is not valid at the position.
319319

clang-tools-extra/clangd/HeaderSourceSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
9797
//
9898
// For each symbol in the original file, we get its target location (decl or
9999
// def) from the index, then award that target file.
100-
bool IsHeader = isHeaderFile(OriginalFile, AST.getASTContext().getLangOpts());
100+
bool IsHeader = isHeaderFile(OriginalFile, AST.getLangOpts());
101101
Index->lookup(Request, [&](const Symbol &Sym) {
102102
if (IsHeader)
103103
AwardTarget(Sym.Definition.FileURI);

clang-tools-extra/clangd/Hover.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
367367
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
368368
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
369369
if (EndLoc.isValid()) {
370-
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM,
371-
AST.getASTContext().getLangOpts());
370+
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, AST.getLangOpts());
372371
bool Invalid;
373372
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
374373
if (!Invalid) {
@@ -391,7 +390,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
391390
const SourceManager &SM = AST.getSourceManager();
392391
llvm::Optional<HoverInfo> HI;
393392
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
394-
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
393+
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
395394

396395
if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
397396
// Find the corresponding decl to populate kind and fetch documentation.
@@ -435,9 +434,8 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
435434
tooling::applyAllReplacements(HI->Definition, Replacements))
436435
HI->Definition = *Formatted;
437436

438-
HI->SymRange =
439-
getTokenRange(AST.getASTContext().getSourceManager(),
440-
AST.getASTContext().getLangOpts(), SourceLocationBeg);
437+
HI->SymRange = getTokenRange(AST.getASTContext().getSourceManager(),
438+
AST.getLangOpts(), SourceLocationBeg);
441439
return HI;
442440
}
443441

clang-tools-extra/clangd/ParsedAST.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class ParsedAST {
7777
return getASTContext().getSourceManager();
7878
}
7979

80+
const LangOptions &getLangOpts() const {
81+
return getASTContext().getLangOpts();
82+
}
83+
8084
/// This function returns top-level decls present in the main file of the AST.
8185
/// The result does not include the decls that come from the preamble.
8286
/// (These should be const, but RecursiveASTVisitor requires Decl*).

clang-tools-extra/clangd/SemanticSelection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ llvm::Expected<std::vector<Range>> getSemanticRanges(ParsedAST &AST,
3030
Position Pos) {
3131
std::vector<Range> Result;
3232
const auto &SM = AST.getSourceManager();
33-
const auto &LangOpts = AST.getASTContext().getLangOpts();
33+
const auto &LangOpts = AST.getLangOpts();
3434

3535
auto FID = SM.getMainFileID();
3636
auto Offset = positionToOffset(SM.getBufferData(FID), Pos);

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
191191

192192
// Macros are simple: there's no declaration/definition distinction.
193193
// As a consequence, there's no need to look them up in the index either.
194-
SourceLocation MaybeMacroLocation =
195-
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
196-
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
194+
SourceLocation MaybeMacroLocation = SM.getMacroArgExpandedLocation(
195+
getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts()));
197196
std::vector<LocatedSymbol> Result;
198197
if (auto M = locateMacroAt(MaybeMacroLocation, AST.getPreprocessor())) {
199198
if (auto Loc = makeLocation(AST.getASTContext(),
@@ -366,17 +365,16 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
366365
auto References = findRefs(
367366
getDeclAtPosition(AST,
368367
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
369-
Pos, SM, AST.getASTContext().getLangOpts())),
368+
Pos, SM, AST.getLangOpts())),
370369
Relations),
371370
AST);
372371

373372
// FIXME: we may get multiple DocumentHighlights with the same location and
374373
// different kinds, deduplicate them.
375374
std::vector<DocumentHighlight> Result;
376375
for (const auto &Ref : References) {
377-
if (auto Range =
378-
getTokenRange(AST.getASTContext().getSourceManager(),
379-
AST.getASTContext().getLangOpts(), Ref.Loc)) {
376+
if (auto Range = getTokenRange(AST.getASTContext().getSourceManager(),
377+
AST.getLangOpts(), Ref.Loc)) {
380378
DocumentHighlight DH;
381379
DH.range = *Range;
382380
if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write))
@@ -404,7 +402,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
404402
return Results;
405403
}
406404
auto Loc = SM.getMacroArgExpandedLocation(
407-
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
405+
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
408406
// TODO: should we handle macros, too?
409407
// We also show references to the targets of using-decls, so we include
410408
// DeclRelation::Underlying.
@@ -424,8 +422,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
424422
}),
425423
MainFileRefs.end());
426424
for (const auto &Ref : MainFileRefs) {
427-
if (auto Range =
428-
getTokenRange(SM, AST.getASTContext().getLangOpts(), Ref.Loc)) {
425+
if (auto Range = getTokenRange(SM, AST.getLangOpts(), Ref.Loc)) {
429426
Location Result;
430427
Result.range = *Range;
431428
Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
@@ -470,7 +467,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
470467
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
471468
const SourceManager &SM = AST.getSourceManager();
472469
auto Loc = SM.getMacroArgExpandedLocation(
473-
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
470+
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
474471

475472
std::vector<SymbolDetails> Results;
476473

@@ -646,7 +643,7 @@ static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
646643
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
647644
const SourceManager &SM = AST.getSourceManager();
648645
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
649-
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
646+
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
650647
DeclRelationSet Relations =
651648
DeclRelation::TemplatePattern | DeclRelation::Underlying;
652649
auto Decls = getDeclAtPosition(AST, SourceLocationBeg, Relations);

clang-tools-extra/clangd/refactor/Rename.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,8 @@ llvm::Expected<FileEdits> rename(const RenameInputs &RInputs) {
397397

398398
return (*Content)->getBuffer().str();
399399
};
400-
SourceLocation SourceLocationBeg =
401-
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
402-
RInputs.Pos, SM, AST.getASTContext().getLangOpts()));
400+
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
401+
getBeginningOfIdentifier(RInputs.Pos, SM, AST.getLangOpts()));
403402
// FIXME: Renaming macros is not supported yet, the macro-handling code should
404403
// be moved to rename tooling library.
405404
if (locateMacroAt(SourceLocationBeg, AST.getPreprocessor()))

clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class DefineOutline : public Tweak {
238238
// FIXME: We might want to consider moving method definitions below class
239239
// definition even if we are inside a source file.
240240
if (!isHeaderFile(Sel.AST.getSourceManager().getFilename(Sel.Cursor),
241-
Sel.AST.getASTContext().getLangOpts()))
241+
Sel.AST.getLangOpts()))
242242
return false;
243243

244244
Source = getSelectedFunction(Sel.ASTSelection.commonAncestor());
@@ -306,9 +306,8 @@ class DefineOutline : public Tweak {
306306
// FIXME: We should also get rid of inline qualifier.
307307
const tooling::Replacement DeleteFuncBody(
308308
Sel.AST.getSourceManager(),
309-
CharSourceRange::getTokenRange(
310-
*toHalfOpenFileRange(SM, Sel.AST.getASTContext().getLangOpts(),
311-
Source->getBody()->getSourceRange())),
309+
CharSourceRange::getTokenRange(*toHalfOpenFileRange(
310+
SM, Sel.AST.getLangOpts(), Source->getBody()->getSourceRange())),
312311
";");
313312
auto HeaderFE = Effect::fileEdit(SM, SM.getMainFileID(),
314313
tooling::Replacements(DeleteFuncBody));

clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ tooling::Replacement createFunctionDefinition(const NewFunction &ExtractedFunc,
645645
bool ExtractFunction::prepare(const Selection &Inputs) {
646646
const Node *CommonAnc = Inputs.ASTSelection.commonAncestor();
647647
const SourceManager &SM = Inputs.AST.getSourceManager();
648-
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
648+
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
649649
if (auto MaybeExtZone = findExtractionZone(CommonAnc, SM, LangOpts)) {
650650
ExtZone = std::move(*MaybeExtZone);
651651
return true;
@@ -655,7 +655,7 @@ bool ExtractFunction::prepare(const Selection &Inputs) {
655655

656656
Expected<Tweak::Effect> ExtractFunction::apply(const Selection &Inputs) {
657657
const SourceManager &SM = Inputs.AST.getSourceManager();
658-
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
658+
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
659659
auto ExtractedFunc = getExtractedFunction(ExtZone, SM, LangOpts);
660660
// FIXME: Add more types of errors.
661661
if (!ExtractedFunc)

clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Expected<Tweak::Effect> RawStringLiteral::apply(const Selection &Inputs) {
9191
auto &SM = Inputs.AST.getSourceManager();
9292
auto Reps = tooling::Replacements(
9393
tooling::Replacement(SM, Str, ("R\"(" + Str->getBytes() + ")\"").str(),
94-
Inputs.AST.getASTContext().getLangOpts()));
94+
Inputs.AST.getLangOpts()));
9595
return Effect::mainFileEdit(SM, std::move(Reps));
9696
}
9797

clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ TEST(CollectMainFileMacros, SelectedMacros) {
8888
break;
8989

9090
auto Loc = getBeginningOfIdentifier(ExpectedRefs.begin()->start, SM,
91-
AST.getASTContext().getLangOpts());
91+
AST.getLangOpts());
9292
auto Macro = locateMacroAt(Loc, PP);
9393
assert(Macro);
9494
auto SID = getSymbolID(Macro->Name, Macro->Info, SM);

clang-tools-extra/clangd/unittests/SelectionTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Range nodeRange(const SelectionTree::Node *N, ParsedAST &AST) {
4040
if (!N)
4141
return Range{};
4242
const SourceManager &SM = AST.getSourceManager();
43-
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
43+
const LangOptions &LangOpts = AST.getLangOpts();
4444
StringRef Buffer = SM.getBufferData(SM.getMainFileID());
4545
if (llvm::isa_and_nonnull<TranslationUnitDecl>(N->ASTNode.get<Decl>()))
4646
return Range{Position{}, offsetToPosition(Buffer, Buffer.size())};

clang-tools-extra/clangd/unittests/SourceCodeTests.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ Bar* bar;
358358
auto AST = TestTU::withCode(TestCase.code()).build();
359359
const auto &SourceMgr = AST.getSourceManager();
360360
SourceLocation Actual = getBeginningOfIdentifier(
361-
TestCase.points().back(), SourceMgr, AST.getASTContext().getLangOpts());
361+
TestCase.points().back(), SourceMgr, AST.getLangOpts());
362362
Position ActualPos = offsetToPosition(
363363
TestCase.code(),
364364
SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual)));
@@ -482,7 +482,7 @@ TEST(SourceCodeTests, GetMacros) {
482482
TestTU TU = TestTU::withCode(Code.code());
483483
auto AST = TU.build();
484484
auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(),
485-
AST.getASTContext().getLangOpts());
485+
AST.getLangOpts());
486486
auto Result = locateMacroAt(Loc, AST.getPreprocessor());
487487
ASSERT_TRUE(Result);
488488
EXPECT_THAT(*Result, MacroName("MACRO"));
@@ -548,7 +548,7 @@ TEST(SourceCodeTests, HalfOpenFileRange) {
548548
ParsedAST AST = TestTU::withCode(Test.code()).build();
549549
llvm::errs() << Test.code();
550550
const SourceManager &SM = AST.getSourceManager();
551-
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
551+
const LangOptions &LangOpts = AST.getLangOpts();
552552
// Turn a SourceLocation into a pair of positions
553553
auto SourceRangeToRange = [&SM](SourceRange SrcRange) {
554554
return Range{sourceLocToPosition(SM, SrcRange.getBegin()),
@@ -588,8 +588,7 @@ TEST(SourceCodeTests, HalfOpenFileRangePathologicalPreprocessor) {
588588
const auto &Body = cast<CompoundStmt>(Func.getBody());
589589
const auto &Loop = cast<WhileStmt>(*Body->child_begin());
590590
llvm::Optional<SourceRange> Range = toHalfOpenFileRange(
591-
AST.getSourceManager(), AST.getASTContext().getLangOpts(),
592-
Loop->getSourceRange());
591+
AST.getSourceManager(), AST.getLangOpts(), Loop->getSourceRange());
593592
ASSERT_TRUE(Range) << "Failed to get file range";
594593
EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getBegin()),
595594
Test.llvm::Annotations::range().Begin);

0 commit comments

Comments
 (0)