[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 |
|
---|
[3268a58] | 18 | #include <cstdlib> // for exit, EXIT_FAILURE
|
---|
| 19 | #include <iostream> // for operator<<, ostream, basic_ostream
|
---|
[08fc48f] | 20 | #include <iterator> // for back_inserter, inserter
|
---|
[3268a58] | 21 | #include <stack> // for stack
|
---|
| 22 | #include <string> // for operator<<, string
|
---|
| 23 | #include <typeindex> // for type_index
|
---|
| 24 |
|
---|
| 25 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
[08fc48f] | 26 | #include "Common/SemanticError.h" // for SemanticError
|
---|
[3268a58] | 27 | #include "Common/utility.h" // for CodeLocation
|
---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
|
---|
[08fc48f] | 29 | #include "SynTree/Mutator.h" // for mutateAll
|
---|
| 30 | #include "SynTree/Visitor.h" // for acceptAll
|
---|
[bf2438c] | 31 |
|
---|
| 32 | class Declaration;
|
---|
[dba6db9] | 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:
|
---|
[3268a58] | 47 | LocationPrinter(size_t printLevel) :
|
---|
[13932f14] | 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();
|
---|
[3268a58] | 66 | }
|
---|
[13932f14] | 67 | else if (nullptr != lastNode) {
|
---|
| 68 | node->location = *lastNode;
|
---|
[3268a58] | 69 | }
|
---|
[13932f14] | 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 |
|
---|
[b3c36f4] | 87 | void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) {
|
---|
[13932f14] | 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: //
|
---|