| 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 | // TrackLoc.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Andrew Beach | 
|---|
| 10 | // Created On       : Tues May 2 15:46:00 2017 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Wed May 3 14:43:00 2017 | 
|---|
| 13 | // Update Count     : 0 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "TrackLoc.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <iostream> | 
|---|
| 19 | #include <sstream> | 
|---|
| 20 | #include <string> | 
|---|
| 21 | #include <cstdlib> | 
|---|
| 22 |  | 
|---|
| 23 | #include "Common/utility.h" | 
|---|
| 24 | #include "Common/VectorMap.h" | 
|---|
| 25 | #include "GenPoly/GenPoly.h" | 
|---|
| 26 | #include "Parser/LinkageSpec.h" | 
|---|
| 27 | #include "SynTree/Declaration.h" | 
|---|
| 28 | #include "SynTree/Initializer.h" | 
|---|
| 29 | #include "SynTree/Visitor.h" | 
|---|
| 30 |  | 
|---|
| 31 | namespace CodeTools { | 
|---|
| 32 |  | 
|---|
| 33 | std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { | 
|---|
| 34 | return out << loc.filename << '[' << loc.linenumber << ']'; | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | class LocationPrinter : public Visitor { | 
|---|
| 38 | unsigned int printLevel; | 
|---|
| 39 | unsigned int currentLevel; | 
|---|
| 40 |  | 
|---|
| 41 | CodeLocation *parent; | 
|---|
| 42 | CodeLocation *lastNode; | 
|---|
| 43 |  | 
|---|
| 44 | public: | 
|---|
| 45 | LocationPrinter(unsigned int printLevel) : | 
|---|
| 46 | Visitor(), printLevel(printLevel), currentLevel(0), | 
|---|
| 47 | parent(nullptr), lastNode(nullptr) | 
|---|
| 48 | {} | 
|---|
| 49 |  | 
|---|
| 50 | void print(char const * name, BaseSyntaxNode *node) { | 
|---|
| 51 | for (unsigned int i = 0 ; i < currentLevel ; ++i) { | 
|---|
| 52 | std::cout << "    "; | 
|---|
| 53 | } | 
|---|
| 54 | if (2 <= printLevel) { | 
|---|
| 55 | std::cout << name << '@'; | 
|---|
| 56 | } | 
|---|
| 57 | std::cout << node->location << std::endl; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 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; | 
|---|
| 71 | exit(EXIT_FAILURE); | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 | if (0 < printLevel) { | 
|---|
| 75 | print(name, node); | 
|---|
| 76 | } | 
|---|
| 77 | lastNode = &node->location; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 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;                                                 \ | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 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) | 
|---|
| 176 |  | 
|---|
| 177 | }; // LocationPrinter | 
|---|
| 178 |  | 
|---|
| 179 | void fillLocations( std::list< Declaration * > & translationUnit, | 
|---|
| 180 | unsigned int printLevel) { | 
|---|
| 181 | LocationPrinter printer(printLevel); | 
|---|
| 182 | acceptAll( translationUnit, printer ); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | } // namespace CodeTools | 
|---|
| 186 |  | 
|---|
| 187 | // Local Variables: // | 
|---|
| 188 | // tab-width: 4 // | 
|---|
| 189 | // mode: c++ // | 
|---|
| 190 | // compile-command: "make install" // | 
|---|
| 191 | // End: // | 
|---|