diff options
author | Kirill Bobyrev <kbobyrev@google.com> | 2020-07-13 15:02:47 +0200 |
---|---|---|
committer | Kirill Bobyrev <kbobyrev@google.com> | 2020-07-13 15:02:53 +0200 |
commit | d7d1af39168ce8afd041f3ae8db1d1fd3d4f70ac (patch) | |
tree | 6c3bef9d2bbaae1e770b51b685d6218242af12f1 /clang-tools-extra/clangd/FindSymbols.cpp | |
parent | 2df46a574387663717a352eebad017979d3b5ef4 (diff) | |
download | llvm-d7d1af39168ce8afd041f3ae8db1d1fd3d4f70ac.tar.gz |
[clangd] Fix DocumentSymbol ranges
Summary:
DocumentSymbol ranges were not previously tested and, as a result, had invalid
end location. This patch addresses the issue.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83668
Diffstat (limited to 'clang-tools-extra/clangd/FindSymbols.cpp')
-rw-r--r-- | clang-tools-extra/clangd/FindSymbols.cpp | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp index 58e2ee1e21c7..f5d6a95aa713 100644 --- a/clang-tools-extra/clangd/FindSymbols.cpp +++ b/clang-tools-extra/clangd/FindSymbols.cpp @@ -136,17 +136,11 @@ llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) { auto &SM = Ctx.getSourceManager(); SourceLocation NameLoc = nameLocation(ND, SM); - // getFileLoc is a good choice for us, but we also need to make sure - // sourceLocToPosition won't switch files, so we call getSpellingLoc on top of - // that to make sure it does not switch files. - // FIXME: sourceLocToPosition should not switch files! SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc())); SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc())); - if (NameLoc.isInvalid() || BeginLoc.isInvalid() || EndLoc.isInvalid()) - return llvm::None; - - if (!SM.isWrittenInMainFile(NameLoc) || !SM.isWrittenInMainFile(BeginLoc) || - !SM.isWrittenInMainFile(EndLoc)) + const auto SymbolRange = + toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc}); + if (!SymbolRange) return llvm::None; Position NameBegin = sourceLocToPosition(SM, NameLoc); @@ -162,8 +156,8 @@ llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) { SI.name = printName(Ctx, ND); SI.kind = SK; SI.deprecated = ND.isDeprecated(); - SI.range = - Range{sourceLocToPosition(SM, BeginLoc), sourceLocToPosition(SM, EndLoc)}; + SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()), + sourceLocToPosition(SM, SymbolRange->getEnd())}; SI.selectionRange = Range{NameBegin, NameEnd}; if (!SI.range.contains(SI.selectionRange)) { // 'selectionRange' must be contained in 'range', so in cases where clang |