| [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
 | 
|---|
| [4f0c520] | 12 | // Last Modified On : Fri Nov 27 18:00:00 2020
 | 
|---|
 | 13 | // Update Count     : 1
 | 
|---|
| [dba6db9] | 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
 | 
|---|
| [4f0c520] | 24 | #include <vector>                    // for vector
 | 
|---|
| [3268a58] | 25 | 
 | 
|---|
 | 26 | #include "Common/PassVisitor.h"      // for PassVisitor
 | 
|---|
 | 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 {
 | 
|---|
| [13932f14] | 35 |         class LocationPrinter {
 | 
|---|
 | 36 |                 size_t printLevel;
 | 
|---|
| [dba6db9] | 37 | 
 | 
|---|
 | 38 |                 CodeLocation *lastNode;
 | 
|---|
 | 39 | 
 | 
|---|
| [4f0c520] | 40 |                 std::stack< CodeLocation *, std::vector< CodeLocation * > > parents;
 | 
|---|
| [13932f14] | 41 |         public:
 | 
|---|
| [3268a58] | 42 |                 LocationPrinter(size_t printLevel) :
 | 
|---|
| [13932f14] | 43 |                         printLevel(printLevel), lastNode(nullptr)
 | 
|---|
 | 44 |                 {}
 | 
|---|
| [dba6db9] | 45 | 
 | 
|---|
| [13932f14] | 46 |                 void print( const std::string& name, BaseSyntaxNode *node) {
 | 
|---|
 | 47 |                         for (size_t i = 0 ; i < parents.size() ; ++i) {
 | 
|---|
| [dba6db9] | 48 |                                 std::cout << "    ";
 | 
|---|
 | 49 |                         }
 | 
|---|
| [13932f14] | 50 |                         if (2 <= printLevel) {
 | 
|---|
| [dba6db9] | 51 |                                 std::cout << name << '@';
 | 
|---|
 | 52 |                         }
 | 
|---|
 | 53 |                         std::cout << node->location << std::endl;
 | 
|---|
| [13932f14] | 54 |                 }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |                 void atNode( BaseSyntaxNode *node ) {
 | 
|---|
 | 57 |                         std::string name = std::type_index(typeid(*node)).name();
 | 
|---|
 | 58 |                         if ( node->location.isUnset() ) {
 | 
|---|
 | 59 |                                 if ( !parents.empty() ) {
 | 
|---|
 | 60 |                                         node->location = *parents.top();
 | 
|---|
| [3268a58] | 61 |                                 }
 | 
|---|
| [13932f14] | 62 |                                 else if (nullptr != lastNode) {
 | 
|---|
 | 63 |                                         node->location = *lastNode;
 | 
|---|
| [3268a58] | 64 |                                 }
 | 
|---|
| [13932f14] | 65 |                                 else {
 | 
|---|
| [014450e] | 66 |                                         assertf( false, "Top level node has no CodeLocation %s", toString( node ).c_str() );
 | 
|---|
| [dba6db9] | 67 |                                 }
 | 
|---|
 | 68 |                         }
 | 
|---|
| [13932f14] | 69 | 
 | 
|---|
| [dba6db9] | 70 |                         if (0 < printLevel) {
 | 
|---|
| [13932f14] | 71 |                                 print( name, node );
 | 
|---|
| [dba6db9] | 72 |                         }
 | 
|---|
 | 73 |                         lastNode = &node->location;
 | 
|---|
 | 74 |                 }
 | 
|---|
 | 75 | 
 | 
|---|
| [13932f14] | 76 |                 void previsit(BaseSyntaxNode * node) {
 | 
|---|
 | 77 |                         atNode(node);
 | 
|---|
 | 78 |                         parents.push( &node->location );
 | 
|---|
| [dba6db9] | 79 |                 }
 | 
|---|
 | 80 | 
 | 
|---|
| [b3c36f4] | 81 |                 void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) {
 | 
|---|
| [13932f14] | 82 |                         parents.pop();
 | 
|---|
 | 83 |                 }
 | 
|---|
| [dba6db9] | 84 | 
 | 
|---|
 | 85 |         }; // LocationPrinter
 | 
|---|
 | 86 | 
 | 
|---|
| [13932f14] | 87 |         void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) {
 | 
|---|
 | 88 |                 PassVisitor<LocationPrinter> printer(printLevel);
 | 
|---|
| [dba6db9] | 89 |                 acceptAll( translationUnit, printer );
 | 
|---|
 | 90 |         }
 | 
|---|
 | 91 | } // namespace CodeTools
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 | // Local Variables: //
 | 
|---|
 | 94 | // tab-width: 4 //
 | 
|---|
 | 95 | // mode: c++ //
 | 
|---|
 | 96 | // compile-command: "make install" //
 | 
|---|
 | 97 | // End: //
 | 
|---|