annotate clang-tools-extra/clangd/Selection.cpp @ 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.cpp ----------------------------------------------------===//
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
anatofuz
parents:
diff changeset
9 #include "Selection.h"
anatofuz
parents:
diff changeset
10 #include "SourceCode.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
11 #include "support/Logger.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
12 #include "support/Trace.h"
150
anatofuz
parents:
diff changeset
13 #include "clang/AST/ASTTypeTraits.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
14 #include "clang/AST/Decl.h"
150
anatofuz
parents:
diff changeset
15 #include "clang/AST/DeclCXX.h"
anatofuz
parents:
diff changeset
16 #include "clang/AST/Expr.h"
anatofuz
parents:
diff changeset
17 #include "clang/AST/ExprCXX.h"
anatofuz
parents:
diff changeset
18 #include "clang/AST/PrettyPrinter.h"
anatofuz
parents:
diff changeset
19 #include "clang/AST/RecursiveASTVisitor.h"
anatofuz
parents:
diff changeset
20 #include "clang/AST/TypeLoc.h"
anatofuz
parents:
diff changeset
21 #include "clang/Basic/OperatorKinds.h"
anatofuz
parents:
diff changeset
22 #include "clang/Basic/SourceLocation.h"
anatofuz
parents:
diff changeset
23 #include "clang/Basic/SourceManager.h"
anatofuz
parents:
diff changeset
24 #include "clang/Basic/TokenKinds.h"
anatofuz
parents:
diff changeset
25 #include "clang/Lex/Lexer.h"
anatofuz
parents:
diff changeset
26 #include "clang/Tooling/Syntax/Tokens.h"
anatofuz
parents:
diff changeset
27 #include "llvm/ADT/STLExtras.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
28 #include "llvm/ADT/StringExtras.h"
150
anatofuz
parents:
diff changeset
29 #include "llvm/Support/Casting.h"
anatofuz
parents:
diff changeset
30 #include "llvm/Support/raw_ostream.h"
anatofuz
parents:
diff changeset
31 #include <algorithm>
anatofuz
parents:
diff changeset
32 #include <string>
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 namespace clang {
anatofuz
parents:
diff changeset
35 namespace clangd {
anatofuz
parents:
diff changeset
36 namespace {
anatofuz
parents:
diff changeset
37 using Node = SelectionTree::Node;
anatofuz
parents:
diff changeset
38 using ast_type_traits::DynTypedNode;
anatofuz
parents:
diff changeset
39
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
40 // Measure the fraction of selections that were enabled by recovery AST.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
41 void recordMetrics(const SelectionTree &S) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
42 static constexpr trace::Metric SelectionUsedRecovery(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
43 "selection_recovery", trace::Metric::Distribution);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
44 const auto *Common = S.commonAncestor();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
45 for (const auto *N = Common; N; N = N->Parent) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
46 if (N->ASTNode.get<RecoveryExpr>()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
47 SelectionUsedRecovery.record(1); // used recovery ast.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
48 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
49 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
50 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
51 if (Common)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
52 SelectionUsedRecovery.record(0); // unused.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
53 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
54
150
anatofuz
parents:
diff changeset
55 // An IntervalSet maintains a set of disjoint subranges of an array.
anatofuz
parents:
diff changeset
56 //
anatofuz
parents:
diff changeset
57 // Initially, it contains the entire array.
anatofuz
parents:
diff changeset
58 // [-----------------------------------------------------------]
anatofuz
parents:
diff changeset
59 //
anatofuz
parents:
diff changeset
60 // When a range is erased(), it will typically split the array in two.
anatofuz
parents:
diff changeset
61 // Claim: [--------------------]
anatofuz
parents:
diff changeset
62 // after: [----------------] [-------------------]
anatofuz
parents:
diff changeset
63 //
anatofuz
parents:
diff changeset
64 // erase() returns the segments actually erased. Given the state above:
anatofuz
parents:
diff changeset
65 // Claim: [---------------------------------------]
anatofuz
parents:
diff changeset
66 // Out: [---------] [------]
anatofuz
parents:
diff changeset
67 // After: [-----] [-----------]
anatofuz
parents:
diff changeset
68 //
anatofuz
parents:
diff changeset
69 // It is used to track (expanded) tokens not yet associated with an AST node.
anatofuz
parents:
diff changeset
70 // On traversing an AST node, its token range is erased from the unclaimed set.
anatofuz
parents:
diff changeset
71 // The tokens actually removed are associated with that node, and hit-tested
anatofuz
parents:
diff changeset
72 // against the selection to determine whether the node is selected.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
73 template <typename T> class IntervalSet {
150
anatofuz
parents:
diff changeset
74 public:
anatofuz
parents:
diff changeset
75 IntervalSet(llvm::ArrayRef<T> Range) { UnclaimedRanges.insert(Range); }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 // Removes the elements of Claim from the set, modifying or removing ranges
anatofuz
parents:
diff changeset
78 // that overlap it.
anatofuz
parents:
diff changeset
79 // Returns the continuous subranges of Claim that were actually removed.
anatofuz
parents:
diff changeset
80 llvm::SmallVector<llvm::ArrayRef<T>, 4> erase(llvm::ArrayRef<T> Claim) {
anatofuz
parents:
diff changeset
81 llvm::SmallVector<llvm::ArrayRef<T>, 4> Out;
anatofuz
parents:
diff changeset
82 if (Claim.empty())
anatofuz
parents:
diff changeset
83 return Out;
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 // General case:
anatofuz
parents:
diff changeset
86 // Claim: [-----------------]
anatofuz
parents:
diff changeset
87 // UnclaimedRanges: [-A-] [-B-] [-C-] [-D-] [-E-] [-F-] [-G-]
anatofuz
parents:
diff changeset
88 // Overlap: ^first ^second
anatofuz
parents:
diff changeset
89 // Ranges C and D are fully included. Ranges B and E must be trimmed.
anatofuz
parents:
diff changeset
90 auto Overlap = std::make_pair(
anatofuz
parents:
diff changeset
91 UnclaimedRanges.lower_bound({Claim.begin(), Claim.begin()}), // C
anatofuz
parents:
diff changeset
92 UnclaimedRanges.lower_bound({Claim.end(), Claim.end()})); // F
anatofuz
parents:
diff changeset
93 // Rewind to cover B.
anatofuz
parents:
diff changeset
94 if (Overlap.first != UnclaimedRanges.begin()) {
anatofuz
parents:
diff changeset
95 --Overlap.first;
anatofuz
parents:
diff changeset
96 // ...unless B isn't selected at all.
anatofuz
parents:
diff changeset
97 if (Overlap.first->end() <= Claim.begin())
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 ++Overlap.first;
150
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100 if (Overlap.first == Overlap.second)
anatofuz
parents:
diff changeset
101 return Out;
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 // First, copy all overlapping ranges into the output.
anatofuz
parents:
diff changeset
104 auto OutFirst = Out.insert(Out.end(), Overlap.first, Overlap.second);
anatofuz
parents:
diff changeset
105 // If any of the overlapping ranges were sliced by the claim, split them:
anatofuz
parents:
diff changeset
106 // - restrict the returned range to the claimed part
anatofuz
parents:
diff changeset
107 // - save the unclaimed part so it can be reinserted
anatofuz
parents:
diff changeset
108 llvm::ArrayRef<T> RemainingHead, RemainingTail;
anatofuz
parents:
diff changeset
109 if (Claim.begin() > OutFirst->begin()) {
anatofuz
parents:
diff changeset
110 RemainingHead = {OutFirst->begin(), Claim.begin()};
anatofuz
parents:
diff changeset
111 *OutFirst = {Claim.begin(), OutFirst->end()};
anatofuz
parents:
diff changeset
112 }
anatofuz
parents:
diff changeset
113 if (Claim.end() < Out.back().end()) {
anatofuz
parents:
diff changeset
114 RemainingTail = {Claim.end(), Out.back().end()};
anatofuz
parents:
diff changeset
115 Out.back() = {Out.back().begin(), Claim.end()};
anatofuz
parents:
diff changeset
116 }
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 // Erase all the overlapping ranges (invalidating all iterators).
anatofuz
parents:
diff changeset
119 UnclaimedRanges.erase(Overlap.first, Overlap.second);
anatofuz
parents:
diff changeset
120 // Reinsert ranges that were merely trimmed.
anatofuz
parents:
diff changeset
121 if (!RemainingHead.empty())
anatofuz
parents:
diff changeset
122 UnclaimedRanges.insert(RemainingHead);
anatofuz
parents:
diff changeset
123 if (!RemainingTail.empty())
anatofuz
parents:
diff changeset
124 UnclaimedRanges.insert(RemainingTail);
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 return Out;
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 private:
anatofuz
parents:
diff changeset
130 using TokenRange = llvm::ArrayRef<T>;
anatofuz
parents:
diff changeset
131 struct RangeLess {
anatofuz
parents:
diff changeset
132 bool operator()(llvm::ArrayRef<T> L, llvm::ArrayRef<T> R) const {
anatofuz
parents:
diff changeset
133 return L.begin() < R.begin();
anatofuz
parents:
diff changeset
134 }
anatofuz
parents:
diff changeset
135 };
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 // Disjoint sorted unclaimed ranges of expanded tokens.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
138 std::set<llvm::ArrayRef<T>, RangeLess> UnclaimedRanges;
150
anatofuz
parents:
diff changeset
139 };
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 // Sentinel value for the selectedness of a node where we've seen no tokens yet.
anatofuz
parents:
diff changeset
142 // This resolves to Unselected if no tokens are ever seen.
anatofuz
parents:
diff changeset
143 // But Unselected + Complete -> Partial, while NoTokens + Complete --> Complete.
anatofuz
parents:
diff changeset
144 // This value is never exposed publicly.
anatofuz
parents:
diff changeset
145 constexpr SelectionTree::Selection NoTokens =
anatofuz
parents:
diff changeset
146 static_cast<SelectionTree::Selection>(
anatofuz
parents:
diff changeset
147 static_cast<unsigned char>(SelectionTree::Complete + 1));
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 // Nodes start with NoTokens, and then use this function to aggregate the
anatofuz
parents:
diff changeset
150 // selectedness as more tokens are found.
anatofuz
parents:
diff changeset
151 void update(SelectionTree::Selection &Result, SelectionTree::Selection New) {
anatofuz
parents:
diff changeset
152 if (New == NoTokens)
anatofuz
parents:
diff changeset
153 return;
anatofuz
parents:
diff changeset
154 if (Result == NoTokens)
anatofuz
parents:
diff changeset
155 Result = New;
anatofuz
parents:
diff changeset
156 else if (Result != New)
anatofuz
parents:
diff changeset
157 // Can only be completely selected (or unselected) if all tokens are.
anatofuz
parents:
diff changeset
158 Result = SelectionTree::Partial;
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
161 // As well as comments, don't count semicolons as real tokens.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
162 // They're not properly claimed as expr-statement is missing from the AST.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
163 bool shouldIgnore(const syntax::Token &Tok) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
164 return Tok.kind() == tok::comment || Tok.kind() == tok::semi;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
165 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
166
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167 // Determine whether 'Target' is the first expansion of the macro
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
168 // argument whose top-level spelling location is 'SpellingLoc'.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
169 bool isFirstExpansion(FileID Target, SourceLocation SpellingLoc,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
170 const SourceManager &SM) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
171 SourceLocation Prev = SpellingLoc;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
172 while (true) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
173 // If the arg is expanded multiple times, getMacroArgExpandedLocation()
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
174 // returns the first expansion.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
175 SourceLocation Next = SM.getMacroArgExpandedLocation(Prev);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
176 // So if we reach the target, target is the first-expansion of the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
177 // first-expansion ...
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
178 if (SM.getFileID(Next) == Target)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
179 return true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
180
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
181 // Otherwise, if the FileID stops changing, we've reached the innermost
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
182 // macro expansion, and Target was on a different branch.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
183 if (SM.getFileID(Next) == SM.getFileID(Prev))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
184 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
185
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
186 Prev = Next;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
187 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
188 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
189 }
150
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 // SelectionTester can determine whether a range of tokens from the PP-expanded
anatofuz
parents:
diff changeset
192 // stream (corresponding to an AST node) is considered selected.
anatofuz
parents:
diff changeset
193 //
anatofuz
parents:
diff changeset
194 // When the tokens result from macro expansions, the appropriate tokens in the
anatofuz
parents:
diff changeset
195 // main file are examined (macro invocation or args). Similarly for #includes.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
196 // However, only the first expansion of a given spelled token is considered
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
197 // selected.
150
anatofuz
parents:
diff changeset
198 //
anatofuz
parents:
diff changeset
199 // It tests each token in the range (not just the endpoints) as contiguous
anatofuz
parents:
diff changeset
200 // expanded tokens may not have contiguous spellings (with macros).
anatofuz
parents:
diff changeset
201 //
anatofuz
parents:
diff changeset
202 // Non-token text, and tokens not modeled in the AST (comments, semicolons)
anatofuz
parents:
diff changeset
203 // are ignored when determining selectedness.
anatofuz
parents:
diff changeset
204 class SelectionTester {
anatofuz
parents:
diff changeset
205 public:
anatofuz
parents:
diff changeset
206 // The selection is offsets [SelBegin, SelEnd) in SelFile.
anatofuz
parents:
diff changeset
207 SelectionTester(const syntax::TokenBuffer &Buf, FileID SelFile,
anatofuz
parents:
diff changeset
208 unsigned SelBegin, unsigned SelEnd, const SourceManager &SM)
anatofuz
parents:
diff changeset
209 : SelFile(SelFile), SM(SM) {
anatofuz
parents:
diff changeset
210 // Find all tokens (partially) selected in the file.
anatofuz
parents:
diff changeset
211 auto AllSpelledTokens = Buf.spelledTokens(SelFile);
anatofuz
parents:
diff changeset
212 const syntax::Token *SelFirst =
anatofuz
parents:
diff changeset
213 llvm::partition_point(AllSpelledTokens, [&](const syntax::Token &Tok) {
anatofuz
parents:
diff changeset
214 return SM.getFileOffset(Tok.endLocation()) <= SelBegin;
anatofuz
parents:
diff changeset
215 });
anatofuz
parents:
diff changeset
216 const syntax::Token *SelLimit = std::partition_point(
anatofuz
parents:
diff changeset
217 SelFirst, AllSpelledTokens.end(), [&](const syntax::Token &Tok) {
anatofuz
parents:
diff changeset
218 return SM.getFileOffset(Tok.location()) < SelEnd;
anatofuz
parents:
diff changeset
219 });
anatofuz
parents:
diff changeset
220 // Precompute selectedness and offset for selected spelled tokens.
anatofuz
parents:
diff changeset
221 for (const syntax::Token *T = SelFirst; T < SelLimit; ++T) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
222 if (shouldIgnore(*T))
150
anatofuz
parents:
diff changeset
223 continue;
anatofuz
parents:
diff changeset
224 SpelledTokens.emplace_back();
anatofuz
parents:
diff changeset
225 Tok &S = SpelledTokens.back();
anatofuz
parents:
diff changeset
226 S.Offset = SM.getFileOffset(T->location());
anatofuz
parents:
diff changeset
227 if (S.Offset >= SelBegin && S.Offset + T->length() <= SelEnd)
anatofuz
parents:
diff changeset
228 S.Selected = SelectionTree::Complete;
anatofuz
parents:
diff changeset
229 else
anatofuz
parents:
diff changeset
230 S.Selected = SelectionTree::Partial;
anatofuz
parents:
diff changeset
231 }
anatofuz
parents:
diff changeset
232 }
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 // Test whether a consecutive range of tokens is selected.
anatofuz
parents:
diff changeset
235 // The tokens are taken from the expanded token stream.
anatofuz
parents:
diff changeset
236 SelectionTree::Selection
anatofuz
parents:
diff changeset
237 test(llvm::ArrayRef<syntax::Token> ExpandedTokens) const {
anatofuz
parents:
diff changeset
238 if (SpelledTokens.empty())
anatofuz
parents:
diff changeset
239 return NoTokens;
anatofuz
parents:
diff changeset
240 SelectionTree::Selection Result = NoTokens;
anatofuz
parents:
diff changeset
241 while (!ExpandedTokens.empty()) {
anatofuz
parents:
diff changeset
242 // Take consecutive tokens from the same context together for efficiency.
anatofuz
parents:
diff changeset
243 FileID FID = SM.getFileID(ExpandedTokens.front().location());
anatofuz
parents:
diff changeset
244 auto Batch = ExpandedTokens.take_while([&](const syntax::Token &T) {
anatofuz
parents:
diff changeset
245 return SM.getFileID(T.location()) == FID;
anatofuz
parents:
diff changeset
246 });
anatofuz
parents:
diff changeset
247 assert(!Batch.empty());
anatofuz
parents:
diff changeset
248 ExpandedTokens = ExpandedTokens.drop_front(Batch.size());
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 update(Result, testChunk(FID, Batch));
anatofuz
parents:
diff changeset
251 }
anatofuz
parents:
diff changeset
252 return Result;
anatofuz
parents:
diff changeset
253 }
anatofuz
parents:
diff changeset
254
anatofuz
parents:
diff changeset
255 // Cheap check whether any of the tokens in R might be selected.
anatofuz
parents:
diff changeset
256 // If it returns false, test() will return NoTokens or Unselected.
anatofuz
parents:
diff changeset
257 // If it returns true, test() may return any value.
anatofuz
parents:
diff changeset
258 bool mayHit(SourceRange R) const {
anatofuz
parents:
diff changeset
259 if (SpelledTokens.empty())
anatofuz
parents:
diff changeset
260 return false;
anatofuz
parents:
diff changeset
261 auto B = SM.getDecomposedLoc(R.getBegin());
anatofuz
parents:
diff changeset
262 auto E = SM.getDecomposedLoc(R.getEnd());
anatofuz
parents:
diff changeset
263 if (B.first == SelFile && E.first == SelFile)
anatofuz
parents:
diff changeset
264 if (E.second < SpelledTokens.front().Offset ||
anatofuz
parents:
diff changeset
265 B.second > SpelledTokens.back().Offset)
anatofuz
parents:
diff changeset
266 return false;
anatofuz
parents:
diff changeset
267 return true;
anatofuz
parents:
diff changeset
268 }
anatofuz
parents:
diff changeset
269
anatofuz
parents:
diff changeset
270 private:
anatofuz
parents:
diff changeset
271 // Hit-test a consecutive range of tokens from a single file ID.
anatofuz
parents:
diff changeset
272 SelectionTree::Selection
anatofuz
parents:
diff changeset
273 testChunk(FileID FID, llvm::ArrayRef<syntax::Token> Batch) const {
anatofuz
parents:
diff changeset
274 assert(!Batch.empty());
anatofuz
parents:
diff changeset
275 SourceLocation StartLoc = Batch.front().location();
anatofuz
parents:
diff changeset
276 // There are several possible categories of FileID depending on how the
anatofuz
parents:
diff changeset
277 // preprocessor was used to generate these tokens:
anatofuz
parents:
diff changeset
278 // main file, #included file, macro args, macro bodies.
anatofuz
parents:
diff changeset
279 // We need to identify the main-file tokens that represent Batch, and
anatofuz
parents:
diff changeset
280 // determine whether we want to exclusively claim them. Regular tokens
anatofuz
parents:
diff changeset
281 // represent one AST construct, but a macro invocation can represent many.
anatofuz
parents:
diff changeset
282
anatofuz
parents:
diff changeset
283 // Handle tokens written directly in the main file.
anatofuz
parents:
diff changeset
284 if (FID == SelFile) {
anatofuz
parents:
diff changeset
285 return testTokenRange(SM.getFileOffset(Batch.front().location()),
anatofuz
parents:
diff changeset
286 SM.getFileOffset(Batch.back().location()));
anatofuz
parents:
diff changeset
287 }
anatofuz
parents:
diff changeset
288
anatofuz
parents:
diff changeset
289 // Handle tokens in another file #included into the main file.
anatofuz
parents:
diff changeset
290 // Check if the #include is selected, but don't claim it exclusively.
anatofuz
parents:
diff changeset
291 if (StartLoc.isFileID()) {
anatofuz
parents:
diff changeset
292 for (SourceLocation Loc = Batch.front().location(); Loc.isValid();
anatofuz
parents:
diff changeset
293 Loc = SM.getIncludeLoc(SM.getFileID(Loc))) {
anatofuz
parents:
diff changeset
294 if (SM.getFileID(Loc) == SelFile)
anatofuz
parents:
diff changeset
295 // FIXME: use whole #include directive, not just the filename string.
anatofuz
parents:
diff changeset
296 return testToken(SM.getFileOffset(Loc));
anatofuz
parents:
diff changeset
297 }
anatofuz
parents:
diff changeset
298 return NoTokens;
anatofuz
parents:
diff changeset
299 }
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 assert(StartLoc.isMacroID());
anatofuz
parents:
diff changeset
302 // Handle tokens that were passed as a macro argument.
anatofuz
parents:
diff changeset
303 SourceLocation ArgStart = SM.getTopMacroCallerLoc(StartLoc);
anatofuz
parents:
diff changeset
304 if (SM.getFileID(ArgStart) == SelFile) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
305 if (isFirstExpansion(FID, ArgStart, SM)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
306 SourceLocation ArgEnd =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
307 SM.getTopMacroCallerLoc(Batch.back().location());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
308 return testTokenRange(SM.getFileOffset(ArgStart),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
309 SM.getFileOffset(ArgEnd));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
310 } else {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
311 /* fall through and treat as part of the macro body */
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
312 }
150
anatofuz
parents:
diff changeset
313 }
anatofuz
parents:
diff changeset
314
anatofuz
parents:
diff changeset
315 // Handle tokens produced by non-argument macro expansion.
anatofuz
parents:
diff changeset
316 // Check if the macro name is selected, don't claim it exclusively.
anatofuz
parents:
diff changeset
317 auto Expansion = SM.getDecomposedExpansionLoc(StartLoc);
anatofuz
parents:
diff changeset
318 if (Expansion.first == SelFile)
anatofuz
parents:
diff changeset
319 // FIXME: also check ( and ) for function-like macros?
anatofuz
parents:
diff changeset
320 return testToken(Expansion.second);
anatofuz
parents:
diff changeset
321 else
anatofuz
parents:
diff changeset
322 return NoTokens;
anatofuz
parents:
diff changeset
323 }
anatofuz
parents:
diff changeset
324
anatofuz
parents:
diff changeset
325 // Is the closed token range [Begin, End] selected?
anatofuz
parents:
diff changeset
326 SelectionTree::Selection testTokenRange(unsigned Begin, unsigned End) const {
anatofuz
parents:
diff changeset
327 assert(Begin <= End);
anatofuz
parents:
diff changeset
328 // Outside the selection entirely?
anatofuz
parents:
diff changeset
329 if (End < SpelledTokens.front().Offset ||
anatofuz
parents:
diff changeset
330 Begin > SpelledTokens.back().Offset)
anatofuz
parents:
diff changeset
331 return SelectionTree::Unselected;
anatofuz
parents:
diff changeset
332
anatofuz
parents:
diff changeset
333 // Compute range of tokens.
anatofuz
parents:
diff changeset
334 auto B = llvm::partition_point(
anatofuz
parents:
diff changeset
335 SpelledTokens, [&](const Tok &T) { return T.Offset < Begin; });
anatofuz
parents:
diff changeset
336 auto E = std::partition_point(
anatofuz
parents:
diff changeset
337 B, SpelledTokens.end(), [&](const Tok &T) { return T.Offset <= End; });
anatofuz
parents:
diff changeset
338
anatofuz
parents:
diff changeset
339 // Aggregate selectedness of tokens in range.
anatofuz
parents:
diff changeset
340 bool ExtendsOutsideSelection = Begin < SpelledTokens.front().Offset ||
anatofuz
parents:
diff changeset
341 End > SpelledTokens.back().Offset;
anatofuz
parents:
diff changeset
342 SelectionTree::Selection Result =
anatofuz
parents:
diff changeset
343 ExtendsOutsideSelection ? SelectionTree::Unselected : NoTokens;
anatofuz
parents:
diff changeset
344 for (auto It = B; It != E; ++It)
anatofuz
parents:
diff changeset
345 update(Result, It->Selected);
anatofuz
parents:
diff changeset
346 return Result;
anatofuz
parents:
diff changeset
347 }
anatofuz
parents:
diff changeset
348
anatofuz
parents:
diff changeset
349 // Is the token at `Offset` selected?
anatofuz
parents:
diff changeset
350 SelectionTree::Selection testToken(unsigned Offset) const {
anatofuz
parents:
diff changeset
351 // Outside the selection entirely?
anatofuz
parents:
diff changeset
352 if (Offset < SpelledTokens.front().Offset ||
anatofuz
parents:
diff changeset
353 Offset > SpelledTokens.back().Offset)
anatofuz
parents:
diff changeset
354 return SelectionTree::Unselected;
anatofuz
parents:
diff changeset
355 // Find the token, if it exists.
anatofuz
parents:
diff changeset
356 auto It = llvm::partition_point(
anatofuz
parents:
diff changeset
357 SpelledTokens, [&](const Tok &T) { return T.Offset < Offset; });
anatofuz
parents:
diff changeset
358 if (It != SpelledTokens.end() && It->Offset == Offset)
anatofuz
parents:
diff changeset
359 return It->Selected;
anatofuz
parents:
diff changeset
360 return NoTokens;
anatofuz
parents:
diff changeset
361 }
anatofuz
parents:
diff changeset
362
anatofuz
parents:
diff changeset
363 struct Tok {
anatofuz
parents:
diff changeset
364 unsigned Offset;
anatofuz
parents:
diff changeset
365 SelectionTree::Selection Selected;
anatofuz
parents:
diff changeset
366 };
anatofuz
parents:
diff changeset
367 std::vector<Tok> SpelledTokens;
anatofuz
parents:
diff changeset
368 FileID SelFile;
anatofuz
parents:
diff changeset
369 const SourceManager &SM;
anatofuz
parents:
diff changeset
370 };
anatofuz
parents:
diff changeset
371
anatofuz
parents:
diff changeset
372 // Show the type of a node for debugging.
anatofuz
parents:
diff changeset
373 void printNodeKind(llvm::raw_ostream &OS, const DynTypedNode &N) {
anatofuz
parents:
diff changeset
374 if (const TypeLoc *TL = N.get<TypeLoc>()) {
anatofuz
parents:
diff changeset
375 // TypeLoc is a hierarchy, but has only a single ASTNodeKind.
anatofuz
parents:
diff changeset
376 // Synthesize the name from the Type subclass (except for QualifiedTypeLoc).
anatofuz
parents:
diff changeset
377 if (TL->getTypeLocClass() == TypeLoc::Qualified)
anatofuz
parents:
diff changeset
378 OS << "QualifiedTypeLoc";
anatofuz
parents:
diff changeset
379 else
anatofuz
parents:
diff changeset
380 OS << TL->getType()->getTypeClassName() << "TypeLoc";
anatofuz
parents:
diff changeset
381 } else {
anatofuz
parents:
diff changeset
382 OS << N.getNodeKind().asStringRef();
anatofuz
parents:
diff changeset
383 }
anatofuz
parents:
diff changeset
384 }
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 #ifndef NDEBUG
anatofuz
parents:
diff changeset
387 std::string printNodeToString(const DynTypedNode &N, const PrintingPolicy &PP) {
anatofuz
parents:
diff changeset
388 std::string S;
anatofuz
parents:
diff changeset
389 llvm::raw_string_ostream OS(S);
anatofuz
parents:
diff changeset
390 printNodeKind(OS, N);
anatofuz
parents:
diff changeset
391 OS << " ";
anatofuz
parents:
diff changeset
392 return std::move(OS.str());
anatofuz
parents:
diff changeset
393 }
anatofuz
parents:
diff changeset
394 #endif
anatofuz
parents:
diff changeset
395
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
396 bool isImplicit(const Stmt *S) {
150
anatofuz
parents:
diff changeset
397 // Some Stmts are implicit and shouldn't be traversed, but there's no
anatofuz
parents:
diff changeset
398 // "implicit" attribute on Stmt/Expr.
anatofuz
parents:
diff changeset
399 // Unwrap implicit casts first if present (other nodes too?).
anatofuz
parents:
diff changeset
400 if (auto *ICE = llvm::dyn_cast<ImplicitCastExpr>(S))
anatofuz
parents:
diff changeset
401 S = ICE->getSubExprAsWritten();
anatofuz
parents:
diff changeset
402 // Implicit this in a MemberExpr is not filtered out by RecursiveASTVisitor.
anatofuz
parents:
diff changeset
403 // It would be nice if RAV handled this (!shouldTraverseImplicitCode()).
anatofuz
parents:
diff changeset
404 if (auto *CTI = llvm::dyn_cast<CXXThisExpr>(S))
anatofuz
parents:
diff changeset
405 if (CTI->isImplicit())
anatofuz
parents:
diff changeset
406 return true;
anatofuz
parents:
diff changeset
407 // Refs to operator() and [] are (almost?) always implicit as part of calls.
anatofuz
parents:
diff changeset
408 if (auto *DRE = llvm::dyn_cast<DeclRefExpr>(S)) {
anatofuz
parents:
diff changeset
409 if (auto *FD = llvm::dyn_cast<FunctionDecl>(DRE->getDecl())) {
anatofuz
parents:
diff changeset
410 switch (FD->getOverloadedOperator()) {
anatofuz
parents:
diff changeset
411 case OO_Call:
anatofuz
parents:
diff changeset
412 case OO_Subscript:
anatofuz
parents:
diff changeset
413 return true;
anatofuz
parents:
diff changeset
414 default:
anatofuz
parents:
diff changeset
415 break;
anatofuz
parents:
diff changeset
416 }
anatofuz
parents:
diff changeset
417 }
anatofuz
parents:
diff changeset
418 }
anatofuz
parents:
diff changeset
419 return false;
anatofuz
parents:
diff changeset
420 }
anatofuz
parents:
diff changeset
421
anatofuz
parents:
diff changeset
422 // We find the selection by visiting written nodes in the AST, looking for nodes
anatofuz
parents:
diff changeset
423 // that intersect with the selected character range.
anatofuz
parents:
diff changeset
424 //
anatofuz
parents:
diff changeset
425 // While traversing, we maintain a parent stack. As nodes pop off the stack,
anatofuz
parents:
diff changeset
426 // we decide whether to keep them or not. To be kept, they must either be
anatofuz
parents:
diff changeset
427 // selected or contain some nodes that are.
anatofuz
parents:
diff changeset
428 //
anatofuz
parents:
diff changeset
429 // For simple cases (not inside macros) we prune subtrees that don't intersect.
anatofuz
parents:
diff changeset
430 class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
anatofuz
parents:
diff changeset
431 public:
anatofuz
parents:
diff changeset
432 // Runs the visitor to gather selected nodes and their ancestors.
anatofuz
parents:
diff changeset
433 // If there is any selection, the root (TUDecl) is the first node.
anatofuz
parents:
diff changeset
434 static std::deque<Node> collect(ASTContext &AST,
anatofuz
parents:
diff changeset
435 const syntax::TokenBuffer &Tokens,
anatofuz
parents:
diff changeset
436 const PrintingPolicy &PP, unsigned Begin,
anatofuz
parents:
diff changeset
437 unsigned End, FileID File) {
anatofuz
parents:
diff changeset
438 SelectionVisitor V(AST, Tokens, PP, Begin, End, File);
anatofuz
parents:
diff changeset
439 V.TraverseAST(AST);
anatofuz
parents:
diff changeset
440 assert(V.Stack.size() == 1 && "Unpaired push/pop?");
anatofuz
parents:
diff changeset
441 assert(V.Stack.top() == &V.Nodes.front());
anatofuz
parents:
diff changeset
442 return std::move(V.Nodes);
anatofuz
parents:
diff changeset
443 }
anatofuz
parents:
diff changeset
444
anatofuz
parents:
diff changeset
445 // We traverse all "well-behaved" nodes the same way:
anatofuz
parents:
diff changeset
446 // - push the node onto the stack
anatofuz
parents:
diff changeset
447 // - traverse its children recursively
anatofuz
parents:
diff changeset
448 // - pop it from the stack
anatofuz
parents:
diff changeset
449 // - hit testing: is intersection(node, selection) - union(children) empty?
anatofuz
parents:
diff changeset
450 // - attach it to the tree if it or any children hit the selection
anatofuz
parents:
diff changeset
451 //
anatofuz
parents:
diff changeset
452 // Two categories of nodes are not "well-behaved":
anatofuz
parents:
diff changeset
453 // - those without source range information, we don't record those
anatofuz
parents:
diff changeset
454 // - those that can't be stored in DynTypedNode.
anatofuz
parents:
diff changeset
455 // We're missing some interesting things like Attr due to the latter.
anatofuz
parents:
diff changeset
456 bool TraverseDecl(Decl *X) {
anatofuz
parents:
diff changeset
457 if (X && isa<TranslationUnitDecl>(X))
anatofuz
parents:
diff changeset
458 return Base::TraverseDecl(X); // Already pushed by constructor.
anatofuz
parents:
diff changeset
459 // Base::TraverseDecl will suppress children, but not this node itself.
anatofuz
parents:
diff changeset
460 if (X && X->isImplicit())
anatofuz
parents:
diff changeset
461 return true;
anatofuz
parents:
diff changeset
462 return traverseNode(X, [&] { return Base::TraverseDecl(X); });
anatofuz
parents:
diff changeset
463 }
anatofuz
parents:
diff changeset
464 bool TraverseTypeLoc(TypeLoc X) {
anatofuz
parents:
diff changeset
465 return traverseNode(&X, [&] { return Base::TraverseTypeLoc(X); });
anatofuz
parents:
diff changeset
466 }
anatofuz
parents:
diff changeset
467 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc X) {
anatofuz
parents:
diff changeset
468 return traverseNode(
anatofuz
parents:
diff changeset
469 &X, [&] { return Base::TraverseNestedNameSpecifierLoc(X); });
anatofuz
parents:
diff changeset
470 }
anatofuz
parents:
diff changeset
471 bool TraverseConstructorInitializer(CXXCtorInitializer *X) {
anatofuz
parents:
diff changeset
472 return traverseNode(
anatofuz
parents:
diff changeset
473 X, [&] { return Base::TraverseConstructorInitializer(X); });
anatofuz
parents:
diff changeset
474 }
anatofuz
parents:
diff changeset
475 // Stmt is the same, but this form allows the data recursion optimization.
anatofuz
parents:
diff changeset
476 bool dataTraverseStmtPre(Stmt *X) {
anatofuz
parents:
diff changeset
477 if (!X || isImplicit(X))
anatofuz
parents:
diff changeset
478 return false;
anatofuz
parents:
diff changeset
479 auto N = DynTypedNode::create(*X);
anatofuz
parents:
diff changeset
480 if (canSafelySkipNode(N))
anatofuz
parents:
diff changeset
481 return false;
anatofuz
parents:
diff changeset
482 push(std::move(N));
anatofuz
parents:
diff changeset
483 if (shouldSkipChildren(X)) {
anatofuz
parents:
diff changeset
484 pop();
anatofuz
parents:
diff changeset
485 return false;
anatofuz
parents:
diff changeset
486 }
anatofuz
parents:
diff changeset
487 return true;
anatofuz
parents:
diff changeset
488 }
anatofuz
parents:
diff changeset
489 bool dataTraverseStmtPost(Stmt *X) {
anatofuz
parents:
diff changeset
490 pop();
anatofuz
parents:
diff changeset
491 return true;
anatofuz
parents:
diff changeset
492 }
anatofuz
parents:
diff changeset
493 // QualifiedTypeLoc is handled strangely in RecursiveASTVisitor: the derived
anatofuz
parents:
diff changeset
494 // TraverseTypeLoc is not called for the inner UnqualTypeLoc.
anatofuz
parents:
diff changeset
495 // This means we'd never see 'int' in 'const int'! Work around that here.
anatofuz
parents:
diff changeset
496 // (The reason for the behavior is to avoid traversing the nested Type twice,
anatofuz
parents:
diff changeset
497 // but we ignore TraverseType anyway).
anatofuz
parents:
diff changeset
498 bool TraverseQualifiedTypeLoc(QualifiedTypeLoc QX) {
anatofuz
parents:
diff changeset
499 return traverseNode<TypeLoc>(
anatofuz
parents:
diff changeset
500 &QX, [&] { return TraverseTypeLoc(QX.getUnqualifiedLoc()); });
anatofuz
parents:
diff changeset
501 }
anatofuz
parents:
diff changeset
502 // Uninteresting parts of the AST that don't have locations within them.
anatofuz
parents:
diff changeset
503 bool TraverseNestedNameSpecifier(NestedNameSpecifier *) { return true; }
anatofuz
parents:
diff changeset
504 bool TraverseType(QualType) { return true; }
anatofuz
parents:
diff changeset
505
anatofuz
parents:
diff changeset
506 // The DeclStmt for the loop variable claims to cover the whole range
anatofuz
parents:
diff changeset
507 // inside the parens, this causes the range-init expression to not be hit.
anatofuz
parents:
diff changeset
508 // Traverse the loop VarDecl instead, which has the right source range.
anatofuz
parents:
diff changeset
509 bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) {
anatofuz
parents:
diff changeset
510 return traverseNode(S, [&] {
anatofuz
parents:
diff changeset
511 return TraverseStmt(S->getInit()) && TraverseDecl(S->getLoopVariable()) &&
anatofuz
parents:
diff changeset
512 TraverseStmt(S->getRangeInit()) && TraverseStmt(S->getBody());
anatofuz
parents:
diff changeset
513 });
anatofuz
parents:
diff changeset
514 }
anatofuz
parents:
diff changeset
515 // OpaqueValueExpr blocks traversal, we must explicitly traverse it.
anatofuz
parents:
diff changeset
516 bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
anatofuz
parents:
diff changeset
517 return traverseNode(E, [&] { return TraverseStmt(E->getSourceExpr()); });
anatofuz
parents:
diff changeset
518 }
anatofuz
parents:
diff changeset
519 // We only want to traverse the *syntactic form* to understand the selection.
anatofuz
parents:
diff changeset
520 bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
anatofuz
parents:
diff changeset
521 return traverseNode(E, [&] { return TraverseStmt(E->getSyntacticForm()); });
anatofuz
parents:
diff changeset
522 }
anatofuz
parents:
diff changeset
523
anatofuz
parents:
diff changeset
524 private:
anatofuz
parents:
diff changeset
525 using Base = RecursiveASTVisitor<SelectionVisitor>;
anatofuz
parents:
diff changeset
526
anatofuz
parents:
diff changeset
527 SelectionVisitor(ASTContext &AST, const syntax::TokenBuffer &Tokens,
anatofuz
parents:
diff changeset
528 const PrintingPolicy &PP, unsigned SelBegin, unsigned SelEnd,
anatofuz
parents:
diff changeset
529 FileID SelFile)
anatofuz
parents:
diff changeset
530 : SM(AST.getSourceManager()), LangOpts(AST.getLangOpts()),
anatofuz
parents:
diff changeset
531 #ifndef NDEBUG
anatofuz
parents:
diff changeset
532 PrintPolicy(PP),
anatofuz
parents:
diff changeset
533 #endif
anatofuz
parents:
diff changeset
534 TokenBuf(Tokens), SelChecker(Tokens, SelFile, SelBegin, SelEnd, SM),
anatofuz
parents:
diff changeset
535 UnclaimedExpandedTokens(Tokens.expandedTokens()) {
anatofuz
parents:
diff changeset
536 // Ensure we have a node for the TU decl, regardless of traversal scope.
anatofuz
parents:
diff changeset
537 Nodes.emplace_back();
anatofuz
parents:
diff changeset
538 Nodes.back().ASTNode = DynTypedNode::create(*AST.getTranslationUnitDecl());
anatofuz
parents:
diff changeset
539 Nodes.back().Parent = nullptr;
anatofuz
parents:
diff changeset
540 Nodes.back().Selected = SelectionTree::Unselected;
anatofuz
parents:
diff changeset
541 Stack.push(&Nodes.back());
anatofuz
parents:
diff changeset
542 }
anatofuz
parents:
diff changeset
543
anatofuz
parents:
diff changeset
544 // Generic case of TraverseFoo. Func should be the call to Base::TraverseFoo.
anatofuz
parents:
diff changeset
545 // Node is always a pointer so the generic code can handle any null checks.
anatofuz
parents:
diff changeset
546 template <typename T, typename Func>
anatofuz
parents:
diff changeset
547 bool traverseNode(T *Node, const Func &Body) {
anatofuz
parents:
diff changeset
548 if (Node == nullptr)
anatofuz
parents:
diff changeset
549 return true;
anatofuz
parents:
diff changeset
550 auto N = DynTypedNode::create(*Node);
anatofuz
parents:
diff changeset
551 if (canSafelySkipNode(N))
anatofuz
parents:
diff changeset
552 return true;
anatofuz
parents:
diff changeset
553 push(DynTypedNode::create(*Node));
anatofuz
parents:
diff changeset
554 bool Ret = Body();
anatofuz
parents:
diff changeset
555 pop();
anatofuz
parents:
diff changeset
556 return Ret;
anatofuz
parents:
diff changeset
557 }
anatofuz
parents:
diff changeset
558
anatofuz
parents:
diff changeset
559 // HIT TESTING
anatofuz
parents:
diff changeset
560 //
anatofuz
parents:
diff changeset
561 // We do rough hit testing on the way down the tree to avoid traversing
anatofuz
parents:
diff changeset
562 // subtrees that don't touch the selection (canSafelySkipNode), but
anatofuz
parents:
diff changeset
563 // fine-grained hit-testing is mostly done on the way back up (in pop()).
anatofuz
parents:
diff changeset
564 // This means children get to claim parts of the selection first, and parents
anatofuz
parents:
diff changeset
565 // are only selected if they own tokens that no child owned.
anatofuz
parents:
diff changeset
566 //
anatofuz
parents:
diff changeset
567 // Nodes *usually* nest nicely: a child's getSourceRange() lies within the
anatofuz
parents:
diff changeset
568 // parent's, and a node (transitively) owns all tokens in its range.
anatofuz
parents:
diff changeset
569 //
anatofuz
parents:
diff changeset
570 // Exception 1: child range claims tokens that should be owned by the parent.
anatofuz
parents:
diff changeset
571 // e.g. in `void foo(int);`, the FunctionTypeLoc should own
anatofuz
parents:
diff changeset
572 // `void (int)` but the parent FunctionDecl should own `foo`.
anatofuz
parents:
diff changeset
573 // To handle this case, certain nodes claim small token ranges *before*
anatofuz
parents:
diff changeset
574 // their children are traversed. (see earlySourceRange).
anatofuz
parents:
diff changeset
575 //
anatofuz
parents:
diff changeset
576 // Exception 2: siblings both claim the same node.
anatofuz
parents:
diff changeset
577 // e.g. `int x, y;` produces two sibling VarDecls.
anatofuz
parents:
diff changeset
578 // ~~~~~ x
anatofuz
parents:
diff changeset
579 // ~~~~~~~~ y
anatofuz
parents:
diff changeset
580 // Here the first ("leftmost") sibling claims the tokens it wants, and the
anatofuz
parents:
diff changeset
581 // other sibling gets what's left. So selecting "int" only includes the left
anatofuz
parents:
diff changeset
582 // VarDecl in the selection tree.
anatofuz
parents:
diff changeset
583
anatofuz
parents:
diff changeset
584 // An optimization for a common case: nodes outside macro expansions that
anatofuz
parents:
diff changeset
585 // don't intersect the selection may be recursively skipped.
anatofuz
parents:
diff changeset
586 bool canSafelySkipNode(const DynTypedNode &N) {
anatofuz
parents:
diff changeset
587 SourceRange S = N.getSourceRange();
anatofuz
parents:
diff changeset
588 if (auto *TL = N.get<TypeLoc>()) {
anatofuz
parents:
diff changeset
589 // DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
anatofuz
parents:
diff changeset
590 // failing
anatofuz
parents:
diff changeset
591 // to descend into the child expression.
anatofuz
parents:
diff changeset
592 // decltype(2+2);
anatofuz
parents:
diff changeset
593 // ~~~~~~~~~~~~~ <-- correct range
anatofuz
parents:
diff changeset
594 // ~~~~~~~~ <-- range reported by getSourceRange()
anatofuz
parents:
diff changeset
595 // ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
anatofuz
parents:
diff changeset
596 // FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
anatofuz
parents:
diff changeset
597 // rid of this patch.
anatofuz
parents:
diff changeset
598 if (auto DT = TL->getAs<DecltypeTypeLoc>())
anatofuz
parents:
diff changeset
599 S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
anatofuz
parents:
diff changeset
600 }
anatofuz
parents:
diff changeset
601 if (!SelChecker.mayHit(S)) {
anatofuz
parents:
diff changeset
602 dlog("{1}skip: {0}", printNodeToString(N, PrintPolicy), indent());
anatofuz
parents:
diff changeset
603 dlog("{1}skipped range = {0}", S.printToString(SM), indent(1));
anatofuz
parents:
diff changeset
604 return true;
anatofuz
parents:
diff changeset
605 }
anatofuz
parents:
diff changeset
606 return false;
anatofuz
parents:
diff changeset
607 }
anatofuz
parents:
diff changeset
608
anatofuz
parents:
diff changeset
609 // There are certain nodes we want to treat as leaves in the SelectionTree,
anatofuz
parents:
diff changeset
610 // although they do have children.
anatofuz
parents:
diff changeset
611 bool shouldSkipChildren(const Stmt *X) const {
anatofuz
parents:
diff changeset
612 // UserDefinedLiteral (e.g. 12_i) has two children (12 and _i).
anatofuz
parents:
diff changeset
613 // Unfortunately TokenBuffer sees 12_i as one token and can't split it.
anatofuz
parents:
diff changeset
614 // So we treat UserDefinedLiteral as a leaf node, owning the token.
anatofuz
parents:
diff changeset
615 return llvm::isa<UserDefinedLiteral>(X);
anatofuz
parents:
diff changeset
616 }
anatofuz
parents:
diff changeset
617
anatofuz
parents:
diff changeset
618 // Pushes a node onto the ancestor stack. Pairs with pop().
anatofuz
parents:
diff changeset
619 // Performs early hit detection for some nodes (on the earlySourceRange).
anatofuz
parents:
diff changeset
620 void push(DynTypedNode Node) {
anatofuz
parents:
diff changeset
621 SourceRange Early = earlySourceRange(Node);
anatofuz
parents:
diff changeset
622 dlog("{1}push: {0}", printNodeToString(Node, PrintPolicy), indent());
anatofuz
parents:
diff changeset
623 Nodes.emplace_back();
anatofuz
parents:
diff changeset
624 Nodes.back().ASTNode = std::move(Node);
anatofuz
parents:
diff changeset
625 Nodes.back().Parent = Stack.top();
anatofuz
parents:
diff changeset
626 Nodes.back().Selected = NoTokens;
anatofuz
parents:
diff changeset
627 Stack.push(&Nodes.back());
anatofuz
parents:
diff changeset
628 claimRange(Early, Nodes.back().Selected);
anatofuz
parents:
diff changeset
629 }
anatofuz
parents:
diff changeset
630
anatofuz
parents:
diff changeset
631 // Pops a node off the ancestor stack, and finalizes it. Pairs with push().
anatofuz
parents:
diff changeset
632 // Performs primary hit detection.
anatofuz
parents:
diff changeset
633 void pop() {
anatofuz
parents:
diff changeset
634 Node &N = *Stack.top();
anatofuz
parents:
diff changeset
635 dlog("{1}pop: {0}", printNodeToString(N.ASTNode, PrintPolicy), indent(-1));
anatofuz
parents:
diff changeset
636 claimRange(N.ASTNode.getSourceRange(), N.Selected);
anatofuz
parents:
diff changeset
637 if (N.Selected == NoTokens)
anatofuz
parents:
diff changeset
638 N.Selected = SelectionTree::Unselected;
anatofuz
parents:
diff changeset
639 if (N.Selected || !N.Children.empty()) {
anatofuz
parents:
diff changeset
640 // Attach to the tree.
anatofuz
parents:
diff changeset
641 N.Parent->Children.push_back(&N);
anatofuz
parents:
diff changeset
642 } else {
anatofuz
parents:
diff changeset
643 // Neither N any children are selected, it doesn't belong in the tree.
anatofuz
parents:
diff changeset
644 assert(&N == &Nodes.back());
anatofuz
parents:
diff changeset
645 Nodes.pop_back();
anatofuz
parents:
diff changeset
646 }
anatofuz
parents:
diff changeset
647 Stack.pop();
anatofuz
parents:
diff changeset
648 }
anatofuz
parents:
diff changeset
649
anatofuz
parents:
diff changeset
650 // Returns the range of tokens that this node will claim directly, and
anatofuz
parents:
diff changeset
651 // is not available to the node's children.
anatofuz
parents:
diff changeset
652 // Usually empty, but sometimes children cover tokens but shouldn't own them.
anatofuz
parents:
diff changeset
653 SourceRange earlySourceRange(const DynTypedNode &N) {
anatofuz
parents:
diff changeset
654 if (const Decl *D = N.get<Decl>()) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
655 // We want constructor name to be claimed by TypeLoc not the constructor
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
656 // itself. Similar for deduction guides, we rather want to select the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
657 // underlying TypeLoc.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
658 // FIXME: Unfortunately this doesn't work, even though RecursiveASTVisitor
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
659 // traverses the underlying TypeLoc inside DeclarationName, it is null for
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
660 // constructors.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
661 if (isa<CXXConstructorDecl>(D) || isa<CXXDeductionGuideDecl>(D))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
662 return SourceRange();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
663 // This will capture Field, Function, MSProperty, NonTypeTemplateParm and
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
664 // VarDecls. We want the name in the declarator to be claimed by the decl
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
665 // and not by any children. For example:
150
anatofuz
parents:
diff changeset
666 // void [[foo]]();
anatofuz
parents:
diff changeset
667 // int (*[[s]])();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
668 // struct X { int [[hash]] [32]; [[operator]] int();}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
669 if (const auto *DD = llvm::dyn_cast<DeclaratorDecl>(D))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
670 return DD->getLocation();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
671 } else if (const auto *CCI = N.get<CXXCtorInitializer>()) {
150
anatofuz
parents:
diff changeset
672 // : [[b_]](42)
anatofuz
parents:
diff changeset
673 return CCI->getMemberLocation();
anatofuz
parents:
diff changeset
674 }
anatofuz
parents:
diff changeset
675 return SourceRange();
anatofuz
parents:
diff changeset
676 }
anatofuz
parents:
diff changeset
677
anatofuz
parents:
diff changeset
678 // Perform hit-testing of a complete Node against the selection.
anatofuz
parents:
diff changeset
679 // This runs for every node in the AST, and must be fast in common cases.
anatofuz
parents:
diff changeset
680 // This is usually called from pop(), so we can take children into account.
anatofuz
parents:
diff changeset
681 // The existing state of Result is relevant (early/late claims can interact).
anatofuz
parents:
diff changeset
682 void claimRange(SourceRange S, SelectionTree::Selection &Result) {
anatofuz
parents:
diff changeset
683 for (const auto &ClaimedRange :
anatofuz
parents:
diff changeset
684 UnclaimedExpandedTokens.erase(TokenBuf.expandedTokens(S)))
anatofuz
parents:
diff changeset
685 update(Result, SelChecker.test(ClaimedRange));
anatofuz
parents:
diff changeset
686
anatofuz
parents:
diff changeset
687 if (Result && Result != NoTokens)
anatofuz
parents:
diff changeset
688 dlog("{1}hit selection: {0}", S.printToString(SM), indent());
anatofuz
parents:
diff changeset
689 }
anatofuz
parents:
diff changeset
690
anatofuz
parents:
diff changeset
691 std::string indent(int Offset = 0) {
anatofuz
parents:
diff changeset
692 // Cast for signed arithmetic.
anatofuz
parents:
diff changeset
693 int Amount = int(Stack.size()) + Offset;
anatofuz
parents:
diff changeset
694 assert(Amount >= 0);
anatofuz
parents:
diff changeset
695 return std::string(Amount, ' ');
anatofuz
parents:
diff changeset
696 }
anatofuz
parents:
diff changeset
697
anatofuz
parents:
diff changeset
698 SourceManager &SM;
anatofuz
parents:
diff changeset
699 const LangOptions &LangOpts;
anatofuz
parents:
diff changeset
700 #ifndef NDEBUG
anatofuz
parents:
diff changeset
701 const PrintingPolicy &PrintPolicy;
anatofuz
parents:
diff changeset
702 #endif
anatofuz
parents:
diff changeset
703 const syntax::TokenBuffer &TokenBuf;
anatofuz
parents:
diff changeset
704 std::stack<Node *> Stack;
anatofuz
parents:
diff changeset
705 SelectionTester SelChecker;
anatofuz
parents:
diff changeset
706 IntervalSet<syntax::Token> UnclaimedExpandedTokens;
anatofuz
parents:
diff changeset
707 std::deque<Node> Nodes; // Stable pointers as we add more nodes.
anatofuz
parents:
diff changeset
708 };
anatofuz
parents:
diff changeset
709
anatofuz
parents:
diff changeset
710 } // namespace
anatofuz
parents:
diff changeset
711
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
712 llvm::SmallString<256> abbreviatedString(DynTypedNode N,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
713 const PrintingPolicy &PP) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
714 llvm::SmallString<256> Result;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
715 {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
716 llvm::raw_svector_ostream OS(Result);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
717 N.print(OS, PP);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
718 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
719 auto Pos = Result.find('\n');
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
720 if (Pos != llvm::StringRef::npos) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
721 bool MoreText =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
722 !llvm::all_of(llvm::StringRef(Result).drop_front(Pos), llvm::isSpace);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
723 Result.resize(Pos);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
724 if (MoreText)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
725 Result.append(" …");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
726 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
727 return Result;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
728 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
729
150
anatofuz
parents:
diff changeset
730 void SelectionTree::print(llvm::raw_ostream &OS, const SelectionTree::Node &N,
anatofuz
parents:
diff changeset
731 int Indent) const {
anatofuz
parents:
diff changeset
732 if (N.Selected)
anatofuz
parents:
diff changeset
733 OS.indent(Indent - 1) << (N.Selected == SelectionTree::Complete ? '*'
anatofuz
parents:
diff changeset
734 : '.');
anatofuz
parents:
diff changeset
735 else
anatofuz
parents:
diff changeset
736 OS.indent(Indent);
anatofuz
parents:
diff changeset
737 printNodeKind(OS, N.ASTNode);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
738 OS << ' ' << abbreviatedString(N.ASTNode, PrintPolicy) << "\n";
150
anatofuz
parents:
diff changeset
739 for (const Node *Child : N.Children)
anatofuz
parents:
diff changeset
740 print(OS, *Child, Indent + 2);
anatofuz
parents:
diff changeset
741 }
anatofuz
parents:
diff changeset
742
anatofuz
parents:
diff changeset
743 std::string SelectionTree::Node::kind() const {
anatofuz
parents:
diff changeset
744 std::string S;
anatofuz
parents:
diff changeset
745 llvm::raw_string_ostream OS(S);
anatofuz
parents:
diff changeset
746 printNodeKind(OS, ASTNode);
anatofuz
parents:
diff changeset
747 return std::move(OS.str());
anatofuz
parents:
diff changeset
748 }
anatofuz
parents:
diff changeset
749
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
750 // Decide which selections emulate a "point" query in between characters.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
751 // If it's ambiguous (the neighboring characters are selectable tokens), returns
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
752 // both possibilities in preference order.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
753 // Always returns at least one range - if no tokens touched, and empty range.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
754 static llvm::SmallVector<std::pair<unsigned, unsigned>, 2>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
755 pointBounds(unsigned Offset, const syntax::TokenBuffer &Tokens) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
756 const auto &SM = Tokens.sourceManager();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
757 SourceLocation Loc = SM.getComposedLoc(SM.getMainFileID(), Offset);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
758 llvm::SmallVector<std::pair<unsigned, unsigned>, 2> Result;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
759 // Prefer right token over left.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
760 for (const syntax::Token &Tok :
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
761 llvm::reverse(spelledTokensTouching(Loc, Tokens))) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
762 if (shouldIgnore(Tok))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
763 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
764 unsigned Offset = Tokens.sourceManager().getFileOffset(Tok.location());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
765 Result.emplace_back(Offset, Offset + Tok.length());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
766 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
767 if (Result.empty())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
768 Result.emplace_back(Offset, Offset);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
769 return Result;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
770 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
771
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
772 bool SelectionTree::createEach(ASTContext &AST,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
773 const syntax::TokenBuffer &Tokens,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
774 unsigned Begin, unsigned End,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
775 llvm::function_ref<bool(SelectionTree)> Func) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
776 if (Begin != End)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
777 return Func(SelectionTree(AST, Tokens, Begin, End));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
778 for (std::pair<unsigned, unsigned> Bounds : pointBounds(Begin, Tokens))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
779 if (Func(SelectionTree(AST, Tokens, Bounds.first, Bounds.second)))
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
780 return true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
781 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
782 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
783
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
784 SelectionTree SelectionTree::createRight(ASTContext &AST,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
785 const syntax::TokenBuffer &Tokens,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
786 unsigned int Begin, unsigned int End) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
787 llvm::Optional<SelectionTree> Result;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
788 createEach(AST, Tokens, Begin, End, [&](SelectionTree T) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
789 Result = std::move(T);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
790 return true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
791 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
792 return std::move(*Result);
150
anatofuz
parents:
diff changeset
793 }
anatofuz
parents:
diff changeset
794
anatofuz
parents:
diff changeset
795 SelectionTree::SelectionTree(ASTContext &AST, const syntax::TokenBuffer &Tokens,
anatofuz
parents:
diff changeset
796 unsigned Begin, unsigned End)
anatofuz
parents:
diff changeset
797 : PrintPolicy(AST.getLangOpts()) {
anatofuz
parents:
diff changeset
798 // No fundamental reason the selection needs to be in the main file,
anatofuz
parents:
diff changeset
799 // but that's all clangd has needed so far.
anatofuz
parents:
diff changeset
800 const SourceManager &SM = AST.getSourceManager();
anatofuz
parents:
diff changeset
801 FileID FID = SM.getMainFileID();
anatofuz
parents:
diff changeset
802 PrintPolicy.TerseOutput = true;
anatofuz
parents:
diff changeset
803 PrintPolicy.IncludeNewlines = false;
anatofuz
parents:
diff changeset
804
anatofuz
parents:
diff changeset
805 dlog("Computing selection for {0}",
anatofuz
parents:
diff changeset
806 SourceRange(SM.getComposedLoc(FID, Begin), SM.getComposedLoc(FID, End))
anatofuz
parents:
diff changeset
807 .printToString(SM));
anatofuz
parents:
diff changeset
808 Nodes = SelectionVisitor::collect(AST, Tokens, PrintPolicy, Begin, End, FID);
anatofuz
parents:
diff changeset
809 Root = Nodes.empty() ? nullptr : &Nodes.front();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
810 recordMetrics(*this);
150
anatofuz
parents:
diff changeset
811 dlog("Built selection tree\n{0}", *this);
anatofuz
parents:
diff changeset
812 }
anatofuz
parents:
diff changeset
813
anatofuz
parents:
diff changeset
814 const Node *SelectionTree::commonAncestor() const {
anatofuz
parents:
diff changeset
815 const Node *Ancestor = Root;
anatofuz
parents:
diff changeset
816 while (Ancestor->Children.size() == 1 && !Ancestor->Selected)
anatofuz
parents:
diff changeset
817 Ancestor = Ancestor->Children.front();
anatofuz
parents:
diff changeset
818 // Returning nullptr here is a bit unprincipled, but it makes the API safer:
anatofuz
parents:
diff changeset
819 // the TranslationUnitDecl contains all of the preamble, so traversing it is a
anatofuz
parents:
diff changeset
820 // performance cliff. Callers can check for null and use root() if they want.
anatofuz
parents:
diff changeset
821 return Ancestor != Root ? Ancestor : nullptr;
anatofuz
parents:
diff changeset
822 }
anatofuz
parents:
diff changeset
823
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
824 const DeclContext &SelectionTree::Node::getDeclContext() const {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
825 for (const Node *CurrentNode = this; CurrentNode != nullptr;
150
anatofuz
parents:
diff changeset
826 CurrentNode = CurrentNode->Parent) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
827 if (const Decl *Current = CurrentNode->ASTNode.get<Decl>()) {
150
anatofuz
parents:
diff changeset
828 if (CurrentNode != this)
anatofuz
parents:
diff changeset
829 if (auto *DC = dyn_cast<DeclContext>(Current))
anatofuz
parents:
diff changeset
830 return *DC;
anatofuz
parents:
diff changeset
831 return *Current->getDeclContext();
anatofuz
parents:
diff changeset
832 }
anatofuz
parents:
diff changeset
833 }
anatofuz
parents:
diff changeset
834 llvm_unreachable("A tree must always be rooted at TranslationUnitDecl.");
anatofuz
parents:
diff changeset
835 }
anatofuz
parents:
diff changeset
836
anatofuz
parents:
diff changeset
837 const SelectionTree::Node &SelectionTree::Node::ignoreImplicit() const {
anatofuz
parents:
diff changeset
838 if (Children.size() == 1 &&
anatofuz
parents:
diff changeset
839 Children.front()->ASTNode.getSourceRange() == ASTNode.getSourceRange())
anatofuz
parents:
diff changeset
840 return Children.front()->ignoreImplicit();
anatofuz
parents:
diff changeset
841 return *this;
anatofuz
parents:
diff changeset
842 }
anatofuz
parents:
diff changeset
843
anatofuz
parents:
diff changeset
844 const SelectionTree::Node &SelectionTree::Node::outerImplicit() const {
anatofuz
parents:
diff changeset
845 if (Parent && Parent->ASTNode.getSourceRange() == ASTNode.getSourceRange())
anatofuz
parents:
diff changeset
846 return Parent->outerImplicit();
anatofuz
parents:
diff changeset
847 return *this;
anatofuz
parents:
diff changeset
848 }
anatofuz
parents:
diff changeset
849
anatofuz
parents:
diff changeset
850 } // namespace clangd
anatofuz
parents:
diff changeset
851 } // namespace clang