Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
SQL-like queries for C++ code: is this …

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

21 Nov 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

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam