Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeTools/TrackLoc.cc

    rb3c36f4 rdba6db9  
    1616#include "TrackLoc.h"
    1717
     18#include <iostream>
     19#include <sstream>
     20#include <string>
    1821#include <cstdlib>
    1922
    20 #include <iostream>
    21 #include <sstream>
    22 #include <stack>
    23 #include <string>
    24 #include <typeindex>
    25 
    2623#include "Common/utility.h"
    27 #include "Common/PassVisitor.h"
    2824#include "Common/VectorMap.h"
    2925#include "GenPoly/GenPoly.h"
     
    3127#include "SynTree/Declaration.h"
    3228#include "SynTree/Initializer.h"
     29#include "SynTree/Visitor.h"
    3330
    3431namespace CodeTools {
    3532
    36         std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {
    37                 return out << loc.filename << '[' << loc.linenumber << ']';
    38         }
     33    std::ostream & operator<<(std::ostream & out, CodeLocation const & loc) {
     34        return out << loc.filename << '[' << loc.linenumber << ']';
     35    }
    3936
    40         class LocationPrinter {
    41                 size_t printLevel;
     37        class LocationPrinter : public Visitor {
     38                unsigned int printLevel;
     39                unsigned int currentLevel;
    4240
     41                CodeLocation *parent;
    4342                CodeLocation *lastNode;
    4443
    45                 std::stack< CodeLocation * > parents;
    46         public:
    47                 LocationPrinter(size_t printLevel) :
    48                         printLevel(printLevel), lastNode(nullptr)
    49                 {}
     44    public:
     45        LocationPrinter(unsigned int printLevel) :
     46            Visitor(), printLevel(printLevel), currentLevel(0),
     47                        parent(nullptr), lastNode(nullptr)
     48        {}
    5049
    51                 void print( const std::string& name, BaseSyntaxNode *node) {
    52                         for (size_t i = 0 ; i < parents.size() ; ++i) {
     50        void print(char const * name, BaseSyntaxNode *node) {
     51            for (unsigned int i = 0 ; i < currentLevel ; ++i) {
    5352                                std::cout << "    ";
    5453                        }
    55                         if (2 <= printLevel) {
     54            if (2 <= printLevel) {
    5655                                std::cout << name << '@';
    5756                        }
    5857                        std::cout << node->location << std::endl;
    59                 }
     58        }
    6059
    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;
     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;
    7271                                        exit(EXIT_FAILURE);
    7372                                }
    7473                        }
    75 
    7674                        if (0 < printLevel) {
    77                                 print( name, node );
     75                                print(name, node);
    7876                        }
    7977                        lastNode = &node->location;
    8078                }
    8179
    82                 void previsit(BaseSyntaxNode * node) {
    83                         atNode(node);
    84                         parents.push( &node->location );
     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;                                                 \
    8589                }
    8690
    87                 void postvisit( __attribute__((unused)) BaseSyntaxNode * node ) {
    88                         parents.pop();
    89                 }
     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)
    90176
    91177        }; // LocationPrinter
    92178
    93         void fillLocations( std::list< Declaration * > & translationUnit, size_t printLevel) {
    94                 PassVisitor<LocationPrinter> printer(printLevel);
     179        void fillLocations( std::list< Declaration * > & translationUnit,
     180                        unsigned int printLevel) {
     181                LocationPrinter printer(printLevel);
    95182                acceptAll( translationUnit, printer );
    96183        }
Note: See TracChangeset for help on using the changeset viewer.