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 | class LocationPrinter { |
---|
36 | size_t printLevel; |
---|
37 | |
---|
38 | CodeLocation *lastNode; |
---|
39 | |
---|
40 | std::stack< CodeLocation * > parents; |
---|
41 | public: |
---|
42 | LocationPrinter(size_t printLevel) : |
---|
43 | printLevel(printLevel), lastNode(nullptr) |
---|
44 | {} |
---|
45 | |
---|
46 | void print( const std::string& name, BaseSyntaxNode *node) { |
---|
47 | for (size_t i = 0 ; i < parents.size() ; ++i) { |
---|
48 | std::cout << " "; |
---|
49 | } |
---|
50 | if (2 <= printLevel) { |
---|
51 | std::cout << name << '@'; |
---|
52 | } |
---|
53 | std::cout << node->location << std::endl; |
---|
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(); |
---|
61 | } |
---|
62 | else if (nullptr != lastNode) { |
---|
63 | node->location = *lastNode; |
---|
64 | } |
---|
65 | else { |
---|
66 | assertf( false, "Top level node has no CodeLocation %s", toString( node ).c_str() ); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | if (0 < printLevel) { |
---|
71 | print( name, node ); |
---|
72 | } |
---|
73 | lastNode = &node->location; |
---|
74 | } |
---|
75 | |
---|
76 | void previsit(BaseSyntaxNode * node) { |
---|
77 | atNode(node); |
---|
78 | parents.push( &node->location ); |
---|
79 | } |
---|
80 | |
---|
81 | void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) { |
---|
82 | parents.pop(); |
---|
83 | } |
---|
84 | |
---|
85 | }; // LocationPrinter |
---|
86 | |
---|
87 | void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { |
---|
88 | PassVisitor<LocationPrinter> printer(printLevel); |
---|
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: // |
---|