[cfe-dev] C++ coding rules checker...
Ted Kremenek
kremenek at apple.com
Wed Jun 23 11:51:48 PDT 2010
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
More information about the cfe-dev
mailing list