| 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/SemanticError.h" // for SemanticError
|
|---|
| 27 | #include "Common/utility.h" // for CodeLocation
|
|---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
|
|---|
| 29 | #include "SynTree/Mutator.h" // for mutateAll
|
|---|
| 30 | #include "SynTree/Visitor.h" // for acceptAll
|
|---|
| 31 |
|
|---|
| 32 | class Declaration;
|
|---|
| 33 |
|
|---|
| 34 | namespace CodeTools {
|
|---|
| 35 |
|
|---|
| 36 | std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {
|
|---|
| 37 | return out << loc.filename << '[' << loc.linenumber << ']';
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | class LocationPrinter {
|
|---|
| 41 | size_t printLevel;
|
|---|
| 42 |
|
|---|
| 43 | CodeLocation *lastNode;
|
|---|
| 44 |
|
|---|
| 45 | std::stack< CodeLocation * > parents;
|
|---|
| 46 | public:
|
|---|
| 47 | LocationPrinter(size_t printLevel) :
|
|---|
| 48 | printLevel(printLevel), lastNode(nullptr)
|
|---|
| 49 | {}
|
|---|
| 50 |
|
|---|
| 51 | void print( const std::string& name, BaseSyntaxNode *node) {
|
|---|
| 52 | for (size_t i = 0 ; i < parents.size() ; ++i) {
|
|---|
| 53 | std::cout << " ";
|
|---|
| 54 | }
|
|---|
| 55 | if (2 <= printLevel) {
|
|---|
| 56 | std::cout << name << '@';
|
|---|
| 57 | }
|
|---|
| 58 | std::cout << node->location << std::endl;
|
|---|
| 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();
|
|---|
| 66 | }
|
|---|
| 67 | else if (nullptr != lastNode) {
|
|---|
| 68 | node->location = *lastNode;
|
|---|
| 69 | }
|
|---|
| 70 | else {
|
|---|
| 71 | std::cerr << "Top level node has no CodeLocation " << name << std::endl;
|
|---|
| 72 | exit(EXIT_FAILURE);
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | if (0 < printLevel) {
|
|---|
| 77 | print( name, node );
|
|---|
| 78 | }
|
|---|
| 79 | lastNode = &node->location;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void previsit(BaseSyntaxNode * node) {
|
|---|
| 83 | atNode(node);
|
|---|
| 84 | parents.push( &node->location );
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) {
|
|---|
| 88 | parents.pop();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | }; // LocationPrinter
|
|---|
| 92 |
|
|---|
| 93 | void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) {
|
|---|
| 94 | PassVisitor<LocationPrinter> printer(printLevel);
|
|---|
| 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: //
|
|---|