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