Debian Codegraph @

libcextract/TopLevelASTIterator.hh

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 #pragma once
17
18 #include "MacroWalker.hh"
19 #include "clang/Frontend/ASTUnit.h"
20
21 #include <vector>
22
23 using namespace clang;
24
25 class TopLevelASTIterator
26 {
27 public:
28 TopLevelASTIterator(ASTUnit *ast, bool skip_macros_in_decls=true);
29
30 enum ReturnType
31 {
32 TYPE_INVALID,
33 TYPE_DECL,
34 TYPE_PREPROCESSED_ENTITY,
35 TYPE_MACRO_UNDEF,
36 };
37
38 struct Return
39 {
40 Return(Decl *d)
41 {
42 AsDecl = d;
43 Type = ReturnType::TYPE_DECL;
44 }
45
46 Return(PreprocessedEntity *p)
47 {
48 AsPrep = p;
49 Type = ReturnType::TYPE_PREPROCESSED_ENTITY;
50 }
51
52 Return(MacroDirective *u)
53 {
54 AsUndef = u;
55 Type = ReturnType::TYPE_MACRO_UNDEF;
56 }
57
58 Return(void)
59 {
60 AsDecl = nullptr;
61 Type = ReturnType::TYPE_INVALID;
62 }
63
64 union
65 {
66 Decl *AsDecl;
67 PreprocessedEntity *AsPrep;
68 MacroDirective *AsUndef;
69 };
70 ReturnType Type;
71
72 SourceLocation Get_Location(void);
73
74 } Current;
75
76 inline struct Return& Get_Current(void)
77 {
78 return Current;
79 }
80
81 bool Advance(void);
82 bool Skip_Until(const SourceLocation &loc);
83
84 inline bool Is_Current_A_Decl()
85 {
86 return Current.Type == ReturnType::TYPE_DECL;
87 }
88
89 inline bool Is_Current_A_Preprocessed_Entity()
90 {
91 return Current.Type == ReturnType::TYPE_PREPROCESSED_ENTITY;
92 }
93
94 inline bool Is_Current_A_Macro_Undef()
95 {
96 return Current.Type == ReturnType::TYPE_MACRO_UNDEF;
97 }
98
99 /** Doing `*it` should access the Current object. */
100 struct Return &operator*()
101 {
102 return Current;
103 }
104
105 const struct Return &operator*() const
106 {
107 return Current;
108 }
109
110 /** Prefix iterator (++it) should advance to next element. */
111 TopLevelASTIterator &operator++(void)
112 {
113 Ended = !Advance();
114 return *this;
115 }
116
117 bool End(void)
118 {
119 return Ended;
120 }
121
122 private:
123 void Populate_Needs_Undef(void);
124
125 ASTUnit *AST;
126 SourceManager &SM;
127 PreprocessingRecord &PrepRec;
128
129 /* AST iterator. */
130 ASTUnit::top_level_iterator DeclIt;
131
132 /* Macro Iterators. */
133 PreprocessingRecord::iterator MacroIt;
134 unsigned UndefIt;
135
136 /* Vector of macros that needs to be undeclared. */
137 std::vector<MacroDirective*> NeedsUndef;
138
139 /* Comparator for SourceLocations. */
140 BeforeThanCompare<SourceLocation> BeforeClass;
141 inline bool Is_Before(const SourceLocation &a, const SourceLocation &b)
142 {
143 assert(a.isValid());
144 assert(b.isValid());
145 return BeforeClass(a, b);
146 }
147
148 inline bool Is_After(const SourceLocation &a, const SourceLocation &b)
149 {
150 assert(a.isValid());
151 assert(b.isValid());
152 return BeforeClass(b, a);
153 }
154
155 MacroWalker MW;
156
157 bool SkipMacrosInDecls;
158 bool Ended;
159 SourceLocation EndLocOfLastDecl;
160
161 public:
162 void Debug_Print(void);
163 };