Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

>
>
SQL-like queries for C++ code: is this …

SQL-like queries for C++ code: is this the task for static analysis?

Nov 21 2012

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".

Popular related articles


Comments (0)

Next comments next comments
close comment form