Changeset 13932f14
- Timestamp:
- May 31, 2017, 2:18:14 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 5d88a0a
- Parents:
- eb0951d0
- Location:
- src
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeTools/TrackLoc.cc
reb0951d0 r13932f14 16 16 #include "TrackLoc.h" 17 17 18 #include <cstdlib> 19 18 20 #include <iostream> 19 21 #include <sstream> 22 #include <stack> 20 23 #include <string> 21 #include < cstdlib>24 #include <typeindex> 22 25 23 26 #include "Common/utility.h" 27 #include "Common/PassVisitor.h" 24 28 #include "Common/VectorMap.h" 25 29 #include "GenPoly/GenPoly.h" … … 27 31 #include "SynTree/Declaration.h" 28 32 #include "SynTree/Initializer.h" 29 #include "SynTree/Visitor.h"30 33 31 34 namespace CodeTools { 32 35 33 34 35 36 std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { 37 return out << loc.filename << '[' << loc.linenumber << ']'; 38 } 36 39 37 class LocationPrinter : public Visitor { 38 unsigned int printLevel; 39 unsigned int currentLevel; 40 class LocationPrinter { 41 size_t printLevel; 40 42 41 CodeLocation *parent;42 43 CodeLocation *lastNode; 43 44 44 public: 45 LocationPrinter(unsigned int printLevel):46 Visitor(), printLevel(printLevel), currentLevel(0), 47 p arent(nullptr), lastNode(nullptr)48 45 std::stack< CodeLocation * > parents; 46 public: 47 LocationPrinter(size_t printLevel) : 48 printLevel(printLevel), lastNode(nullptr) 49 {} 49 50 50 void print(char const *name, BaseSyntaxNode *node) {51 for (unsigned int i = 0 ; i < currentLevel; ++i) {51 void print( const std::string& name, BaseSyntaxNode *node) { 52 for (size_t i = 0 ; i < parents.size() ; ++i) { 52 53 std::cout << " "; 53 54 } 54 55 if (2 <= printLevel) { 55 56 std::cout << name << '@'; 56 57 } 57 58 std::cout << node->location << std::endl; 58 59 } 59 60 60 void atNode( char const * name, BaseSyntaxNode *node) {61 if (-1 == node->location.linenumber) {62 if (nullptr != parent) {63 node->location.linenumber = parent->linenumber;64 node->location .filename = parent->filename;65 } else if (nullptr != lastNode) {66 node->location.linenumber = lastNode->linenumber;67 node->location .filename = lastNode->filename;68 } else {69 std::cerr << "Top level node has no CodeLocation " <<70 61 void atNode( BaseSyntaxNode *node ) { 62 std::string name = std::type_index(typeid(*node)).name(); 63 if ( node->location.isUnset() ) { 64 if ( !parents.empty() ) { 65 node->location = *parents.top(); 66 } 67 else if (nullptr != lastNode) { 68 node->location = *lastNode; 69 } 70 else { 71 std::cerr << "Top level node has no CodeLocation " << name << std::endl; 71 72 exit(EXIT_FAILURE); 72 73 } 73 74 } 75 74 76 if (0 < printLevel) { 75 print( name, node);77 print( name, node ); 76 78 } 77 79 lastNode = &node->location; 78 80 } 79 81 80 #define VISIT_FUNCTION(SyntaxNodeType) \ 81 virtual void visit(SyntaxNodeType *node) { \ 82 atNode(#SyntaxNodeType, node); \ 83 ++currentLevel; \ 84 CodeLocation * myParent = parent; \ 85 parent = &node->location; \ 86 Visitor::visit(node); \ 87 parent = myParent; \ 88 --currentLevel; \ 82 void previsit(BaseSyntaxNode * node) { 83 atNode(node); 84 parents.push( &node->location ); 89 85 } 90 86 91 VISIT_FUNCTION(ObjectDecl) 92 VISIT_FUNCTION(FunctionDecl) 93 VISIT_FUNCTION(StructDecl) 94 VISIT_FUNCTION(UnionDecl) 95 VISIT_FUNCTION(EnumDecl) 96 VISIT_FUNCTION(TraitDecl) 97 VISIT_FUNCTION(TypeDecl) 98 VISIT_FUNCTION(TypedefDecl) 99 VISIT_FUNCTION(AsmDecl) 100 101 VISIT_FUNCTION(CompoundStmt) 102 VISIT_FUNCTION(ExprStmt) 103 VISIT_FUNCTION(AsmStmt) 104 VISIT_FUNCTION(IfStmt) 105 VISIT_FUNCTION(WhileStmt) 106 VISIT_FUNCTION(ForStmt) 107 VISIT_FUNCTION(SwitchStmt) 108 VISIT_FUNCTION(CaseStmt) 109 VISIT_FUNCTION(BranchStmt) 110 VISIT_FUNCTION(ReturnStmt) 111 VISIT_FUNCTION(TryStmt) 112 VISIT_FUNCTION(CatchStmt) 113 VISIT_FUNCTION(FinallyStmt) 114 VISIT_FUNCTION(NullStmt) 115 VISIT_FUNCTION(DeclStmt) 116 VISIT_FUNCTION(ImplicitCtorDtorStmt) 117 118 VISIT_FUNCTION(ApplicationExpr) 119 VISIT_FUNCTION(UntypedExpr) 120 VISIT_FUNCTION(NameExpr) 121 VISIT_FUNCTION(CastExpr) 122 VISIT_FUNCTION(AddressExpr) 123 VISIT_FUNCTION(LabelAddressExpr) 124 VISIT_FUNCTION(UntypedMemberExpr) 125 VISIT_FUNCTION(MemberExpr) 126 VISIT_FUNCTION(VariableExpr) 127 VISIT_FUNCTION(ConstantExpr) 128 VISIT_FUNCTION(SizeofExpr) 129 VISIT_FUNCTION(AlignofExpr) 130 VISIT_FUNCTION(UntypedOffsetofExpr) 131 VISIT_FUNCTION(OffsetofExpr) 132 VISIT_FUNCTION(OffsetPackExpr) 133 VISIT_FUNCTION(AttrExpr) 134 VISIT_FUNCTION(LogicalExpr) 135 VISIT_FUNCTION(ConditionalExpr) 136 VISIT_FUNCTION(CommaExpr) 137 VISIT_FUNCTION(TypeExpr) 138 VISIT_FUNCTION(AsmExpr) 139 VISIT_FUNCTION(ImplicitCopyCtorExpr) 140 VISIT_FUNCTION(ConstructorExpr) 141 VISIT_FUNCTION(CompoundLiteralExpr) 142 VISIT_FUNCTION(UntypedValofExpr) 143 VISIT_FUNCTION(RangeExpr) 144 VISIT_FUNCTION(UntypedTupleExpr) 145 VISIT_FUNCTION(TupleExpr) 146 VISIT_FUNCTION(TupleIndexExpr) 147 VISIT_FUNCTION(MemberTupleExpr) 148 VISIT_FUNCTION(TupleAssignExpr) 149 VISIT_FUNCTION(StmtExpr) 150 VISIT_FUNCTION(UniqueExpr) 151 152 VISIT_FUNCTION(VoidType) 153 VISIT_FUNCTION(BasicType) 154 VISIT_FUNCTION(PointerType) 155 VISIT_FUNCTION(ArrayType) 156 VISIT_FUNCTION(FunctionType) 157 VISIT_FUNCTION(StructInstType) 158 VISIT_FUNCTION(UnionInstType) 159 VISIT_FUNCTION(EnumInstType) 160 VISIT_FUNCTION(TraitInstType) 161 VISIT_FUNCTION(TypeInstType) 162 VISIT_FUNCTION(TupleType) 163 VISIT_FUNCTION(TypeofType) 164 VISIT_FUNCTION(AttrType) 165 VISIT_FUNCTION(VarArgsType) 166 VISIT_FUNCTION(ZeroType) 167 VISIT_FUNCTION(OneType) 168 169 VISIT_FUNCTION(SingleInit) 170 VISIT_FUNCTION(ListInit) 171 VISIT_FUNCTION(ConstructorInit) 172 173 //VISIT_FUNCTION(Subrange) 174 175 //VISIT_FUNCTION(Constant) 87 void postvisit(BaseSyntaxNode * node) { 88 parents.pop(); 89 } 176 90 177 91 }; // LocationPrinter 178 92 179 void fillLocations( std::list< Declaration * > & translationUnit, 180 unsigned int printLevel) { 181 LocationPrinter printer(printLevel); 93 void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { 94 PassVisitor<LocationPrinter> printer(printLevel); 182 95 acceptAll( translationUnit, printer ); 183 96 } -
src/CodeTools/TrackLoc.h
reb0951d0 r13932f14 24 24 // printLevel: how much printing while filling in the node locations. 25 25 // 0 - No Printing, 1 - Print Location, 2 - Print Node Type and Location 26 void fillLocations( std::list< Declaration * > &translationUnit, 27 unsigned int printLevel = 0 ); 26 void fillLocations( std::list< Declaration * > &translationUnit, size_t printLevel = 0 ); 28 27 29 28 } // namespace CodeTools -
src/Common/utility.h
reb0951d0 r13932f14 334 334 {} 335 335 336 CodeLocation( const CodeLocation& rhs ) = default; 337 336 338 bool isSet () const { 337 339 return -1 != linenumber; -
src/main.cc
reb0951d0 r13932f14 317 317 } // if 318 318 319 CodeTools::fillLocations( translationUnit );319 CodeTools::fillLocations( translationUnit, 2 ); 320 320 CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, ! nolinemarks ); 321 321
Note: See TracChangeset
for help on using the changeset viewer.