3 #include "llvm/Support/raw_ostream.h"
4 #include "llvm/System/Host.h"
5 #include "llvm/Config/config.h"
7 #include "clang/Frontend/DiagnosticOptions.h"
8 #include "clang/Frontend/TextDiagnosticPrinter.h"
10 #include "clang/Basic/LangOptions.h"
11 #include "clang/Basic/FileSystemOptions.h"
13 #include "clang/Index/TranslationUnit.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Lex/HeaderSearch.h"
16 #include "clang/Basic/FileManager.h"
18 #include "clang/Frontend/HeaderSearchOptions.h"
19 #include "clang/Frontend/Utils.h"
21 #include "clang/Basic/TargetOptions.h"
22 #include "clang/Basic/TargetInfo.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Frontend/PreprocessorOptions.h"
26 #include "clang/Frontend/FrontendOptions.h"
28 #include "clang/Frontend/CompilerInvocation.h"
30 #include "clang/Basic/IdentifierTable.h"
31 #include "clang/Basic/Builtins.h"
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"
43 #include "clang/Parse/Parser.h"
45 #include "clang/Parse/ParseAST.h"
46 #include "clang/Basic/Version.h"
49 #include "stringoper.h"
51 using namespace clang;
54 class FindStates : public ASTConsumer
60 virtual void Initialize(ASTContext &ctx)//run after the AST is constructed
62 SourceManager &sman = ctx.getSourceManager();
63 fSloc = new FullSourceLoc(loc, sman);
66 virtual void HandleTopLevelDecl(DeclGroupRef DGR)// traverse all top level declarations
69 const SourceManager &sman = fSloc->getManager();
71 std::string super_class, output;
72 llvm::raw_string_ostream x(output);
73 for (DeclGroupRef::iterator i = DGR.begin(), e = DGR.end(); i != e; ++i)
75 const Decl *decl = *i;
76 loc = decl->getLocation();
79 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
80 //std::cout<<decl->getDeclKindName()<<"\n";
81 if(!sman.isInSystemHeader(loc))
83 if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
85 if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class
87 const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
88 line = cut_commentary(clean_spaces(get_line_of_code(sman.getCharacterData(loc))));
91 if(find_states(cRecDecl, line))
93 const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);
94 std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
95 find_transitions(namedDecl->getNameAsString(), declCont);
100 if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
103 DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
104 //declCont->dumpDeclContext();
105 recursive_visit(declCont);
112 void recursive_visit(const DeclContext *declCont) //recursively visit all decls inside namespace
114 const SourceManager &sman = fSloc->getManager();
117 for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
119 const Decl *decl = *i;
120 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
122 //std::cout<<"a "<<decl->getDeclKindName()<<"\n";
123 loc = decl->getLocation();
126 if (const TagDecl *tagDecl = dyn_cast<TagDecl>(decl))
128 if(tagDecl->isStruct() || tagDecl->isClass()) //is it a structure or class
130 const CXXRecordDecl *cRecDecl = dyn_cast<CXXRecordDecl>(decl);
131 line = cut_commentary(clean_spaces(get_line_of_code(sman.getCharacterData(loc))));
134 if(find_states(cRecDecl, line))
136 const DeclContext *declCont = tagDecl->castToDeclContext(tagDecl);
137 std::cout << "New state: " << namedDecl->getNameAsString() << "\n";
138 find_transitions(namedDecl->getNameAsString(), declCont);
143 if(const NamespaceDecl *namespaceDecl = dyn_cast<NamespaceDecl>(decl))
145 DeclContext *declCont = namespaceDecl->castToDeclContext(namespaceDecl);
146 //declCont->dumpDeclContext();
147 recursive_visit(declCont);
152 bool find_states(const CXXRecordDecl *cRecDecl, std::string line)
154 std::string super_class = get_super_class(line), base;
155 if(cRecDecl->getNumBases()>1)
157 for(int i = 0; i<cRecDecl->getNumBases();i++ )
159 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
160 else base = super_class;
161 if(is_state(base)) return true;
164 super_class = get_next_base(super_class);
171 if(is_state(super_class)) return true;
176 void find_transitions (const std::string name_of_state,const DeclContext *declCont) // traverse all methods for finding declarations of transitions
178 std::string output, line, event, dest, params;
179 llvm::raw_string_ostream x(output);
181 for (DeclContext::decl_iterator i = declCont->decls_begin(), e = declCont->decls_end(); i != e; ++i)
183 const Decl *decl = *i;
186 if (const TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(decl))
188 const NamedDecl *namedDecl = dyn_cast<NamedDecl>(decl);
193 line = clean_spaces(cut_typedef(output));
194 params = get_transition_params(line);
195 pos = params.find(",");
196 event = params.substr(0,pos);
197 dest = params.substr(pos+1);
198 if(is_transition(line)) std::cout << "New transition: " << name_of_state<<" -> "<<event<<" -> "<< dest<<"\n";
205 int main(int argc, char *argv[])
207 DiagnosticOptions diagnosticOptions;
208 TextDiagnosticPrinter *tdp = new TextDiagnosticPrinter(llvm::nulls(), diagnosticOptions);
209 llvm::IntrusiveRefCntPtr<DiagnosticIDs> dis(new DiagnosticIDs());
210 Diagnostic diag(dis,tdp);
211 FileSystemOptions fileSysOpt;
215 FileManager fm (fileSysOpt);
217 SourceManager sm ( diag, fm);
218 HeaderSearch *headers = new HeaderSearch(fm);
219 CompilerInvocation::setLangDefaults(lang, IK_ObjCXX);
221 HeaderSearchOptions hsopts;
222 hsopts.ResourceDir=LLVM_PREFIX "/lib/clang/" CLANG_VERSION_STRING;
223 hsopts.AddPath("/home/petr/Dokumenty/BOOST/boost_1_44_0",
224 clang::frontend::Angled,
229 to.Triple = llvm::sys::getHostTriple();
230 TargetInfo *ti = TargetInfo::CreateTargetInfo(diag, to);
231 clang::ApplyHeaderSearchOptions(
236 Preprocessor pp(diag, lang, *ti, sm, *headers);
238 PreprocessorOptions ppio;
239 InitializePreprocessor(pp, ppio,hsopts,f);
240 const FileEntry *file = fm.getFile("test.cpp");
241 sm.createMainFileID(file);
242 IdentifierTable tab(lang);
244 Builtin::Context builtins(*ti);
246 ASTContext ctx(lang, sm, *ti, tab, sel, builtins,0);
247 tdp->BeginSourceFile(lang, &pp);
248 ParseAST(pp, &c, ctx, false, false);
249 tdp->EndSourceFile();