[llvm-dev] Creating a clang-tidy const position check

Aaron Ballman via llvm-dev llvm-dev at lists.llvm.org
Thu Sep 22 07:21:05 PDT 2016


On Wed, Sep 21, 2016 at 8:17 PM, Matthias Vallentin via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Thanks for your help, Piotr and Aaraon. I got further with my match
> expression, which now looks like this:
>
>     void ConstPositionCheck::registerMatchers(MatchFinder *Finder) {
>       auto const_qualified = hasType(
>           anyOf(
>             references(qualType(isConstQualified())),
>             pointsTo(qualType(isConstQualified())),
>             qualType(isConstQualified())));
>       Finder->addMatcher(valueDecl(const_qualified).bind("value"), this);
>       Finder->addMatcher(typedefDecl(const_qualified).bind("typedef"), this);
>       Finder->addMatcher(typeAliasDecl(const_qualified).bind("alias"), this);
>       Finder->addMatcher(templateTypeParmDecl().bind("template"), this);
>       Finder->addMatcher(nonTypeTemplateParmDecl().bind("nontemplate"), this);
>     }
>
> The idea is to catch references, pointers, and raw types with a const
> qualification. For the templateTypeParmDecl(), I'll be looking at the
> type obtained via getDefaultArgument. Similarly, for
> nonTypeTemplateParmDecl I'll inspect the type itself. It seems to work
> in clang-query, i.e., if I type
>
>     let cq hasType(anyOf(...))
>
> and then play with match valueDecl(cq), I get the results I want.
> However, the above C++ code doesn't compile yet:
>
>     ../extra/clang-tidy/misc/ConstPositionCheck.cpp:23:26: error: call to 'hasType' is ambiguous
>       auto const_qualified = hasType(
>                              ^~~~~~~
>
> I tried to debug this error but the macro magic discouraged me from
> diving deeper. What's the correct way to share the hasType(..) match
> expression between matchers?
>
> Also, if you can think of any other match expressions that I'm
> forgetting, please let me know.

The C++ DSL is more picky about type information than the clang-query
dynamic matchers. The problem here is that anyOf() is not return a
specific enough type for hasType() to handle, so hasType() is an
ambiguous call. You should be able to do something like this instead:
hasType(qualType(anyOf(...))).

~Aaron


>
>     Matthias
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


More information about the llvm-dev mailing list