clang-tools  10.0.0svn
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.getASTContext().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
Position start
The range&#39;s start position.
Definition: Protocol.h:158
const syntax::TokenBuffer & getTokens() const
Tokens recorded while parsing the main file.
Definition: ParsedAST.h:98
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:424
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:149
const Decl * D
Definition: XRefs.cpp:849
llvm::Expected< std::vector< Range > > getSemanticRanges(ParsedAST &AST, Position Pos)
Returns the list of all interesting ranges around the Position Pos.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:194
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
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:541
SourceManager & getSourceManager()
Definition: ParsedAST.h:73
size_t Offset
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
Position end
The range&#39;s end position.
Definition: Protocol.h:161