clang: lib/Tooling/Refactoring/ASTSelection.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
12#include "llvm/Support/SaveAndRestore.h"
13#include
14
15using namespace clang;
17
18namespace {
19
24
25
28 R.getEnd(), tok::raw_identifier, SM, LangOpts,
29 false);
30 return LocAfterEnd.isValid()
33}
34
35
36
37class ASTSelectionFinder
39public:
40 ASTSelectionFinder(SourceRange Selection, FileID TargetFile,
41 const ASTContext &Context)
42 : LexicallyOrderedRecursiveASTVisitor(Context.getSourceManager()),
43 SelectionBegin(Selection.getBegin()),
44 SelectionEnd(Selection.getBegin() == Selection.getEnd()
45 ? SourceLocation()
46 : Selection.getEnd()),
47 TargetFile(TargetFile), Context(Context) {
48
49 SelectionStack.push_back(
50 SelectedASTNode(DynTypedNode::create(*Context.getTranslationUnitDecl()),
51 SourceSelectionKind::None));
52 }
53
54 std::optional getSelectedASTNode() {
55 assert(SelectionStack.size() == 1 && "stack was not popped");
56 SelectedASTNode Result = std::move(SelectionStack.back());
57 SelectionStack.pop_back();
58 if (Result.Children.empty())
59 return std::nullopt;
60 return std::move(Result);
61 }
62
63 bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
64
65
66
67 llvm::SaveAndRestore LookThrough(LookThroughOpaqueValueExprs, true);
69 }
70
71 bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
72 if (!LookThroughOpaqueValueExprs)
73 return true;
74 llvm::SaveAndRestore LookThrough(LookThroughOpaqueValueExprs, false);
76 }
77
78 bool TraverseDecl(Decl *D) {
80 return LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
82 return true;
83
84
86 const SourceManager &SM = Context.getSourceManager();
87 SourceLocation FileLoc;
89 FileLoc = DeclRange.getEnd();
90 else
91 FileLoc = SM.getSpellingLoc(DeclRange.getBegin());
92 if (SM.getFileID(FileLoc) != TargetFile)
93 return true;
94
96 selectionKindFor(getLexicalDeclRange(D, SM, Context.getLangOpts()));
97 SelectionStack.push_back(
98 SelectedASTNode(DynTypedNode::create(*D), SelectionKind));
99 LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
100 popAndAddToSelectionIfSelected(SelectionKind);
101
103 SM.isBeforeInTranslationUnit(SelectionEnd.isValid() ? SelectionEnd
104 : SelectionBegin,
105 DeclRange.getEnd())) {
106
107 return false;
108 }
109 return true;
110 }
111
112 bool TraverseStmt(Stmt *S) {
113 if (!S)
114 return true;
115 if (auto *Opaque = dyn_cast(S))
116 return TraverseOpaqueValueExpr(Opaque);
117
118 if (auto *TE = dyn_cast(S)) {
119 if (TE->isImplicit())
120 return true;
121 }
122
125 SelectionStack.push_back(
126 SelectedASTNode(DynTypedNode::create(*S), SelectionKind));
127 LexicallyOrderedRecursiveASTVisitor::TraverseStmt(S);
128 popAndAddToSelectionIfSelected(SelectionKind);
129 return true;
130 }
131
132private:
134 SelectedASTNode Node = std::move(SelectionStack.back());
135 SelectionStack.pop_back();
136 if (SelectionKind != SourceSelectionKind::None || !Node.Children.empty())
137 SelectionStack.back().Children.push_back(std::move(Node));
138 }
139
141 SourceLocation End = Range.getEnd();
142 const SourceManager &SM = Context.getSourceManager();
143 if (Range.isTokenRange())
145 if (!SourceLocation::isPairOfFileLocations(Range.getBegin(), End))
146 return SourceSelectionKind::None;
147 if (!SelectionEnd.isValid()) {
148
149 if (SM.isPointWithin(SelectionBegin, Range.getBegin(), End))
150 return SourceSelectionKind::ContainsSelection;
151 return SourceSelectionKind::None;
152 }
153 bool HasStart = SM.isPointWithin(SelectionBegin, Range.getBegin(), End);
154 bool HasEnd = SM.isPointWithin(SelectionEnd, Range.getBegin(), End);
155 if (HasStart && HasEnd)
156 return SourceSelectionKind::ContainsSelection;
157 if (SM.isPointWithin(Range.getBegin(), SelectionBegin, SelectionEnd) &&
158 SM.isPointWithin(End, SelectionBegin, SelectionEnd))
159 return SourceSelectionKind::InsideSelection;
160
161
162 if (HasStart && SelectionBegin != End)
163 return SourceSelectionKind::ContainsSelectionStart;
164 if (HasEnd && SelectionEnd != Range.getBegin())
165 return SourceSelectionKind::ContainsSelectionEnd;
166
167 return SourceSelectionKind::None;
168 }
169
170 const SourceLocation SelectionBegin, SelectionEnd;
171 FileID TargetFile;
172 const ASTContext &Context;
173 std::vector SelectionStack;
174
175
176
177 bool LookThroughOpaqueValueExprs = false;
178};
179
180}
181
182std::optional
185 assert(SelectionRange.isValid() &&
187 SelectionRange.getEnd()) &&
188 "Expected a file range");
190 Context.getSourceManager().getFileID(SelectionRange.getBegin());
191 assert(Context.getSourceManager().getFileID(SelectionRange.getEnd()) ==
192 TargetFile &&
193 "selection range must span one file");
194
195 ASTSelectionFinder Visitor(SelectionRange, TargetFile, Context);
196 Visitor.TraverseDecl(Context.getTranslationUnitDecl());
197 return Visitor.getSelectedASTNode();
198}
199
201 switch (Kind) {
202 case SourceSelectionKind::None:
203 return "none";
204 case SourceSelectionKind::ContainsSelection:
205 return "contains-selection";
206 case SourceSelectionKind::ContainsSelectionStart:
207 return "contains-selection-start";
208 case SourceSelectionKind::ContainsSelectionEnd:
209 return "contains-selection-end";
210 case SourceSelectionKind::InsideSelection:
211 return "inside";
212 }
213 llvm_unreachable("invalid selection kind");
214}
215
217 unsigned Indent = 0) {
218 OS.indent(Indent * 2);
221 if (const auto *ND = dyn_cast(D))
222 OS << " \"" << ND->getDeclName() << '"';
225 }
227 for (const auto &Child : Node.Children)
229}
230
232
233
234
235
236
237
241 for (const auto &Child : Node.Children) {
242 if (Child.SelectionKind == Kind)
243 return true;
246 }
247 return false;
248}
249
250namespace {
251struct SelectedNodeWithParents {
253 llvm::SmallVector<SelectedASTNode::ReferenceType, 8> Parents;
254
255
256
257 void canonicalize();
258};
259
260enum SelectionCanonicalizationAction { KeepSelection, SelectParent };
261
262
263
264SelectionCanonicalizationAction
265getSelectionCanonizalizationAction(const Stmt *S, const Stmt *Parent) {
266
267
268
269
271 return SelectParent;
272
273
274
275
276
277
278
279 else if (const auto *CE = dyn_cast(Parent)) {
281 CE->getCallee()->IgnoreImpCasts() == S)
282 return SelectParent;
283 }
284
285 return KeepSelection;
286}
287
288}
289
290void SelectedNodeWithParents::canonicalize() {
291 const Stmt *S = Node.get().Node.get();
292 assert(S && "non statement selection!");
293 const Stmt *Parent = Parents[Parents.size() - 1].get().Node.get();
294 if (!Parent)
295 return;
296
297
298 unsigned ParentIndex = 1;
300 ++ParentIndex) {
301 const Stmt *NewParent =
302 Parents[Parents.size() - ParentIndex - 1].get().Node.get();
303 if (!NewParent)
304 break;
305 Parent = NewParent;
306 }
307
308 switch (getSelectionCanonizalizationAction(S, Parent)) {
309 case SelectParent:
310 Node = Parents[Parents.size() - ParentIndex];
311 for (; ParentIndex != 0; --ParentIndex)
312 Parents.pop_back();
313 break;
314 case KeepSelection:
315 break;
316 }
317}
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
347
348
349 for (const auto &Child : ASTSelection.Children) {
351 MatchingNodes.push_back(SelectedNodeWithParents{
352 std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
353 return;
354 }
355 }
356 } else {
358
359 MatchingNodes.push_back(SelectedNodeWithParents{
360 std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
361 return;
362 }
363 }
364
365 ParentStack.push_back(std::cref(ASTSelection));
366 for (const auto &Child : ASTSelection.Children)
368 ParentStack.pop_back();
369}
370
378
379std::optional
382
383 if (SelectionRange.getBegin() == SelectionRange.getEnd())
384 return std::nullopt;
388
389
390 if (ContainSelection.size() != 1)
391 return std::nullopt;
392 SelectedNodeWithParents &Selected = ContainSelection[0];
393 if (!Selected.Node.get().Node.get<Stmt>())
394 return std::nullopt;
395 const Stmt *CodeRangeStmt = Selected.Node.get().Node.get<Stmt>();
397 Selected.canonicalize();
399 false);
400 }
401
402
403
404
405
406
407
408
409 Selected.Parents.push_back(Selected.Node);
411 true);
412}
413
418
420 bool IsPrevCompound = false;
421
422
423
424 for (const auto &Parent : llvm::reverse(Parents)) {
425 const DynTypedNode &Node = Parent.get().Node;
426 if (const auto *D = Node.get<Decl>()) {
428 return IsPrevCompound;
429
430
431
432
434 return false;
435 }
437 }
438 return false;
439}
440
442 for (const auto &Parent : llvm::reverse(Parents)) {
443 const DynTypedNode &Node = Parent.get().Node;
444 if (const auto *D = Node.get<Decl>()) {
446 return D;
447 }
448 }
449 return nullptr;
450}
static bool isFunctionLikeDeclaration(const Decl *D)
Definition ASTSelection.cpp:414
static const char * selectionKindToString(SourceSelectionKind Kind)
Definition ASTSelection.cpp:200
static void dump(const SelectedASTNode &Node, llvm::raw_ostream &OS, unsigned Indent=0)
Definition ASTSelection.cpp:216
static void findDeepestWithKind(const SelectedASTNode &ASTSelection, llvm::SmallVectorImpl< SelectedNodeWithParents > &MatchingNodes, SourceSelectionKind Kind, llvm::SmallVectorImpl< SelectedASTNode::ReferenceType > &ParentStack)
Finds the set of bottom-most selected AST nodes that are in the selection tree with the specified sel...
Definition ASTSelection.cpp:341
static bool hasAnyDirectChildrenWithKind(const SelectedASTNode &Node, SourceSelectionKind Kind)
Returns true if the given node has any direct children with the following selection kind.
Definition ASTSelection.cpp:238
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
static CharSourceRange getTokenRange(SourceRange R)
CompoundStmt - This represents a group of statements like { stmt stmt }.
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
const char * getDeclKindName() const
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
A dynamically typed AST node container.
const T * get() const
Retrieve the stored node as type T.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static SourceLocation findLocationAfterToken(SourceLocation loc, tok::TokenKind TKind, const SourceManager &SM, const LangOptions &LangOpts, bool SkipTrailingWhitespaceAndNewLine)
Checks that the given token is the first token that occurs after the given location (this excludes co...
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
A RecursiveASTVisitor subclass that guarantees that AST traversal is performed in a lexical order (i....
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Encodes a location in the source.
static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End)
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
const char * getStmtClassName() const
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
@ Result
The result type of a method or function.