source: src/Common/CodeLocationTools.cpp @ 365c8dcb

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 365c8dcb was 4ec9513, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Converted validate C, including adding DimensionExpr? to the new ast.

  • Property mode set to 100644
File size: 8.5 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 : Mon Mar 14 15:14:00 2022
13// Update Count     : 4
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(DirectiveDecl, DirectiveDecl) \
105    macro(StaticAssertDecl, StaticAssertDecl) \
106    macro(CompoundStmt, CompoundStmt) \
107    macro(ExprStmt, Stmt) \
108    macro(AsmStmt, Stmt) \
109    macro(DirectiveStmt, Stmt) \
110    macro(IfStmt, Stmt) \
111    macro(WhileDoStmt, Stmt) \
112    macro(ForStmt, Stmt) \
113    macro(SwitchStmt, Stmt) \
114    macro(CaseClause, CaseClause) \
115    macro(BranchStmt, Stmt) \
116    macro(ReturnStmt, Stmt) \
117    macro(ThrowStmt, Stmt) \
118    macro(TryStmt, Stmt) \
119    macro(CatchClause, CatchClause) \
120    macro(FinallyClause, FinallyClause) \
121    macro(SuspendStmt, Stmt) \
122    macro(WaitForStmt, Stmt) \
123    macro(WithStmt, Decl) \
124    macro(NullStmt, NullStmt) \
125    macro(DeclStmt, Stmt) \
126    macro(ImplicitCtorDtorStmt, Stmt) \
127    macro(MutexStmt, Stmt) \
128    macro(ApplicationExpr, Expr) \
129    macro(UntypedExpr, Expr) \
130    macro(NameExpr, Expr) \
131    macro(AddressExpr, Expr) \
132    macro(LabelAddressExpr, Expr) \
133    macro(CastExpr, Expr) \
134    macro(KeywordCastExpr, Expr) \
135    macro(VirtualCastExpr, Expr) \
136    macro(UntypedMemberExpr, Expr) \
137    macro(MemberExpr, Expr) \
138    macro(VariableExpr, Expr) \
139    macro(ConstantExpr, Expr) \
140    macro(SizeofExpr, Expr) \
141    macro(AlignofExpr, Expr) \
142    macro(UntypedOffsetofExpr, Expr) \
143    macro(OffsetofExpr, Expr) \
144    macro(OffsetPackExpr, Expr) \
145    macro(LogicalExpr, Expr) \
146    macro(ConditionalExpr, Expr) \
147    macro(CommaExpr, Expr) \
148    macro(TypeExpr, Expr) \
149    macro(DimensionExpr, Expr) \
150    macro(AsmExpr, Expr) \
151    macro(ImplicitCopyCtorExpr, Expr) \
152    macro(ConstructorExpr, Expr) \
153    macro(CompoundLiteralExpr, Expr) \
154    macro(RangeExpr, Expr) \
155    macro(UntypedTupleExpr, Expr) \
156    macro(TupleExpr, Expr) \
157    macro(TupleIndexExpr, Expr) \
158    macro(TupleAssignExpr, Expr) \
159    macro(StmtExpr, Expr) \
160    macro(UniqueExpr, Expr) \
161    macro(UntypedInitExpr, Expr) \
162    macro(InitExpr, Expr) \
163    macro(DeletedExpr, Expr) \
164    macro(DefaultArgExpr, Expr) \
165    macro(GenericExpr, Expr) \
166    macro(VoidType, Type) \
167    macro(BasicType, Type) \
168    macro(PointerType, Type) \
169    macro(ArrayType, Type) \
170    macro(ReferenceType, Type) \
171    macro(QualifiedType, Type) \
172    macro(FunctionType, Type) \
173    macro(StructInstType, Type) \
174    macro(UnionInstType, Type) \
175    macro(EnumInstType, Type) \
176    macro(TraitInstType, Type) \
177    macro(TypeInstType, Type) \
178    macro(TupleType, Type) \
179    macro(TypeofType, Type) \
180    macro(VTableType, Type) \
181    macro(VarArgsType, Type) \
182    macro(ZeroType, Type) \
183    macro(OneType, Type) \
184    macro(GlobalScopeType, Type) \
185    macro(Designation, Designation) \
186    macro(SingleInit, Init) \
187    macro(ListInit, Init) \
188    macro(ConstructorInit, Init) \
189    macro(Attribute, Attribute) \
190    macro(TypeSubstitution, TypeSubstitution)
191
192// These could even go into the ast namespace.
193enum class LeafKind {
194#define VISIT(node_type, return_type) node_type,
195        ALL_VISITS(VISIT)
196#undef VISIT
197};
198
199struct LeafKindVisitor : public ast::Visitor {
200        LeafKind kind;
201
202#define VISIT(node_type, return_type) \
203        const ast::return_type * visit( const ast::node_type * ) final { \
204                kind = LeafKind::node_type; \
205                return nullptr; \
206        }
207        ALL_VISITS(VISIT)
208#undef VISIT
209};
210
211constexpr size_t leaf_kind_count = (1 + (size_t)LeafKind::TypeSubstitution);
212
213LeafKind get_leaf_kind( ast::Node const * node ) {
214        LeafKindVisitor visitor;
215        node->accept( visitor );
216        return visitor.kind;
217}
218
219const char * leaf_kind_names[leaf_kind_count] = {
220#define VISIT(node_type, return_type) #node_type,
221        ALL_VISITS(VISIT)
222#undef VISIT
223};
224
225// Collect pointers to all the nodes with unset code locations.
226class CollectCore {
227        std::list< ast::ptr< ast::Node > > & unset;
228public:
229        CollectCore( std::list< ast::ptr< ast::Node > > & unset ) :
230                unset( unset )
231        {}
232
233        template<typename node_t>
234        void previsit( node_t const * node ) {
235                CodeLocation const * location = get_code_location( node );
236                if ( location && location->isUnset() ) {
237                        unset.push_back( node );
238                }
239        }
240};
241
242class LocalFillCore : public ast::WithGuards {
243        CodeLocation const * parent;
244public:
245        LocalFillCore( CodeLocation const & location ) : parent( &location ) {
246                assert( location.isSet() );
247        }
248
249        template<typename node_t>
250        auto previsit( node_t const * node )
251                        -> typename std::enable_if<has_code_location<node_t>::value, node_t const *>::type {
252                if ( node->location.isSet() ) {
253                        GuardValue( parent ) = &node->location;
254                        return node;
255                } else {
256                        node_t * mut = ast::mutate( node );
257                        mut->location = *parent;
258                        return mut;
259                }
260        }
261};
262
263} // namespace
264
265void checkAllCodeLocations( ast::TranslationUnit const & unit ) {
266        checkAllCodeLocations( "unknown location", unit );
267}
268
269void checkAllCodeLocations( char const * label, ast::TranslationUnit const & unit ) {
270        std::list< ast::ptr< ast::Node > > unsetNodes;
271        {
272                ast::Pass<CollectCore> collector( unsetNodes );
273                for ( auto node : unit.decls ) {
274                        node->accept( collector );
275                }
276        }
277        if ( unsetNodes.empty() ) {
278                return;
279        }
280
281        std::cerr << "Code Location check at " << label << " failed." << std::endl;
282        std::cerr << "Total nodes without a set code location: "
283                << unsetNodes.size() << std::endl;
284
285        size_t node_counts[leaf_kind_count] = {0};
286        for ( auto unset : unsetNodes ) {
287                node_counts[(size_t)get_leaf_kind(unset)] += 1;
288        }
289        for ( size_t i = 0 ; i < leaf_kind_count ; ++i ) {
290                if ( node_counts[i] ) {
291                        std::cerr << '\t' << node_counts[i]
292                                << " of type " << leaf_kind_names[i] << std::endl;
293                }
294        }
295
296        assert( unsetNodes.empty() );
297}
298
299void forceFillCodeLocations( ast::TranslationUnit & unit ) {
300        ast::Pass<FillCore>::run( unit );
301}
302
303ast::Node const * localFillCodeLocations(
304                CodeLocation const & location , ast::Node const * node ) {
305        ast::Pass<LocalFillCore> visitor( location );
306        return node->accept( visitor );
307}
Note: See TracBrowser for help on using the repository browser.