2 ////////////////////////////////////////////////////////////////////////////////////////
4 // This file is part of Boost Statechart Viewer.
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.
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.
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/>.
19 ////////////////////////////////////////////////////////////////////////////////////////
24 using namespace clang;
26 string clean_spaces(const string line) /** Remove gaps from string.*/
30 for(i = 0;i<line.length();i++)
32 if(!isspace(line[i])) new_line+=line[i];
38 string cut_commentary(const string line) /** This function cuts commentaries at the end of line. */
41 for(i = 0;i<line.length()-1;i++)
43 if(line[i]=='/' && line[i+1]=='/') return line.substr(0,i);
44 if(line[i]=='/' && line[i+1]=='*') return line.substr(0,i);
49 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.*/
53 for(i = 0;i<code.length();i++)
55 if(code[i]=='\n'||code[i]=='{') break;
57 ret = code.substr(0,i);
58 ret = clean_spaces(ret);
59 return cut_commentary(ret);
62 string get_return(const string code) /** Return return statement. */
66 for(i = 0;i<code.length();i++)
68 if(code[i]==' ') break;
70 return code.substr(0,i);
73 string cut_namespaces(string line) /** Cut namespaces from the declarations. */
75 int i = line.rfind("::");
76 if(i==-1) return line;
77 return line.substr(i+2);
80 bool is_derived(const string line) /** Test whether this struct or class is derived */
83 for(i = 0;i<line.length()-1;i++)
87 if(line[i+1]!=':') return true;
94 string get_super_class(const string line) /** Get the super classes of this declarations. */
97 for(i = 0;i<line.length()-1;i++)
101 if(line[i+1]!=':') break;
105 return line.substr(i+1);
108 string get_next_base(const string line) /** Get next super classes of this declarations Do the first super class is ommitted. */
112 for(i = 0;i<line.length()-1;i++)
114 if(line[i]=='<') brackets+=1;
115 if(line[i]=='>') brackets-=1;
116 if(line[i]==',' && brackets == 0) break;
118 return line.substr(i+1);
121 string get_first_base(const string line) /** Get the first super class of this declarations. */
125 for(i = 0;i<line.length()-1;i++)
127 if(line[i]=='<') brackets+=1;
128 if(line[i]=='>') brackets-=1;
129 if(line[i]==',' && brackets == 0) break;
131 return line.substr(0,i);
134 bool is_state(const string line) /** Test if this declaration is a state. It is used to test the base classes. */
136 if(cut_namespaces(line).compare(0,12,"simple_state")==0)
142 if(cut_namespaces(line).compare(0,5,"state")==0)
150 string cut_type(string line) /** This function cuts type of the declaration from the string. */
153 for(i = 0;i<line.length();i++)
155 if(line[i]==' ') break;
157 return line.substr(i);
160 int count(const string line, const char c) /** Count all specified char in string. So it returns the number of specified characters. */
163 for(unsigned i = 0;i<line.length();i++)
165 if(line[i]==c) number+=1;
170 bool is_list(const string line) /** Test if this decl is mpl::list. */
174 for(unsigned i = 0; i<line.length();i++)
176 if(line[i]=='<') break;
177 if(line[i]==':' && line[i+1]==':') pos = i+2;
179 if(line.substr(pos).compare(0,4,"list")==0)
189 string get_inner_part(const string line) /** Get inner part of the list. */
193 for(i = 0;i<line.length();i++)
195 if(line[i]=='<') break;
197 str = line.substr(i+1);
198 for(i = 0;i<str.length();i++)
200 if(str[i]=='<') pos+=1;
207 return str.substr(0,i);
210 bool test_model(const string line, const string model) /** Test the string to has a specified model. */
212 if(cut_namespaces(line).compare(0,model.length(),model)==0)
222 string get_params(string line) /** Return parameters of the specified transition */
224 int pos_front = line.find("<")+1;
225 int pos_end = line.find(">");
226 return line.substr(pos_front,pos_end-pos_front);
229 string find_states(const CXXRecordDecl *cRecDecl, string line) /** test if the struct/class is he state (must be derived from simple_state or state). */
231 string super_class = get_super_class(line), base, params;
232 if(cRecDecl->getNumBases()>1)
234 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
236 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
237 else base = super_class;
238 if(is_state(super_class))
240 params = get_params(super_class);
244 super_class = get_next_base(super_class);
250 if(is_state(super_class))
252 params = get_params(super_class);
259 string find_name_of_machine(const CXXRecordDecl *cRecDecl, string line) /** Find name of the state machine and the start state. */
261 string super_class = get_super_class(line), base, params;
262 if(cRecDecl->getNumBases()>1)
264 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
266 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
267 else base = super_class;
268 if(test_model(base,"state_machine"))
270 params = get_params(base);
274 super_class = get_next_base(super_class);
280 if(test_model(super_class,"state_machine"))
282 params = get_params(super_class);
288 string find_transitions (const string name_of_state, string line) /** Traverse all methods for finding declarations of transitions. */
290 string dest, params, base, trans;
291 int num = count(line,'<');
297 line = get_inner_part(line);
300 for(int j = 0;j<num;j++)
302 if(j!=num-1) base = get_first_base(line);
304 if(test_model(base,"transition"))
306 dest = name_of_state;
307 params = get_params(base);
308 //cout<<params<<"\n";
311 line = get_next_base(line);
313 if(j+1!=num) trans.append(";");
315 else if(test_model(base,"custom_reaction"))
318 dest.append(name_of_state);
319 params = get_params(base);
320 //cout<<params<<"\n";
323 line = get_next_base(line);
325 if(j+1!=num) trans.append(";");
329 line = get_next_base(line);
332 if(trans[trans.length()-1]==';') return trans.substr(0,trans.length()-1);
336 bool find_events(const CXXRecordDecl *cRecDecl, string line) /** This function provides testing if the decl is decl of event*/
338 string super_class = get_super_class(line), base, params;
339 if(cRecDecl->getNumBases()>1)
341 for(unsigned i = 0; i<cRecDecl->getNumBases();i++ )
343 if(i!=cRecDecl->getNumBases()-1) base = get_first_base(super_class);
344 else base = super_class;
345 if(test_model(base,"event")) return true;
348 super_class = get_next_base(super_class);
354 if(test_model(super_class,"event"))return true;