source: src/Common/CodeLocationTools.cpp @ 364a03c

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 364a03c was 364a03c, checked in by Andrew Beach <ajbeach@…>, 12 months ago

Added more information to checkAllCodeLocations.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// CodeLocationTools.cpp -- Additional tools for code locations.
8//
9// Author           : Andrew Beach
10// Created On       : Fri Dec  4 15:42:00 2020
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Dec  9  9:42:00 2020
13// Update Count     : 1
14//
15
16#include "CodeLocationTools.hpp"
17
18#include <type_traits>
19
20#include "AST/Pass.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "Common/CodeLocation.h"
23
24namespace {
25
26// There are a lot of helpers in this file that could be used much more
27// generally if anyone has another use for them.
28
29// Check if a node type has a code location.
30template<typename node_t>
31struct has_code_location : public std::is_base_of<ast::ParseNode, node_t> {};
32
33template<typename node_t, bool has_location>
34struct __GetCL;
35
36template<typename node_t>
37struct __GetCL<node_t, true> {
38        static inline CodeLocation const * get( node_t const * node ) {
39                return &node->location;
40        }
41
42        static inline CodeLocation * get( node_t * node ) {
43                return &node->location;
44        }
45};
46
47template<typename node_t>
48struct __GetCL<node_t, false> {
49        static inline CodeLocation * get( node_t const * ) {
50                return nullptr;
51        }
52};
53
54template<typename node_t>
55CodeLocation const * get_code_location( node_t const * node ) {
56        return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
57}
58
59template<typename node_t>
60CodeLocation * get_code_location( node_t * node ) {
61        return __GetCL< node_t, has_code_location< node_t >::value >::get( node );
62}
63
64// Fill every location with a nearby (parent) location.
65class FillCore : public ast::WithGuards {
66        CodeLocation const * parent;
67public:
68        FillCore() : parent( nullptr ) {}
69
70        template<typename node_t>
71        node_t const * previsit( node_t const * node ) {
72                GuardValue( parent );
73                CodeLocation const * location = get_code_location( node );
74                if ( location && location->isUnset() ) {
75                        assert( parent );
76                        node_t * newNode = ast::mutate( node );
77                        CodeLocation * newLocation = get_code_location( newNode );
78                        assert( newLocation );
79                        *newLocation = *parent;
80                        parent = newLocation;
81                        return newNode;
82                } else if ( location ) {
83                        parent = location;
84                }
85                return node;
86        }
87};
88
89// ALL_VISITS(macro)
90// Expands `macro(node_type, return_type)` for every visit method of the
91// ast::Visitor class where node_type is the name of the parameter and
92// return_type is the name of the return type; not including the namespace,
93// pointer or const qualifiers.
94#define ALL_VISITS(macro) \
95    macro(ObjectDecl, DeclWithType) \
96    macro(FunctionDecl, DeclWithType) \
97    macro(StructDecl, Decl) \
98    macro(UnionDecl, Decl) \
99    macro(EnumDecl, Decl) \
100    macro(TraitDecl, Decl) \
101    macro(TypeDecl, Decl) \
102    macro(TypedefDecl, Decl) \
103    macro(AsmDecl, AsmDecl) \
104    macro(StaticAssertDecl, StaticAssertDecl) \
105    macro(CompoundStmt, CompoundStmt) \
106    macro(ExprStmt, Stmt) \
107    macro(AsmStmt, Stmt) \
108    macro(DirectiveStmt, Stmt) \
109    macro(IfStmt, Stmt) \
110    macro(WhileStmt, Stmt) \
111    macro(ForStmt, Stmt) \
112    macro(SwitchStmt, Stmt) \
113    macro(CaseStmt, Stmt) \
114    macro(BranchStmt, Stmt) \
115    macro(ReturnStmt, Stmt) \
116    macro(ThrowStmt, Stmt) \
117    macro(TryStmt, Stmt) \
118    macro(CatchStmt, Stmt) \
119    macro(FinallyStmt, Stmt) \
120    macro(SuspendStmt, Stmt) \
121    macro(WaitForStmt, Stmt) \
122    macro(WithStmt, Decl) \
123    macro(NullStmt, NullStmt) \
124    macro(DeclStmt, Stmt) \
125    macro(ImplicitCtorDtorStmt, Stmt) \
126    macro(ApplicationExpr, Expr) \
127    macro(UntypedExpr, Expr) \
128    macro(NameExpr, Expr) \
129    macro(AddressExpr, Expr) \
130    macro(LabelAddressExpr, Expr) \
131    macro(CastExpr, Expr) \
132    macro(KeywordCastExpr, Expr) \
133    macro(VirtualCastExpr, Expr) \
134    macro(UntypedMemberExpr, Expr) \
135    macro(MemberExpr, Expr) \
136    macro(VariableExpr, Expr) \
137    macro(ConstantExpr, Expr) \
138    macro(SizeofExpr, Expr) \
139    macro(AlignofExpr, Expr) \
140    macro(UntypedOffsetofExpr, Expr) \
141    macro(OffsetofExpr, Expr) \
142    macro(OffsetPackExpr, Expr) \
143    macro(LogicalExpr, Expr) \
144    macro(ConditionalExpr, Expr) \
145    macro(CommaExpr, Expr) \
146    macro(TypeExpr, Expr) \
147    macro(AsmExpr, Expr) \
148    macro(ImplicitCopyCtorExpr, Expr) \
149    macro(ConstructorExpr, Expr) \
150    macro(CompoundLiteralExpr, Expr) \
151    macro(RangeExpr, Expr) \
152    macro(UntypedTupleExpr, Expr) \
153    macro(TupleExpr, Expr) \
154    macro(TupleIndexExpr, Expr) \
155    macro(TupleAssignExpr, Expr) \
156    macro(StmtExpr, Expr) \
157    macro(UniqueExpr, Expr) \
158    macro(UntypedInitExpr, Expr) \
159    macro(InitExpr, Expr) \
160    macro(DeletedExpr, Expr) \
161    macro(DefaultArgExpr, Expr) \
162    macro(GenericExpr, Expr) \
163    macro(VoidType, Type) \
164    macro(BasicType, Type) \
165    macro(PointerType, Type) \
166    macro(ArrayType, Type) \
167    macro(ReferenceType, Type) \
168    macro(QualifiedType, Type) \
169    macro(FunctionType, Type) \
170    macro(StructInstType, Type) \
171    macro(UnionInstType, Type) \
172    macro(EnumInstType, Type) \
173    macro(TraitInstType, Type) \
174    macro(TypeInstType, Type) \
175    macro(TupleType, Type) \
176    macro(TypeofType, Type) \
177    macro(VarArgsType, Type) \
178    macro(ZeroType, Type) \
179    macro(OneType, Type) \
180    macro(GlobalScopeType, Type) \
181    macro(Designation, Designation) \
182    macro(SingleInit, Init) \
183    macro(ListInit, Init) \
184    macro(ConstructorInit, Init) \
185    macro(Attribute, Attribute) \
186    macro(TypeSubstitution, TypeSubstitution)
187
188// These could even go into the ast namespace.
189enum class LeafKind {
190#define VISIT(node_type, return_type) node_type,
191        ALL_VISITS(VISIT)
192#undef VISIT
193};
194
195struct LeafKindVisitor : public ast::Visitor {
196        LeafKind kind;
197
198#define VISIT(node_type, return_type) \
199        const ast::return_type * visit( const ast::node_type * ) final { \
200                kind = LeafKind::node_type; \
201                return nullptr; \
202        }
203        ALL_VISITS(VISIT)
204#undef VISIT
205};
206
207constexpr size_t leaf_kind_count = (1 + (size_t)LeafKind::TypeSubstitution);
208
209LeafKind get_leaf_kind( ast::Node const * node ) {
210        LeafKindVisitor visitor;
211        node->accept( visitor );
212        return visitor.kind;
213}
214
215const char * leaf_kind_names[leaf_kind_count] = {
216#define VISIT(node_type, return_type) #node_type,
217        ALL_VISITS(VISIT)
218#undef VISIT
219};
220
221// Collect pointers to all the nodes with unset code locations.
222class CollectCore {
223        std::list< ast::ptr< ast::Node > > & unset;
224public:
225        CollectCore( std::list< ast::ptr< ast::Node > > & unset ) :
226                unset( unset )
227        {}
228
229        template<typename node_t>
230        void previsit( node_t const * node ) {
231                CodeLocation const * location = get_code_location( node );
232                if ( location && location->isUnset() ) {
233                        unset.push_back( node );
234                }
235        }
236};
237
238} // namespace
239
240void checkAllCodeLocations( ast::TranslationUnit const & unit ) {
241        checkAllCodeLocations( "unknown location", unit );
242}
243
244void checkAllCodeLocations( char const * label, ast::TranslationUnit const & unit ) {
245        std::list< ast::ptr< ast::Node > > unsetNodes;
246        {
247                ast::Pass<CollectCore> collector( unsetNodes );
248                for ( auto node : unit.decls ) {
249                        node->accept( collector );
250                }
251        }
252        if ( unsetNodes.empty() ) {
253                return;
254        }
255
256        std::cerr << "Code Location check at " << label << " failed." << std::endl;
257        std::cerr << "Total nodes without a set code location: "
258                << unsetNodes.size() << std::endl;
259
260        size_t node_counts[leaf_kind_count] = {0};
261        for ( auto unset : unsetNodes ) {
262                node_counts[(size_t)get_leaf_kind(unset)] += 1;
263        }
264        for ( size_t i = 0 ; i < leaf_kind_count ; ++i ) {
265                if ( node_counts[i] ) {
266                        std::cerr << '\t' << node_counts[i]
267                                << " of type " << leaf_kind_names[i] << std::endl;
268                }
269        }
270
271        assert( unsetNodes.empty() );
272}
273
274void forceFillCodeLocations( ast::TranslationUnit & unit ) {
275        ast::Pass<FillCore>::run( unit );
276}
Note: See TracBrowser for help on using the repository browser.