+++ /dev/null
-/** @file */
-////////////////////////////////////////////////////////////////////////////////////////
-//
-// This file is part of Boost Statechart Viewer.
-//
-// Boost Statechart Viewer is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Boost Statechart Viewer is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Boost Statechart Viewer. If not, see <http://www.gnu.org/licenses/>.
-//
-////////////////////////////////////////////////////////////////////////////////////////
-
-#include <fstream>
-#include <list>
-#include <string>
-
-#include "stringoper.h"
-
-using namespace std;
-
-/**
-* This class provides saving information about state machine to a specified output file. It saves states and transitions and also it creates the transition table.
-*/
-class IO_operations
-{
- list<string> transitions; /** list of transitions */
- list<string> states; /** list of states */
- list<string> events; /** list of events */
- string outputFilename;
- string name_of_machine;
- string name_of_first_state;
- string *table; /** transition table. It is being allocated when starting the creation of output file. */
- int nState;
- int cols, rows;
- /** This function finds place in the transition table to put a transition there. */
- int find_place(string model, int type)
- {
- if(type == 1)
- {
- for(int i = 3;i<cols;i++)
- {
- if(model.compare(0,model.size(),table[i])==0) return i;
- }
- }
- else
- {
- for(int i = 1;i<rows;i++)
- if(model.compare(0,model.size(),table[i*cols+2])==0) return i;
- }
- return -1;
- }
- public:
- IO_operations() {} /** Implicit constructor */
- /** Constructor that fill in all private variables in this class */
- IO_operations( const string outputFile, const string FSM_name, const string firstState, const list<string> trans, const list<string> state, const list<string> ev )
- {
- outputFilename = outputFile;
- name_of_machine = FSM_name;
- name_of_first_state = firstState;
- transitions = trans;
- states = state;
- events = ev;
- }
-
- ~IO_operations() /** destructor. It deallocates the transition table.*/
- {
- delete table;
- }
-
- void setEvents(list<string> events) /** Set list of events to an attribute */
- {
- this->events = events;
- }
-
- void setTransitions(list<string> transitions) /** Set list of transitions to an attribute */
- {
- this->transitions = transitions;
- }
-
- void setStates(list<string> states) /** Set list of states to an attribute */
- {
- this->states = states;
- }
-
- void setNameOfStateMachine(string name_of_FSM) /** Set name of FSM to an attribute */
- {
- name_of_machine = name_of_FSM;
- }
-
- void setNameOfFirstState(string first_state) /** Set name of start state to an attribute */
- {
- name_of_first_state = first_state;
- }
-
- void setOutputFilename(string outputFilename) /** Set name of an output file to an attribute */
- {
- this->outputFilename = outputFilename;
- }
-
- bool write_states(ofstream& filestr) /** This method write states to the output file and also to transition table. */
- {
- int pos1, pos2, cnt, subs;
- nState = 1;
- string context, state, ctx, sState, str;
- list<string> nstates = states;
- context = name_of_machine;
- table[0] = "S";
- table[1] = "Context";
- table[2] = "State";
- for(list<string>::iterator i = nstates.begin();i!=nstates.end();i++) // write all states in the context of the automaton
- {
- state = *i;
- cnt = count(state,',');
- if(cnt==1)
- {
- pos1 = state.find(",");
- ctx = cut_namespaces(state.substr(pos1+1));
- if(ctx.compare(0,context.length(),context)==0)
- {
- str = cut_namespaces(state.substr(0,pos1));
- filestr<<str<<";\n";
- table[cols*nState+2] = str;
- table[cols*nState+1] = context;
- nState+=1;
- nstates.erase(i);
- i--;
- if(str.compare(0,str.length(),name_of_first_state)==0) table[cols*(nState-1)] = "*";
- }
- }
- if(cnt==2)
- {
- pos1 = state.find(",");
- pos2 = state.rfind(",");
- ctx = cut_namespaces(state.substr(pos1+1,pos2-pos1-1));
- if(ctx.compare(0,context.length(),context)==0)
- {
- str = cut_namespaces(state.substr(0,pos1));
- filestr<<str<<";\n";
- table[cols*nState+2] = str;
- table[cols*nState+1] = context;
- nState+=1;
- if(str.compare(0,str.length(),name_of_first_state)==0) table[cols*(nState-1)] = "*";
- }
- }
- }
- filestr<<name_of_first_state<<" [peripheries=2] ;\n";
- subs = 0;
- while(!nstates.empty()) // substates ?
- {
- state = nstates.front();
- filestr<<"subgraph cluster"<<subs<<" {\n";
- pos1 = state.find(",");
- pos2 = state.rfind(",");
- if(pos1 == pos2) return false;
-
- context = cut_namespaces(state.substr(0,pos1));
- filestr<<"label=\""<<context<<"\";\n";
- sState = cut_namespaces(state.substr(pos2+1));
- filestr<<sState<<" [peripheries=2] ;\n";
- nstates.pop_front();
- for(list<string>::iterator i = nstates.begin();i!=nstates.end();i++)
- {
- state = *i;
- cnt = count(state,',');
- if(cnt==1)
- {
- pos1 = state.find(",");
- ctx = cut_namespaces(state.substr(pos1+1));
- if(ctx.compare(0,context.length(),context)==0)
- {
- str = cut_namespaces(state.substr(0,pos1));
- filestr<<str<<";\n";
- table[cols*nState+2] = str;
- table[cols*nState+1] = context;
- nState+=1;
- nstates.erase(i);
- i--;
- if(str.compare(0,str.length(),sState)==0) table[cols*(nState-1)] = "*";
- }
- }
- if(cnt==2)
- {
- pos1 = state.find(",");
- pos2 = state.rfind(",");
- ctx = cut_namespaces(state.substr(pos1+1,pos2-pos1-1));
- if(ctx.compare(0,context.length(),context)==0)
- {
- str = cut_namespaces(state.substr(0,pos1));
- filestr<<str<<";\n";
- table[cols*nState+2] = str;
- table[cols*nState+1] = context;
- nState+=1;
- if(str.compare(0,str.length(),sState)==0) table[cols*(nState-1)] = "*";
- }
- }
- }
- filestr<<"}\n";
- subs+=1;
- }
- return true;
- }
-
- void write_transitions(ofstream& filestr) /** Write transitions to the output file nad the transition table. */
- {
- int pos1, pos2;
- string params, state, event, dest;
- for(list<string>::iterator i = transitions.begin();i!=transitions.end();i++) // write all transitions
- {
- params = *i;
- pos1 = params.find(",");
- state = cut_namespaces(params.substr(0,pos1));
- filestr<<state<<"->";
- pos2 = params.rfind(",");
- dest = cut_namespaces(params.substr(pos2+1));
- filestr<<dest;
- event = cut_namespaces(params.substr(pos1+1,pos2-pos1-1));
- filestr<<"[label=\""<<event<<"\"];\n";
- table[find_place(state,2)*cols+find_place(event,1)]=dest;
- }
- return;
- }
-
- void fill_table_with_events() /** Fill the first row of the transition table with events. */
- {
- int j = 3;
- for(list<string>::iterator i = events.begin();i!=events.end();i++)
- {
- table[j] = *i;
- j++;
- }
- }
-
- void save_to_file() /** Create output file stream and write there the name of state machine. It controls the whole process of writing into output files. */
- {
- if(!name_of_first_state.empty())
- {
- ofstream filestr(outputFilename.c_str());
- filestr<<"digraph "<< name_of_machine<< " {\n";
- cols = events.size()+3;
- rows = states.size()+1;
- table = new string [cols*rows];
- fill_table_with_events();
- if(!write_states(filestr))
- {
- cerr<<"Error during writing states.\n";
- filestr<<"}";
- filestr.close();
- return;
- }
- write_transitions(filestr);
- filestr<<"}";
- filestr.close();
- print_table();
- }
- else cout<<"No state machine was found. So no output file was created.\n";
- return;
- }
-
- void print_table() /** This function prints the transition table. At first it counts the size of all columns. */
- {
- cout<<"\nTRANSITION TABLE\n";
- unsigned * len = new unsigned[cols];
- len[0] = 1;
- string line = "-|---|-";
- for(int i = 1; i<cols; i++)
- {
- len[i] = 0;
- for(int j = 0;j<rows;j++)
- {
- if(len[i]<table[j*cols+i].length()) len[i] = table[j*cols+i].length();
- }
- for(unsigned k = 0; k<len[i]; k++)
- {
- line.append("-");
- }
- line.append("-|-");
- }
- cout<<line<<"\n";
- for(int i = 0; i<rows; i++)
- {
- cout<<" | ";
- for(int j = 0;j<cols;j++)
- {
- cout.width(len[j]);
- cout<<left<<table[i*cols+j]<<" | ";
- }
- cout<<"\n";
- cout<<line<<"\n";
- }
- delete len;
- }
-
- void print_stats() /** Print short statistics about the state machine */
- {
- cout<<"\n"<<"Statistics: \n";
- cout<<"Number of states: "<<states.size()<<"\n";
- cout<<"Number of events: "<<events.size()<<"\n";
- cout<<"Number of transitions: "<<transitions.size()<<"\n\n";
- return;
- }
-
-};
+++ /dev/null
-/** @file */
-////////////////////////////////////////////////////////////////////////////////////////
-//
-// This file is part of Boost Statechart Viewer.
-//
-// Boost Statechart Viewer is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Boost Statechart Viewer is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Boost Statechart Viewer. If not, see <http://www.gnu.org/licenses/>.
-//
-////////////////////////////////////////////////////////////////////////////////////////
-
-#include <string>
-
-using namespace std;
-using namespace clang;
-
-string clean_spaces(const string line) /** Remove gaps from string.*/
-{
- unsigned i;
- string new_line = "";
- for(i = 0;i<line.length();i++)
- {
- if(!isspace(line[i])) new_line+=line[i];
- }
- //cout<<new_line;
- return new_line;
-}
-
-string cut_commentary(const string line) /** This function cuts commentaries at the end of line. */
-{
- unsigned i = 0;
- for(i = 0;i<line.length()-1;i++)
- {
- if(line[i]=='/' && line[i+1]=='/') return line.substr(0,i);
- if(line[i]=='/' && line[i+1]=='*') return line.substr(0,i);
- }
- return line;
-}
-
-string get_line_of_code(const string code) /** Thie function return the line of code that belongs to a declaration. The output string line doesn't have gaps and commentaries.*/
-{
- string ret;
- unsigned i;
- for(i = 0;i<code.length();i++)
- {
- if(code[i]=='\n'||code[i]=='{') break;
- }
- ret = code.substr(0,i);
- ret = clean_spaces(ret);
- return cut_commentary(ret);
-}
-
-string get_return(const string code) /** Return return statement. */
-{
- string ret;
- unsigned i;
- for(i = 0;i<code.length();i++)
- {
- if(code[i]==' ') break;
- }
- return code.substr(0,i);
-}
-
-string cut_namespaces(string line) /** Cut namespaces from the declarations. */
-{
- int i = line.rfind("::");
- if(i==-1) return line;
- return line.substr(i+2);
-}
-
-bool is_derived(const string line) /** Test whether this struct or class is derived */
-{
- unsigned i;
- for(i = 0;i<line.length()-1;i++)
- {
- if(line[i]==':')
- {
- if(line[i+1]!=':') return true;
- else i+=1;
- }
- }
- return false;
-}
-
-string get_super_class(const string line) /** Get the super classes of this declarations. */
-{
- unsigned i;
- for(i = 0;i<line.length()-1;i++)
- {
- if(line[i]==':')
- {
- if(line[i+1]!=':') break;
- else i+=1;
- }
- }
- return line.substr(i+1);
-}
-
-string get_next_base(const string line) /** Get next super classes of this declarations Do the first super class is ommitted. */
-{
- unsigned i;
- int brackets = 0;
- for(i = 0;i<line.length()-1;i++)
- {
- if(line[i]=='<') brackets+=1;
- if(line[i]=='>') brackets-=1;
- if(line[i]==',' && brackets == 0) break;
- }
- return line.substr(i+1);
-}
-
-string get_first_base(const string line) /** Get the first super class of this declarations. */
-{
- unsigned i;
- int brackets = 0;
- for(i = 0;i<line.length()-1;i++)
- {
- if(line[i]=='<') brackets+=1;
- if(line[i]=='>') brackets-=1;
- if(line[i]==',' && brackets == 0) break;
- }
- return line.substr(0,i);
-}
-
-bool is_state(const string line) /** Test if this declaration is a state. It is used to test the base classes. */
-{
- if(cut_namespaces(line).compare(0,12,"simple_state")==0)
- {
- return true;
- }
- else
- {
- if(cut_namespaces(line).compare(0,5,"state")==0)
- {
- return true;
- }
- return false;
- }
-}
-
-string cut_type(string line) /** This function cuts type of the declaration from the string. */
-{
- unsigned i;
- for(i = 0;i<line.length();i++)
- {
- if(line[i]==' ') break;
- }
- return line.substr(i);
-}
-
-int count(const string line, const char c) /** Count all specified char in string. So it returns the number of specified characters. */
-{
- int number = 0;
- for(unsigned i = 0;i<line.length();i++)
- {
- if(line[i]==c) number+=1;
- }
- return number;
-}
-
-bool is_list(const string line) /** Test if this decl is mpl::list. */
-{
- //cout<<line<<"\n";
- int pos = 0;
- for(unsigned i = 0; i<line.length();i++)
- {
- if(line[i]=='<') break;
- if(line[i]==':' && line[i+1]==':') pos = i+2;
- }
- if(line.substr(pos).compare(0,4,"list")==0)
- {
- return true;
- }
- else
- {
- return false;
- }
-}
-
-string get_inner_part(const string line) /** Get inner part of the list. */
-{
- string str;
- unsigned i, pos = 0;
- for(i = 0;i<line.length();i++)
- {
- if(line[i]=='<') break;
- }
- str = line.substr(i+1);
- for(i = 0;i<str.length();i++)
- {
- if(str[i]=='<') pos+=1;
- if(str[i]=='>')
- {
- if(pos==0) break;
- else pos-=1;
- }
- }
- return str.substr(0,i);
-}
-
-bool test_model(const string line, const string model) /** Test the string to has a specified model. */
-{
- if(cut_namespaces(line).compare(0,model.length(),model)==0)
- {
- return true;
- }
- else
- {
- return false;
- }
-}
-
-string get_params(string line) /** Return parameters of the specified transition */
-{
- int pos_front = line.find("<")+1;
- int pos_end = line.find(">");
- return line.substr(pos_front,pos_end-pos_front);
-}
-
-string find_states(const CXXRecordDecl *cRecDecl, string line) /** test if the struct/class is he state (must be derived from simple_state or state). */
-{
- string super_class = get_super_class(line), base, params;
- if(cRecDecl->getNumBases()>1)
- {
- for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
- {
- if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
- else base = super_class;
- if(is_state(super_class))
- {
- params = get_params(super_class);
- }
- else
- {
- super_class = get_next_base(super_class);
- }
- }
- }
- else
- {
- if(is_state(super_class))
- {
- params = get_params(super_class);
- }
- else params = "";
- }
- return params;
-}
-
-string find_name_of_machine(const CXXRecordDecl *cRecDecl, string line) /** Find name of the state machine and the start state. */
-{
- string super_class = get_super_class(line), base, params;
- if(cRecDecl->getNumBases()>1)
- {
- for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
- {
- if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
- else base = super_class;
- if(test_model(base,"state_machine"))
- {
- params = get_params(base);
- }
- else
- {
- super_class = get_next_base(super_class);
- }
- }
- }
- else
- {
- if(test_model(super_class,"state_machine"))
- {
- params = get_params(super_class);
- }
- }
- return params;
-}
-
-string find_transitions (const string name_of_state, string line) /** Traverse all methods for finding declarations of transitions. */
-{
- string dest, params, base, trans;
- int num = count(line,'<');
- if(num>1)
- {
- num-=1;
- if(is_list(line))
- {
- line = get_inner_part(line);
- }
- }
- for(int j = 0;j<num;j++)
- {
- if(j!=num-1) base = get_first_base(line);
- else base = line;
- if(test_model(base,"transition"))
- {
- dest = name_of_state;
- params = get_params(base);
- //cout<<params<<"\n";
- dest.append(",");
- dest.append(params);
- line = get_next_base(line);
- trans.append(dest);
- if(j+1!=num) trans.append(";");
- }
- else if(test_model(base,"custom_reaction"))
- {
- dest = ";";
- dest.append(name_of_state);
- params = get_params(base);
- //cout<<params<<"\n";
- dest.append(",");
- dest.append(params);
- line = get_next_base(line);
- trans.append(dest);
- if(j+1!=num) trans.append(";");
- }
- else
- {
- line = get_next_base(line);
- }
- }
- if(trans[trans.length()-1]==';') return trans.substr(0,trans.length()-1);
- else return trans;
-}
-
-bool find_events(const CXXRecordDecl *cRecDecl, string line) /** This function provides testing if the decl is decl of event*/
-{
- string super_class = get_super_class(line), base, params;
- if(cRecDecl->getNumBases()>1)
- {
- for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
- {
- if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
- else base = super_class;
- if(test_model(base,"event")) return true;
- else
- {
- super_class = get_next_base(super_class);
- }
- }
- }
- else
- {
- if(test_model(super_class,"event"))return true;
- }
- return false;
-}
-
+++ /dev/null
-/** @file */
-////////////////////////////////////////////////////////////////////////////////////////
-//
-// This file is part of Boost Statechart Viewer.
-//
-// Boost Statechart Viewer is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Boost Statechart Viewer is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Boost Statechart Viewer. If not, see <http://www.gnu.org/licenses/>.
-//
-////////////////////////////////////////////////////////////////////////////////////////
-
-//standard header files
-#include <iostream>
-
-//LLVM Header files
-#include "llvm\Support\raw_ostream.h"
-#include "llvm\Support\Host.h"
-#include "llvm\Config\config.h"
-
-//clang header files
-#include "clang\Frontend\TextDiagnosticPrinter.h"
-#include "clang\Lex\HeaderSearch.h"
-#include "clang\Basic\FileManager.h"
-#include "clang\Frontend\Utils.h"
-#include "clang\Basic\TargetInfo.h"
-#include "clang\Lex\Preprocessor.h"
-#include "clang\Frontend\CompilerInstance.h"
-#include "clang\AST\ASTConsumer.h"
-#include "clang\Sema\Lookup.h"
-#include "clang\Parse\ParseAST.h"
-#include "clang\Basic\Version.h"
-#include "clang\Driver\Driver.h"
-#include "clang\Driver\Compilation.h"
-
-//my own header files
-#include "iooper.h"
-
-using namespace clang;
-using namespace clang::driver;
-using namespace std;
-
-/**
- * This class provides Simple diagnostic Client. It uses implementation in library for printing diagnostci information.
- * Also it counts number of warnings, errors, ... When an error occurs the program is stopped.
- */
-class MyDiagnosticClient : public TextDiagnosticPrinter
-{
- int nwarnings; /** Save number of Warnings occured during diagnostic */
- int nnotes;
- int nignored;
- int nerrors;
- public:
- /**
- * Initialize number of warnings, errors, ...
- */
- MyDiagnosticClient(llvm::raw_ostream &os, const DiagnosticOptions &diags, bool OwnsOutputStream = false):TextDiagnosticPrinter(os, diags, OwnsOutputStream = false)
- {
- nwarnings=0;
- nnotes=0;
- nignored=0;
- nerrors = 0;
- }
- /**
- * This method prints diagnostic and counts diagnostic types.
- */
- virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, const DiagnosticInfo &Info)
- {
- TextDiagnosticPrinter::HandleDiagnostic(DiagLevel, Info); // print diagnostic information using library implementation
- switch (DiagLevel) // count number of all diagnostic information
- {
- case 0 : nignored+=1; break;
- case 1 : nnotes+=1; break;
- case 2 : nwarnings+=1; break;
- default : nerrors+=1;
- print_stats();
- exit(1);
- }
- }
- /**
- * Print statistics about diagnostic.
- */
- void print_stats()
- {
- cout<<"\n--Diagnostic Info--\n";
- cout<<"Number of ignored: "<<nignored<<"\n";
- cout<<"Number of notes: "<<nnotes<<"\n";
- cout<<"Number of warnings: "<<nwarnings<<"\n";
- cout<<"Number of errors and fatal errors: "<<nerrors<<"\n";
- }
-
- int getNbrOfWarnings() /** Return number of warnings */
- {
- return nwarnings;
- }
-
- int getNbrOfNotes() /** Return number of notes */
- {
- return nnotes;
- }
-
- int getNbrOfIgnored() /** Return number of ignored */
- {
- return nignored;
- }
-};
-
-/**
- * My ASTConsumer provides interface for traversing AST. It uses recursive traversing in namespaces.
- */
-class FindStates : public ASTConsumer
-{
- list<string> transitions;
- list<string> cReactions; /** list of custom reactions. After all files are traversed this list should be empty. */
- list<string> events;
- list<string> states;
- string name_of_machine;
- string name_of_start;
- FullSourceLoc *fsloc; /** Full Source Location instance for holding Source Manager. */
- public:
-
- list<string> getStates() /** Return list of states. */
- {
- return states;
- }
-
- list<string> getTransitions() /** Return list of transitions. */
- {
- return transitions;
- }
-
- list<string> getEvents() /** Return list of events. */
- {
- return events;
- }
-
- string getStateMachine() /** Return name of the state machine. */
- {
- return name_of_machine;
- }
-
- string getNameOfFirstState() /** Return name of start state. */
- {
- return name_of_start;
- }
-
- virtual void Initialize(ASTContext &ctx)/** Run after the AST is constructed before the consumer starts to work. So this function works like constructor. */
- {
- fsloc = new FullSourceLoc(* new SourceLocation(), ctx.getSourceManager());
- name_of_start = "";
- name_of_machine = "";
- }
-
-/**
-* Traverse global decls using DeclGroupRef for handling all global decls. But only interesting decls are processed. Interesting decls are Struct, Class, C++ methods and Namespace.
-* When Namespace is found it recursively traverse all decls inside this Namespace using method recursive_visit.
-*/
- virtual void HandleTopLevelDecl(DeclGroupRef DGR)
- {
- SourceLocation loc;
- string line, output, event;
- llvm::raw_string_ostream x(output);
- for (DeclGroupRef::iterator i = DGR.begin(), e = DGR.end(); i != e; ++i)
- {
- const Decl *decl = *i;
- loc = decl->getLocation();
- if(loc.isValid())
- {
- if(decl->getKind()==35)
- {
- method_decl(decl);
- }
- if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
- {
- if(tagDecl->isStruct() || tagDecl->isClass()) //is it a struct or class
- {
- struct_class(decl);
- }
- }
- if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
- {
-
- DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
- recursive_visit(declCont);
-
- }
- }
- output = "";
- }
- }
-
-/**
-* It is used to recursive traverse decls in Namespaces. This method do the same as HandleTopLevelDecl.
-*/
- void recursive_visit(const DeclContext *declCont)
- {
- string line, output, event;
- llvm::raw_string_ostream x(output);
- SourceLocation loc;
- for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
- {
- const Decl *decl = *i;
- loc = decl->getLocation();
- if(loc.isValid())
- {
- if(decl->getKind()==35)
- {
- method_decl(decl);
- }
- else if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
- {
- if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class
- {
- struct_class(decl);
- }
- }
- else if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
- {
- DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
- recursive_visit(declCont);
- }
- }
- output = "";
- }
- }
-
-/**
-* This function works with class or struct. It splits the decl into 3 interesting parts.
-* The state machine decl, state decl and event decl.
-*/
- void struct_class(const Decl *decl)
- {
- string output, line, ret, trans, event;
- llvm::raw_string_ostream x(output);
- decl->print(x);
- line = get_line_of_code(x.str());
- output = "";
- int pos;
- const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
- if(is_derived(line))
- {
- const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
-
- if(find_events(cRecDecl, line))
- {
- events.push_back(namedDecl->getNameAsString());
- }
- else if(name_of_machine == "")
- {
- ret = find_name_of_machine(cRecDecl, line);
- if(!ret.empty())
- {
- pos = ret.find(",");
- name_of_machine = ret.substr(0,pos);
- name_of_start = ret.substr(pos+1);
- }
- }
- else
- {
- ret = find_states(cRecDecl, line);
- if(!ret.empty())
- {
- states.push_back(ret);
- methods_in_class(decl,namedDecl->getNameAsString());
- }
- }
- }
- }
-
-/**
-* This function provides traversing all methods and other context indide class. If
-* typedef or classic method decl is found. If typedef is found then it is being testted for transitions and custom reactions.
-*/
- void methods_in_class(const Decl *decl, const string state)
- {
- string output, line, ret, trans, event;
- llvm::raw_string_ostream x(output);
- int pos, num;
- const TagDecl *tagDecl = dyn_cast<TagDecl>(decl);
- const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);
- output="";
- for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
- {
- if (i->getKind()==26) // typedefs
- {
- i->print(x);
- output = x.str();
- line = clean_spaces(cut_type(output));
- ret = find_transitions(state,line);
- if(!ret.empty())
- {
- num = count(ret,';')+1;
- for(int i = 0;i<num;i++)
- {
- pos = ret.find(";");
- if(pos == 0)
- {
- ret = ret.substr(1);
- pos = ret.find(";");
- if(pos==-1) cReactions.push_back(ret);
- else cReactions.push_back(ret.substr(0,pos));
- num-=1;
- }
- else
- {
- if(pos==-1) transitions.push_back(ret);
- else transitions.push_back(ret.substr(0,pos));
- }
- if(i!=num-1) ret = ret.substr(pos+1);
- }
- output="";
- }
- }
- if(i->getKind()==35) method_decl(decl);// C++ method
- }
- }
-
- /**
- * Traverse method declaration using classes with main class Stmt.
- */
- void method_decl(const Decl *decl)
- {
- string output, line, event;
- llvm::raw_string_ostream x(output);
- if(decl->hasBody())
- {
- decl->print(x);
- line = get_return(x.str());
- if(test_model(line,"result"))
- {
- const FunctionDecl *fDecl = dyn_cast<FunctionDecl>(decl);
- const ParmVarDecl *pvd = fDecl->getParamDecl(0);
- QualType qt = pvd->getOriginalType();
- event = qt.getAsString();
- if(event[event.length()-1]=='&') event = event.substr(0,event.length()-2);
- event = event.substr(event.rfind(" ")+1);
- line = dyn_cast<NamedDecl>(decl)->getQualifiedNameAsString();
- line = cut_namespaces(line.substr(0,line.rfind("::")));
- line.append(",");
- line.append(event);
- find_return_stmt(decl->getBody(),line);
- for(list<string>::iterator i = cReactions.begin();i!=cReactions.end();i++) // erase info about it from list of custom reactions
- {
- event = *i;
- if(line.compare(event)==0)
- {
- cReactions.erase(i);
- break;
- }
- }
- }
- }
- }
-
- void find_return_stmt(Stmt *statemt,string event) /** Traverse all statements in function for finding all return Statements.*/
- {
- if(statemt->getStmtClass() == 99) test_stmt(dyn_cast<CaseStmt>(statemt)->getSubStmt(), event);
- else
- {
- for (Stmt::child_range range = statemt->children(); range; ++range)
- {
- test_stmt(*range, event);
- }
- }
- }
-
- void test_stmt(Stmt *stmt, string event) /** test statement for its kind Using number as identifier for all Statement Classes.*/
- {
- const SourceManager &sman = fsloc->getManager();
- int type;
- string line, param;
- type = stmt->getStmtClass();
- switch(type)
- {
- case 8 : find_return_stmt(dyn_cast<DoStmt>(stmt)->getBody(), event); // do
- break;
- case 86 : find_return_stmt(dyn_cast<ForStmt>(stmt)->getBody(), event); // for
- break;
- case 88 : find_return_stmt(dyn_cast<IfStmt>(stmt)->getThen(), event); //if then
- find_return_stmt(dyn_cast<IfStmt>(stmt)->getElse(), event); //if else
- break;
- case 90 : find_return_stmt(dyn_cast<LabelStmt>(stmt)->getSubStmt(), event); //label
- break;
- case 98 : line = sman.getCharacterData(dyn_cast<ReturnStmt>(stmt)->getReturnLoc());
- line = get_line_of_code(line).substr(6);
- line = line.substr(0,line.find("("));
- if(test_model(line,"transit"))
- {
- param = get_params(line);
- transitions.push_back(event.append(",").append(param));
- }
- break;
- case 99 : find_return_stmt(stmt, event);
- break;
- case 101 : find_return_stmt(dyn_cast<SwitchStmt>(stmt)->getBody(), event); // switch
- break;
- case 102 : find_return_stmt(dyn_cast<WhileStmt>(stmt)->getBody(), event); // while
- break;
- }
- }
-
-};
-/**
- * Main function provides all initialization before starting analysis of AST. Diagnostic Client is initialized,
- * Command line options are processed.
- */
-int main(int argc, char **argv)
-{
- string inputFilename = "";
- string outputFilename = "graph.dot"; // initialize output Filename
- DiagnosticOptions dopts;
- dopts.ShowColors=1;
- MyDiagnosticClient *mdc = new MyDiagnosticClient(llvm::errs(), dopts);
- llvm::IntrusiveRefCntPtr<DiagnosticIDs> dis(new DiagnosticIDs());
- Diagnostic diag(dis,mdc);
- FileManager fm( * new FileSystemOptions());
- SourceManager sm (diag, fm);
- HeaderSearch *headers = new HeaderSearch(fm);
-
- Driver TheDriver(LLVM_BINDIR, llvm::sys::getHostTriple(), "", false, false, diag);
- TheDriver.setCheckInputsExist(true);
- TheDriver.CCCIsCXX = 1;
- TheDriver.ResourceDir = LLVM_PREFIX "\\lib\\clang\\" CLANG_VERSION_STRING;
-
- CompilerInvocation compInv;
- llvm::SmallVector<const char *, 16> Args(argv, argv + argc);
- llvm::OwningPtr<Compilation> C(TheDriver.BuildCompilation(Args.size(),
- Args.data()));
- const driver::JobList &Jobs = C->getJobs();
- const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
- const driver::ArgStringList &CCArgs = Cmd->getArguments();
- for(unsigned i = 0; i<Args.size();i++) // find -o in ArgStringList
- {
- if(strncmp(Args[i],"-o",2)==0)
- {
- if(strlen(Args[i])>2)
- {
- string str = Args[i];
- outputFilename = str.substr(2);
- }
- else outputFilename = Args[i+1];
- break;
- }
- }
-
- CompilerInvocation::CreateFromArgs(compInv,
- const_cast<const char **>(CCArgs.data()),
- const_cast<const char **>(CCArgs.data())+CCArgs.size(),
- diag);
-
- HeaderSearchOptions hsopts = compInv.getHeaderSearchOpts();
- LangOptions lang = compInv.getLangOpts();
- CompilerInvocation::setLangDefaults(lang, IK_ObjCXX);
- TargetInfo *ti = TargetInfo::CreateTargetInfo(diag, compInv.getTargetOpts());
- FrontendOptions f = compInv.getFrontendOpts();
- inputFilename = f.Inputs[0].second;
-
- cout<<"Input filename: "<<inputFilename<<"\n"; // print Input filename
- cout<<"Output filename: "<<outputFilename<<"\n"; // print Output filename
-
-
- Preprocessor pp(diag, lang, *ti, sm, *headers);
- pp.getBuiltinInfo().InitializeBuiltins(pp.getIdentifierTable(), lang);
-
- InitializePreprocessor(pp, compInv.getPreprocessorOpts(),hsopts,f);
-
- const FileEntry *file = fm.getFile(inputFilename);
- sm.createMainFileID(file);
- IdentifierTable tab(lang);
- Builtin::Context builtins(*ti);
- FindStates c;
- ASTContext ctx(lang, sm, *ti, tab, * new SelectorTable(), builtins,0);
- mdc->BeginSourceFile(lang, &pp);//start using diagnostic
- ParseAST(pp, &c, ctx, false, false);
- mdc->EndSourceFile(); //end using diagnostic
- IO_operations *io = new IO_operations(outputFilename, c.getStateMachine(), c.getNameOfFirstState(), c.getTransitions(), c.getStates(), c.getEvents());
- io->save_to_file();
- io->print_stats();
- mdc->print_stats();
- return 0;
-}