source: src/CodeTools/TrackLoc.cc @ 80ac42d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 80ac42d was 80ac42d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Begin to clean up CodeLocations?

  • Property mode set to 100644
File size: 2.6 KB
Line 
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
32class Declaration;
33
34namespace 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                                        std::cerr << "Top level node has no CodeLocation " << name << std::endl;
67                                        exit(EXIT_FAILURE);
68                                }
69                        }
70
71                        if (0 < printLevel) {
72                                print( name, node );
73                        }
74                        lastNode = &node->location;
75                }
76
77                void previsit(BaseSyntaxNode * node) {
78                        atNode(node);
79                        parents.push( &node->location );
80                }
81
82                void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) {
83                        parents.pop();
84                }
85
86        }; // LocationPrinter
87
88        void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) {
89                PassVisitor<LocationPrinter> printer(printLevel);
90                acceptAll( translationUnit, printer );
91        }
92} // namespace CodeTools
93
94// Local Variables: //
95// tab-width: 4 //
96// mode: c++ //
97// compile-command: "make install" //
98// End: //
Note: See TracBrowser for help on using the repository browser.