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

Aaron Ballman via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 21 14:30:58 PDT 2016

On Wed, Sep 21, 2016 at 3:48 PM, Matthias Vallentin via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I'm in the process of writing a clang-tidy check that concerns the
> position of the "const" keyword. The check should either enforce "T
> const" or "const T", based on a flag. Naturally, this should extend to
> all sorts of variations and qualifiers, e.g., X<const T&, Y const*>
> const&.
> My approach is to first find the right AST matcher expression to flag
> style violations, and then use the lexer (getLocStart/End) to correct
> them [1]. Does that sound reasonable? There's also
> Qualifiers::removeConst and Qualifiers::addConst, but I'm not sure how
> it affects the position of const.
> I have trouble with finding all const-qualified types. I looked at
> misc-misplaced-const for inspiration, which contains this matcher:
>     valueDecl(hasType(isConstQualified()))
> Why doesn't this yield a match? My test code has this form:
>     template <class T>
>     struct type {};
>     template <class T>
>     void f(const type<const double>&, const T&) {
>     }
>     using alias = const int&;
> I've tested this expression with clang-query (master branch). Any help
> that steers me in the right direction would be much appreciated.

In all three cases, you do not have a const qualified type, you have a
reference to a const qualified type. You could try something like
valueDecl(hasType(references(qualType(isConstQualified())))). Note
that the type alias will not be matched regardless because it is not a


>     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