SQL-like queries for C++ code: is this the task for static analysis?
Static analysis tools' users often wonder how to fulfill the task of searching for certain code fragments. For instance, how to find a function longer than 1000 lines; or how to find a class containing more than 100 methods; or which functions contain the largest (or the smallest) number of comments. Why do they want to know it?
There are different reasons for that:
- Choosing methods and classes for further code refactoring.
- Search of poorly documented or, vice versa, overdocumented functions.
- Analyzing the changes of the project's statistical characteristics: the number of methods, classes, files.
At first sight, all these tasks seem to be the right target for static analysis: it's the static code analysis technology that can parse the whole code into smallest "bricks".
However, I suppose that static code analysis is intended for detecting errors in software, first of all. To be more exact, to detect fragments in program code to which the programmer should pay more attention to probably find an error. Although static analysis tools can answer SQL-like queries, they probably don't have to: there are tools intended for this particular purpose. For example, the CppDepend tool employs a special query language CQLinq (stands for "Code Query Linq"). As you can see from its name, this language (very much similar to Microsoft LINQ) allows you to create queries to the code base being analyzed.
Which public methods have size larger than 30 lines:
from m in Application.Methods where m.NbLinesOfCode > 30 && m.IsPublic select m
Which classes inherit from a concrete class:
particular from t in Types where t.IsClass && t.DeriveFrom ("CBase") select t
Which complex methods are poorly commented:
from t in Types where t.IsClass && t.DeriveFrom ("CBase") select t
You can find a lot of examples of queries like the above mentioned on the webpage with the CQLinq description. They all are quite transparent and comprehensible. That's why when somebody asks me if static analysis can be used to find "methods that contain ...", I answer at once: "Static analysis is not quite the right thing to fulfill such tasks. You'd better try CQLinq in CppDepend".