Ignore:
Timestamp:
Jun 1, 2017, 4:57:02 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
b1d4d60
Parents:
b78275b (diff), 676cc8c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/TrackLoc.cc

    rb78275b r28c9ff3  
    1616#include "TrackLoc.h"
    1717
     18#include <cstdlib>
     19
    1820#include <iostream>
    1921#include <sstream>
     22#include <stack>
    2023#include <string>
    21 #include <cstdlib>
     24#include <typeindex>
    2225
    2326#include "Common/utility.h"
     27#include "Common/PassVisitor.h"
    2428#include "Common/VectorMap.h"
    2529#include "GenPoly/GenPoly.h"
     
    2731#include "SynTree/Declaration.h"
    2832#include "SynTree/Initializer.h"
    29 #include "SynTree/Visitor.h"
    3033
    3134namespace CodeTools {
    3235
    33     std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {
    34         return out << loc.filename << '[' << loc.linenumber << ']';
    35     }
     36        std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {
     37                return out << loc.filename << '[' << loc.linenumber << ']';
     38        }
    3639
    37         class LocationPrinter : public Visitor {
    38                 unsigned int printLevel;
    39                 unsigned int currentLevel;
     40        class LocationPrinter {
     41                size_t printLevel;
    4042
    41                 CodeLocation *parent;
    4243                CodeLocation *lastNode;
    4344
    44     public:
    45         LocationPrinter(unsigned int printLevel) :
    46             Visitor(), printLevel(printLevel), currentLevel(0),
    47                         parent(nullptr), lastNode(nullptr)
    48         {}
     45                std::stack< CodeLocation * > parents;
     46        public:
     47                LocationPrinter(size_t printLevel) :
     48                        printLevel(printLevel), lastNode(nullptr)
     49                {}
    4950
    50         void print(char const * name, BaseSyntaxNode *node) {
    51             for (unsigned int i = 0 ; i < currentLevel ; ++i) {
     51                void print( const std::string& name, BaseSyntaxNode *node) {
     52                        for (size_t i = 0 ; i < parents.size() ; ++i) {
    5253                                std::cout << "    ";
    5354                        }
    54             if (2 <= printLevel) {
     55                        if (2 <= printLevel) {
    5556                                std::cout << name << '@';
    5657                        }
    5758                        std::cout << node->location << std::endl;
    58         }
     59                }
    5960
    60                 void atNode(char const * name, BaseSyntaxNode *node) {
    61                         if (-1 == node->location.linenumber) {
    62                                 if (nullptr != parent) {
    63                                         node->location.linenumber = parent->linenumber;
    64                                         node->location.filename = parent->filename;
    65                                 } else if (nullptr != lastNode) {
    66                                         node->location.linenumber = lastNode->linenumber;
    67                                         node->location.filename = lastNode->filename;
    68                                 } else {
    69                                         std::cerr << "Top level node has no CodeLocation " <<
    70                                                                 name << std::endl;
     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;
    7172                                        exit(EXIT_FAILURE);
    7273                                }
    7374                        }
     75
    7476                        if (0 < printLevel) {
    75                                 print(name, node);
     77                                print( name, node );
    7678                        }
    7779                        lastNode = &node->location;
    7880                }
    7981
    80 #define VISIT_FUNCTION(SyntaxNodeType)                          \
    81                 virtual void visit(SyntaxNodeType *node) {      \
    82                         atNode(#SyntaxNodeType, node);                  \
    83                         ++currentLevel;                                                 \
    84                         CodeLocation * myParent = parent;               \
    85                         parent = &node->location;                               \
    86                         Visitor::visit(node);                                   \
    87                         parent = myParent;                                              \
    88                         --currentLevel;                                                 \
     82                void previsit(BaseSyntaxNode * node) {
     83                        atNode(node);
     84                        parents.push( &node->location );
    8985                }
    9086
    91                 VISIT_FUNCTION(ObjectDecl)
    92                 VISIT_FUNCTION(FunctionDecl)
    93                 VISIT_FUNCTION(StructDecl)
    94                 VISIT_FUNCTION(UnionDecl)
    95                 VISIT_FUNCTION(EnumDecl)
    96                 VISIT_FUNCTION(TraitDecl)
    97                 VISIT_FUNCTION(TypeDecl)
    98                 VISIT_FUNCTION(TypedefDecl)
    99                 VISIT_FUNCTION(AsmDecl)
    100 
    101                 VISIT_FUNCTION(CompoundStmt)
    102                 VISIT_FUNCTION(ExprStmt)
    103                 VISIT_FUNCTION(AsmStmt)
    104                 VISIT_FUNCTION(IfStmt)
    105                 VISIT_FUNCTION(WhileStmt)
    106                 VISIT_FUNCTION(ForStmt)
    107                 VISIT_FUNCTION(SwitchStmt)
    108                 VISIT_FUNCTION(CaseStmt)
    109                 VISIT_FUNCTION(BranchStmt)
    110                 VISIT_FUNCTION(ReturnStmt)
    111                 VISIT_FUNCTION(TryStmt)
    112                 VISIT_FUNCTION(CatchStmt)
    113                 VISIT_FUNCTION(FinallyStmt)
    114                 VISIT_FUNCTION(NullStmt)
    115                 VISIT_FUNCTION(DeclStmt)
    116                 VISIT_FUNCTION(ImplicitCtorDtorStmt)
    117 
    118                 VISIT_FUNCTION(ApplicationExpr)
    119                 VISIT_FUNCTION(UntypedExpr)
    120                 VISIT_FUNCTION(NameExpr)
    121                 VISIT_FUNCTION(CastExpr)
    122                 VISIT_FUNCTION(AddressExpr)
    123                 VISIT_FUNCTION(LabelAddressExpr)
    124                 VISIT_FUNCTION(UntypedMemberExpr)
    125                 VISIT_FUNCTION(MemberExpr)
    126                 VISIT_FUNCTION(VariableExpr)
    127                 VISIT_FUNCTION(ConstantExpr)
    128                 VISIT_FUNCTION(SizeofExpr)
    129                 VISIT_FUNCTION(AlignofExpr)
    130                 VISIT_FUNCTION(UntypedOffsetofExpr)
    131                 VISIT_FUNCTION(OffsetofExpr)
    132                 VISIT_FUNCTION(OffsetPackExpr)
    133                 VISIT_FUNCTION(AttrExpr)
    134                 VISIT_FUNCTION(LogicalExpr)
    135                 VISIT_FUNCTION(ConditionalExpr)
    136                 VISIT_FUNCTION(CommaExpr)
    137                 VISIT_FUNCTION(TypeExpr)
    138                 VISIT_FUNCTION(AsmExpr)
    139                 VISIT_FUNCTION(ImplicitCopyCtorExpr)
    140                 VISIT_FUNCTION(ConstructorExpr)
    141                 VISIT_FUNCTION(CompoundLiteralExpr)
    142                 VISIT_FUNCTION(UntypedValofExpr)
    143                 VISIT_FUNCTION(RangeExpr)
    144                 VISIT_FUNCTION(UntypedTupleExpr)
    145                 VISIT_FUNCTION(TupleExpr)
    146                 VISIT_FUNCTION(TupleIndexExpr)
    147                 VISIT_FUNCTION(MemberTupleExpr)
    148                 VISIT_FUNCTION(TupleAssignExpr)
    149                 VISIT_FUNCTION(StmtExpr)
    150                 VISIT_FUNCTION(UniqueExpr)
    151 
    152                 VISIT_FUNCTION(VoidType)
    153                 VISIT_FUNCTION(BasicType)
    154                 VISIT_FUNCTION(PointerType)
    155                 VISIT_FUNCTION(ArrayType)
    156                 VISIT_FUNCTION(FunctionType)
    157                 VISIT_FUNCTION(StructInstType)
    158                 VISIT_FUNCTION(UnionInstType)
    159                 VISIT_FUNCTION(EnumInstType)
    160                 VISIT_FUNCTION(TraitInstType)
    161                 VISIT_FUNCTION(TypeInstType)
    162                 VISIT_FUNCTION(TupleType)
    163                 VISIT_FUNCTION(TypeofType)
    164                 VISIT_FUNCTION(AttrType)
    165                 VISIT_FUNCTION(VarArgsType)
    166                 VISIT_FUNCTION(ZeroType)
    167                 VISIT_FUNCTION(OneType)
    168 
    169                 VISIT_FUNCTION(SingleInit)
    170                 VISIT_FUNCTION(ListInit)
    171                 VISIT_FUNCTION(ConstructorInit)
    172 
    173                 //VISIT_FUNCTION(Subrange)
    174 
    175                 //VISIT_FUNCTION(Constant)
     87                void postvisit(BaseSyntaxNode * node) {
     88                        parents.pop();
     89                }
    17690
    17791        }; // LocationPrinter
    17892
    179         void fillLocations( std::list< Declaration * > & translationUnit,
    180                         unsigned int printLevel) {
    181                 LocationPrinter printer(printLevel);
     93        void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) {
     94                PassVisitor<LocationPrinter> printer(printLevel);
    18295                acceptAll( translationUnit, printer );
    18396        }
Note: See TracChangeset for help on using the changeset viewer.