| [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 | 
|---|
|  | 20 | #include <stack>                     // for stack | 
|---|
|  | 21 | #include <string>                    // for operator<<, string | 
|---|
|  | 22 | #include <typeindex>                 // for type_index | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include "Common/PassVisitor.h"      // for PassVisitor | 
|---|
|  | 25 | #include "Common/utility.h"          // for CodeLocation | 
|---|
|  | 26 | #include "SynTree/BaseSyntaxNode.h"  // for BaseSyntaxNode | 
|---|
| [bf2438c] | 27 |  | 
|---|
|  | 28 | class Declaration; | 
|---|
| [dba6db9] | 29 |  | 
|---|
|  | 30 | namespace CodeTools { | 
|---|
|  | 31 |  | 
|---|
| [13932f14] | 32 | std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { | 
|---|
|  | 33 | return out << loc.filename << '[' << loc.linenumber << ']'; | 
|---|
|  | 34 | } | 
|---|
| [dba6db9] | 35 |  | 
|---|
| [13932f14] | 36 | class LocationPrinter { | 
|---|
|  | 37 | size_t printLevel; | 
|---|
| [dba6db9] | 38 |  | 
|---|
|  | 39 | CodeLocation *lastNode; | 
|---|
|  | 40 |  | 
|---|
| [13932f14] | 41 | std::stack< CodeLocation * > parents; | 
|---|
|  | 42 | public: | 
|---|
| [3268a58] | 43 | LocationPrinter(size_t printLevel) : | 
|---|
| [13932f14] | 44 | printLevel(printLevel), lastNode(nullptr) | 
|---|
|  | 45 | {} | 
|---|
| [dba6db9] | 46 |  | 
|---|
| [13932f14] | 47 | void print( const std::string& name, BaseSyntaxNode *node) { | 
|---|
|  | 48 | for (size_t i = 0 ; i < parents.size() ; ++i) { | 
|---|
| [dba6db9] | 49 | std::cout << "    "; | 
|---|
|  | 50 | } | 
|---|
| [13932f14] | 51 | if (2 <= printLevel) { | 
|---|
| [dba6db9] | 52 | std::cout << name << '@'; | 
|---|
|  | 53 | } | 
|---|
|  | 54 | std::cout << node->location << std::endl; | 
|---|
| [13932f14] | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | void atNode( BaseSyntaxNode *node ) { | 
|---|
|  | 58 | std::string name = std::type_index(typeid(*node)).name(); | 
|---|
|  | 59 | if ( node->location.isUnset() ) { | 
|---|
|  | 60 | if ( !parents.empty() ) { | 
|---|
|  | 61 | node->location = *parents.top(); | 
|---|
| [3268a58] | 62 | } | 
|---|
| [13932f14] | 63 | else if (nullptr != lastNode) { | 
|---|
|  | 64 | node->location = *lastNode; | 
|---|
| [3268a58] | 65 | } | 
|---|
| [13932f14] | 66 | else { | 
|---|
|  | 67 | std::cerr << "Top level node has no CodeLocation " << name << std::endl; | 
|---|
| [dba6db9] | 68 | exit(EXIT_FAILURE); | 
|---|
|  | 69 | } | 
|---|
|  | 70 | } | 
|---|
| [13932f14] | 71 |  | 
|---|
| [dba6db9] | 72 | if (0 < printLevel) { | 
|---|
| [13932f14] | 73 | print( name, node ); | 
|---|
| [dba6db9] | 74 | } | 
|---|
|  | 75 | lastNode = &node->location; | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [13932f14] | 78 | void previsit(BaseSyntaxNode * node) { | 
|---|
|  | 79 | atNode(node); | 
|---|
|  | 80 | parents.push( &node->location ); | 
|---|
| [dba6db9] | 81 | } | 
|---|
|  | 82 |  | 
|---|
| [b3c36f4] | 83 | void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) { | 
|---|
| [13932f14] | 84 | parents.pop(); | 
|---|
|  | 85 | } | 
|---|
| [dba6db9] | 86 |  | 
|---|
|  | 87 | }; // LocationPrinter | 
|---|
|  | 88 |  | 
|---|
| [13932f14] | 89 | void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { | 
|---|
|  | 90 | PassVisitor<LocationPrinter> printer(printLevel); | 
|---|
| [dba6db9] | 91 | acceptAll( translationUnit, printer ); | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | } // namespace CodeTools | 
|---|
|  | 95 |  | 
|---|
|  | 96 | // Local Variables: // | 
|---|
|  | 97 | // tab-width: 4 // | 
|---|
|  | 98 | // mode: c++ // | 
|---|
|  | 99 | // compile-command: "make install" // | 
|---|
|  | 100 | // End: // | 
|---|