[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.
This commit is contained in:
Alex Lorenz 2019-12-04 15:09:35 -08:00
parent acda2bc0ad
commit c0ee0224c4
13 changed files with 35 additions and 39 deletions

View File

@ -312,8 +312,8 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
const auto &SM = AST.getSourceManager();
SourceLocation Loc =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
auto Range = getTokenRange(SM, AST.getASTContext().getLangOpts(), Loc);
Pos, AST.getSourceManager(), AST.getLangOpts()));
auto Range = getTokenRange(SM, AST.getLangOpts(), Loc);
if (!Range)
return CB(llvm::None); // "rename" is not valid at the position.

View File

@ -97,7 +97,7 @@ llvm::Optional<Path> getCorrespondingHeaderOrSource(const Path &OriginalFile,
//
// For each symbol in the original file, we get its target location (decl or
// def) from the index, then award that target file.
bool IsHeader = isHeaderFile(OriginalFile, AST.getASTContext().getLangOpts());
bool IsHeader = isHeaderFile(OriginalFile, AST.getLangOpts());
Index->lookup(Request, [&](const Symbol &Sym) {
if (IsHeader)
AwardTarget(Sym.Definition.FileURI);

View File

@ -367,8 +367,7 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
if (EndLoc.isValid()) {
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM,
AST.getASTContext().getLangOpts());
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, AST.getLangOpts());
bool Invalid;
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
if (!Invalid) {
@ -391,7 +390,7 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
const SourceManager &SM = AST.getSourceManager();
llvm::Optional<HoverInfo> HI;
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
if (auto Deduced = getDeducedType(AST.getASTContext(), SourceLocationBeg)) {
// Find the corresponding decl to populate kind and fetch documentation.
@ -435,9 +434,8 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
tooling::applyAllReplacements(HI->Definition, Replacements))
HI->Definition = *Formatted;
HI->SymRange =
getTokenRange(AST.getASTContext().getSourceManager(),
AST.getASTContext().getLangOpts(), SourceLocationBeg);
HI->SymRange = getTokenRange(AST.getASTContext().getSourceManager(),
AST.getLangOpts(), SourceLocationBeg);
return HI;
}

View File

@ -77,6 +77,10 @@ public:
return getASTContext().getSourceManager();
}
const LangOptions &getLangOpts() const {
return getASTContext().getLangOpts();
}
/// This function returns top-level decls present in the main file of the AST.
/// The result does not include the decls that come from the preamble.
/// (These should be const, but RecursiveASTVisitor requires Decl*).

View File

@ -30,7 +30,7 @@ llvm::Expected<std::vector<Range>> getSemanticRanges(ParsedAST &AST,
Position Pos) {
std::vector<Range> Result;
const auto &SM = AST.getSourceManager();
const auto &LangOpts = AST.getASTContext().getLangOpts();
const auto &LangOpts = AST.getLangOpts();
auto FID = SM.getMainFileID();
auto Offset = positionToOffset(SM.getBufferData(FID), Pos);

View File

@ -191,9 +191,8 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
// Macros are simple: there's no declaration/definition distinction.
// As a consequence, there's no need to look them up in the index either.
SourceLocation MaybeMacroLocation =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, AST.getSourceManager(), AST.getASTContext().getLangOpts()));
SourceLocation MaybeMacroLocation = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts()));
std::vector<LocatedSymbol> Result;
if (auto M = locateMacroAt(MaybeMacroLocation, AST.getPreprocessor())) {
if (auto Loc = makeLocation(AST.getASTContext(),
@ -366,7 +365,7 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
auto References = findRefs(
getDeclAtPosition(AST,
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
Pos, SM, AST.getASTContext().getLangOpts())),
Pos, SM, AST.getLangOpts())),
Relations),
AST);
@ -374,9 +373,8 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
// different kinds, deduplicate them.
std::vector<DocumentHighlight> Result;
for (const auto &Ref : References) {
if (auto Range =
getTokenRange(AST.getASTContext().getSourceManager(),
AST.getASTContext().getLangOpts(), Ref.Loc)) {
if (auto Range = getTokenRange(AST.getASTContext().getSourceManager(),
AST.getLangOpts(), Ref.Loc)) {
DocumentHighlight DH;
DH.range = *Range;
if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write))
@ -404,7 +402,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
return Results;
}
auto Loc = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
// TODO: should we handle macros, too?
// We also show references to the targets of using-decls, so we include
// DeclRelation::Underlying.
@ -424,8 +422,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
}),
MainFileRefs.end());
for (const auto &Ref : MainFileRefs) {
if (auto Range =
getTokenRange(SM, AST.getASTContext().getLangOpts(), Ref.Loc)) {
if (auto Range = getTokenRange(SM, AST.getLangOpts(), Ref.Loc)) {
Location Result;
Result.range = *Range;
Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
@ -470,7 +467,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
const SourceManager &SM = AST.getSourceManager();
auto Loc = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
std::vector<SymbolDetails> Results;
@ -646,7 +643,7 @@ static void fillSuperTypes(const CXXRecordDecl &CXXRD, ASTContext &ASTCtx,
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
const SourceManager &SM = AST.getSourceManager();
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(Pos, SM, AST.getASTContext().getLangOpts()));
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
DeclRelationSet Relations =
DeclRelation::TemplatePattern | DeclRelation::Underlying;
auto Decls = getDeclAtPosition(AST, SourceLocationBeg, Relations);

View File

@ -397,9 +397,8 @@ llvm::Expected<FileEdits> rename(const RenameInputs &RInputs) {
return (*Content)->getBuffer().str();
};
SourceLocation SourceLocationBeg =
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
RInputs.Pos, SM, AST.getASTContext().getLangOpts()));
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
getBeginningOfIdentifier(RInputs.Pos, SM, AST.getLangOpts()));
// FIXME: Renaming macros is not supported yet, the macro-handling code should
// be moved to rename tooling library.
if (locateMacroAt(SourceLocationBeg, AST.getPreprocessor()))

View File

@ -238,7 +238,7 @@ public:
// FIXME: We might want to consider moving method definitions below class
// definition even if we are inside a source file.
if (!isHeaderFile(Sel.AST.getSourceManager().getFilename(Sel.Cursor),
Sel.AST.getASTContext().getLangOpts()))
Sel.AST.getLangOpts()))
return false;
Source = getSelectedFunction(Sel.ASTSelection.commonAncestor());
@ -306,9 +306,8 @@ public:
// FIXME: We should also get rid of inline qualifier.
const tooling::Replacement DeleteFuncBody(
Sel.AST.getSourceManager(),
CharSourceRange::getTokenRange(
*toHalfOpenFileRange(SM, Sel.AST.getASTContext().getLangOpts(),
Source->getBody()->getSourceRange())),
CharSourceRange::getTokenRange(*toHalfOpenFileRange(
SM, Sel.AST.getLangOpts(), Source->getBody()->getSourceRange())),
";");
auto HeaderFE = Effect::fileEdit(SM, SM.getMainFileID(),
tooling::Replacements(DeleteFuncBody));

View File

@ -645,7 +645,7 @@ tooling::Replacement createFunctionDefinition(const NewFunction &ExtractedFunc,
bool ExtractFunction::prepare(const Selection &Inputs) {
const Node *CommonAnc = Inputs.ASTSelection.commonAncestor();
const SourceManager &SM = Inputs.AST.getSourceManager();
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
if (auto MaybeExtZone = findExtractionZone(CommonAnc, SM, LangOpts)) {
ExtZone = std::move(*MaybeExtZone);
return true;
@ -655,7 +655,7 @@ bool ExtractFunction::prepare(const Selection &Inputs) {
Expected<Tweak::Effect> ExtractFunction::apply(const Selection &Inputs) {
const SourceManager &SM = Inputs.AST.getSourceManager();
const LangOptions &LangOpts = Inputs.AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = Inputs.AST.getLangOpts();
auto ExtractedFunc = getExtractedFunction(ExtZone, SM, LangOpts);
// FIXME: Add more types of errors.
if (!ExtractedFunc)

View File

@ -91,7 +91,7 @@ Expected<Tweak::Effect> RawStringLiteral::apply(const Selection &Inputs) {
auto &SM = Inputs.AST.getSourceManager();
auto Reps = tooling::Replacements(
tooling::Replacement(SM, Str, ("R\"(" + Str->getBytes() + ")\"").str(),
Inputs.AST.getASTContext().getLangOpts()));
Inputs.AST.getLangOpts()));
return Effect::mainFileEdit(SM, std::move(Reps));
}

View File

@ -88,7 +88,7 @@ TEST(CollectMainFileMacros, SelectedMacros) {
break;
auto Loc = getBeginningOfIdentifier(ExpectedRefs.begin()->start, SM,
AST.getASTContext().getLangOpts());
AST.getLangOpts());
auto Macro = locateMacroAt(Loc, PP);
assert(Macro);
auto SID = getSymbolID(Macro->Name, Macro->Info, SM);

View File

@ -40,7 +40,7 @@ Range nodeRange(const SelectionTree::Node *N, ParsedAST &AST) {
if (!N)
return Range{};
const SourceManager &SM = AST.getSourceManager();
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = AST.getLangOpts();
StringRef Buffer = SM.getBufferData(SM.getMainFileID());
if (llvm::isa_and_nonnull<TranslationUnitDecl>(N->ASTNode.get<Decl>()))
return Range{Position{}, offsetToPosition(Buffer, Buffer.size())};

View File

@ -358,7 +358,7 @@ Bar* bar;
auto AST = TestTU::withCode(TestCase.code()).build();
const auto &SourceMgr = AST.getSourceManager();
SourceLocation Actual = getBeginningOfIdentifier(
TestCase.points().back(), SourceMgr, AST.getASTContext().getLangOpts());
TestCase.points().back(), SourceMgr, AST.getLangOpts());
Position ActualPos = offsetToPosition(
TestCase.code(),
SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual)));
@ -482,7 +482,7 @@ TEST(SourceCodeTests, GetMacros) {
TestTU TU = TestTU::withCode(Code.code());
auto AST = TU.build();
auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(),
AST.getASTContext().getLangOpts());
AST.getLangOpts());
auto Result = locateMacroAt(Loc, AST.getPreprocessor());
ASSERT_TRUE(Result);
EXPECT_THAT(*Result, MacroName("MACRO"));
@ -548,7 +548,7 @@ TEST(SourceCodeTests, HalfOpenFileRange) {
ParsedAST AST = TestTU::withCode(Test.code()).build();
llvm::errs() << Test.code();
const SourceManager &SM = AST.getSourceManager();
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
const LangOptions &LangOpts = AST.getLangOpts();
// Turn a SourceLocation into a pair of positions
auto SourceRangeToRange = [&SM](SourceRange SrcRange) {
return Range{sourceLocToPosition(SM, SrcRange.getBegin()),
@ -588,8 +588,7 @@ TEST(SourceCodeTests, HalfOpenFileRangePathologicalPreprocessor) {
const auto &Body = cast<CompoundStmt>(Func.getBody());
const auto &Loop = cast<WhileStmt>(*Body->child_begin());
llvm::Optional<SourceRange> Range = toHalfOpenFileRange(
AST.getSourceManager(), AST.getASTContext().getLangOpts(),
Loop->getSourceRange());
AST.getSourceManager(), AST.getLangOpts(), Loop->getSourceRange());
ASSERT_TRUE(Range) << "Failed to get file range";
EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getBegin()),
Test.llvm::Annotations::range().Begin);