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