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

Matthias Vallentin via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 21 17:17:54 PDT 2016


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.

    Matthias


More information about the llvm-dev mailing list