| [f57faf6f] | 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
 | 
|---|
| [33b7d49] | 11 | // Last Modified By : Andrew Beach
 | 
|---|
| [7675f58] | 12 | // Last Modified On : Wed May 11 16:16:00 2022
 | 
|---|
 | 13 | // Update Count     : 5
 | 
|---|
| [f57faf6f] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "CodeLocationTools.hpp"
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include <type_traits>
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "AST/Pass.hpp"
 | 
|---|
 | 21 | #include "AST/TranslationUnit.hpp"
 | 
|---|
| [c92bdcc] | 22 | #include "Common/CodeLocation.hpp"
 | 
|---|
| [f57faf6f] | 23 | 
 | 
|---|
 | 24 | namespace {
 | 
|---|
 | 25 | 
 | 
|---|
| [7675f58] | 26 | // Fill every location with a nearby (parent) location.
 | 
|---|
 | 27 | class FillCore : public ast::WithGuards {
 | 
|---|
 | 28 |         CodeLocation const * parent;
 | 
|---|
| [f57faf6f] | 29 | 
 | 
|---|
| [7675f58] | 30 |         template<typename node_t>
 | 
|---|
 | 31 |         node_t const * parse_visit( node_t const * node ) {
 | 
|---|
 | 32 |                 if ( node->location.isUnset() ) {
 | 
|---|
 | 33 |                         assert( parent );
 | 
|---|
 | 34 |                         node_t * newNode = ast::mutate( node );
 | 
|---|
 | 35 |                         newNode->location = *parent;
 | 
|---|
 | 36 |                         return newNode;
 | 
|---|
 | 37 |                 }
 | 
|---|
 | 38 |                 GuardValue( parent ) = &node->location;
 | 
|---|
 | 39 |                 return node;
 | 
|---|
 | 40 |         }
 | 
|---|
| [f57faf6f] | 41 | 
 | 
|---|
| [7675f58] | 42 |         bool hasUnsetLabels( const ast::Stmt * stmt ) {
 | 
|---|
 | 43 |                 for ( const ast::Label& label : stmt->labels ) {
 | 
|---|
 | 44 |                         if ( label.location.isUnset() ) {
 | 
|---|
 | 45 |                                 return true;
 | 
|---|
 | 46 |                         }
 | 
|---|
 | 47 |                 }
 | 
|---|
 | 48 |                 return false;
 | 
|---|
| [c9e0991] | 49 |         }
 | 
|---|
 | 50 | 
 | 
|---|
| [7675f58] | 51 |         template<typename node_t>
 | 
|---|
 | 52 |         node_t const * stmt_visit( node_t const * node ) {
 | 
|---|
 | 53 |                 assert( node->location.isSet() );
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |                 if ( hasUnsetLabels( node ) ) {
 | 
|---|
 | 56 |                         node_t * newNode = ast::mutate( node );
 | 
|---|
 | 57 |                         for ( ast::Label& label : newNode->labels ) {
 | 
|---|
 | 58 |                                 if ( label.location.isUnset() ) {
 | 
|---|
 | 59 |                                         label.location = newNode->location;
 | 
|---|
 | 60 |                                 }
 | 
|---|
 | 61 |                         }
 | 
|---|
 | 62 |                         return newNode;
 | 
|---|
 | 63 |                 }
 | 
|---|
 | 64 |                 return node;
 | 
|---|
| [f57faf6f] | 65 |         }
 | 
|---|
 | 66 | 
 | 
|---|
| [7675f58] | 67 |         template<typename node_t>
 | 
|---|
 | 68 |         auto visit( node_t const * node, long ) {
 | 
|---|
 | 69 |                 return node;
 | 
|---|
| [f57faf6f] | 70 |         }
 | 
|---|
 | 71 | 
 | 
|---|
| [7675f58] | 72 |         template<typename node_t>
 | 
|---|
 | 73 |         auto visit( node_t const * node, int ) -> typename
 | 
|---|
 | 74 |                         std::remove_reference< decltype( node->location, node ) >::type {
 | 
|---|
 | 75 |                 return parse_visit( node );
 | 
|---|
 | 76 |         }
 | 
|---|
| [f57faf6f] | 77 | 
 | 
|---|
| [7675f58] | 78 |         template<typename node_t>
 | 
|---|
 | 79 |         auto visit( node_t const * node, char ) -> typename
 | 
|---|
 | 80 |                         std::remove_reference< decltype( node->labels, node ) >::type {
 | 
|---|
 | 81 |                 return stmt_visit( parse_visit( node ) );
 | 
|---|
 | 82 |         }
 | 
|---|
| [c9e0991] | 83 | 
 | 
|---|
 | 84 | public:
 | 
|---|
 | 85 |         FillCore() : parent( nullptr ) {}
 | 
|---|
| [7675f58] | 86 |         FillCore( const CodeLocation& location ) : parent( &location ) {
 | 
|---|
 | 87 |                 assert( location.isSet() );
 | 
|---|
 | 88 |         }
 | 
|---|
| [c9e0991] | 89 | 
 | 
|---|
 | 90 |         template<typename node_t>
 | 
|---|
 | 91 |         node_t const * previsit( node_t const * node ) {
 | 
|---|
| [7675f58] | 92 |                 return visit( node, '\0' );
 | 
|---|
| [c9e0991] | 93 |         }
 | 
|---|
 | 94 | };
 | 
|---|
 | 95 | 
 | 
|---|
| [364a03c] | 96 | // ALL_VISITS(macro)
 | 
|---|
 | 97 | // Expands `macro(node_type, return_type)` for every visit method of the
 | 
|---|
 | 98 | // ast::Visitor class where node_type is the name of the parameter and
 | 
|---|
 | 99 | // return_type is the name of the return type; not including the namespace,
 | 
|---|
 | 100 | // pointer or const qualifiers.
 | 
|---|
 | 101 | #define ALL_VISITS(macro) \
 | 
|---|
 | 102 |     macro(ObjectDecl, DeclWithType) \
 | 
|---|
 | 103 |     macro(FunctionDecl, DeclWithType) \
 | 
|---|
 | 104 |     macro(StructDecl, Decl) \
 | 
|---|
 | 105 |     macro(UnionDecl, Decl) \
 | 
|---|
 | 106 |     macro(EnumDecl, Decl) \
 | 
|---|
 | 107 |     macro(TraitDecl, Decl) \
 | 
|---|
 | 108 |     macro(TypeDecl, Decl) \
 | 
|---|
 | 109 |     macro(TypedefDecl, Decl) \
 | 
|---|
 | 110 |     macro(AsmDecl, AsmDecl) \
 | 
|---|
| [2d019af] | 111 |     macro(DirectiveDecl, DirectiveDecl) \
 | 
|---|
| [364a03c] | 112 |     macro(StaticAssertDecl, StaticAssertDecl) \
 | 
|---|
| [71806e0] | 113 |     macro(InlineMemberDecl, DeclWithType) \
 | 
|---|
| [364a03c] | 114 |     macro(CompoundStmt, CompoundStmt) \
 | 
|---|
 | 115 |     macro(ExprStmt, Stmt) \
 | 
|---|
 | 116 |     macro(AsmStmt, Stmt) \
 | 
|---|
 | 117 |     macro(DirectiveStmt, Stmt) \
 | 
|---|
 | 118 |     macro(IfStmt, Stmt) \
 | 
|---|
| [3b0bc16] | 119 |     macro(WhileDoStmt, Stmt) \
 | 
|---|
| [364a03c] | 120 |     macro(ForStmt, Stmt) \
 | 
|---|
| [fca78f1] | 121 |     macro(ForeachStmt, Stmt) \
 | 
|---|
| [364a03c] | 122 |     macro(SwitchStmt, Stmt) \
 | 
|---|
| [400b8be] | 123 |     macro(CaseClause, CaseClause) \
 | 
|---|
| [364a03c] | 124 |     macro(BranchStmt, Stmt) \
 | 
|---|
 | 125 |     macro(ReturnStmt, Stmt) \
 | 
|---|
 | 126 |     macro(ThrowStmt, Stmt) \
 | 
|---|
 | 127 |     macro(TryStmt, Stmt) \
 | 
|---|
| [400b8be] | 128 |     macro(CatchClause, CatchClause) \
 | 
|---|
 | 129 |     macro(FinallyClause, FinallyClause) \
 | 
|---|
| [364a03c] | 130 |     macro(SuspendStmt, Stmt) \
 | 
|---|
| [c86b08d] | 131 |     macro(WhenClause, WhenClause) \
 | 
|---|
| [364a03c] | 132 |     macro(WaitForStmt, Stmt) \
 | 
|---|
| [f6e6a55] | 133 |     macro(WaitForClause, WaitForClause) \
 | 
|---|
| [c86b08d] | 134 |     macro(WaitUntilStmt, Stmt) \
 | 
|---|
| [364a03c] | 135 |     macro(WithStmt, Decl) \
 | 
|---|
 | 136 |     macro(NullStmt, NullStmt) \
 | 
|---|
 | 137 |     macro(DeclStmt, Stmt) \
 | 
|---|
 | 138 |     macro(ImplicitCtorDtorStmt, Stmt) \
 | 
|---|
| [de52331] | 139 |     macro(MutexStmt, Stmt) \
 | 
|---|
| [eb779d5] | 140 |     macro(CorunStmt, Stmt) \
 | 
|---|
| [3d9d017] | 141 |         macro(CoforStmt, Stmt) \
 | 
|---|
| [364a03c] | 142 |     macro(ApplicationExpr, Expr) \
 | 
|---|
 | 143 |     macro(UntypedExpr, Expr) \
 | 
|---|
 | 144 |     macro(NameExpr, Expr) \
 | 
|---|
| [b0d9ff7] | 145 |         macro(QualifiedNameExpr, Expr) \
 | 
|---|
| [364a03c] | 146 |     macro(AddressExpr, Expr) \
 | 
|---|
 | 147 |     macro(LabelAddressExpr, Expr) \
 | 
|---|
 | 148 |     macro(CastExpr, Expr) \
 | 
|---|
 | 149 |     macro(KeywordCastExpr, Expr) \
 | 
|---|
 | 150 |     macro(VirtualCastExpr, Expr) \
 | 
|---|
 | 151 |     macro(UntypedMemberExpr, Expr) \
 | 
|---|
 | 152 |     macro(MemberExpr, Expr) \
 | 
|---|
 | 153 |     macro(VariableExpr, Expr) \
 | 
|---|
 | 154 |     macro(ConstantExpr, Expr) \
 | 
|---|
 | 155 |     macro(SizeofExpr, Expr) \
 | 
|---|
 | 156 |     macro(AlignofExpr, Expr) \
 | 
|---|
| [857b5f9] | 157 |     macro(CountofExpr, Expr ) \
 | 
|---|
| [364a03c] | 158 |     macro(UntypedOffsetofExpr, Expr) \
 | 
|---|
 | 159 |     macro(OffsetofExpr, Expr) \
 | 
|---|
 | 160 |     macro(OffsetPackExpr, Expr) \
 | 
|---|
 | 161 |     macro(LogicalExpr, Expr) \
 | 
|---|
 | 162 |     macro(ConditionalExpr, Expr) \
 | 
|---|
 | 163 |     macro(CommaExpr, Expr) \
 | 
|---|
 | 164 |     macro(TypeExpr, Expr) \
 | 
|---|
| [4ec9513] | 165 |     macro(DimensionExpr, Expr) \
 | 
|---|
| [364a03c] | 166 |     macro(AsmExpr, Expr) \
 | 
|---|
 | 167 |     macro(ImplicitCopyCtorExpr, Expr) \
 | 
|---|
 | 168 |     macro(ConstructorExpr, Expr) \
 | 
|---|
 | 169 |     macro(CompoundLiteralExpr, Expr) \
 | 
|---|
 | 170 |     macro(RangeExpr, Expr) \
 | 
|---|
 | 171 |     macro(UntypedTupleExpr, Expr) \
 | 
|---|
 | 172 |     macro(TupleExpr, Expr) \
 | 
|---|
 | 173 |     macro(TupleIndexExpr, Expr) \
 | 
|---|
 | 174 |     macro(TupleAssignExpr, Expr) \
 | 
|---|
 | 175 |     macro(StmtExpr, Expr) \
 | 
|---|
 | 176 |     macro(UniqueExpr, Expr) \
 | 
|---|
 | 177 |     macro(UntypedInitExpr, Expr) \
 | 
|---|
 | 178 |     macro(InitExpr, Expr) \
 | 
|---|
 | 179 |     macro(DeletedExpr, Expr) \
 | 
|---|
 | 180 |     macro(DefaultArgExpr, Expr) \
 | 
|---|
 | 181 |     macro(GenericExpr, Expr) \
 | 
|---|
 | 182 |     macro(VoidType, Type) \
 | 
|---|
 | 183 |     macro(BasicType, Type) \
 | 
|---|
 | 184 |     macro(PointerType, Type) \
 | 
|---|
 | 185 |     macro(ArrayType, Type) \
 | 
|---|
 | 186 |     macro(ReferenceType, Type) \
 | 
|---|
 | 187 |     macro(QualifiedType, Type) \
 | 
|---|
 | 188 |     macro(FunctionType, Type) \
 | 
|---|
 | 189 |     macro(StructInstType, Type) \
 | 
|---|
 | 190 |     macro(UnionInstType, Type) \
 | 
|---|
 | 191 |     macro(EnumInstType, Type) \
 | 
|---|
 | 192 |     macro(TraitInstType, Type) \
 | 
|---|
 | 193 |     macro(TypeInstType, Type) \
 | 
|---|
 | 194 |     macro(TupleType, Type) \
 | 
|---|
 | 195 |     macro(TypeofType, Type) \
 | 
|---|
| [ccb6fd8] | 196 |     macro(VTableType, Type) \
 | 
|---|
| [364a03c] | 197 |     macro(VarArgsType, Type) \
 | 
|---|
 | 198 |     macro(ZeroType, Type) \
 | 
|---|
 | 199 |     macro(OneType, Type) \
 | 
|---|
 | 200 |     macro(GlobalScopeType, Type) \
 | 
|---|
 | 201 |     macro(Designation, Designation) \
 | 
|---|
 | 202 |     macro(SingleInit, Init) \
 | 
|---|
 | 203 |     macro(ListInit, Init) \
 | 
|---|
 | 204 |     macro(ConstructorInit, Init) \
 | 
|---|
 | 205 |     macro(Attribute, Attribute) \
 | 
|---|
 | 206 |     macro(TypeSubstitution, TypeSubstitution)
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 | // These could even go into the ast namespace.
 | 
|---|
 | 209 | enum class LeafKind {
 | 
|---|
 | 210 | #define VISIT(node_type, return_type) node_type,
 | 
|---|
 | 211 |         ALL_VISITS(VISIT)
 | 
|---|
 | 212 | #undef VISIT
 | 
|---|
 | 213 | };
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 | struct LeafKindVisitor : public ast::Visitor {
 | 
|---|
| [b05beaa] | 216 |         LeafKind result;
 | 
|---|
| [364a03c] | 217 | 
 | 
|---|
 | 218 | #define VISIT(node_type, return_type) \
 | 
|---|
 | 219 |         const ast::return_type * visit( const ast::node_type * ) final { \
 | 
|---|
| [b05beaa] | 220 |                 result = LeafKind::node_type; \
 | 
|---|
| [364a03c] | 221 |                 return nullptr; \
 | 
|---|
 | 222 |         }
 | 
|---|
 | 223 |         ALL_VISITS(VISIT)
 | 
|---|
 | 224 | #undef VISIT
 | 
|---|
 | 225 | };
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 | constexpr size_t leaf_kind_count = (1 + (size_t)LeafKind::TypeSubstitution);
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 | LeafKind get_leaf_kind( ast::Node const * node ) {
 | 
|---|
| [b05beaa] | 230 |         return ast::Pass<LeafKindVisitor>::read( node );
 | 
|---|
| [364a03c] | 231 | }
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 | const char * leaf_kind_names[leaf_kind_count] = {
 | 
|---|
 | 234 | #define VISIT(node_type, return_type) #node_type,
 | 
|---|
 | 235 |         ALL_VISITS(VISIT)
 | 
|---|
 | 236 | #undef VISIT
 | 
|---|
 | 237 | };
 | 
|---|
 | 238 | 
 | 
|---|
| [f57faf6f] | 239 | // Collect pointers to all the nodes with unset code locations.
 | 
|---|
 | 240 | class CollectCore {
 | 
|---|
 | 241 |         std::list< ast::ptr< ast::Node > > & unset;
 | 
|---|
 | 242 | public:
 | 
|---|
 | 243 |         CollectCore( std::list< ast::ptr< ast::Node > > & unset ) :
 | 
|---|
 | 244 |                 unset( unset )
 | 
|---|
 | 245 |         {}
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |         template<typename node_t>
 | 
|---|
| [7675f58] | 248 |         auto previsit( node_t const * node ) -> decltype( node->location, void() ) {
 | 
|---|
 | 249 |                 if ( node->location.isUnset() ) {
 | 
|---|
| [f57faf6f] | 250 |                         unset.push_back( node );
 | 
|---|
 | 251 |                 }
 | 
|---|
 | 252 |         }
 | 
|---|
 | 253 | };
 | 
|---|
 | 254 | 
 | 
|---|
 | 255 | } // namespace
 | 
|---|
 | 256 | 
 | 
|---|
 | 257 | void checkAllCodeLocations( ast::TranslationUnit const & unit ) {
 | 
|---|
| [364a03c] | 258 |         checkAllCodeLocations( "unknown location", unit );
 | 
|---|
 | 259 | }
 | 
|---|
 | 260 | 
 | 
|---|
 | 261 | void checkAllCodeLocations( char const * label, ast::TranslationUnit const & unit ) {
 | 
|---|
| [f57faf6f] | 262 |         std::list< ast::ptr< ast::Node > > unsetNodes;
 | 
|---|
 | 263 |         {
 | 
|---|
 | 264 |                 ast::Pass<CollectCore> collector( unsetNodes );
 | 
|---|
 | 265 |                 for ( auto node : unit.decls ) {
 | 
|---|
 | 266 |                         node->accept( collector );
 | 
|---|
 | 267 |                 }
 | 
|---|
 | 268 |         }
 | 
|---|
 | 269 |         if ( unsetNodes.empty() ) {
 | 
|---|
 | 270 |                 return;
 | 
|---|
 | 271 |         }
 | 
|---|
 | 272 | 
 | 
|---|
| [364a03c] | 273 |         std::cerr << "Code Location check at " << label << " failed." << std::endl;
 | 
|---|
| [f57faf6f] | 274 |         std::cerr << "Total nodes without a set code location: "
 | 
|---|
 | 275 |                 << unsetNodes.size() << std::endl;
 | 
|---|
 | 276 | 
 | 
|---|
| [364a03c] | 277 |         size_t node_counts[leaf_kind_count] = {0};
 | 
|---|
 | 278 |         for ( auto unset : unsetNodes ) {
 | 
|---|
 | 279 |                 node_counts[(size_t)get_leaf_kind(unset)] += 1;
 | 
|---|
 | 280 |         }
 | 
|---|
 | 281 |         for ( size_t i = 0 ; i < leaf_kind_count ; ++i ) {
 | 
|---|
 | 282 |                 if ( node_counts[i] ) {
 | 
|---|
 | 283 |                         std::cerr << '\t' << node_counts[i]
 | 
|---|
 | 284 |                                 << " of type " << leaf_kind_names[i] << std::endl;
 | 
|---|
 | 285 |                 }
 | 
|---|
 | 286 |         }
 | 
|---|
 | 287 | 
 | 
|---|
| [f57faf6f] | 288 |         assert( unsetNodes.empty() );
 | 
|---|
 | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | void forceFillCodeLocations( ast::TranslationUnit & unit ) {
 | 
|---|
 | 292 |         ast::Pass<FillCore>::run( unit );
 | 
|---|
 | 293 | }
 | 
|---|
| [33b7d49] | 294 | 
 | 
|---|
 | 295 | ast::Node const * localFillCodeLocations(
 | 
|---|
 | 296 |                 CodeLocation const & location , ast::Node const * node ) {
 | 
|---|
| [7675f58] | 297 |         ast::Pass<FillCore> visitor( location );
 | 
|---|
| [33b7d49] | 298 |         return node->accept( visitor );
 | 
|---|
 | 299 | }
 | 
|---|