42 llvm_unreachable(
"Invalid Traversal type!");
52template <
typename T,
typename...
U>
61 template <
typename,
typename...>
friend struct ::MatchParents;
66 ParentVector() =
default;
73 return Seen.contains(
Value);
76 if (!
Value.getMemoizationData() || Seen.insert(
Value).second)
77 Items.push_back(
Value);
82 llvm::SmallDenseSet<DynTypedNode, 2> Seen;
88 using ParentMapPointers =
89 llvm::DenseMap<
const void *,
90 llvm::PointerUnion<
const Decl *,
const Stmt *,
95 using ParentMapOtherNodes =
97 llvm::PointerUnion<
const Decl *,
const Stmt *,
100 ParentMapPointers PointerParents;
101 ParentMapOtherNodes OtherParents;
105 getSingleDynTypedNodeFromParentMap(ParentMapPointers::mapped_type
U) {
106 if (
const auto *
D =
U.dyn_cast<
const Decl *>())
108 if (
const auto *S =
U.dyn_cast<
const Stmt *>())
113 template <
typename NodeTy,
typename MapTy>
116 auto I = Map.find(
Node);
117 if (I == Map.end()) {
120 if (
const auto *
V = I->second.template dyn_cast<ParentVector *>()) {
123 return getSingleDynTypedNodeFromParentMap(I->second);
129 for (
const auto &Entry : PointerParents) {
132 }
else if (Entry.second.is<ParentVector *>()) {
133 delete Entry.second.
get<ParentVector *>();
136 for (
const auto &Entry : OtherParents) {
139 }
else if (Entry.second.is<ParentVector *>()) {
140 delete Entry.second.
get<ParentVector *>();
159 auto RewrittenBinOpParentsList = ParentList;
161 while (ChildExpr && RewrittenBinOpParentsList.size() == 1 &&
163 const auto *S = RewrittenBinOpParentsList[0].get<
Stmt>();
167 const auto *RWBO = dyn_cast<CXXRewrittenBinaryOperator>(S);
169 RewrittenBinOpParentsList = getDynNodeFromMap(S, PointerParents);
172 if (RWBO->getLHS()->IgnoreUnlessSpelledInSource() != ChildExpr &&
173 RWBO->getRHS()->IgnoreUnlessSpelledInSource() != ChildExpr)
179 const auto *ParentExpr = ParentList[0].get<
Expr>();
180 if (ParentExpr && ChildExpr)
181 return AscendIgnoreUnlessSpelledInSource(ParentExpr, ChildExpr);
185 matchParents<DeclStmt, CXXForRangeStmt>(ParentList,
this);
186 if (std::get<bool>(AncestorNodes) &&
187 std::get<const CXXForRangeStmt *>(AncestorNodes)
188 ->getLoopVarStmt() ==
189 std::get<const DeclStmt *>(AncestorNodes))
190 return std::get<DynTypedNodeList>(AncestorNodes);
193 auto AncestorNodes = matchParents<VarDecl, DeclStmt, CXXForRangeStmt>(
195 if (std::get<bool>(AncestorNodes) &&
196 std::get<const CXXForRangeStmt *>(AncestorNodes)
198 std::get<const DeclStmt *>(AncestorNodes))
199 return std::get<DynTypedNodeList>(AncestorNodes);
203 matchParents<CXXMethodDecl, CXXRecordDecl, LambdaExpr>(ParentList,
205 if (std::get<bool>(AncestorNodes))
206 return std::get<DynTypedNodeList>(AncestorNodes);
210 matchParents<FunctionTemplateDecl, CXXRecordDecl, LambdaExpr>(
212 if (std::get<bool>(AncestorNodes))
213 return std::get<DynTypedNodeList>(AncestorNodes);
218 return getDynNodeFromMap(
Node, OtherParents);
224 auto ShouldSkip = [](
const Expr *
E,
const Expr *Child) {
225 if (isa<ImplicitCastExpr>(
E))
228 if (isa<FullExpr>(
E))
231 if (isa<MaterializeTemporaryExpr>(
E))
234 if (isa<CXXBindTemporaryExpr>(
E))
237 if (isa<ParenExpr>(
E))
240 if (isa<ExprWithCleanups>(
E))
243 auto SR = Child->getSourceRange();
245 if (
const auto *
C = dyn_cast<CXXFunctionalCastExpr>(
E)) {
246 if (
C->getSourceRange() == SR)
250 if (
const auto *
C = dyn_cast<CXXConstructExpr>(
E)) {
251 if (
C->getSourceRange() == SR ||
C->isElidable())
255 if (
const auto *
C = dyn_cast<CXXMemberCallExpr>(
E)) {
256 if (
C->getSourceRange() == SR)
260 if (
const auto *
C = dyn_cast<MemberExpr>(
E)) {
261 if (
C->getSourceRange() == SR)
267 while (ShouldSkip(
E, Child)) {
268 auto It = PointerParents.find(
E);
269 if (It == PointerParents.end())
271 const auto *S = It->second.dyn_cast<
const Stmt *>();
273 if (
auto *Vec = It->second.dyn_cast<ParentVector *>())
275 return getSingleDynTypedNodeFromParentMap(It->second);
277 const auto *
P = dyn_cast<Expr>(S);
291 if (
const auto *TypedNode = NodeList[0].get<T>()) {
292 auto NextParentList =
294 if (NextParentList.size() == 1) {
296 if (std::get<bool>(TailTuple)) {
299 return std::make_tuple(
true, NodeList, TypedNode, TupleTail...);
305 return std::tuple_cat(std::make_tuple(
false, NodeList),
306 std::tuple<const T *, const U *...>());
311 static std::tuple<bool, DynTypedNodeList, const T *>
314 if (
const auto *TypedNode = NodeList[0].get<T>()) {
315 auto NextParentList =
317 if (NextParentList.size() == 1)
318 return std::make_tuple(
true, NodeList, TypedNode);
320 return std::make_tuple(
false, NodeList,
nullptr);
324template <
typename T,
typename...
U>
364 bool shouldVisitTemplateInstantiations()
const {
return true; }
366 bool shouldVisitImplicitCode()
const {
return true; }
371 template <
typename MapNodeTy,
typename MapTy>
372 void addParent(MapNodeTy MapNode,
MapTy *Parents) {
373 if (ParentStack.empty())
386 auto &NodeOrVector = (*Parents)[MapNode];
387 if (NodeOrVector.isNull()) {
388 if (
const auto *
D = ParentStack.back().get<
Decl>())
390 else if (
const auto *S = ParentStack.back().get<
Stmt>())
395 if (!NodeOrVector.template is<ParentVector *>()) {
396 auto *
Vector =
new ParentVector(
397 1, getSingleDynTypedNodeFromParentMap(NodeOrVector));
398 delete NodeOrVector.template dyn_cast<DynTypedNode *>();
402 auto *
Vector = NodeOrVector.template get<ParentVector *>();
407 bool Found = ParentStack.back().getMemoizationData() &&
408 llvm::is_contained(*
Vector, ParentStack.back());
410 Vector->push_back(ParentStack.back());
414 template <
typename T>
static bool isNull(
T Node) {
return !
Node; }
417 template <
typename T,
typename MapNodeTy,
typename BaseTraverseFn,
419 bool TraverseNode(
T Node, MapNodeTy MapNode, BaseTraverseFn BaseTraverse,
423 addParent(MapNode, Parents);
425 bool Result = BaseTraverse();
426 ParentStack.pop_back();
430 bool TraverseDecl(
Decl *DeclNode) {
433 &Map.PointerParents);
435 bool TraverseTypeLoc(
TypeLoc TypeLocNode) {
447 bool TraverseAttr(
Attr *AttrNode) {
450 &Map.PointerParents);
460 bool dataTraverseStmtPre(
Stmt *StmtNode) {
461 addParent(StmtNode, &Map.PointerParents);
465 bool dataTraverseStmtPost(
Stmt *StmtNode) {
466 ParentStack.pop_back();
482 Parents = std::make_unique<ParentMap>(ASTCtx);
std::tuple< bool, DynTypedNodeList, const T *, const U *... > matchParents(const DynTypedNodeList &NodeList, ParentMapContext::ParentMap *ParentMap)
static DynTypedNode createDynTypedNode(const T &Node)
Template specializations to abstract away from pointers and TypeLocs.
llvm::DenseMap< Stmt *, Stmt * > MapTy
static bool contains(const std::set< tok::TokenKind > &Terminators, const Token &Tok)
A RecursiveASTVisitor that builds a map from nodes to their parents as defined by the RecursiveASTVis...
ASTVisitor(ParentMap &Map)
DynTypedNodeList getParents(TraversalKind TK, const DynTypedNode &Node)
DynTypedNodeList AscendIgnoreUnlessSpelledInSource(const Expr *E, const Expr *Child)
ParentMap(ASTContext &Ctx)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
constexpr bool hasPointerIdentity() const
Check if the given ASTNodeKind identifies a type that offers pointer identity.
Attr - This represents one attribute.
Decl - This represents one declaration (or definition), e.g.
Container for either a single DynTypedNode or for an ArrayRef to DynTypedNode.
A dynamically typed AST node container.
ASTNodeKind getNodeKind() const
const T * get() const
Retrieve the stored node as type T.
static DynTypedNode create(const T &Node)
Creates a DynTypedNode from Node.
const void * getMemoizationData() const
Returns a pointer that identifies the stored AST node.
This represents one expression.
Expr * IgnoreUnlessSpelledInSource()
Skip past any invisible AST nodes which might surround this statement, such as ExprWithCleanups or Im...
A C++ nested-name-specifier augmented with source location information.
const Expr * traverseIgnored(const Expr *E) const
void clear()
Clear parent maps.
TraversalKind getTraversalKind() const
DynTypedNodeList getParents(const NodeT &Node)
Returns the parents of the given node (within the traversal scope).
ParentMapContext(ASTContext &Ctx)
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseObjCProtocolLoc(ObjCProtocolLoc ProtocolLoc)
Recursively visit an Objective-C protocol reference with location information.
bool TraverseAST(ASTContext &AST)
Recursively visits an entire AST, starting from the TranslationUnitDecl.
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS)
Recursively visit a C++ nested-name-specifier with location information.
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
bool TraverseTypeLoc(TypeLoc TL)
Recursively visit a type with location, by dispatching to Traverse*TypeLoc() based on the argument ty...
bool TraverseAttr(Attr *At)
Recursively visit an attribute, by dispatching to Traverse*Attr() based on the argument's dynamic typ...
Stmt - This represents one statement.
Base wrapper for a particular "section" of type source info.
The JSON file list parser is used to communicate input to InstallAPI.
@ Vector
'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
TraversalKind
Defines how we descend a level in the AST when we pass through expressions.
@ TK_AsIs
Will traverse all child nodes.
@ TK_IgnoreUnlessSpelledInSource
Ignore AST nodes not written in the source.
@ Result
The result type of a method or function.
const FunctionProtoType * T
static std::tuple< bool, DynTypedNodeList, const T * > match(const DynTypedNodeList &NodeList, ParentMapContext::ParentMap *ParentMap)
static std::tuple< bool, DynTypedNodeList, const T *, const U *... > match(const DynTypedNodeList &NodeList, ParentMapContext::ParentMap *ParentMap)