summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2019-06-25 08:01:46 +0000
committerHaojian Wu <hokein@google.com>2019-06-25 08:01:46 +0000
commit92c32574771465606807dea77c928c4d7768bbe9 (patch)
tree15d3417ce93b218d9382d50677b4cd797f79b2c3
parent303c9861e90cb9a15b15d31fab06d0374d42c9af (diff)
downloadbcm5719-llvm-92c32574771465606807dea77c928c4d7768bbe9.tar.gz
bcm5719-llvm-92c32574771465606807dea77c928c4d7768bbe9.zip
[clangd] Cleanup the duplicated getTokenRange.
Summary: Also lift it to SourceCode.h, so that it can be used in other places (semantic code highlighting). Reviewers: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63714 llvm-svn: 364280
-rw-r--r--clang-tools-extra/clangd/SourceCode.cpp11
-rw-r--r--clang-tools-extra/clangd/SourceCode.h5
-rw-r--r--clang-tools-extra/clangd/XRefs.cpp70
3 files changed, 48 insertions, 38 deletions
diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index 6c52ca6c01c..2dcf3d4070f 100644
--- a/clang-tools-extra/clangd/SourceCode.cpp
+++ b/clang-tools-extra/clangd/SourceCode.cpp
@@ -196,6 +196,17 @@ Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc) {
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;
diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h
index 85db9458589..3ff67bc13cd 100644
--- a/clang-tools-extra/clangd/SourceCode.h
+++ b/clang-tools-extra/clangd/SourceCode.h
@@ -65,6 +65,11 @@ Position offsetToPosition(llvm::StringRef Code, size_t Offset);
/// 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,
diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp
index 2dc3ac03095..687652ca78f 100644
--- a/clang-tools-extra/clangd/XRefs.cpp
+++ b/clang-tools-extra/clangd/XRefs.cpp
@@ -260,14 +260,6 @@ IdentifiedSymbol getSymbolAtPosition(ParsedAST &AST, SourceLocation Pos) {
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 @@ llvm::Optional<Location> makeLocation(ASTContext &AST, SourceLocation TokLoc,
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<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
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)) {
+ 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 @@ fetchTemplateParameters(const TemplateParameterList *Params,
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 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
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 @@ std::vector<Location> findReferences(ParsedAST &AST, Position Pos,
// 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.
OpenPOWER on IntegriCloud