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