]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blob - src/visualizer.cpp
eda81974df4424f8d3c7f3481a95539bdf9fc656
[boost-statechart-viewer.git] / src / visualizer.cpp
1 /** @file */
2 ////////////////////////////////////////////////////////////////////////////////////////
3 //
4 //    This file is part of Boost Statechart Viewer.
5 //
6 //    Boost Statechart Viewer is free software: you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation, either version 3 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Boost Statechart Viewer is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License
17 //    along with Boost Statechart Viewer.  If not, see <http://www.gnu.org/licenses/>.
18 //
19 ////////////////////////////////////////////////////////////////////////////////////////
20
21 //standard header files
22 #include <iomanip>
23 #include <fstream>
24 #include <map>
25
26 //LLVM Header files
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/raw_os_ostream.h"
29
30 //clang header files
31 #include "clang/AST/ASTConsumer.h"
32 #include "clang/AST/ASTContext.h"
33 #include "clang/AST/CXXInheritance.h"
34 #include "clang/AST/RecursiveASTVisitor.h"
35 #include "clang/Frontend/CompilerInstance.h"
36 #include "clang/Frontend/FrontendPluginRegistry.h"
37
38 using namespace clang;
39 using namespace std;
40
41 namespace Model
42 {
43
44     inline int getIndentLevelIdx() {
45         static int i = ios_base::xalloc();
46         return i;
47     }
48
49     ostream& indent(ostream& os) { os << setw(2*os.iword(getIndentLevelIdx())) << ""; return os; }
50     ostream& indent_inc(ostream& os) { os.iword(getIndentLevelIdx())++; return os; }
51     ostream& indent_dec(ostream& os) { os.iword(getIndentLevelIdx())--; return os; }
52
53     class State;
54
55     class Context : public map<string, State*> {
56     public:
57         iterator add(State *state);
58         Context *findContext(const string &name);
59     };
60
61     class State : public Context
62     {
63         string initialInnerState;
64         list<string> defferedEvents;
65     public:
66         const string name;
67         bool noTypedef;
68         explicit State(string name) : name(name), noTypedef(false) {}
69         void setInitialInnerState(string name) { initialInnerState = name; }
70         void addDeferredEvent(const string &name) { defferedEvents.push_back(name); }
71         void setNoTypedef() { noTypedef = true;}
72         friend ostream& operator<<(ostream& os, const State& s);
73     };
74
75
76     Context::iterator Context::add(State *state)
77     {
78         pair<iterator, bool> ret =  insert(value_type(state->name, state));
79         return ret.first;
80     }
81
82     Context *Context::findContext(const string &name)
83     {
84         iterator i = find(name), e;
85         if (i != end())
86             return i->second;
87         for (i = begin(), e = end(); i != e; ++i) {
88             Context *c = i->second->findContext(name);
89             if (c)
90                 return c;
91         }
92         return 0;
93     }
94
95     ostream& operator<<(ostream& os, const Context& c);
96
97     ostream& operator<<(ostream& os, const State& s)
98     {
99         string label = s.name;
100         for (list<string>::const_iterator i = s.defferedEvents.begin(), e = s.defferedEvents.end(); i != e; ++i)
101             label.append("<br />").append(*i).append(" / defer");
102         if(s.noTypedef) os << indent << s.name << " [label=<" << label << ">, color=\"red\"]\n";
103         else os << indent << s.name << " [label=<" << label << ">]\n";
104         if (s.size()) {
105             os << indent << s.name << " -> " << s.initialInnerState << " [style = dashed]\n";
106             os << indent << "subgraph cluster_" << s.name << " {\n" << indent_inc;
107             os << indent << "label = \"" << s.name << "\"\n";
108             os << indent << s.initialInnerState << " [peripheries=2]\n";
109             os << static_cast<Context>(s);
110             os << indent_dec << indent << "}\n";
111         }
112         return os;
113     }
114
115
116     ostream& operator<<(ostream& os, const Context& c)
117     {
118         for (Context::const_iterator i = c.begin(), e = c.end(); i != e; i++) {
119             os << *i->second;
120         }
121         return os;
122     }
123
124
125     class Transition
126     {
127     public:
128         const string src, dst, event;
129         Transition(string src, string dst, string event) : src(src), dst(dst), event(event) {}
130     };
131
132     ostream& operator<<(ostream& os, const Transition& t)
133     {
134         os << indent << t.src << " -> " << t.dst << " [label = \"" << t.event << "\"]\n";
135         return os;
136     }
137
138
139     class Machine : public Context
140     {
141     protected:
142         string initial_state;
143     public:
144         const string name;
145         explicit Machine(string name) : name(name) {}
146
147         void setInitialState(string name) { initial_state = name; }
148
149         friend ostream& operator<<(ostream& os, const Machine& m);
150     };
151
152     ostream& operator<<(ostream& os, const Machine& m)
153     {
154         os << indent << "subgraph " << m.name << " {\n" << indent_inc;
155         os << indent << m.initial_state << " [peripheries=2]\n";
156         os << static_cast<Context>(m);
157         os << indent_dec << indent << "}\n";
158         return os;
159     }
160
161
162     class Model : public map<string, Machine>
163     {
164         Context undefined;      // For forward-declared state classes
165     public:
166         list< Transition*> transitions;
167
168         iterator add(const Machine &m)
169         {
170             pair<iterator, bool> ret =  insert(value_type(m.name, m));
171             return ret.first;
172         }
173
174         void addUndefinedState(State *m)
175         {
176             undefined[m->name] = m;
177         }
178
179
180         Context *findContext(const string &name)
181         {
182             Context::iterator ci = undefined.find(name);
183             if (ci != undefined.end())
184                 return ci->second;
185             iterator i = find(name), e;
186             if (i != end())
187                 return &i->second;
188             for (i = begin(), e = end(); i != e; ++i) {
189                 Context *c = i->second.findContext(name);
190                 if (c)
191                     return c;
192             }
193             return 0;
194         }
195
196         State *findState(const string &name)
197         {
198             for (iterator i = begin(), e = end(); i != e; ++i) {
199                 Context *c = i->second.findContext(name);
200                 if (c)
201                     return static_cast<State*>(c);
202             }
203             return 0;
204         }
205
206
207         State *removeFromUndefinedContexts(const string &name)
208         {
209             Context::iterator ci = undefined.find(name);
210             if (ci == undefined.end())
211                 return 0;
212             undefined.erase(ci);
213             return ci->second;
214         }
215
216         void write_as_dot_file(string fn)
217         {
218             ofstream f(fn.c_str());
219             f << "digraph statecharts {\n" << indent_inc;
220             for (iterator i = begin(), e = end(); i != e; i++)
221                 f << i->second;
222             for (list<Transition*>::iterator t = transitions.begin(), e = transitions.end(); t != e; ++t)
223                 f << **t;
224             f << indent_dec << "}\n";
225         }
226     };
227 };
228
229
230 class MyCXXRecordDecl : public CXXRecordDecl
231 {
232     static bool FindBaseClassString(const CXXBaseSpecifier *Specifier,
233                                     CXXBasePath &Path,
234                                     void *qualName)
235     {
236         string qn(static_cast<const char*>(qualName));
237         const RecordType *rt = Specifier->getType()->getAs<RecordType>();
238         assert(rt);
239         TagDecl *canon = rt->getDecl()->getCanonicalDecl();
240         return canon->getQualifiedNameAsString() == qn;
241     }
242
243 public:
244     bool isDerivedFrom(const char *baseStr, CXXBaseSpecifier const **Base = 0) const {
245         CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/!!Base, /*DetectVirtual=*/false);
246         Paths.setOrigin(const_cast<MyCXXRecordDecl*>(this));
247         if (!lookupInBases(&FindBaseClassString, const_cast<char*>(baseStr), Paths))
248             return false;
249         if (Base)
250             *Base = Paths.front().back().Base;
251         return true;
252     }
253 };
254
255 class FindTransitVisitor : public RecursiveASTVisitor<FindTransitVisitor>
256 {
257     Model::Model &model;
258     const CXXRecordDecl *SrcState;
259     const Type *EventType;
260 public:
261     explicit FindTransitVisitor(Model::Model &model, const CXXRecordDecl *SrcState, const Type *EventType)
262         : model(model), SrcState(SrcState), EventType(EventType) {}
263
264     bool VisitMemberExpr(MemberExpr *E) {
265         if (E->getMemberNameInfo().getAsString() != "transit")
266             return true;
267         if (E->hasExplicitTemplateArgs()) {
268             const Type *DstStateType = E->getExplicitTemplateArgs()[0].getArgument().getAsType().getTypePtr();
269             CXXRecordDecl *DstState = DstStateType->getAsCXXRecordDecl();
270             CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
271             Model::Transition *T = new Model::Transition(SrcState->getName(), DstState->getName(), Event->getName());
272             model.transitions.push_back(T);
273         }
274         return true;
275     }
276 };
277
278 class Visitor : public RecursiveASTVisitor<Visitor>
279 {
280     ASTContext *ASTCtx;
281     Model::Model &model;
282     DiagnosticsEngine &Diags;
283     unsigned diag_unhandled_reaction_type, diag_unhandled_reaction_decl,
284         diag_found_state, diag_found_statemachine, diag_no_history, diag_warning;
285
286 public:
287     bool shouldVisitTemplateInstantiations() const { return true; }
288
289     explicit Visitor(ASTContext *Context, Model::Model &model, DiagnosticsEngine &Diags)
290         : ASTCtx(Context), model(model), Diags(Diags)
291     {
292         diag_found_statemachine =
293             Diags.getCustomDiagID(DiagnosticsEngine::Note, "Found statemachine '%0'");
294         diag_found_state =
295             Diags.getCustomDiagID(DiagnosticsEngine::Note, "Found state '%0'");
296         diag_unhandled_reaction_type =
297             Diags.getCustomDiagID(DiagnosticsEngine::Error, "Unhandled reaction type '%0'");
298         diag_unhandled_reaction_decl =
299             Diags.getCustomDiagID(DiagnosticsEngine::Error, "Unhandled reaction decl '%0'");
300         diag_unhandled_reaction_decl =
301             Diags.getCustomDiagID(DiagnosticsEngine::Error, "History is not yet supported");
302         diag_warning =
303             Diags.getCustomDiagID(DiagnosticsEngine::Warning, "'%0' %1");
304     }
305
306     DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) { return Diags.Report(Loc, DiagID); }
307
308     void HandleCustomReaction(const CXXRecordDecl *SrcState, const Type *EventType)
309     {
310         IdentifierInfo& II = ASTCtx->Idents.get("react");
311         // TODO: Lookup for react even in base classes - probably by using Sema::LookupQualifiedName()
312         for (DeclContext::lookup_const_result ReactRes = SrcState->lookup(DeclarationName(&II));
313              ReactRes.first != ReactRes.second; ++ReactRes.first) {
314             if (CXXMethodDecl *React = dyn_cast<CXXMethodDecl>(*ReactRes.first)) {
315                 if (React->getNumParams() >= 1) {
316                     const ParmVarDecl *p = React->getParamDecl(0);
317                     const Type *ParmType = p->getType().getTypePtr();
318                     if (ParmType->isLValueReferenceType())
319                         ParmType = dyn_cast<LValueReferenceType>(ParmType)->getPointeeType().getTypePtr();
320                     if (ParmType == EventType)
321                         FindTransitVisitor(model, SrcState, EventType).TraverseStmt(React->getBody());
322                 } else
323                     Diag(React->getLocStart(), diag_warning)
324                         << React << "has not a parameter";
325             } else
326                 Diag((*ReactRes.first)->getSourceRange().getBegin(), diag_warning)
327                     << (*ReactRes.first)->getDeclKindName() << "is not supported as react method";
328         }
329     }
330
331     void HandleReaction(const Type *T, const SourceLocation Loc, CXXRecordDecl *SrcState)
332     {
333         // TODO: Improve Loc tracking
334         if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(T))
335             HandleReaction(ET->getNamedType().getTypePtr(), Loc, SrcState);
336         else if (const TemplateSpecializationType *TST = dyn_cast<TemplateSpecializationType>(T)) {
337             string name = TST->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
338             if (name == "boost::statechart::transition") {
339                 const Type *EventType = TST->getArg(0).getAsType().getTypePtr();
340                 const Type *DstStateType = TST->getArg(1).getAsType().getTypePtr();
341                 CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
342                 CXXRecordDecl *DstState = DstStateType->getAsCXXRecordDecl();
343
344                 Model::Transition *T = new Model::Transition(SrcState->getName(), DstState->getName(), Event->getName());
345                 model.transitions.push_back(T);
346             } else if (name == "boost::statechart::custom_reaction") {
347                 const Type *EventType = TST->getArg(0).getAsType().getTypePtr();
348                 HandleCustomReaction(SrcState, EventType);
349             } else if (name == "boost::statechart::deferral") {
350                 const Type *EventType = TST->getArg(0).getAsType().getTypePtr();
351                 CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
352
353                 Model::State *s = model.findState(SrcState->getName());
354                 assert(s);
355                 s->addDeferredEvent(Event->getName());
356             } else if (name == "boost::mpl::list") {
357                 for (TemplateSpecializationType::iterator Arg = TST->begin(), End = TST->end(); Arg != End; ++Arg)
358                     HandleReaction(Arg->getAsType().getTypePtr(), Loc, SrcState);
359             } else
360                 Diag(Loc, diag_unhandled_reaction_type) << name;
361         } else
362             Diag(Loc, diag_unhandled_reaction_type) << T->getTypeClassName();
363     }
364
365     void HandleReaction(const NamedDecl *Decl, CXXRecordDecl *SrcState)
366     {
367         if (const TypedefDecl *r = dyn_cast<TypedefDecl>(Decl))
368             HandleReaction(r->getCanonicalDecl()->getUnderlyingType().getTypePtr(),
369                            r->getLocStart(), SrcState);
370         else
371             Diag(Decl->getLocation(), diag_unhandled_reaction_decl) << Decl->getDeclKindName();
372     }
373
374     TemplateArgumentLoc getTemplateArgLoc(const TypeLoc &T, unsigned ArgNum)
375     {
376         if (const ElaboratedTypeLoc *ET = dyn_cast<ElaboratedTypeLoc>(&T))
377             return getTemplateArgLoc(ET->getNamedTypeLoc(), ArgNum);
378         else if (const TemplateSpecializationTypeLoc *TST = dyn_cast<TemplateSpecializationTypeLoc>(&T)) {
379             if (TST->getNumArgs() >= ArgNum+1) {
380                 return TST->getArgLoc(ArgNum);
381             } else
382                 if(ArgNum!=2)
383                     Diag(TST->getBeginLoc(), diag_warning) << TST->getType()->getTypeClassName() << "has not enough arguments" << TST->getSourceRange();
384         } else
385             Diag(T.getBeginLoc(), diag_warning) << T.getType()->getTypeClassName() << "type as template argument is not supported" << T.getSourceRange();
386         return TemplateArgumentLoc();
387     }
388
389     TemplateArgumentLoc getTemplateArgLocOfBase(const CXXBaseSpecifier *Base, unsigned ArgNum) {
390         return getTemplateArgLoc(Base->getTypeSourceInfo()->getTypeLoc(), ArgNum);
391     }
392
393     CXXRecordDecl *getTemplateArgDeclOfBase(const CXXBaseSpecifier *Base, unsigned ArgNum, TemplateArgumentLoc &Loc) {
394         Loc = getTemplateArgLocOfBase(Base, ArgNum);
395         switch (Loc.getArgument().getKind()) {
396         case TemplateArgument::Type:
397             return Loc.getTypeSourceInfo()->getType()->getAsCXXRecordDecl();
398         case TemplateArgument::Null:
399             // Diag() was already called
400             break;
401         default:
402             Diag(Loc.getSourceRange().getBegin(), diag_warning) << Loc.getArgument().getKind() << "unsupported kind" << Loc.getSourceRange();
403         }
404         return 0;
405     }
406
407     CXXRecordDecl *getTemplateArgDeclOfBase(const CXXBaseSpecifier *Base, unsigned ArgNum) {
408         TemplateArgumentLoc Loc;
409         return getTemplateArgDeclOfBase(Base, ArgNum, Loc);
410     }
411
412     void handleSimpleState(CXXRecordDecl *RecordDecl, const CXXBaseSpecifier *Base)
413     {
414         int typedef_num = 0;
415         string name(RecordDecl->getName()); //getQualifiedNameAsString());
416         printf("stav %s\n", RecordDecl->getName());
417         Diag(RecordDecl->getLocStart(), diag_found_state) << name;
418
419         Model::State *state;
420         // Either we saw a reference to forward declared state
421         // before, or we create a new state.
422         if (!(state = model.removeFromUndefinedContexts(name)))
423             state = new Model::State(name);
424
425         CXXRecordDecl *Context = getTemplateArgDeclOfBase(Base, 1);
426         if (Context) {
427             Model::Context *c = model.findContext(Context->getName());
428             if (!c) {
429                 Model::State *s = new Model::State(Context->getName());
430                 model.addUndefinedState(s);
431                 c = s;
432             }
433             c->add(state);
434         }
435         //TODO support more innitial states
436         TemplateArgumentLoc Loc;
437         if (MyCXXRecordDecl *InnerInitialState =
438             static_cast<MyCXXRecordDecl*>(getTemplateArgDeclOfBase(Base, 2, Loc))) {
439               if (InnerInitialState->isDerivedFrom("boost::statechart::simple_state") ||
440                 InnerInitialState->isDerivedFrom("boost::statechart::state_machine")) {
441                   state->setInitialInnerState(InnerInitialState->getName());
442             }
443             else
444                 Diag(Loc.getTypeSourceInfo()->getTypeLoc().getLocStart(), diag_warning)
445                     << InnerInitialState->getName() << " as inner initial state is not supported" << Loc.getSourceRange();
446         }
447
448 //          if (CXXRecordDecl *History = getTemplateArgDecl(Base->getType().getTypePtr(), 3))
449 //              Diag(History->getLocStart(), diag_no_history);
450
451         IdentifierInfo& II = ASTCtx->Idents.get("reactions");
452         // TODO: Lookup for reactions even in base classes - probably by using Sema::LookupQualifiedName()
453         for (DeclContext::lookup_result Reactions = RecordDecl->lookup(DeclarationName(&II));
454              Reactions.first != Reactions.second; ++Reactions.first, typedef_num++)
455             HandleReaction(*Reactions.first, RecordDecl);
456         if(typedef_num == 0) {
457             Diag(RecordDecl->getLocStart(), diag_warning)
458                 << " missing typedef for reactions in state : " << RecordDecl->getName();
459             state->setNoTypedef();
460         }
461     }
462
463     void handleStateMachine(CXXRecordDecl *RecordDecl, const CXXBaseSpecifier *Base)
464     {
465         Model::Machine m(RecordDecl->getName());
466         Diag(RecordDecl->getLocStart(), diag_found_statemachine) << m.name;
467
468         if (MyCXXRecordDecl *InitialState =
469             static_cast<MyCXXRecordDecl*>(getTemplateArgDeclOfBase(Base, 1)))
470             m.setInitialState(InitialState->getName());
471         model.add(m);
472     }
473
474     bool VisitCXXRecordDecl(CXXRecordDecl *Declaration)
475     {
476         if (!Declaration->isCompleteDefinition())
477             return true;
478         if (Declaration->getQualifiedNameAsString() == "boost::statechart::state" ||
479             Declaration->getQualifiedNameAsString() == "TimedState" ||
480             Declaration->getQualifiedNameAsString() == "TimedSimpleState")
481             return true; // This is an "abstract class" not a real state
482
483         MyCXXRecordDecl *RecordDecl = static_cast<MyCXXRecordDecl*>(Declaration);
484         const CXXBaseSpecifier *Base;
485
486         if (RecordDecl->isDerivedFrom("boost::statechart::simple_state", &Base))
487             handleSimpleState(RecordDecl, Base);
488         else if (RecordDecl->isDerivedFrom("boost::statechart::state_machine", &Base))
489             handleStateMachine(RecordDecl, Base);
490         else if (RecordDecl->isDerivedFrom("boost::statechart::event"))
491         {
492             //sc.events.push_back(RecordDecl->getNameAsString());
493         }
494         return true;
495     }
496 };
497
498
499 class VisualizeStatechartConsumer : public clang::ASTConsumer
500 {
501     Model::Model model;
502     Visitor visitor;
503     string destFileName;
504 public:
505     explicit VisualizeStatechartConsumer(ASTContext *Context, std::string destFileName,
506                                          DiagnosticsEngine &D)
507         : visitor(Context, model, D), destFileName(destFileName) {}
508
509     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
510         visitor.TraverseDecl(Context.getTranslationUnitDecl());
511         model.write_as_dot_file(destFileName);
512     }
513 };
514
515 class VisualizeStatechartAction : public PluginASTAction
516 {
517 protected:
518   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
519     size_t dot = getCurrentFile().find_last_of('.');
520     std::string dest = getCurrentFile().substr(0, dot);
521     dest.append(".dot");
522     return new VisualizeStatechartConsumer(&CI.getASTContext(), dest, CI.getDiagnostics());
523   }
524
525   bool ParseArgs(const CompilerInstance &CI,
526                  const std::vector<std::string>& args) {
527     for (unsigned i = 0, e = args.size(); i != e; ++i) {
528       llvm::errs() << "Visualizer arg = " << args[i] << "\n";
529
530       // Example error handling.
531       if (args[i] == "-an-error") {
532         DiagnosticsEngine &D = CI.getDiagnostics();
533         unsigned DiagID = D.getCustomDiagID(
534           DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
535         D.Report(DiagID);
536         return false;
537       }
538     }
539     if (args.size() && args[0] == "help")
540       PrintHelp(llvm::errs());
541
542     return true;
543   }
544   void PrintHelp(llvm::raw_ostream& ros) {
545     ros << "Help for Visualize Statechart plugin goes here\n";
546   }
547
548 };
549
550 static FrontendPluginRegistry::Add<VisualizeStatechartAction> X("visualize-statechart", "visualize statechart");
551
552 // Local Variables:
553 // c-basic-offset: 4
554 // End: