]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blob - src/visualizer.cpp
The start state has double outlline. The arrows has label that is equal to the event.
[boost-statechart-viewer.git] / src / visualizer.cpp
1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <list>
5
6
7 #include "llvm/Support/raw_ostream.h"
8 #include "llvm/System/Host.h"
9 #include "llvm/Config/config.h"
10
11 #include "clang/Frontend/DiagnosticOptions.h"
12 #include "clang/Frontend/TextDiagnosticPrinter.h"
13
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/FileSystemOptions.h"
16
17 #include "clang/Index/TranslationUnit.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Basic/FileManager.h"
21
22 #include "clang/Frontend/HeaderSearchOptions.h"
23 #include "clang/Frontend/Utils.h"
24
25 #include "clang/Basic/TargetOptions.h"
26 #include "clang/Basic/TargetInfo.h"
27
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Frontend/PreprocessorOptions.h"
30 #include "clang/Frontend/FrontendOptions.h"
31
32 #include "clang/Frontend/CompilerInvocation.h"
33
34 #include "clang/Basic/IdentifierTable.h"
35 #include "clang/Basic/Builtins.h"
36
37 #include "clang/AST/ASTContext.h"
38 #include "clang/AST/ASTConsumer.h"
39 #include "clang/Sema/Sema.h"
40 #include "clang/AST/DeclBase.h"
41 #include "clang/AST/Type.h"
42 #include "clang/AST/Decl.h"
43 #include "clang/Sema/Lookup.h"
44 #include "clang/Sema/Ownership.h"
45 #include "clang/AST/DeclGroup.h"
46
47 #include "clang/Parse/Parser.h"
48
49 #include "clang/Parse/ParseAST.h"
50 #include "clang/Basic/Version.h"
51
52 #include "llvm/Support/CommandLine.h"
53
54 //my own header files
55 #include "stringoper.h"
56 #include "commandlineopt.h"
57
58 using namespace clang;
59
60
61 class FindStates : public ASTConsumer
62 {
63         std::list<string> transitions;
64         std::list<string> events;
65         std::list<string> states;
66         std::string name_of_machine;
67         std::string name_of_start;
68         public:
69
70         virtual void Initialize(ASTContext &ctx)//run after the AST is constructed
71         {       
72                 name_of_start = "";
73                 name_of_machine = "";
74         }
75
76         virtual void HandleTopLevelDecl(DeclGroupRef DGR)// traverse all top level declarations
77         {
78                 SourceLocation loc;
79                 std::string line;
80                 std::string super_class, output;
81                 llvm::raw_string_ostream x(output);
82                 for (DeclGroupRef::iterator i = DGR.begin(), e = DGR.end(); i != e; ++i) 
83                 {
84                         const Decl *decl = *i;
85                         loc = decl->getLocation();
86                         if(loc.isValid())
87                         {
88                                 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
89                                 //std::cout<<decl->getDeclKindName()<<"\n";
90                                 if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
91                                 {
92                                         if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
93                                         {
94                                                 const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
95                                                 decl->print(x);
96                                                 //decl->dump();                                                 
97                                                 line = cut_commentary(clean_spaces(get_line_of_code(x.str())));
98                                                 output = "";
99                                                 if(is_derived(line))
100                                                 {
101                                                         if(name_of_machine == "")
102                                                         {
103                                                                 find_name_of_machine(cRecDecl, line);
104                                                         }
105                                                         else
106                                                         {
107                                                                 if(find_states(cRecDecl, line))
108                                                                 {                               
109                                                                         const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                      
110                                                                         std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
111                                                                         find_transitions(namedDecl->getNameAsString(), declCont);
112                                                                 }
113                                                         }
114                                                 }
115                                         }
116                                 }       
117                                 if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
118                                 {
119                                         DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
120                                         //declCont->dumpDeclContext();                          
121                                         recursive_visit(declCont);
122                                 
123                                 }
124                         }
125                 }
126         }
127         void recursive_visit(const DeclContext *declCont) //recursively visit all decls inside namespace
128         {
129                 std::string line, output;
130                 SourceLocation loc;
131                 llvm::raw_string_ostream x(output);
132                 for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
133                 {
134                         const Decl *decl = *i;
135                         const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
136                         
137                         //std::cout<<"a "<<decl->getDeclKindName()<<"\n";
138                         loc = decl->getLocation();
139                         if(loc.isValid())
140                         {                       
141                                 if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
142                                 {
143                                         if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
144                                         {
145                                                 const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
146                                                 decl->print(x);
147                                                 line = cut_commentary(clean_spaces(get_line_of_code(x.str())));
148                                                 output = "";
149                                                 if(is_derived(line))
150                                                 {
151                                                         if(name_of_machine == "")
152                                                         {
153                                                                 find_name_of_machine(cRecDecl, line);
154                                                         }
155                                                         else
156                                                         {
157                                                                 if(find_states(cRecDecl, line))
158                                                                 {                               
159                                                                         const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                      
160                                                                         states.push_back(namedDecl->getNameAsString());
161                                                                         std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
162                                                                         find_transitions(namedDecl->getNameAsString(), declCont);
163                                                                 }
164                                                         }
165                                                 }
166                                         }       
167                                 }
168                                 if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
169                                 {
170                                         DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
171                                         //declCont->dumpDeclContext();                          
172                                         recursive_visit(declCont);
173                                 }
174                         }
175                 } 
176         }
177         bool find_states(const CXXRecordDecl *cRecDecl, std::string line)
178         {       
179                 std::string super_class = get_super_class(line), base;          
180                 if(cRecDecl->getNumBases()>1)
181                 {
182                         for(int i = 0; i<cRecDecl->getNumBases();i++ )
183                         {
184                                 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
185                                 else base = super_class;
186                                 if(is_state(base)) return true;
187                                 else
188                                 {
189                                         super_class = get_next_base(super_class);
190                                 }
191                         }
192                         return false;
193                 }
194                 else
195                 { 
196                         if(is_state(super_class)) return true;
197                         else return false;
198                 }
199         }
200                 
201         void find_name_of_machine(const CXXRecordDecl *cRecDecl, std::string line)
202         {       
203                 std::string super_class = get_super_class(line), base, params;
204                 
205                 int pos = 0;
206                 if(cRecDecl->getNumBases()>1)
207                 {
208                         for(int i = 0; i<cRecDecl->getNumBases();i++ )
209                         {
210                                 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
211                                 else base = super_class;
212                                 if(is_machine(base))
213                                 {
214                                         params = get_params(base);
215                                         pos = params.find(",");
216                                         name_of_machine = params.substr(0,pos);
217                                         name_of_start = params.substr(pos);
218                                         std::cout<<"Name of the state machine: "<<name_of_machine<<"\n";
219                                         std::cout<<"Name of the first state: "<<name_of_start<<"\n";
220                                 }
221                                 else
222                                 {
223                                         super_class = get_next_base(super_class);
224                                 }
225                         }
226                 }
227                 else
228                 { 
229                         if(is_machine(super_class))
230                         {
231                                 //std::cout<<super_class;
232                                 params = get_params(super_class);
233                                 //std::cout<<params;
234                                 pos = params.find(",");
235                                 name_of_machine = cut_namespaces(params.substr(0,pos));
236                                 name_of_start = cut_namespaces(params.substr(pos+1));
237                                 std::cout<<"Name of the state machine:"<<name_of_machine<<"\n";
238                                 std::cout<<"Name of the first state:"<<name_of_start<<"\n";
239                         }
240                 }
241         }
242
243         void find_transitions (const std::string name_of_state,const DeclContext *declCont) // traverse all methods for finding declarations of transitions
244         {       
245                 std::string output, line, dest, params, base;   
246                 llvm::raw_string_ostream x(output);
247                 int pos;
248                 int num;                
249                 for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i) 
250                 {
251                         const Decl *decl = *i;
252                         
253                         if (const TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(decl)) 
254                         {
255                                         decl->print(x);
256                                         output = x.str();
257                                         line = clean_spaces(cut_typedef(output));
258                                         num = count(output);                                    
259                                         if(num>1)
260                                         {
261                                                 num-=1;
262                                                 if(is_list(line))
263                                                 {
264                                                         line = get_inner_part(line);
265                                                 }
266                                         }
267                                         for(int j = 0;j<num;j++)
268                                         {
269                                                 if(j!=num-1) base = get_first_base(line);                       
270                                                 else base = line;
271                                                 if(is_transition(base))
272                                                 {
273                                                         dest = name_of_state;
274                                                         params = get_params(base);
275                                                         dest.append(",");                                                       
276                                                         dest.append(params);
277                                                         transitions.push_back(dest);
278                                                         line = get_next_base(line);
279                                                 }
280                                                 else
281                                                 {
282                                                         line = get_next_base(line);
283                                                 }
284                                         }
285                         }
286                 }       
287         }
288         void save_to_file(std::string output)
289         {
290                 std::string state;
291                 int pos1, pos2;
292                 std::ofstream filestr(output.c_str());
293                 std::cout<<output<<"\n";
294                 filestr<<"digraph "<< name_of_machine<< " {\n";
295                 for(list<string>::iterator i = transitions.begin();i!=transitions.end();i++)
296                 {
297                         state = *i;
298                         pos1 = state.find(",");
299                         filestr<<cut_namespaces(state.substr(0,pos1))<<"->";
300                         pos2 = state.rfind(",");
301                         filestr<<cut_namespaces(state.substr(pos2+1));
302                         filestr<<"[label=\""<<cut_namespaces(state.substr(pos1+1,pos2-pos1-1))<<"\"];\n";
303                 }
304                 filestr<<name_of_start<<" [peripheries=2] ;\n";
305                 filestr<<"}";
306                 filestr.close();
307         }       
308 };
309
310 int main(int argc, char *argv[])
311 {
312         llvm::cl::ParseCommandLineOptions(argc, argv);  
313         std::cout<<"Input file: "<<inputFilename<<"\n"; 
314         DiagnosticOptions diagnosticOptions;
315         TextDiagnosticPrinter *tdp = new TextDiagnosticPrinter(llvm::nulls(), diagnosticOptions);
316         llvm::IntrusiveRefCntPtr<DiagnosticIDs> dis(new DiagnosticIDs());
317         Diagnostic diag(dis,tdp);
318         FileSystemOptions fileSysOpt;     
319         LangOptions lang;
320         lang.BCPLComment=1;
321         lang.CPlusPlus=1; 
322         FileManager fm (fileSysOpt);
323
324         SourceManager sm ( diag, fm);
325         HeaderSearch *headers = new HeaderSearch(fm);
326         CompilerInvocation::setLangDefaults(lang, IK_ObjCXX);
327
328         HeaderSearchOptions hsopts;
329         hsopts.ResourceDir=LLVM_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
330         for(int i = 0; i<includeFiles.size();i++)
331         {       
332                 hsopts.AddPath(includeFiles[i],
333                                 clang::frontend::Angled,
334                                 false,
335                                 false,
336                                 true);
337         }
338         TargetOptions to;
339         to.Triple = llvm::sys::getHostTriple();
340         TargetInfo *ti = TargetInfo::CreateTargetInfo(diag, to);
341         clang::ApplyHeaderSearchOptions(
342         *headers,
343         hsopts,
344         lang,
345         ti->getTriple());
346         Preprocessor pp(diag, lang, *ti, sm, *headers);
347         FrontendOptions f;
348         PreprocessorOptions ppio;
349         InitializePreprocessor(pp, ppio,hsopts,f);
350         const FileEntry *file = fm.getFile(inputFilename);
351         sm.createMainFileID(file);
352         IdentifierTable tab(lang);
353         SelectorTable sel;
354         Builtin::Context builtins(*ti);
355         FindStates c;
356         ASTContext ctx(lang, sm, *ti, tab, sel, builtins,0);
357         tdp->BeginSourceFile(lang, &pp);
358         ParseAST(pp, &c, ctx, false, false);
359         tdp->EndSourceFile();
360         
361         c.save_to_file(outputFile);
362         return 0;
363
364 }