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 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; |
---|
34 | |
---|
35 | namespace CodeTools { |
---|
36 | |
---|
37 | std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) { |
---|
38 | return out << loc.filename << '[' << loc.linenumber << ']'; |
---|
39 | } |
---|
40 | |
---|
41 | class LocationPrinter { |
---|
42 | size_t printLevel; |
---|
43 | |
---|
44 | CodeLocation *lastNode; |
---|
45 | |
---|
46 | std::stack< CodeLocation * > parents; |
---|
47 | public: |
---|
48 | LocationPrinter(size_t printLevel) : |
---|
49 | printLevel(printLevel), lastNode(nullptr) |
---|
50 | {} |
---|
51 | |
---|
52 | void print( const std::string& name, BaseSyntaxNode *node) { |
---|
53 | for (size_t i = 0 ; i < parents.size() ; ++i) { |
---|
54 | std::cout << " "; |
---|
55 | } |
---|
56 | if (2 <= printLevel) { |
---|
57 | std::cout << name << '@'; |
---|
58 | } |
---|
59 | std::cout << node->location << std::endl; |
---|
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; |
---|
73 | exit(EXIT_FAILURE); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if (0 < printLevel) { |
---|
78 | print( name, node ); |
---|
79 | } |
---|
80 | lastNode = &node->location; |
---|
81 | } |
---|
82 | |
---|
83 | void previsit(BaseSyntaxNode * node) { |
---|
84 | atNode(node); |
---|
85 | parents.push( &node->location ); |
---|
86 | } |
---|
87 | |
---|
88 | void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) { |
---|
89 | parents.pop(); |
---|
90 | } |
---|
91 | |
---|
92 | }; // LocationPrinter |
---|
93 | |
---|
94 | void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) { |
---|
95 | PassVisitor<LocationPrinter> printer(printLevel); |
---|
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: // |
---|