[dba6db9] | 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 | |
---|
[13932f14] | 18 | #include <cstdlib> |
---|
| 19 | |
---|
[dba6db9] | 20 | #include <iostream> |
---|
| 21 | #include <sstream> |
---|
[13932f14] | 22 | #include <stack> |
---|
[dba6db9] | 23 | #include <string> |
---|
[13932f14] | 24 | #include <typeindex> |
---|
[dba6db9] | 25 | |
---|
| 26 | #include "Common/utility.h" |
---|
[13932f14] | 27 | #include "Common/PassVisitor.h" |
---|
[dba6db9] | 28 | #include "Common/VectorMap.h" |
---|
| 29 | #include "GenPoly/GenPoly.h" |
---|
| 30 | #include "Parser/LinkageSpec.h" |
---|
| 31 | #include "SynTree/Declaration.h" |
---|
| 32 | #include "SynTree/Initializer.h" |
---|
| 33 | |
---|
| 34 | namespace CodeTools { |
---|
| 35 | |
---|
[13932f14] | 36 | std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { |
---|
| 37 | return out << loc.filename << '[' << loc.linenumber << ']'; |
---|
| 38 | } |
---|
[dba6db9] | 39 | |
---|
[13932f14] | 40 | class LocationPrinter { |
---|
| 41 | size_t printLevel; |
---|
[dba6db9] | 42 | |
---|
| 43 | CodeLocation *lastNode; |
---|
| 44 | |
---|
[13932f14] | 45 | std::stack< CodeLocation * > parents; |
---|
| 46 | public: |
---|
| 47 | LocationPrinter(size_t printLevel) : |
---|
| 48 | printLevel(printLevel), lastNode(nullptr) |
---|
| 49 | {} |
---|
[dba6db9] | 50 | |
---|
[13932f14] | 51 | void print( const std::string& name, BaseSyntaxNode *node) { |
---|
| 52 | for (size_t i = 0 ; i < parents.size() ; ++i) { |
---|
[dba6db9] | 53 | std::cout << " "; |
---|
| 54 | } |
---|
[13932f14] | 55 | if (2 <= printLevel) { |
---|
[dba6db9] | 56 | std::cout << name << '@'; |
---|
| 57 | } |
---|
| 58 | std::cout << node->location << std::endl; |
---|
[13932f14] | 59 | } |
---|
| 60 | |
---|
| 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; |
---|
[dba6db9] | 72 | exit(EXIT_FAILURE); |
---|
| 73 | } |
---|
| 74 | } |
---|
[13932f14] | 75 | |
---|
[dba6db9] | 76 | if (0 < printLevel) { |
---|
[13932f14] | 77 | print( name, node ); |
---|
[dba6db9] | 78 | } |
---|
| 79 | lastNode = &node->location; |
---|
| 80 | } |
---|
| 81 | |
---|
[13932f14] | 82 | void previsit(BaseSyntaxNode * node) { |
---|
| 83 | atNode(node); |
---|
| 84 | parents.push( &node->location ); |
---|
[dba6db9] | 85 | } |
---|
| 86 | |
---|
[13932f14] | 87 | void postvisit(BaseSyntaxNode * node) { |
---|
| 88 | parents.pop(); |
---|
| 89 | } |
---|
[dba6db9] | 90 | |
---|
| 91 | }; // LocationPrinter |
---|
| 92 | |
---|
[13932f14] | 93 | void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { |
---|
| 94 | PassVisitor<LocationPrinter> printer(printLevel); |
---|
[dba6db9] | 95 | acceptAll( translationUnit, printer ); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | } // namespace CodeTools |
---|
| 99 | |
---|
| 100 | // Local Variables: // |
---|
| 101 | // tab-width: 4 // |
---|
| 102 | // mode: c++ // |
---|
| 103 | // compile-command: "make install" // |
---|
| 104 | // End: // |
---|