annotate clang-tools-extra/clangd/Selection.h @ 206:f17a3b42b08b

Added tag before-12 for changeset b7591485f4cd
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 07 Jun 2021 21:25:57 +0900
parents 0572611fdcc8
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- Selection.h - What's under the cursor? -------------------*-C++-*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 // Many features are triggered at locations/ranges and operate on AST nodes.
anatofuz
parents:
diff changeset
9 // (e.g. go-to-definition or code tweaks).
anatofuz
parents:
diff changeset
10 // At a high level, such features need to work out which node is the correct
anatofuz
parents:
diff changeset
11 // target.
anatofuz
parents:
diff changeset
12 //
anatofuz
parents:
diff changeset
13 // There are a few levels of ambiguity here:
anatofuz
parents:
diff changeset
14 //
anatofuz
parents:
diff changeset
15 // Which tokens are included:
anatofuz
parents:
diff changeset
16 // int x = one + two; // what should "go to definition" do?
anatofuz
parents:
diff changeset
17 // ^^^^^^
anatofuz
parents:
diff changeset
18 //
anatofuz
parents:
diff changeset
19 // Same token means multiple things:
anatofuz
parents:
diff changeset
20 // string("foo") // class string, or a constructor?
anatofuz
parents:
diff changeset
21 // ^
anatofuz
parents:
diff changeset
22 //
anatofuz
parents:
diff changeset
23 // Which level of the AST is interesting?
anatofuz
parents:
diff changeset
24 // if (err) { // reference to 'err', or operator bool(),
anatofuz
parents:
diff changeset
25 // ^ // or the if statement itself?
anatofuz
parents:
diff changeset
26 //
anatofuz
parents:
diff changeset
27 // Here we build and expose a data structure that allows features to resolve
anatofuz
parents:
diff changeset
28 // these ambiguities in an appropriate way:
anatofuz
parents:
diff changeset
29 // - we determine which low-level nodes are partly or completely covered
anatofuz
parents:
diff changeset
30 // by the selection.
anatofuz
parents:
diff changeset
31 // - we expose a tree of the selected nodes and their lexical parents.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
32 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
33 // Sadly LSP specifies locations as being between characters, and this causes
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
34 // some ambiguities we cannot cleanly resolve:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
35 // lhs+rhs // targeting '+' or 'lhs'?
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36 // ^ // in GUI editors, double-clicking 'lhs' yields this position!
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
37 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
38 // The best we can do in these cases is try both, which leads to the awkward
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
39 // SelectionTree::createEach() API.
150
anatofuz
parents:
diff changeset
40 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SELECTION_H
anatofuz
parents:
diff changeset
43 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SELECTION_H
anatofuz
parents:
diff changeset
44 #include "clang/AST/ASTTypeTraits.h"
anatofuz
parents:
diff changeset
45 #include "clang/AST/PrettyPrinter.h"
anatofuz
parents:
diff changeset
46 #include "clang/Tooling/Syntax/Tokens.h"
anatofuz
parents:
diff changeset
47 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 namespace clang {
anatofuz
parents:
diff changeset
50 namespace clangd {
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 // A selection can partially or completely cover several AST nodes.
anatofuz
parents:
diff changeset
53 // The SelectionTree contains nodes that are covered, and their parents.
anatofuz
parents:
diff changeset
54 // SelectionTree does not contain all AST nodes, rather only:
anatofuz
parents:
diff changeset
55 // Decl, Stmt, TypeLoc, NestedNamespaceSpecifierLoc, CXXCtorInitializer.
anatofuz
parents:
diff changeset
56 // (These are the nodes with source ranges that fit in DynTypedNode).
anatofuz
parents:
diff changeset
57 //
anatofuz
parents:
diff changeset
58 // Usually commonAncestor() is the place to start:
anatofuz
parents:
diff changeset
59 // - it's the simplest answer to "what node is under the cursor"
anatofuz
parents:
diff changeset
60 // - the selected Expr (for example) can be found by walking up the parent
anatofuz
parents:
diff changeset
61 // chain and checking Node->ASTNode.
anatofuz
parents:
diff changeset
62 // - if you want to traverse the selected nodes, they are all under
anatofuz
parents:
diff changeset
63 // commonAncestor() in the tree.
anatofuz
parents:
diff changeset
64 //
anatofuz
parents:
diff changeset
65 // SelectionTree tries to behave sensibly in the presence of macros, but does
anatofuz
parents:
diff changeset
66 // not model any preprocessor concepts: the output is a subset of the AST.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
67 // When a macro argument is specifically selected, only its first expansion is
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
68 // selected in the AST. (Returning a selection forest is unreasonably difficult
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
69 // for callers to handle correctly.)
150
anatofuz
parents:
diff changeset
70 //
anatofuz
parents:
diff changeset
71 // Comments, directives and whitespace are completely ignored.
anatofuz
parents:
diff changeset
72 // Semicolons are also ignored, as the AST generally does not model them well.
anatofuz
parents:
diff changeset
73 //
anatofuz
parents:
diff changeset
74 // The SelectionTree owns the Node structures, but the ASTNode attributes
anatofuz
parents:
diff changeset
75 // point back into the AST it was constructed with.
anatofuz
parents:
diff changeset
76 class SelectionTree {
anatofuz
parents:
diff changeset
77 public:
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
78 // Create selection trees for the given range, and pass them to Func.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
79 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
80 // There may be multiple possible selection trees:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
81 // - if the range is empty and borders two tokens, a tree for the right token
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
82 // and a tree for the left token will be yielded.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
83 // - Func should return true on success (stop) and false on failure (continue)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
84 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
85 // Always yields at least one tree. If no tokens are touched, it is empty.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
86 static bool createEach(ASTContext &AST, const syntax::TokenBuffer &Tokens,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
87 unsigned Begin, unsigned End,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
88 llvm::function_ref<bool(SelectionTree)> Func);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
89
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
90 // Create a selection tree for the given range.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
91 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
92 // Where ambiguous (range is empty and borders two tokens), prefer the token
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
93 // on the right.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
94 static SelectionTree createRight(ASTContext &AST,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
95 const syntax::TokenBuffer &Tokens,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
96 unsigned Begin, unsigned End);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
97
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 // Copies are no good - contain pointers to other nodes.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
99 SelectionTree(const SelectionTree &) = delete;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
100 SelectionTree &operator=(const SelectionTree &) = delete;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
101 // Moves are OK though - internal storage is pointer-stable when moved.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
102 SelectionTree(SelectionTree &&) = default;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
103 SelectionTree &operator=(SelectionTree &&) = default;
150
anatofuz
parents:
diff changeset
104
anatofuz
parents:
diff changeset
105 // Describes to what extent an AST node is covered by the selection.
anatofuz
parents:
diff changeset
106 enum Selection : unsigned char {
anatofuz
parents:
diff changeset
107 // The AST node owns no characters covered by the selection.
anatofuz
parents:
diff changeset
108 // Note that characters owned by children don't count:
anatofuz
parents:
diff changeset
109 // if (x == 0) scream();
anatofuz
parents:
diff changeset
110 // ^^^^^^
anatofuz
parents:
diff changeset
111 // The IfStmt would be Unselected because all the selected characters are
anatofuz
parents:
diff changeset
112 // associated with its children.
anatofuz
parents:
diff changeset
113 // (Invisible nodes like ImplicitCastExpr are always unselected).
anatofuz
parents:
diff changeset
114 Unselected,
anatofuz
parents:
diff changeset
115 // The AST node owns selected characters, but is not completely covered.
anatofuz
parents:
diff changeset
116 Partial,
anatofuz
parents:
diff changeset
117 // The AST node owns characters, and is covered by the selection.
anatofuz
parents:
diff changeset
118 Complete,
anatofuz
parents:
diff changeset
119 };
anatofuz
parents:
diff changeset
120 // An AST node that is implicated in the selection.
anatofuz
parents:
diff changeset
121 // (Either selected directly, or some descendant is selected).
anatofuz
parents:
diff changeset
122 struct Node {
anatofuz
parents:
diff changeset
123 // The parent within the selection tree. nullptr for TranslationUnitDecl.
anatofuz
parents:
diff changeset
124 Node *Parent;
anatofuz
parents:
diff changeset
125 // Direct children within the selection tree.
anatofuz
parents:
diff changeset
126 llvm::SmallVector<const Node *, 8> Children;
anatofuz
parents:
diff changeset
127 // The corresponding node from the full AST.
anatofuz
parents:
diff changeset
128 ast_type_traits::DynTypedNode ASTNode;
anatofuz
parents:
diff changeset
129 // The extent to which this node is covered by the selection.
anatofuz
parents:
diff changeset
130 Selection Selected;
anatofuz
parents:
diff changeset
131 // Walk up the AST to get the DeclContext of this Node,
anatofuz
parents:
diff changeset
132 // which is not the node itself.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
133 const DeclContext &getDeclContext() const;
150
anatofuz
parents:
diff changeset
134 // Printable node kind, like "CXXRecordDecl" or "AutoTypeLoc".
anatofuz
parents:
diff changeset
135 std::string kind() const;
anatofuz
parents:
diff changeset
136 // If this node is a wrapper with no syntax (e.g. implicit cast), return
anatofuz
parents:
diff changeset
137 // its contents. (If multiple wrappers are present, unwraps all of them).
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
138 const Node &ignoreImplicit() const;
150
anatofuz
parents:
diff changeset
139 // If this node is inside a wrapper with no syntax (e.g. implicit cast),
anatofuz
parents:
diff changeset
140 // return that wrapper. (If multiple are present, unwraps all of them).
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
141 const Node &outerImplicit() const;
150
anatofuz
parents:
diff changeset
142 };
anatofuz
parents:
diff changeset
143 // The most specific common ancestor of all the selected nodes.
anatofuz
parents:
diff changeset
144 // Returns nullptr if the common ancestor is the root.
anatofuz
parents:
diff changeset
145 // (This is to avoid accidentally traversing the TUDecl and thus preamble).
anatofuz
parents:
diff changeset
146 const Node *commonAncestor() const;
anatofuz
parents:
diff changeset
147 // The selection node corresponding to TranslationUnitDecl.
anatofuz
parents:
diff changeset
148 const Node &root() const { return *Root; }
anatofuz
parents:
diff changeset
149
anatofuz
parents:
diff changeset
150 private:
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
151 // Creates a selection tree for the given range in the main file.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
152 // The range includes bytes [Start, End).
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
153 SelectionTree(ASTContext &AST, const syntax::TokenBuffer &Tokens,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
154 unsigned Start, unsigned End);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
155
150
anatofuz
parents:
diff changeset
156 std::deque<Node> Nodes; // Stable-pointer storage.
anatofuz
parents:
diff changeset
157 const Node *Root;
anatofuz
parents:
diff changeset
158 clang::PrintingPolicy PrintPolicy;
anatofuz
parents:
diff changeset
159
anatofuz
parents:
diff changeset
160 void print(llvm::raw_ostream &OS, const Node &N, int Indent) const;
anatofuz
parents:
diff changeset
161 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
anatofuz
parents:
diff changeset
162 const SelectionTree &T) {
anatofuz
parents:
diff changeset
163 T.print(OS, T.root(), 1);
anatofuz
parents:
diff changeset
164 return OS;
anatofuz
parents:
diff changeset
165 }
anatofuz
parents:
diff changeset
166 };
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 } // namespace clangd
anatofuz
parents:
diff changeset
169 } // namespace clang
anatofuz
parents:
diff changeset
170 #endif