1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_AST_LEXICALLY_ORDERED_RECURSIVEASTVISITOR_H |
15 | #define LLVM_CLANG_AST_LEXICALLY_ORDERED_RECURSIVEASTVISITOR_H |
16 | |
17 | #include "clang/AST/RecursiveASTVisitor.h" |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "clang/Basic/SourceManager.h" |
20 | #include "llvm/Support/SaveAndRestore.h" |
21 | |
22 | namespace clang { |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | template <typename Derived> |
59 | class LexicallyOrderedRecursiveASTVisitor |
60 | : public RecursiveASTVisitor<Derived> { |
61 | using BaseType = RecursiveASTVisitor<Derived>; |
62 | |
63 | public: |
64 | LexicallyOrderedRecursiveASTVisitor(const SourceManager &SM) : SM(SM) {} |
65 | |
66 | bool TraverseObjCImplementationDecl(ObjCImplementationDecl *D) { |
67 | |
68 | |
69 | |
70 | bool Result = BaseType::TraverseObjCImplementationDecl(D); |
71 | return TraverseAdditionalLexicallyNestedDeclarations() ? Result : false; |
72 | } |
73 | |
74 | bool TraverseObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
75 | bool Result = BaseType::TraverseObjCCategoryImplDecl(D); |
76 | return TraverseAdditionalLexicallyNestedDeclarations() ? Result : false; |
77 | } |
78 | |
79 | bool TraverseDeclContextHelper(DeclContext *DC) { |
80 | if (!DC) |
81 | return true; |
82 | |
83 | for (auto I = DC->decls_begin(), E = DC->decls_end(); I != E;) { |
84 | Decl *Child = *I; |
85 | if (BaseType::canIgnoreChildDeclWhileTraversingDeclContext(Child)) { |
86 | ++I; |
87 | continue; |
88 | } |
89 | if (!isa<ObjCImplementationDecl>(Child) && |
90 | !isa<ObjCCategoryImplDecl>(Child)) { |
91 | if (!BaseType::getDerived().TraverseDecl(Child)) |
92 | return false; |
93 | ++I; |
94 | continue; |
95 | } |
96 | |
97 | |
98 | LexicallyNestedDeclarations.clear(); |
99 | for (++I; I != E; ++I) { |
100 | Decl *Sibling = *I; |
101 | if (!SM.isBeforeInTranslationUnit(Sibling->getBeginLoc(), |
102 | Child->getEndLoc())) |
103 | break; |
104 | if (!BaseType::canIgnoreChildDeclWhileTraversingDeclContext(Sibling)) |
105 | LexicallyNestedDeclarations.push_back(Sibling); |
106 | } |
107 | if (!BaseType::getDerived().TraverseDecl(Child)) |
108 | return false; |
109 | } |
110 | return true; |
111 | } |
112 | |
113 | Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); } |
114 | |
115 | SmallVector<Stmt *, 8> getStmtChildren(CXXOperatorCallExpr *CE) { |
116 | SmallVector<Stmt *, 8> Children(CE->children()); |
117 | bool Swap; |
118 | |
119 | |
120 | switch (CE->getOperator()) { |
121 | case OO_Arrow: |
122 | case OO_Call: |
123 | case OO_Subscript: |
124 | Swap = true; |
125 | break; |
126 | case OO_PlusPlus: |
127 | case OO_MinusMinus: |
128 | |
129 | Swap = Children.size() != 2; |
130 | break; |
131 | default: |
132 | Swap = CE->isInfixBinaryOp(); |
133 | break; |
134 | } |
135 | if (Swap && Children.size() > 1) |
136 | std::swap(Children[0], Children[1]); |
137 | return Children; |
138 | } |
139 | |
140 | private: |
141 | bool TraverseAdditionalLexicallyNestedDeclarations() { |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | (0) . __assert_fail ("!BaseType..getDerived().shouldTraversePostOrder() && \"post-order traversal is not supported for lexically ordered \" \"recursive ast visitor\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h", 149, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!BaseType::getDerived().shouldTraversePostOrder() && |
148 | (0) . __assert_fail ("!BaseType..getDerived().shouldTraversePostOrder() && \"post-order traversal is not supported for lexically ordered \" \"recursive ast visitor\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h", 149, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "post-order traversal is not supported for lexically ordered " |
149 | (0) . __assert_fail ("!BaseType..getDerived().shouldTraversePostOrder() && \"post-order traversal is not supported for lexically ordered \" \"recursive ast visitor\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h", 149, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "recursive ast visitor"); |
150 | for (Decl *D : LexicallyNestedDeclarations) { |
151 | if (!BaseType::getDerived().TraverseDecl(D)) |
152 | return false; |
153 | } |
154 | return true; |
155 | } |
156 | |
157 | const SourceManager &SM; |
158 | llvm::SmallVector<Decl *, 8> LexicallyNestedDeclarations; |
159 | }; |
160 | |
161 | } |
162 | |
163 | #endif |
164 | |