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