| 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 : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Mar 12 18:35:37 2021 | 
|---|
| 13 | // Update Count     : 2 | 
|---|
| 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 | // 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. | 
|---|
| 30 | template<typename node_t> | 
|---|
| 31 | struct has_code_location : public std::is_base_of<ast::ParseNode, node_t> {}; | 
|---|
| 32 |  | 
|---|
| 33 | template<typename node_t, bool has_location> | 
|---|
| 34 | struct __GetCL; | 
|---|
| 35 |  | 
|---|
| 36 | template<typename node_t> | 
|---|
| 37 | struct __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 |  | 
|---|
| 47 | template<typename node_t> | 
|---|
| 48 | struct __GetCL<node_t, false> { | 
|---|
| 49 | static inline CodeLocation * get( node_t const * ) { | 
|---|
| 50 | return nullptr; | 
|---|
| 51 | } | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | template<typename node_t> | 
|---|
| 55 | CodeLocation const * get_code_location( node_t const * node ) { | 
|---|
| 56 | return __GetCL< node_t, has_code_location< node_t >::value >::get( node ); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | template<typename node_t> | 
|---|
| 60 | CodeLocation * 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. | 
|---|
| 65 | class FillCore : public ast::WithGuards { | 
|---|
| 66 | CodeLocation const * parent; | 
|---|
| 67 | public: | 
|---|
| 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(WhileStmt, Stmt) \ | 
|---|
| 112 | macro(ForStmt, Stmt) \ | 
|---|
| 113 | macro(SwitchStmt, Stmt) \ | 
|---|
| 114 | macro(CaseStmt, Stmt) \ | 
|---|
| 115 | macro(BranchStmt, Stmt) \ | 
|---|
| 116 | macro(ReturnStmt, Stmt) \ | 
|---|
| 117 | macro(ThrowStmt, Stmt) \ | 
|---|
| 118 | macro(TryStmt, Stmt) \ | 
|---|
| 119 | macro(CatchStmt, Stmt) \ | 
|---|
| 120 | macro(FinallyStmt, Stmt) \ | 
|---|
| 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(ApplicationExpr, Expr) \ | 
|---|
| 128 | macro(UntypedExpr, Expr) \ | 
|---|
| 129 | macro(NameExpr, Expr) \ | 
|---|
| 130 | macro(AddressExpr, Expr) \ | 
|---|
| 131 | macro(LabelAddressExpr, Expr) \ | 
|---|
| 132 | macro(CastExpr, Expr) \ | 
|---|
| 133 | macro(KeywordCastExpr, Expr) \ | 
|---|
| 134 | macro(VirtualCastExpr, Expr) \ | 
|---|
| 135 | macro(UntypedMemberExpr, Expr) \ | 
|---|
| 136 | macro(MemberExpr, Expr) \ | 
|---|
| 137 | macro(VariableExpr, Expr) \ | 
|---|
| 138 | macro(ConstantExpr, Expr) \ | 
|---|
| 139 | macro(SizeofExpr, Expr) \ | 
|---|
| 140 | macro(AlignofExpr, Expr) \ | 
|---|
| 141 | macro(UntypedOffsetofExpr, Expr) \ | 
|---|
| 142 | macro(OffsetofExpr, Expr) \ | 
|---|
| 143 | macro(OffsetPackExpr, Expr) \ | 
|---|
| 144 | macro(LogicalExpr, Expr) \ | 
|---|
| 145 | macro(ConditionalExpr, Expr) \ | 
|---|
| 146 | macro(CommaExpr, Expr) \ | 
|---|
| 147 | macro(TypeExpr, Expr) \ | 
|---|
| 148 | macro(AsmExpr, Expr) \ | 
|---|
| 149 | macro(ImplicitCopyCtorExpr, Expr) \ | 
|---|
| 150 | macro(ConstructorExpr, Expr) \ | 
|---|
| 151 | macro(CompoundLiteralExpr, Expr) \ | 
|---|
| 152 | macro(RangeExpr, Expr) \ | 
|---|
| 153 | macro(UntypedTupleExpr, Expr) \ | 
|---|
| 154 | macro(TupleExpr, Expr) \ | 
|---|
| 155 | macro(TupleIndexExpr, Expr) \ | 
|---|
| 156 | macro(TupleAssignExpr, Expr) \ | 
|---|
| 157 | macro(StmtExpr, Expr) \ | 
|---|
| 158 | macro(UniqueExpr, Expr) \ | 
|---|
| 159 | macro(UntypedInitExpr, Expr) \ | 
|---|
| 160 | macro(InitExpr, Expr) \ | 
|---|
| 161 | macro(DeletedExpr, Expr) \ | 
|---|
| 162 | macro(DefaultArgExpr, Expr) \ | 
|---|
| 163 | macro(GenericExpr, Expr) \ | 
|---|
| 164 | macro(VoidType, Type) \ | 
|---|
| 165 | macro(BasicType, Type) \ | 
|---|
| 166 | macro(PointerType, Type) \ | 
|---|
| 167 | macro(ArrayType, Type) \ | 
|---|
| 168 | macro(ReferenceType, Type) \ | 
|---|
| 169 | macro(QualifiedType, Type) \ | 
|---|
| 170 | macro(FunctionType, Type) \ | 
|---|
| 171 | macro(StructInstType, Type) \ | 
|---|
| 172 | macro(UnionInstType, Type) \ | 
|---|
| 173 | macro(EnumInstType, Type) \ | 
|---|
| 174 | macro(TraitInstType, Type) \ | 
|---|
| 175 | macro(TypeInstType, Type) \ | 
|---|
| 176 | macro(TupleType, Type) \ | 
|---|
| 177 | macro(TypeofType, Type) \ | 
|---|
| 178 | macro(VTableType, Type) \ | 
|---|
| 179 | macro(VarArgsType, Type) \ | 
|---|
| 180 | macro(ZeroType, Type) \ | 
|---|
| 181 | macro(OneType, Type) \ | 
|---|
| 182 | macro(GlobalScopeType, Type) \ | 
|---|
| 183 | macro(Designation, Designation) \ | 
|---|
| 184 | macro(SingleInit, Init) \ | 
|---|
| 185 | macro(ListInit, Init) \ | 
|---|
| 186 | macro(ConstructorInit, Init) \ | 
|---|
| 187 | macro(Attribute, Attribute) \ | 
|---|
| 188 | macro(TypeSubstitution, TypeSubstitution) | 
|---|
| 189 |  | 
|---|
| 190 | // These could even go into the ast namespace. | 
|---|
| 191 | enum class LeafKind { | 
|---|
| 192 | #define VISIT(node_type, return_type) node_type, | 
|---|
| 193 | ALL_VISITS(VISIT) | 
|---|
| 194 | #undef VISIT | 
|---|
| 195 | }; | 
|---|
| 196 |  | 
|---|
| 197 | struct LeafKindVisitor : public ast::Visitor { | 
|---|
| 198 | LeafKind kind; | 
|---|
| 199 |  | 
|---|
| 200 | #define VISIT(node_type, return_type) \ | 
|---|
| 201 | const ast::return_type * visit( const ast::node_type * ) final { \ | 
|---|
| 202 | kind = LeafKind::node_type; \ | 
|---|
| 203 | return nullptr; \ | 
|---|
| 204 | } | 
|---|
| 205 | ALL_VISITS(VISIT) | 
|---|
| 206 | #undef VISIT | 
|---|
| 207 | }; | 
|---|
| 208 |  | 
|---|
| 209 | constexpr size_t leaf_kind_count = (1 + (size_t)LeafKind::TypeSubstitution); | 
|---|
| 210 |  | 
|---|
| 211 | LeafKind get_leaf_kind( ast::Node const * node ) { | 
|---|
| 212 | LeafKindVisitor visitor; | 
|---|
| 213 | node->accept( visitor ); | 
|---|
| 214 | return visitor.kind; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | const char * leaf_kind_names[leaf_kind_count] = { | 
|---|
| 218 | #define VISIT(node_type, return_type) #node_type, | 
|---|
| 219 | ALL_VISITS(VISIT) | 
|---|
| 220 | #undef VISIT | 
|---|
| 221 | }; | 
|---|
| 222 |  | 
|---|
| 223 | // Collect pointers to all the nodes with unset code locations. | 
|---|
| 224 | class CollectCore { | 
|---|
| 225 | std::list< ast::ptr< ast::Node > > & unset; | 
|---|
| 226 | public: | 
|---|
| 227 | CollectCore( std::list< ast::ptr< ast::Node > > & unset ) : | 
|---|
| 228 | unset( unset ) | 
|---|
| 229 | {} | 
|---|
| 230 |  | 
|---|
| 231 | template<typename node_t> | 
|---|
| 232 | void previsit( node_t const * node ) { | 
|---|
| 233 | CodeLocation const * location = get_code_location( node ); | 
|---|
| 234 | if ( location && location->isUnset() ) { | 
|---|
| 235 | unset.push_back( node ); | 
|---|
| 236 | } | 
|---|
| 237 | } | 
|---|
| 238 | }; | 
|---|
| 239 |  | 
|---|
| 240 | } // namespace | 
|---|
| 241 |  | 
|---|
| 242 | void checkAllCodeLocations( ast::TranslationUnit const & unit ) { | 
|---|
| 243 | checkAllCodeLocations( "unknown location", unit ); | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | void checkAllCodeLocations( char const * label, ast::TranslationUnit const & unit ) { | 
|---|
| 247 | std::list< ast::ptr< ast::Node > > unsetNodes; | 
|---|
| 248 | { | 
|---|
| 249 | ast::Pass<CollectCore> collector( unsetNodes ); | 
|---|
| 250 | for ( auto node : unit.decls ) { | 
|---|
| 251 | node->accept( collector ); | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 | if ( unsetNodes.empty() ) { | 
|---|
| 255 | return; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | std::cerr << "Code Location check at " << label << " failed." << std::endl; | 
|---|
| 259 | std::cerr << "Total nodes without a set code location: " | 
|---|
| 260 | << unsetNodes.size() << std::endl; | 
|---|
| 261 |  | 
|---|
| 262 | size_t node_counts[leaf_kind_count] = {0}; | 
|---|
| 263 | for ( auto unset : unsetNodes ) { | 
|---|
| 264 | node_counts[(size_t)get_leaf_kind(unset)] += 1; | 
|---|
| 265 | } | 
|---|
| 266 | for ( size_t i = 0 ; i < leaf_kind_count ; ++i ) { | 
|---|
| 267 | if ( node_counts[i] ) { | 
|---|
| 268 | std::cerr << '\t' << node_counts[i] | 
|---|
| 269 | << " of type " << leaf_kind_names[i] << std::endl; | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | assert( unsetNodes.empty() ); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | void forceFillCodeLocations( ast::TranslationUnit & unit ) { | 
|---|
| 277 | ast::Pass<FillCore>::run( unit ); | 
|---|
| 278 | } | 
|---|