Debian Codegraph @

libcextract/TopLevelASTIterator.cpp

raw
1 //===- TopLevelASTIterator.cpp - Iterate through toplevel entities *- C++ -*-===//
2 //
3 // This project is licensed under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Iterate through toplevel entities: both macros and decls.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #include "TopLevelASTIterator.hh"
17
18 TopLevelASTIterator::TopLevelASTIterator(ASTUnit *ast, bool skip_macros_in_decls)
19 : AST(ast),
20 SM(AST->getSourceManager()),
21 PrepRec(*AST->getPreprocessor().getPreprocessingRecord()),
22 DeclIt(AST->top_level_begin()),
23 MacroIt(PrepRec.begin()),
24 UndefIt(0),
25 NeedsUndef({}),
26 BeforeClass(SM),
27 MW(AST->getPreprocessor()),
28 SkipMacrosInDecls(skip_macros_in_decls),
29 Ended(false),
30 EndLocOfLastDecl(PrepRec.begin()->getSourceRange().getBegin())
31 {
32 Populate_Needs_Undef();
33 Advance();
34 }
35
36 SourceLocation TopLevelASTIterator::Return::Get_Location(void)
37 {
38 switch (Type)
39 {
40 case ReturnType::TYPE_INVALID:
41 return SourceLocation();
42 break;
43
44 case ReturnType::TYPE_DECL:
45 return AsDecl->getLocation();
46 break;
47
48 case ReturnType::TYPE_PREPROCESSED_ENTITY:
49 return AsPrep->getSourceRange().getBegin();
50 break;
51
52 case ReturnType::TYPE_MACRO_UNDEF:
53 return AsUndef->getLocation();
54 break;
55 }
56 __builtin_unreachable();
57 }
58
59 void TopLevelASTIterator::Populate_Needs_Undef(void)
60 {
61 PreprocessingRecord *rec = AST->getPreprocessor().getPreprocessingRecord();
62
63 for (PreprocessedEntity *entity : *rec) {
64 if (MacroDefinitionRecord *def = dyn_cast<MacroDefinitionRecord>(entity)) {
65
66 MacroDirective *directive = MW.Get_Macro_Directive(def);
67
68 /* If there is no history, then doesn't bother analyzing. */
69 if (directive == nullptr)
70 continue;
71
72 SourceLocation undef_loc =
73 directive->getDefinition().getUndefLocation();
74
75 /* In case the macro isn't undefined then its location is invalid. */
76 if (undef_loc.isValid()) {
77 /* If we somehow lost the undef location, then mark it to output. */
78 NeedsUndef.push_back(directive);
79 }
80 }
81 }
82
83 /* Sort macros by location where they should be undefined. */
84 class CompareMacroUndefLoc
85 {
86 public:
87 CompareMacroUndefLoc(BeforeThanCompare<SourceLocation> &beforeclass)
88 : BeforeClassRef(beforeclass)
89 { }
90
91 bool operator()(const MacroDirective *a, const MacroDirective *b)
92 {
93 return BeforeClassRef(a->getDefinition().getUndefLocation(),
94 b->getDefinition().getUndefLocation());
95 }
96
97 BeforeThanCompare<SourceLocation> &BeforeClassRef;
98 };
99 std::sort(NeedsUndef.begin(), NeedsUndef.end(), CompareMacroUndefLoc(BeforeClass));
100 }
101
102 bool TopLevelASTIterator::Advance(void)
103 {
104 SourceManager &sm = AST->getSourceManager();
105 const SourceLocation &end = sm.getLocForEndOfFile(sm.getMainFileID());
106
107 #define NEXT_DECL_IF_EXISTS(DeclIt, end) ((DeclIt) != AST->top_level_end()) \
108 ? (*(DeclIt))->getLocation() \
109 : (end)
110 #define NEXT_PREP_IF_EXISTS(MacroIt, end) ((MacroIt) != PrepRec.end()) \
111 ? (*(MacroIt))->getSourceRange().getBegin() \
112 : (end)
113 #define NEXT_UNDEF_IF_EXISTS(UndefIt, end) (UndefIt < NeedsUndef.size()) \
114 ? (NeedsUndef)[UndefIt]->getLocation() \
115 : (end);
116
117 SourceLocation end_of_last_decl = EndLocOfLastDecl;
118
119 do {
120 const SourceLocation &next_decl_loc = NEXT_DECL_IF_EXISTS(DeclIt, end);
121 const SourceLocation &next_prep_loc = NEXT_PREP_IF_EXISTS(MacroIt, end);
122 const SourceLocation &next_undef_loc = NEXT_UNDEF_IF_EXISTS(UndefIt, end);
123
124 // Find out what comes first.
125 std::vector<SourceLocation> vec = {
126 next_decl_loc,
127 next_prep_loc,
128 next_undef_loc,
129 };
130
131 class CompareLocWithAssertion
132 {
133 public:
134 CompareLocWithAssertion(BeforeThanCompare<SourceLocation> &beforeclass)
135 : BeforeClassRef(beforeclass)
136 { }
137
138 bool operator()(const SourceLocation &a, const SourceLocation &b)
139 {
140 assert(a.isValid());
141 assert(b.isValid());
142 return BeforeClassRef(a, b);
143 }
144
145 BeforeThanCompare<SourceLocation> &BeforeClassRef;
146 };
147
148 std::sort(vec.begin(), vec.end(), CompareLocWithAssertion(BeforeClass));
149
150 /* Check which comes next. */
151 const SourceLocation &next = vec[0];
152 if (next == end) {
153 /* We reached the end. */
154 Ended = true;
155 return false;
156 }
157
158 if (next == next_decl_loc) {
159 EndLocOfLastDecl = (*DeclIt)->getEndLoc();
160 Current = Return(*DeclIt);
161 ++DeclIt;
162
163 } else if (next == next_prep_loc) {
164 Current = Return(*MacroIt);
165 ++MacroIt;
166 }
167 else if (next == next_undef_loc) {
168 Current = Return(NeedsUndef[UndefIt]);
169 ++UndefIt;
170 }
171 } while (SkipMacrosInDecls && Is_Before(Current.Get_Location(), end_of_last_decl));
172
173 return true;
174 }
175
176 bool TopLevelASTIterator::Skip_Until(const SourceLocation &loc)
177 {
178 bool valid = Current.Type != ReturnType::TYPE_INVALID;
179 SourceLocation current = Current.Get_Location();
180 while (valid && Is_Before(current, loc)) {
181 valid = Advance();
182 current = Current.Get_Location();
183 }
184
185 return valid;
186 }
187
188 #include "PrettyPrint.hh"
189
190 void Debug_TopLevelASTWalker(ASTUnit *ast)
191 {
192 TopLevelASTIterator it(ast);
193 for (; !it.End(); ++it) {
194 TopLevelASTIterator::Return &Current = *it;
195
196 switch (Current.Type) {
197 case TopLevelASTIterator::ReturnType::TYPE_INVALID:
198 assert(0 && "Invalid type.");
199 break;
200 case TopLevelASTIterator::ReturnType::TYPE_DECL:
201 PrettyPrint::Debug_Decl(Current.AsDecl);
202 break;
203 case TopLevelASTIterator::ReturnType::TYPE_PREPROCESSED_ENTITY:
204 if (MacroDefinitionRecord *def = dyn_cast<MacroDefinitionRecord>(Current.AsPrep))
205 {
206 PrettyPrint::Debug_Macro_Def(def);
207 } else {
208 llvm::outs() << PrettyPrint::Get_Source_Text(Current.AsPrep->getSourceRange());
209 llvm::outs() << '\n';
210 }
211 break;
212 case TopLevelASTIterator::ReturnType::TYPE_MACRO_UNDEF:
213 PrettyPrint::Debug_Macro_Undef(Current.AsUndef);
214 break;
215 }
216 }
217 }