;
16using namespacetooling;
22 if(!isa<ObjCImplDecl>(
D))
28R.
getEnd(), tok::raw_identifier,
SM, LangOpts,
37classASTSelectionFinder
43SelectionBegin(Selection.getBegin()),
44SelectionEnd(Selection.getBegin() == Selection.getEnd()
46: Selection.getEnd()),
47TargetFile(TargetFile), Context(Context) {
49SelectionStack.push_back(
51SourceSelectionKind::None));
54std::optional<SelectedASTNode> getSelectedASTNode() {
55assert(SelectionStack.size() == 1 &&
"stack was not popped");
57SelectionStack.pop_back();
58 if(Result.Children.empty())
60 returnstd::move(Result);
72 if(!LookThroughOpaqueValueExprs)
79 if(isa<TranslationUnitDecl>(
D))
89FileLoc = DeclRange.
getEnd();
91FileLoc =
SM.getSpellingLoc(DeclRange.
getBegin());
92 if(
SM.getFileID(FileLoc) != TargetFile)
96selectionKindFor(getLexicalDeclRange(
D,
SM, Context.
getLangOpts()));
97SelectionStack.push_back(
100popAndAddToSelectionIfSelected(SelectionKind);
103 SM.isBeforeInTranslationUnit(SelectionEnd.isValid() ? SelectionEnd
115 if(
auto*Opaque = dyn_cast<OpaqueValueExpr>(S))
116 returnTraverseOpaqueValueExpr(Opaque);
118 if(
auto*TE = dyn_cast<CXXThisExpr>(S)) {
119 if(TE->isImplicit())
125SelectionStack.push_back(
128popAndAddToSelectionIfSelected(SelectionKind);
135SelectionStack.pop_back();
136 if(SelectionKind != SourceSelectionKind::None || !
Node.Children.empty())
137SelectionStack.back().Children.push_back(std::move(
Node));
143 if(
Range.isTokenRange())
146 returnSourceSelectionKind::None;
147 if(!SelectionEnd.isValid()) {
149 if(
SM.isPointWithin(SelectionBegin,
Range.getBegin(), End))
150 returnSourceSelectionKind::ContainsSelection;
151 returnSourceSelectionKind::None;
153 boolHasStart =
SM.isPointWithin(SelectionBegin,
Range.getBegin(), End);
154 boolHasEnd =
SM.isPointWithin(SelectionEnd,
Range.getBegin(), End);
155 if(HasStart && HasEnd)
156 returnSourceSelectionKind::ContainsSelection;
157 if(
SM.isPointWithin(
Range.getBegin(), SelectionBegin, SelectionEnd) &&
158 SM.isPointWithin(End, SelectionBegin, SelectionEnd))
159 returnSourceSelectionKind::InsideSelection;
162 if(HasStart && SelectionBegin != End)
163 returnSourceSelectionKind::ContainsSelectionStart;
164 if(HasEnd && SelectionEnd !=
Range.getBegin())
165 returnSourceSelectionKind::ContainsSelectionEnd;
167 returnSourceSelectionKind::None;
173std::vector<SelectedASTNode> SelectionStack;
177 boolLookThroughOpaqueValueExprs =
false;
182std::optional<SelectedASTNode>
185assert(SelectionRange.
isValid() &&
187SelectionRange.
getEnd()) &&
188 "Expected a file range");
193 "selection range must span one file");
195ASTSelectionFinder Visitor(SelectionRange, TargetFile, Context);
197 returnVisitor.getSelectedASTNode();
202 caseSourceSelectionKind::None:
204 caseSourceSelectionKind::ContainsSelection:
205 return "contains-selection";
206 caseSourceSelectionKind::ContainsSelectionStart:
207 return "contains-selection-start";
208 caseSourceSelectionKind::ContainsSelectionEnd:
209 return "contains-selection-end";
210 caseSourceSelectionKind::InsideSelection:
213llvm_unreachable(
"invalid selection kind");
217 unsignedIndent = 0) {
218OS.indent(Indent * 2);
221 if(
const auto*ND = dyn_cast<NamedDecl>(
D))
222OS <<
" \""<< ND->getDeclName() <<
'"';
224OS << S->getStmtClassName();
227 for(
const auto&Child :
Node.Children)
228 dump(Child, OS, Indent + 1);
241 for(
const auto&Child :
Node.Children) {
242 if(Child.SelectionKind == Kind)
251structSelectedNodeWithParents {
260enumSelectionCanonicalizationAction { KeepSelection, SelectParent };
264SelectionCanonicalizationAction
265getSelectionCanonizalizationAction(
const Stmt*S,
const Stmt*
Parent) {
270 if(isa<StringLiteral>(S) && isa<ObjCStringLiteral>(
Parent))
279 else if(
const auto*CE = dyn_cast<CallExpr>(
Parent)) {
280 if((isa<MemberExpr>(S) || isa<DeclRefExpr>(S)) &&
281CE->getCallee()->IgnoreImpCasts() == S)
285 returnKeepSelection;
290voidSelectedNodeWithParents::canonicalize() {
292assert(S &&
"non statement selection!");
293 const Stmt*
Parent= Parents[Parents.size() - 1].get().Node.get<
Stmt>();
298 unsignedParentIndex = 1;
299 for(; (ParentIndex + 1) <= Parents.size() && isa<ImplicitCastExpr>(
Parent);
301 const Stmt*NewParent =
302Parents[Parents.size() - ParentIndex - 1].get().Node.get<
Stmt>();
308 switch(getSelectionCanonizalizationAction(S,
Parent)) {
310 Node= Parents[Parents.size() - ParentIndex];
311 for(; ParentIndex != 0; --ParentIndex)
349 for(
const auto&Child : ASTSelection.
Children) {
351MatchingNodes.push_back(SelectedNodeWithParents{
352std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
359MatchingNodes.push_back(SelectedNodeWithParents{
360std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
365ParentStack.push_back(std::cref(ASTSelection));
366 for(
const auto&Child : ASTSelection.
Children)
368ParentStack.pop_back();
379std::optional<CodeRangeASTSelection>
390 if(ContainSelection.size() != 1)
392SelectedNodeWithParents &Selected = ContainSelection[0];
393 if(!Selected.Node.get().Node.get<
Stmt>())
395 const Stmt*CodeRangeStmt = Selected.Node.get().Node.get<
Stmt>();
396 if(!isa<CompoundStmt>(CodeRangeStmt)) {
397Selected.canonicalize();
409Selected.Parents.push_back(Selected.Node);
416 returnisa<FunctionDecl>(
D) || isa<ObjCMethodDecl>(
D);
420 boolIsPrevCompound =
false;
424 for(
const auto&
Parent: llvm::reverse(Parents)) {
428 returnIsPrevCompound;
433 if(isa<TypeDecl>(
D))
442 for(
const auto&
Parent: llvm::reverse(Parents)) {
static bool isFunctionLikeDeclaration(const Decl *D)
static const char * selectionKindToString(SourceSelectionKind Kind)
static void dump(const SelectedASTNode &Node, llvm::raw_ostream &OS, unsigned Indent=0)
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...
static bool hasAnyDirectChildrenWithKind(const SelectedASTNode &Node, SourceSelectionKind Kind)
Returns true if the given node has any direct children with the following selection kind.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
const LangOptions & getLangOpts() const
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.
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
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....
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
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.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
The JSON file list parser is used to communicate input to InstallAPI.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4