]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blob - visualizer.cpp
5d7e942615c0e0353e2639d11e411d1c4a39ea86
[boost-statechart-viewer.git] / visualizer.cpp
1 #include <iostream>
2
3 #include "llvm/Support/raw_ostream.h"
4 #include "llvm/System/Host.h"
5 #include "llvm/Config/config.h"
6
7 #include "clang/Frontend/DiagnosticOptions.h"
8 #include "clang/Frontend/TextDiagnosticPrinter.h"
9
10 #include "clang/Basic/LangOptions.h"
11 #include "clang/Basic/FileSystemOptions.h"
12
13 #include "clang/Index/TranslationUnit.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Lex/HeaderSearch.h"
16 #include "clang/Basic/FileManager.h"
17
18 #include "clang/Frontend/HeaderSearchOptions.h"
19 #include "clang/Frontend/Utils.h"
20
21 #include "clang/Basic/TargetOptions.h"
22 #include "clang/Basic/TargetInfo.h"
23
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Frontend/PreprocessorOptions.h"
26 #include "clang/Frontend/FrontendOptions.h"
27
28 #include "clang/Frontend/CompilerInvocation.h"
29
30 #include "clang/Basic/IdentifierTable.h"
31 #include "clang/Basic/Builtins.h"
32
33 #include "clang/AST/ASTContext.h"
34 #include "clang/AST/ASTConsumer.h"
35 #include "clang/Sema/Sema.h"
36 #include "clang/AST/DeclBase.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/Decl.h"
39 #include "clang/Sema/Lookup.h"
40 #include "clang/Sema/Ownership.h"
41 #include "clang/AST/DeclGroup.h"
42
43 #include "clang/Parse/Parser.h"
44
45 #include "clang/Parse/ParseAST.h"
46 #include "clang/Basic/Version.h"
47
48 //my own header files
49 #include "stringoper.h"
50
51 using namespace clang;
52
53
54 class FindStates : public ASTConsumer
55 {
56         public:
57         SourceLocation loc;
58         FullSourceLoc *fSloc;
59         
60         virtual void Initialize(ASTContext &ctx)//run after the AST is constructed
61         {       
62                 SourceManager &sman = ctx.getSourceManager();
63                 fSloc = new FullSourceLoc(loc, sman);
64         }
65
66         virtual void HandleTopLevelDecl(DeclGroupRef DGR)// traverse all top level declarations
67         {
68                 SourceLocation loc;
69                 const SourceManager &sman = fSloc->getManager();
70                 std::string line;
71                 std::string super_class;
72                 for (DeclGroupRef::iterator i = DGR.begin(), e = DGR.end(); i != e; ++i) 
73                 {
74                         const Decl *decl = *i;
75                         loc = decl->getLocation();
76                                 
77                         if(sman.isFromMainFile(loc)) //only declaration in Main file interested us
78                         {
79                                 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
80                                 if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
81                                 {
82                                         if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class      
83                                         {
84                                                 line = cut_commentary(clean_spaces(get_line_of_code(sman.getCharacterData(loc))));
85                                                 if(is_derived(line))
86                                                 {
87                                                         super_class = get_super_class(line);
88                                                         if(is_state(super_class))
89                                                         {                               
90                                                                 const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
91                                                                 if(cRecDecl->getNumBases()==1) //state is derived from one base class simple_state              
92                                                                 {       
93                                                                         const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);                                              
94                                                                         std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
95                                                                         find_transitions(namedDecl->getNameAsString(), declCont);
96                                                                 }
97                                                         }
98                                                 }
99                                         }       
100                                 }
101                         }
102                 }
103         }
104
105         void find_transitions (const std::string name_of_state,const DeclContext *declCont) // traverse all methods for finding declarations of transitions
106         {       
107                 std::string output, line, event, dest, params;  
108                 llvm::raw_string_ostream x(output);
109                 int pos;                
110                 for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i) 
111                 {
112                         const Decl *decl = *i;
113                         //decl->dump();
114                         
115                         if (const TypedefDecl *typeDecl = llvm::dyn_cast<TypedefDecl>(decl)) 
116                         {
117                                 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
118                                 decl->print(x);
119                                 output = x.str();
120                                 if(count(output)==1)
121                                 {
122                                         line = clean_spaces(cut_typedef(output));
123                                         params = get_transition_params(line);
124                                         pos = params.find(",");
125                                         event = params.substr(0,pos);
126                                         dest = params.substr(pos+1);
127                                         if(is_transition(line)) std::cout << "New transition: " << name_of_state<<" -> "<<event<<" -> "<< dest<<"\n";
128                                 }
129                         }
130                         /* TODO else test na projiti*/
131                 }       
132         }       
133 };
134
135 int main(int argc, char *argv[])
136 {
137         DiagnosticOptions diagnosticOptions;
138         TextDiagnosticPrinter *tdp = new TextDiagnosticPrinter(llvm::nulls(), diagnosticOptions);
139         llvm::IntrusiveRefCntPtr<DiagnosticIDs> dis(new DiagnosticIDs());
140         Diagnostic diag(dis,tdp);
141         FileSystemOptions fileSysOpt;     
142         LangOptions lang;
143         lang.BCPLComment=1;
144         lang.CPlusPlus=1; 
145         FileManager fm (fileSysOpt);
146
147         SourceManager sm ( diag, fm);
148         HeaderSearch *headers = new HeaderSearch(fm);
149         CompilerInvocation::setLangDefaults(lang, IK_ObjCXX);
150
151         HeaderSearchOptions hsopts;
152         hsopts.ResourceDir=LLVM_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
153         hsopts.AddPath("/home/petr/Dokumenty/BOOST/boost_1_44_0",
154                         clang::frontend::Angled,
155                         false,
156                         false,
157                         false);
158         TargetOptions to;
159         to.Triple = llvm::sys::getHostTriple();
160         TargetInfo *ti = TargetInfo::CreateTargetInfo(diag, to);
161         clang::ApplyHeaderSearchOptions(
162         *headers,
163         hsopts,
164         lang,
165         ti->getTriple());
166         Preprocessor pp(diag, lang, *ti, sm, *headers);
167         FrontendOptions f;
168         PreprocessorOptions ppio;
169         InitializePreprocessor(pp, ppio,hsopts,f);
170         const FileEntry *file = fm.getFile("test.cpp");
171         sm.createMainFileID(file);
172         IdentifierTable tab(lang);
173         SelectorTable sel;
174         Builtin::Context builtins(*ti);
175         FindStates c;
176         ASTContext ctx(lang, sm, *ti, tab, sel, builtins,0);
177         tdp->BeginSourceFile(lang, &pp);
178         ParseAST(pp, &c, ctx, false, false);
179         tdp->EndSourceFile();
180         return 0;
181
182 }