CQLinq

CQLinq

CQLinq is the code query language of CppDepend. It is based on LINQ (Language Integrated Query) and allows you to write queries to explore your codebase, find code smells, and enforce coding standards.

With CQLinq, you can write queries to find types, methods, fields, and other code elements that match specific criteria. You can also write rules that warn you when certain conditions are met.

CQLinq is a powerful tool for code exploration, refactoring, and quality assurance. It is used extensively in CppDepend for all kinds of analysis tasks.

Getting Started with CQLinq

To get started with CQLinq, you can use the Queries and Rules Explorer panel in CppDepend. This panel contains a list of predefined queries and rules that you can run against your codebase.

You can also write your own queries using the CQLinq Editor. The editor provides syntax highlighting, code completion, and error checking to help you write queries quickly and accurately.

CQLinq queries are written in a syntax similar to SQL, but with C#-style syntax. Here is an example of a simple CQLinq query:

from m in Application.Methods
where m.NbLinesOfCode > 30
select m

This query finds all methods in the application that have more than 30 lines of code.

Query Examples

Here are some examples of CQLinq queries that you can use to explore your codebase:

Find all public methods with more than 5 parameters

from m in Application.Methods
where m.IsPublic && m.NbParameters > 5
select m

Find all types that implement a specific interface

from t in Application.Types
where t.Implement("MyNamespace.IMyInterface")
select t

Find all methods that are never called

from m in Application.Methods
where m.NbMethodsCallingMe == 0 && m.IsPublic
select m

Code Rules

CQLinq can also be used to write code rules. Code rules are queries that generate warnings when certain conditions are met.

To create a code rule, you write a CQLinq query and add a warnif or failif clause at the beginning. Here is an example:

warnif count > 0
from m in Application.Methods
where m.CyclomaticComplexity > 20
select m

This rule will generate a warning for every method that has a cyclomatic complexity greater than 20.

Metrics and Aggregation

CQLinq supports a wide range of metrics that you can use in your queries. These include code metrics like lines of code, cyclomatic complexity, and depth of inheritance, as well as dependency metrics like afferent and efferent coupling.

You can also use aggregation functions in your queries to compute statistics across your codebase. Here is an example:

from m in Application.Methods
select new { m, m.NbLinesOfCode, m.CyclomaticComplexity }

Try CppDepend Today

Start your 14-day free trial with full access to all documentation features. No credit card required.