Changes in src/CodeTools/TrackLoc.cc [b3c36f4:dba6db9]
- File:
-
- 1 edited
-
src/CodeTools/TrackLoc.cc (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeTools/TrackLoc.cc
rb3c36f4 rdba6db9 16 16 #include "TrackLoc.h" 17 17 18 #include <iostream> 19 #include <sstream> 20 #include <string> 18 21 #include <cstdlib> 19 22 20 #include <iostream>21 #include <sstream>22 #include <stack>23 #include <string>24 #include <typeindex>25 26 23 #include "Common/utility.h" 27 #include "Common/PassVisitor.h"28 24 #include "Common/VectorMap.h" 29 25 #include "GenPoly/GenPoly.h" … … 31 27 #include "SynTree/Declaration.h" 32 28 #include "SynTree/Initializer.h" 29 #include "SynTree/Visitor.h" 33 30 34 31 namespace CodeTools { 35 32 36 std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {37 return out << loc.filename << '[' << loc.linenumber << ']';38 }33 std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { 34 return out << loc.filename << '[' << loc.linenumber << ']'; 35 } 39 36 40 class LocationPrinter { 41 size_t printLevel; 37 class LocationPrinter : public Visitor { 38 unsigned int printLevel; 39 unsigned int currentLevel; 42 40 41 CodeLocation *parent; 43 42 CodeLocation *lastNode; 44 43 45 std::stack< CodeLocation * > parents; 46 public:47 LocationPrinter(size_t printLevel) : 48 p rintLevel(printLevel), lastNode(nullptr)49 {}44 public: 45 LocationPrinter(unsigned int printLevel) : 46 Visitor(), printLevel(printLevel), currentLevel(0), 47 parent(nullptr), lastNode(nullptr) 48 {} 50 49 51 void print( const std::string&name, BaseSyntaxNode *node) {52 for (size_t i = 0 ; i < parents.size(); ++i) {50 void print(char const * name, BaseSyntaxNode *node) { 51 for (unsigned int i = 0 ; i < currentLevel ; ++i) { 53 52 std::cout << " "; 54 53 } 55 if (2 <= printLevel) {54 if (2 <= printLevel) { 56 55 std::cout << name << '@'; 57 56 } 58 57 std::cout << node->location << std::endl; 59 }58 } 60 59 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;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 name << std::endl; 72 71 exit(EXIT_FAILURE); 73 72 } 74 73 } 75 76 74 if (0 < printLevel) { 77 print( name, node);75 print(name, node); 78 76 } 79 77 lastNode = &node->location; 80 78 } 81 79 82 void previsit(BaseSyntaxNode * node) { 83 atNode(node); 84 parents.push( &node->location ); 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; \ 85 89 } 86 90 87 void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) { 88 parents.pop(); 89 } 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) 90 176 91 177 }; // LocationPrinter 92 178 93 void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { 94 PassVisitor<LocationPrinter> printer(printLevel); 179 void fillLocations( std::list< Declaration * > & translationUnit, 180 unsigned int printLevel) { 181 LocationPrinter printer(printLevel); 95 182 acceptAll( translationUnit, printer ); 96 183 }
Note:
See TracChangeset
for help on using the changeset viewer.