1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
/* This file is part of KDevelop
Copyright (C) 2003 Roberto Raggi <[email protected]>
Copyright (C) 2003-2004 Alexander Dymo <[email protected]>
Copyright (C) 2004 Jonas Jacobi<[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CODEMODEL_UTILS_H
#define CODEMODEL_UTILS_H
#include "codemodel.h"
/**
@file codemodel_utils.h
Utility functions and classes for the CodeModel.
*/
/**
@class Pred
The predicate.
Pred is not a real class, it is only a template parameter used in @ref CodeModelUtils functions.
<b>How to create the predicate:</b>@n
Predicate is simply a class that have
@code bool operator() (predicateArgument) @endcode.
The return value of that operator is the result of a predicate.
For example we want to find all function definitions with a particular name.
We can use @ref CodeModelUtils::findFunctionDefinitions functions that require
you to write a predicate for function definition DOM's.
This can be done with following code:
@code
class MyPred{
public:
MyPred(const QString &name): m_name(name) {}
bool operator() (const FunctionDefinitionDom &def) const
{
return def->name() == m_name;
}
private:
QString m_name;
};
@endcode
*/
/**Namespace which contains utility functions and classes for the CodeModel.*/
namespace CodeModelUtils
{
/**Finds function definitions which match given predicate in files.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param fileList The list of files to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const FileList& fileList, FunctionDefinitionList & lst );
/**Finds function definitions which match given predicate in the namespace.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param ns The namespace to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const NamespaceDom& ns, FunctionDefinitionList & lst );
/**Finds function definitions which match given predicate in namespaces.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param namespaceList The list of namespaces to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const NamespaceList& namespaceList, FunctionDefinitionList & lst );
/**Finds function definitions which match given predicate in classes.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param classList The list of classes to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const ClassList& classList, FunctionDefinitionList & lst );
/**Finds function definitions which match given predicate in the list of function definitions.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param functionDefinitionList The list of function definitions to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const FunctionDefinitionList& functionDefinitionList, FunctionDefinitionList & lst );
/**Finds function definitions which match given predicate in the class.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param klass The class to find function definitions in.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const ClassDom& klass, FunctionDefinitionList & lst );
/**Applies a predicate to a function definition.
Predicate can be considered as a condition. If it is true then the function definition is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function definition before it is returned.
@param fun The function definition.
@param lst The reference to a list of function definitions. Will be filled by this function.*/
template <class Pred> void findFunctionDefinitions( Pred pred, const FunctionDefinitionDom& fun, FunctionDefinitionList & lst );
/**Finds function declarations which match given predicate in files.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param fileList The list of files to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const FileList& fileList, FunctionList & lst );
/**Finds function declarations which match given predicate in the namespace.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param ns The namespace to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const NamespaceDom& ns, FunctionList & lst );
/**Finds function declarations which match given predicate in namespaces.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param namespaceList The list of namespaces to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const NamespaceList& namespaceList, FunctionList & lst );
/**Finds function declarations which match given predicate in classes.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param classList The list of classes to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const ClassList& classList, FunctionList & lst );
/**Finds function declarations which match given predicate in the list of function declarations.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param functionList The list of function declarations to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const FunctionList& functionList, FunctionList & lst );
/**Finds function declarations which match given predicate in the class.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param klass The class to find function declarations in.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const ClassDom& klass, FunctionList & lst );
/**Applies a predicate to a function declaration.
Predicate can be considered as a condition. If it is true then the function declaration is
added to the result list otherwise it is skipped.
@see Pred class documentation for a detailed description on how to create and use predicates.
@param pred Predicate which is applied to a function declaration before it is returned.
@param fun The function declaration.
@param lst The reference to a list of function declarations. Will be filled by this function.*/
template <class Pred> void findFunctionDeclarations( Pred pred, const FunctionDom& fun, FunctionList & lst );
//implementations of function templates defined above:
template <class Pred>
void findFunctionDefinitions( Pred pred, const FileList& fileList, FunctionDefinitionList & lst )
{
for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it )
findFunctionDefinitions( pred, model_cast<NamespaceDom>(*it), lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const NamespaceDom& ns, FunctionDefinitionList & lst )
{
findFunctionDefinitions( pred, ns->namespaceList(), lst );
findFunctionDefinitions( pred, ns->classList(), lst );
findFunctionDefinitions( pred, ns->functionDefinitionList(), lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const NamespaceList& namespaceList, FunctionDefinitionList & lst )
{
for( NamespaceList::ConstIterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
findFunctionDefinitions( pred, *it, lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const ClassList& classList, FunctionDefinitionList & lst )
{
for( ClassList::ConstIterator it=classList.begin(); it!=classList.end(); ++it )
findFunctionDefinitions( pred, *it, lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const FunctionDefinitionList& functionDefinitionList, FunctionDefinitionList & lst )
{
for( FunctionDefinitionList::ConstIterator it=functionDefinitionList.begin(); it!=functionDefinitionList.end(); ++it )
findFunctionDefinitions( pred, *it, lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const ClassDom& klass, FunctionDefinitionList & lst )
{
findFunctionDefinitions( pred, klass->classList(), lst );
findFunctionDefinitions( pred, klass->functionDefinitionList(), lst );
}
template <class Pred>
void findFunctionDefinitions( Pred pred, const FunctionDefinitionDom& fun, FunctionDefinitionList & lst )
{
if( pred(fun) )
lst << fun;
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const FileList& fileList, FunctionList & lst )
{
for( FileList::ConstIterator it=fileList.begin(); it!=fileList.end(); ++it )
findFunctionDeclarations( pred, model_cast<NamespaceDom>(*it), lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const NamespaceDom& ns, FunctionList & lst )
{
findFunctionDeclarations( pred, ns->namespaceList(), lst );
findFunctionDeclarations( pred, ns->classList(), lst );
findFunctionDeclarations( pred, ns->functionList(), lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const NamespaceList& namespaceList, FunctionList & lst )
{
for( NamespaceList::ConstIterator it=namespaceList.begin(); it!=namespaceList.end(); ++it )
findFunctionDeclarations( pred, *it, lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const ClassList& classList, FunctionList & lst )
{
for( ClassList::ConstIterator it=classList.begin(); it!=classList.end(); ++it )
findFunctionDeclarations( pred, *it, lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const FunctionList& functionList, FunctionList & lst )
{
for( FunctionList::ConstIterator it=functionList.begin(); it!=functionList.end(); ++it )
findFunctionDeclarations( pred, *it, lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const ClassDom& klass, FunctionList & lst )
{
findFunctionDeclarations( pred, klass->classList(), lst );
findFunctionDeclarations( pred, klass->functionList(), lst );
}
template <class Pred>
void findFunctionDeclarations( Pred pred, const FunctionDom& fun, FunctionList & lst )
{
if( pred(fun) )
lst << fun;
}
/**A scope.*/
struct Scope{
/**Class.*/
ClassDom klass;
/**Namespace.*/
NamespaceDom ns;
};
/**Information about functions.*/
struct AllFunctions{
/**Scope of functions.*/
QMap<FunctionDom, Scope> relations;
/**List of functions.*/
FunctionList functionList;
};
/**Information about function definitions.*/
struct AllFunctionDefinitions{
/**Scope of function definitions.*/
QMap<FunctionDefinitionDom, Scope> relations;
/**List of function definitions.*/
FunctionDefinitionList functionList;
};
/**Namespace with utilities to find functions in the @ref CodeModel.*/
namespace Functions{
/**Looks for functions in the class.
@param list The list of functions found by this routine.
@param dom The class to look for functions.*/
void processClasses(FunctionList &list, const ClassDom dom);
/**Looks for functions in the namespace.
@param list The list of functions found by this routine.
@param dom The namespace to look for functions.*/
void processNamespaces(FunctionList &list, const NamespaceDom dom);
/**Looks for functions in the class and also saves their scope.
@param list The list of functions found by this routine.
@param dom The class to look for functions.
@param relations The scope information.*/
void processClasses(FunctionList &list, const ClassDom dom, QMap<FunctionDom, Scope> &relations);
/**Looks for functions in the class and also saves their scope.
Used for classes withing a namespace.
@param list The list of functions found by this routine.
@param dom The class to look for functions.
@param relations The scope information.
@param nsdom The namespace which contains a class.*/
void processClasses(FunctionList &list, const ClassDom dom, QMap<FunctionDom, Scope> &relations, const NamespaceDom &nsdom);
/**Looks for functions in the namespace and also saves their scope.
@param list The list of functions found by this routine.
@param dom The namespace to look for functions.
@param relations The scope information.*/
void processNamespaces(FunctionList &list, const NamespaceDom dom, QMap<FunctionDom, Scope> &relations);
}
/**Namespace with utilities to find function definitions in the @ref CodeModel.*/
namespace FunctionDefinitions{
/**Looks for function definitions in the class.
@param list The list of function definitions found by this routine.
@param dom The class to look for function definitions.*/
void processClasses(FunctionDefinitionList &list, const ClassDom dom);
/**Looks for function definitions in the namespace.
@param list The list of function definitions found by this routine.
@param dom The namespace to look for function definitions.*/
void processNamespaces(FunctionDefinitionList &list, const NamespaceDom dom);
/**Looks for function definitions in the class and also saves their scope.
@param list The list of function definitions found by this routine.
@param dom The class to look for function definitions.
@param relations The scope information.*/
void processClasses(FunctionDefinitionList &list, const ClassDom dom, QMap<FunctionDefinitionDom, Scope> &relations);
/**Looks for function definitions in the class and also saves their scope.
Used for classes withing a namespace.
@param list The list of function definitions found by this routine.
@param dom The class to look for function definitions .
@param relations The scope information.
@param nsdom The namespace which contains a class.*/
void processClasses(FunctionDefinitionList &list, const ClassDom dom, QMap<FunctionDefinitionDom, Scope> &relations, const NamespaceDom &nsdom);
/**Looks for function definitions in the namespace and also saves their scope.
@param list The list of function definitions found by this routine.
@param dom The namespace to look for function definitions.
@param relations The scope information.*/
void processNamespaces(FunctionDefinitionList &list, const NamespaceDom dom, QMap<FunctionDefinitionDom, Scope> &relations);
}
/**
* Compares a declaration and a defintion of a function.
* @param dec declaration
* @param def definition
* @return true, if dec is the declaration of the function definition def, false otherwise
* @author Jonas Jacobi <[email protected]>
*/
bool compareDeclarationToDefinition(const FunctionDom& dec, const FunctionDefinitionDom& def);
/**
* Compares a declaration and a defintion of a function.
* @param dec declaration
* @param def definition
* @param nsImports namespace imports for the namespace the definition appears in
* @return true, if dec is the declaration of the function definition def, false otherwise
*/
bool compareDeclarationToDefinition(const FunctionDom& dec, const FunctionDefinitionDom& def, const std::set<NamespaceImportModel>& nsImports);
/**
* Predicate for use with findFunctionDefintions. Searches for a defintion matching a declaration.
* @sa Pred documentation to learn more about predicates used with code model.
* @author Jonas Jacobi
*/
class PredDefinitionMatchesDeclaration{
public:
PredDefinitionMatchesDeclaration(const FunctionDom& func) : m_declaration(func){};
bool operator() (const FunctionDefinitionDom& def) const
{
return compareDeclarationToDefinition(m_declaration, def);
}
private:
const FunctionDom m_declaration;
};
template <class InputDomType>
class PredAmOwner{
public:
PredAmOwner(const FileDom& file) : m_file(file){};
bool operator() (const InputDomType& def) const
{
return def->file() == m_file;
}
private:
const FileDom m_file;
};
/**@return A list of all functions in the file.
@param dom File Dom to look for functions in.*/
FunctionList allFunctions(const FileDom &dom);
/**@return A detailed list of all functions in the file (detailed list contains
the information about a scope of each FunctionDom found).
@param dom File Dom to look for functions in.*/
AllFunctions allFunctionsDetailed(const FileDom &dom);
/**@return A detailed list of all function definitions in the file (detailed list contains
the information about a scope of each FunctionDefinitionDom found).
@param dom File Dom to look for functions in.*/
AllFunctionDefinitions allFunctionDefinitionsDetailed(const FileDom &dom);
/**@return A list of all functions in the file.
This version searches the file's whole group for
functions that may have been inserted into the other file's
structure.
Unlike the methods above, this guarantees that all returned
functions physically belong to that file.
@param dom File Dom to look for functions in. */
FunctionList allFunctionsExhaustive(FileDom &dom);
/**@return A list of all function-definitions in the file.
This version searches the file's whole group for
functions that may have been inserted into the other file's
structure.
Unlike the methods above, this guarantees that all returned
functions physically belong to that file.
@param dom File Dom to look for functions in. */
FunctionDefinitionList allFunctionDefinitionsExhaustive(FileDom &dom);
/**
* Finds a class by its position in a file(position inside the part of the file, where the class is declared).
* In the case of nested classes the innermost class which is declared at/around the provided position.
* @param nameSpace A namespace to search for the class.
* @param line A linenumber inside the class declaration.
* @param col The colum of line.
* @return The innermost class, which is declared at/around position defined with line / col, or 0 if no class is found.
* @author Jonas Jacobi <[email protected]>
*/
ClassDom findClassByPosition( NamespaceModel* nameSpace, int line, int col );
/**
* Same as above, just searches inside a class instead of a namespace.
*/
ClassDom findClassByPosition( ClassModel* aClass, int line, int col );
/**
* Finds the last occurrence (line of file wise) of a method inside a class declaration with specific access specificer.
* This can be used e.g. to find a position to new methods to the class.
* @param aClass class to search for method.
* @param access the access specifier with which methods are searched for.
* @return The last line a Method with access specifier access is found,
* or -1 if no method with that access specifier was found.
* @author Jonas Jacobi <[email protected]>
*/
int findLastMethodLine( ClassDom aClass, CodeModelItem::Access access );
/**
* Same as above, but finds a membervariable instead of a method.
*/
int findLastVariableLine( ClassDom aClass, CodeModelItem::Access access );
/**
* Get the string representation of an accesss pecifier
* @param access An access specifier to get a string representation of.
* @return string The representation of an access (e.g. "public").
* @author Jonas Jacobi <[email protected]>
*/
QString accessSpecifierToString( CodeModelItem::Access access );
class CodeModelHelper {
private:
CodeModel* m_model;
FileList m_files;
QString m_fileName;
FunctionDefinitionDom functionDefinitionAt(NamespaceDom ns, int line, int column);
FunctionDefinitionDom functionDefinitionAt(ClassDom klass, int line, int column);
FunctionDefinitionDom functionDefinitionAt(FunctionDefinitionDom fun, int line, int );
FunctionDom functionDeclarationAt(NamespaceDom ns, int line, int column);
FunctionDom functionDeclarationAt(ClassDom klass, int line, int column);
FunctionDom functionDeclarationAt(FunctionDom fun, int line, int column );
ClassDom classAt(NamespaceDom ns, int line, int column);
ClassDom classAt(ClassDom klass, int line, int column);
public:
CodeModelHelper( CodeModel* model, FileDom file );
enum FunctionTypes {
Declaration = 1,
Definition = 2
};
FunctionDom functionAt( int line, int column, FunctionTypes types = (FunctionTypes)3 );
ClassDom classAt( int line, int column );
};
}
#endif
|