Index: clang-tools-extra/trunk/clangd/SourceCode.h =================================================================== --- clang-tools-extra/trunk/clangd/SourceCode.h +++ clang-tools-extra/trunk/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 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 sourceLocationInMainFile(const SourceManager &SM, Index: clang-tools-extra/trunk/clangd/SourceCode.cpp =================================================================== --- clang-tools-extra/trunk/clangd/SourceCode.cpp +++ clang-tools-extra/trunk/clangd/SourceCode.cpp @@ -196,6 +196,17 @@ return P; } +llvm::Optional 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; Index: clang-tools-extra/trunk/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/trunk/clangd/XRefs.cpp +++ clang-tools-extra/trunk/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 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 @@ -471,15 +467,19 @@ std::vector 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)) { + DocumentHighlight DH; + 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 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(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; } @@ -933,10 +923,14 @@ // TODO: should we handle macros, too? 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)) { + Location Result; + Result.range = *Range; + Result.uri = URIForFile::canonicalize(*MainFilePath, *MainFilePath); + Results.push_back(std::move(Result)); + } } // Now query the index for references from other files.