C++ coding rules checker...

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

C++ coding rules checker...

Fons Rademakers
Hi,

   we use in our C++ project a set of coding rules, like:

- class names have to start with T
- POD structs have to end with _t
- data members have to start with f followed by upper case letter
- static data members have to start with fg followed by upper case letter
- ...

Currently we use a third party code checker written in java, with each rule
written as a small java class. The parser is not a full C++ parser, etc.
Anyway, this seems like an ideal problem where clang/llvm could be the
solution. Has somebody already developed such a checker using Clang? If yes
can we use and modify it? If not, what would be the best way to build such
a code checker, starting with adding rules to the clang-analyzer, as it has
the interface we would like too (drop-in in the Makefile and outputting
html files)?

Cheers, Fons.

--
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: [hidden email]              Phone: +41 22 7679248
WWW:    http://fons.rademakers.org           Fax:   +41 22 7669640
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: C++ coding rules checker...

Nick Lewycky
On 23 June 2010 09:36, Fons Rademakers <[hidden email]> wrote:
Hi,

  we use in our C++ project a set of coding rules, like:

- class names have to start with T
- POD structs have to end with _t
- data members have to start with f followed by upper case letter
- static data members have to start with fg followed by upper case letter
- ...

Currently we use a third party code checker written in java, with each rule
written as a small java class. The parser is not a full C++ parser, etc.
Anyway, this seems like an ideal problem where clang/llvm could be the
solution. Has somebody already developed such a checker using Clang? If yes
can we use and modify it? If not, what would be the best way to build such
a code checker, starting with adding rules to the clang-analyzer, as it has
the interface we would like too (drop-in in the Makefile and outputting
html files)?

This sounds like it should be possible today. The convention checker in lib/Checker/LLVMConventionsChecker.cpp which may give you some ideas.

Nick
 

Cheers, Fons.

--
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: [hidden email]              Phone: +41 22 7679248
WWW:    http://fons.rademakers.org           Fax:   +41 22 7669640
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: C++ coding rules checker...

Ted Kremenek
In reply to this post by Fons Rademakers

On Jun 23, 2010, at 9:36 AM, Fons Rademakers wrote:

> Hi,
>
>   we use in our C++ project a set of coding rules, like:
>
> - class names have to start with T
> - POD structs have to end with _t
> - data members have to start with f followed by upper case letter
> - static data members have to start with fg followed by upper case letter
> - ...
>
> Currently we use a third party code checker written in java, with each rule
> written as a small java class. The parser is not a full C++ parser, etc.
> Anyway, this seems like an ideal problem where clang/llvm could be the
> solution. Has somebody already developed such a checker using Clang? If yes
> can we use and modify it? If not, what would be the best way to build such
> a code checker, starting with adding rules to the clang-analyzer, as it has
> the interface we would like too (drop-in in the Makefile and outputting
> html files)?
>
> Cheers, Fons.

Hi Fons,

The analyzer doesn't support drop-in plugins yet for user-defined checks, but this is easy to add if you feel comfortable modifying the Clang codebase directly.

For a good example, take a look at LLVMConventionsChecker.cpp in lib/Checker.  It contains some domain-specific checks for checking a handful of coding conventions in the LLVM codebase.  Basically the entry point for the check is here:

  void clang::CheckLLVMConventions(TranslationUnitDecl &TU, BugReporter &BR) { ... }

That function calls logic that scans the ASTs and pattern-matches for various conventions.  It issues warnings by using the BugReporter API.

To hook up your new check, I'd imitate the logic used to call CheckLLVMConventions:

$ git grep CheckLLVMConventions
include/clang/Checker/Checkers/LocalCheckers.h:void CheckLLVMConventions(TranslationUnitDecl &TU, BugReporter &BR);
lib/Checker/AnalysisConsumer.cpp:  CheckLLVMConventions(TU, BR);
lib/Checker/LLVMConventionsChecker.cpp:void clang::CheckLLVMConventions(TranslationUnitDecl &TU,

You will also need to hook up the driver support:

$ git grep LLVMConvention
Checker/Checkers/LocalCheckers.h:void CheckLLVMConventions(TranslationUnitDecl &TU, BugReporter &BR);
Driver/CC1Options.td:def analysis_LLVMConventionChecker : Flag<"-analyzer-check-llvm-conventions">,
Frontend/Analyses.def:ANALYSIS(LLVMConventionChecker, "analyzer-check-llvm-conventions",

In the future hopefully most of this boilerplate will be unnecessary to add new checks.

Ted
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev