clang-tools 10.0.0

SemanticSelection.cpp
Go to the documentation of this file.
1 //===--- SemanticSelection.cpp -----------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "SemanticSelection.h"
9 #include "ParsedAST.h"
10 #include "Protocol.h"
11 #include "Selection.h"
12 #include "SourceCode.h"
13 #include "clang/AST/DeclBase.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "llvm/Support/Error.h"
16
17 namespace clang {
18 namespace clangd {
19 namespace {
20 // Adds Range \p R to the Result if it is distinct from the last added Range.
21 // Assumes that only consecutive ranges can coincide.
22 void addIfDistinct(const Range &R, std::vector<Range> &Result) {
23 if (Result.empty() || Result.back() != R) {
24 Result.push_back(R);
25 }
26 }
27 } // namespace
28
29 llvm::Expected<std::vector<Range>> getSemanticRanges(ParsedAST &AST,
30 Position Pos) {
31 std::vector<Range> Result;
32 const auto &SM = AST.getSourceManager();
33 const auto &LangOpts = AST.getLangOpts();
34
35 auto FID = SM.getMainFileID();
36 auto Offset = positionToOffset(SM.getBufferData(FID), Pos);
37 if (!Offset) {
38 return Offset.takeError();
39 }
40
41 // Get node under the cursor.
42 SelectionTree ST(AST.getASTContext(), AST.getTokens(), *Offset);
43 for (const auto *Node = ST.commonAncestor(); Node != nullptr;
44 Node = Node->Parent) {
45 if (const Decl *D = Node->ASTNode.get<Decl>()) {
46 if (llvm::isa<TranslationUnitDecl>(D)) {
47 break;
48 }
49 }
50
51 auto SR = toHalfOpenFileRange(SM, LangOpts, Node->ASTNode.getSourceRange());
52 if (!SR.hasValue() || SM.getFileID(SR->getBegin()) != SM.getMainFileID()) {
53 continue;
54 }
55 Range R;
56 R.start = sourceLocToPosition(SM, SR->getBegin());
57 R.end = sourceLocToPosition(SM, SR->getEnd());
58 addIfDistinct(R, Result);
59 }
60 return Result;
61 }
62
63 } // namespace clangd
64 } // namespace clang
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:93
clang::clangd::Range::start
Position start
The range&#39;s start position.
Definition: Protocol.h:158
clang::clangd::ParsedAST::getLangOpts
const LangOptions & getLangOpts() const
Definition: ParsedAST.h:80
SourceCode.h
Protocol.h
clang::clangd::Position
Definition: Protocol.h:127
clang::clangd::ParsedAST::getTokens
const syntax::TokenBuffer & getTokens() const
Tokens recorded while parsing the main file.
Definition: ParsedAST.h:102
clang::clangd::ParsedAST::getASTContext
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:424
clang::clangd::Range
Definition: Protocol.h:156
clang::clangd::positionToOffset
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
Definition: SourceCode.cpp:155
clang::clangd::getSemanticRanges
llvm::Expected< std::vector< Range > > getSemanticRanges(ParsedAST &AST, Position Pos)
Returns the list of all interesting ranges around the Position Pos.
Definition: SemanticSelection.cpp:29
Pos
Position Pos
Definition: SourceCode.cpp:772
SemanticSelection.h
clang::clangd::sourceLocToPosition
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:200
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang::clangd::toHalfOpenFileRange
llvm::Optional< SourceRange > toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R)
Turns a token range into a half-open range and checks its correctness.
Definition: SourceCode.cpp:538
clang::clangd::ParsedAST::getSourceManager
SourceManager & getSourceManager()
Definition: ParsedAST.h:73
Offset
size_t Offset
Definition: CodeComplete.cpp:1022
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ParsedAST.h
clang::clangd::SelectionTree
Definition: Selection.h:65
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::clangd::Range::end
Position end
The range&#39;s end position.
Definition: Protocol.h:161
Selection.h