hokein created this revision. hokein added a reviewer: kadircet. Herald added subscribers: arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
Also lift it to SourceCode.h, so that it can be used in other places (semantic code highlighting). Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D63714 Files: clang-tools-extra/clangd/SourceCode.cpp clang-tools-extra/clangd/SourceCode.h clang-tools-extra/clangd/XRefs.cpp
Index: clang-tools-extra/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/clangd/XRefs.cpp +++ clang-tools-extra/clangd/XRefs.cpp @@ -260,14 +260,6 @@ return {DeclMacrosFinder.getFoundDecls(), DeclMacrosFinder.takeMacroInfos()}; } -Range getTokenRange(ASTContext &AST, SourceLocation TokLoc) { - const SourceManager &SourceMgr = AST.getSourceManager(); - SourceLocation LocEnd = - Lexer::getLocForEndOfToken(TokLoc, 0, SourceMgr, AST.getLangOpts()); - return {sourceLocToPosition(SourceMgr, TokLoc), - sourceLocToPosition(SourceMgr, LocEnd)}; -} - llvm::Optional<Location> makeLocation(ASTContext &AST, SourceLocation TokLoc, llvm::StringRef TUPath) { const SourceManager &SourceMgr = AST.getSourceManager(); @@ -279,10 +271,14 @@ log("failed to get path!"); return None; } - Location L; - L.uri = URIForFile::canonicalize(*FilePath, TUPath); - L.range = getTokenRange(AST, TokLoc); - return L; + if (auto Range = + getTokenRange(AST.getSourceManager(), AST.getLangOpts(), TokLoc)) { + Location L; + L.uri = URIForFile::canonicalize(*FilePath, TUPath); + L.range = *Range; + return L; + } + return None; } } // namespace @@ -472,14 +468,18 @@ std::vector<DocumentHighlight> Result; for (const auto &Ref : References) { DocumentHighlight DH; - DH.range = getTokenRange(AST.getASTContext(), Ref.Loc); - if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write)) - DH.kind = DocumentHighlightKind::Write; - else if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Read)) - DH.kind = DocumentHighlightKind::Read; - else - DH.kind = DocumentHighlightKind::Text; - Result.push_back(std::move(DH)); + if (auto Range = + getTokenRange(AST.getASTContext().getSourceManager(), + AST.getASTContext().getLangOpts(), Ref.Loc)) { + DH.range = *Range; + if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write)) + DH.kind = DocumentHighlightKind::Write; + else if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Read)) + DH.kind = DocumentHighlightKind::Read; + else + DH.kind = DocumentHighlightKind::Text; + Result.push_back(std::move(DH)); + } } return Result; } @@ -610,18 +610,6 @@ return TempParameters; } -static llvm::Optional<Range> getTokenRange(SourceLocation Loc, - const ASTContext &Ctx) { - if (!Loc.isValid()) - return llvm::None; - SourceLocation End = Lexer::getLocForEndOfToken( - Loc, 0, Ctx.getSourceManager(), Ctx.getLangOpts()); - if (!End.isValid()) - return llvm::None; - return halfOpenToRange(Ctx.getSourceManager(), - CharSourceRange::getCharRange(Loc, End)); -} - static const FunctionDecl *getUnderlyingFunction(const Decl *D) { // Extract lambda from variables. if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) { @@ -910,7 +898,9 @@ tooling::applyAllReplacements(HI->Definition, Replacements)) HI->Definition = *Formatted; - HI->SymRange = getTokenRange(SourceLocationBeg, AST.getASTContext()); + HI->SymRange = + getTokenRange(AST.getASTContext().getSourceManager(), + AST.getASTContext().getLangOpts(), SourceLocationBeg); return HI; } @@ -934,9 +924,13 @@ auto MainFileRefs = findRefs(Symbols.Decls, AST); for (const auto &Ref : MainFileRefs) { Location Result; - Result.range = getTokenRange(AST.getASTContext(), Ref.Loc); - Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath); - Results.push_back(std::move(Result)); + if (auto Range = + getTokenRange(AST.getASTContext().getSourceManager(), + AST.getASTContext().getLangOpts(), Ref.Loc)) { + Result.range = *Range; + Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath); + Results.push_back(std::move(Result)); + } } // Now query the index for references from other files. Index: clang-tools-extra/clangd/SourceCode.h =================================================================== --- clang-tools-extra/clangd/SourceCode.h +++ clang-tools-extra/clangd/SourceCode.h @@ -65,6 +65,11 @@ /// FIXME: This should return an error if the location is invalid. Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc); +/// Returns the taken range at \p TokLoc. +llvm::Optional<Range> getTokenRange(const SourceManager &SM, + const LangOptions &LangOpts, + SourceLocation TokLoc); + /// Return the file location, corresponding to \p P. Note that one should take /// care to avoid comparing the result with expansion locations. llvm::Expected<SourceLocation> sourceLocationInMainFile(const SourceManager &SM, Index: clang-tools-extra/clangd/SourceCode.cpp =================================================================== --- clang-tools-extra/clangd/SourceCode.cpp +++ clang-tools-extra/clangd/SourceCode.cpp @@ -196,6 +196,17 @@ return P; } +llvm::Optional<Range> getTokenRange(const SourceManager &SM, + const LangOptions &LangOpts, + SourceLocation TokLoc) { + if (!TokLoc.isValid()) + return llvm::None; + SourceLocation End = Lexer::getLocForEndOfToken(TokLoc, 0, SM, LangOpts); + if (!End.isValid()) + return llvm::None; + return halfOpenToRange(SM, CharSourceRange::getCharRange(TokLoc, End)); +} + bool isValidFileRange(const SourceManager &Mgr, SourceRange R) { if (!R.getBegin().isValid() || !R.getEnd().isValid()) return false;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits