]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/commitdiff
The transitions are simply saved into the file. You need to add -I with boost source...
authorsilhape2 <silhape2@postel.local>
Thu, 24 Feb 2011 09:21:34 +0000 (10:21 +0100)
committersilhape2 <silhape2@postel.local>
Thu, 24 Feb 2011 09:21:34 +0000 (10:21 +0100)
commandlineopt.h
stringoper.h
visualizer.cpp

index 7a3bef03b6d68339071f4d96e0380fdef4d45b71..7990d9cd44e6b8ff7b13d02fdd78c92021f83cdf 100644 (file)
@@ -4,5 +4,7 @@ using namespace std;
 
 llvm::cl::opt<string> outputFile("o", llvm::cl::desc("Specify output filename"), llvm::cl::value_desc("filename"), llvm::cl::init("Graph")); //option -o is not required. implicit filename is Graph
 
-llvm::cl::list<string> includeFiles("I", llvm::cl::desc("Specify the location of include files"),  llvm::cl::value_desc("Source location"), llvm::cl::OneOrMore); //option -I must be there at least once
+llvm::cl::list<string> includeFiles("I", llvm::cl::desc("Specify the location of included files"),  llvm::cl::value_desc("Source location"), llvm::cl::OneOrMore); //option -I must be there at least once
+
+llvm::cl::opt<string> inputFilename(llvm::cl::Positional, llvm::cl::desc("input file"), llvm::cl::Required); // input file
 
index 99b19a0b1085432ea93e779a6264e8df530fdfab..b2b2d3f9fd69e053a8ed4f178fe710df8b32796e 100644 (file)
@@ -1,9 +1,10 @@
 #include <string>
 
-std::string get_line_of_code(const char* code) //return the line of code
+std::string get_line_of_code(const std::string code) //return the line of code
 {
        std::string ret = code; 
        int i;  
+       //std::cout<<code<<"\n\n";
        for(i = 0;i<ret.length();i++)
        {
                if(ret[i]=='\n'||ret[i]=='{') break;
@@ -23,6 +24,13 @@ std::string cut_commentary(const std::string line) //cut commentary at the end o
 
 }
 
+std::string cut_namespaces(std::string line) //cut namespaces from the name of the state
+{
+       int i = line.rfind("::");
+       if(i==-1) return line;
+       return line.substr(i+2);
+}
+
 bool is_derived(const std::string line) // return true if the struct or class is derived
 {
        int i = 0;
@@ -93,25 +101,28 @@ std::string clean_spaces(const std::string line)
 bool is_state(const std::string line)
 {
        int pos = line.find("::");
-       if(pos==10)
+       if(pos == 5)
        {
-               if(line.compare(0,24,"statechart::simple_state")==0)
+               if(line.compare(0,31,"boost::statechart::simple_state")==0)
                {
                        return true;    
                }
                else
                {
-                       std::string str = line.substr(pos+2);
-                       if(str.compare(0,12,"simple_state")==0)return true;
+                       return false;
                }
        }
        else
        {
-               std::string str = line.substr(pos+2);
-               //std::cout<<str;
-               if(str.compare(0,12,"simple_state")==0)return true;
+               if(line.compare(0,24,"statechart::simple_state")==0)
+               {
+                       return true;    
+               }
+               else
+               {
+                       return false;
+               }
        }
-       return false;
 }
 // Transitions
 std::string cut_typedef(std::string line) // cut typedef from the beginning
@@ -136,25 +147,25 @@ int count(std::string line) //count all < in string
 bool is_list(const std::string line)
 {
        int pos = line.find("::");
-       if(pos==10)
+       if(pos == 5)
        {
-               if(line.compare(0,9,"mpl::list")==0)
+               if(line.compare(0,16,"boost::mpl::list")==0)
                {
                        return true;    
                }
                else
                {
-                       std::string str = line.substr(pos+2);
-                       if(str.compare(0,4,"list")==0)return true;
+                       return false;
                }
        }
+       if(line.compare(0,9,"mpl::list")==0)
+       {
+               return true;    
+       }
        else
        {
-               std::string str = line.substr(pos+2);
-               //std::cout<<str;
-               if(str.compare(0,4,"list")==0)return true;
+               return false;
        }
-       return false;
 }
 
 std::string get_inner_part(const std::string line)
@@ -182,25 +193,28 @@ std::string get_inner_part(const std::string line)
 bool is_transition(const std::string line)
 {
        int pos = line.find("::");
-       if(pos==10)
+       if(pos == 5)
        {
-               if(line.compare(0,22,"statechart::transition")==0)
+               if(line.compare(0,29,"boost::statechart::transition")==0)
                {
                        return true;    
                }
                else
                {
-                       std::string str = line.substr(pos+2);
-                       if(str.compare(0,10,"transition")==0)return true;
+                       return false;
                }
        }
        else
-       {
-               std::string str = line.substr(pos+2);
-               //std::cout<<str;
-               if(str.compare(0,10,"transition")==0)return true;
+       {       
+               if(line.compare(0,22,"statechart::transition")==0)
+               {
+                       return true;    
+               }
+               else
+               {
+                       return false;
+               }
        }
-       return false;
 }
 
 std::string get_transition_params(std::string line)
index b781be350bb2ff356406bc6d8fc9ba759e1330b1..362ebe15f38b73b527c5e565befbe15fa3728118 100644 (file)
@@ -1,4 +1,8 @@
 #include <iostream>
+#include <string>
+#include <fstream>
+#include <list>
+
 
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Host.h"
@@ -56,20 +60,18 @@ using namespace clang;
 
 class FindStates : public ASTConsumer
 {
+       std::list<string> transitions;
+       std::list<string> events;
+       std::list<string> states;
        public:
-       SourceLocation loc;
-       FullSourceLoc *fSloc;
-       
+
        virtual void Initialize(ASTContext &ctx)//run after the AST is constructed
        {       
-               SourceManager &sman = ctx.getSourceManager();
-               fSloc = new FullSourceLoc(loc, sman);
        }
 
        virtual void HandleTopLevelDecl(DeclGroupRef DGR)// traverse all top level declarations
        {
                SourceLocation loc;
-               const SourceManager &sman = fSloc->getManager();
                std::string line;
                std::string super_class, output;
                llvm::raw_string_ostream x(output);
@@ -81,42 +83,41 @@ class FindStates : public ASTConsumer
                        {
                                const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
                                //std::cout<<decl->getDeclKindName()<<"\n";
-                               if(!sman.isInSystemHeader(loc))
+                               if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
                                {
-                                       if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
+                                       if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
                                        {
-                                               if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
+                                               const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
+                                               decl->print(x);
+                                               //decl->dump();                                                 
+                                               line = cut_commentary(clean_spaces(get_line_of_code(x.str())));
+                                               output = "";
+                                               if(is_derived(line))
                                                {
-                                                       const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
-                                                       line = cut_commentary(clean_spaces(get_line_of_code(sman.getCharacterData(loc))));
-                                                       if(is_derived(line))
-                                                       {
-                                                               if(find_states(cRecDecl, line))
-                                                               {                               
-                                                                       const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                      
-                                                                       std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
-                                                                       find_transitions(namedDecl->getNameAsString(), declCont);
-                                                               }
+                                                       if(find_states(cRecDecl, line))
+                                                       {                               
+                                                               const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                      
+                                                               std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
+                                                               find_transitions(namedDecl->getNameAsString(), declCont);
                                                        }
-                                               }       
-                                       }
-                                       if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
-                                       {
-                                       
-                                                       DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
-                                                       //declCont->dumpDeclContext();                          
-                                                       recursive_visit(declCont);
-                                       
+                                               }
                                        }
+                               }       
+                               if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
+                               {
+                                       DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
+                                       //declCont->dumpDeclContext();                          
+                                       recursive_visit(declCont);
+                               
                                }
                        }
                }
        }
        void recursive_visit(const DeclContext *declCont) //recursively visit all decls inside namespace
        {
-               const SourceManager &sman = fSloc->getManager();
-               std::string line;
+               std::string line, output;
                SourceLocation loc;
+               llvm::raw_string_ostream x(output);
                for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
                {
                        const Decl *decl = *i;
@@ -131,13 +132,16 @@ class FindStates : public ASTConsumer
                                        if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
                                        {
                                                const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
-                                               line = cut_commentary(clean_spaces(get_line_of_code(sman.getCharacterData(loc))));
+                                               decl->print(x);
+                                               line = cut_commentary(clean_spaces(get_line_of_code(x.str())));
+                                               output = "";
                                                if(is_derived(line))
                                                {
                                                        if(find_states(cRecDecl, line))
                                                        {                               
                                                                const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                      
-                                                               std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
+                                                               states.push_back(namedDecl->getNameAsString());
+                                                               //std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
                                                                find_transitions(namedDecl->getNameAsString(), declCont);
                                                        }
                                                }
@@ -176,7 +180,7 @@ class FindStates : public ASTConsumer
                }
        }
 
-       void find_transitions (const std::string name_of_state,const DeclContext *declCont) // traverse all methods for finding declarations of transitions
+       void find_transitions (std::string name_of_state,const DeclContext *declCont) // traverse all methods for finding declarations of transitions
        {       
                std::string output, line, event, dest, params, base;    
                llvm::raw_string_ostream x(output);
@@ -207,11 +211,10 @@ class FindStates : public ASTConsumer
                                                if(is_transition(base))
                                                {
                                                        params = get_transition_params(line);
-                                                       pos = params.find(",");
-                                                       event = params.substr(0,pos);
-                                                       dest = params.substr(pos+1);
-                                                       std::cout << "New transition: " << name_of_state<<" -> "<<event<<" -> "<< dest<<"\n";
-                                                       line = get_next_base(line);
+                                                       name_of_state.append(",");                                                      
+                                                       name_of_state.append(params);
+                                                       transitions.push_back(name_of_state);   
+                                                       break;
                                                }
                                                else
                                                {
@@ -220,12 +223,31 @@ class FindStates : public ASTConsumer
                                        }
                        }
                }       
+       }
+       void save_to_file(std::string output)
+       {
+               std::string state;
+               int pos;
+               std::ofstream filestr(output.c_str());
+               std::cout<<output<<"\n";
+               filestr<<"digraph G {\n";
+               for(list<string>::iterator i = transitions.begin();i!=transitions.end();i++)
+               {
+                       state = *i;
+                       pos = state.find(",");
+                       filestr<<cut_namespaces(state.substr(0,pos))<<"->";
+                       pos = state.rfind(",");
+                       filestr<<cut_namespaces(state.substr(pos+1))<<";\n";
+               }
+               filestr<<"}";
+               filestr.close();
        }       
 };
 
 int main(int argc, char *argv[])
 {
        llvm::cl::ParseCommandLineOptions(argc, argv);  
+       std::cout<<"Input file: "<<inputFilename<<"\n"; 
        DiagnosticOptions diagnosticOptions;
        TextDiagnosticPrinter *tdp = new TextDiagnosticPrinter(llvm::nulls(), diagnosticOptions);
        llvm::IntrusiveRefCntPtr<DiagnosticIDs> dis(new DiagnosticIDs());
@@ -262,7 +284,7 @@ int main(int argc, char *argv[])
        FrontendOptions f;
        PreprocessorOptions ppio;
        InitializePreprocessor(pp, ppio,hsopts,f);
-       const FileEntry *file = fm.getFile("test.cpp");
+       const FileEntry *file = fm.getFile(inputFilename);
        sm.createMainFileID(file);
        IdentifierTable tab(lang);
        SelectorTable sel;
@@ -272,6 +294,8 @@ int main(int argc, char *argv[])
        tdp->BeginSourceFile(lang, &pp);
        ParseAST(pp, &c, ctx, false, false);
        tdp->EndSourceFile();
+       
+       c.save_to_file(outputFile);
        return 0;
 
 }