[cfe-dev] clang-tidy or static analyzer or ...

Stephen Kelly via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 10 15:52:40 PDT 2019


On 10/09/2019 22:47, Billy O'Mahony via cfe-dev wrote:
> Hi Artem,
> 
> (or anyone else :D ).
> 
> I managed to create a dummy clang-tidy check and run it.
> 
> For the tests I want to write I've decided the easiest first step is to 
> just check for more than one return statement in my functions (this will 
> only be applying to specific functions all with a common prefix e.g 
> "api_foo".
> 
> So I managed to write a matcher which seems to work ok.
>      Finder->addMatcher(returnStmt().bind("x"), this);
> 
> And get it to print a warning for each rtn statement in MyCheck::check:
>      const auto *MatchedDecl = Result.Nodes.getNodeAs<ReturnStmt>("x");
>      diag(MatchedDecl->getReturnLoc(), "found rtn stmt");
> 
> So next I want to be able to find the name of the function that contains 
> the returnStatement but I can't figure out how to do this.

.
  Finder->addMatcher(
     returnStmt(
         forFunction(functionDecl().bind("func"))
         ).bind("returnStmt"),
     this);


Your `check` function will then be called once for each return statement 
and you can extract both bound nodes.

> 
> Or maybe I should start with a FunctionDecl matcher that matches fns 
> named (api_...) and then somehow traverse that function's AST and count 
> the returnStmt's. ?

If you wish to do counting, then traversing from the Function might 
indeed make more sense.

> I've found a few examples of clang-tidy checks on-line I think this one 
> https://bbannier.github.io/blog/2015/05/02/Writing-a-basic-clang-static-analysis-check.html 
> is the best. But recommendations for better resources also appreciated.


I linked to some resources I wrote here which you might find useful:

 
https://steveire.wordpress.com/2018/11/11/future-developments-in-clang-query/

You can also see some of the other posts on my blog.

Thanks,

Stephen.




More information about the cfe-dev mailing list