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

Piotr Padlewski via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 21 14:09:42 PDT 2016


2016-09-21 12:48 GMT-07:00 Matthias Vallentin via llvm-dev <
llvm-dev at lists.llvm.org>:

> 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.
>
> Writing simple matcher and then do lexer magic seems the best idea.


> 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&) {
>     }
>
> Not really sure, but I would guess that this matcher might work only for
template instantiations (if you will use this function somewhere)[
I guess the misc-misplaced-const might have a bug.



>     using alias = const int&;
>
I guess this doesn't match because it is alias declaration. Use

typedefNameDecl


>
> I've tested this expression with clang-query (master branch). Any help
> that steers me in the right direction would be much appreciated.
>
>     Matthias
>
Piotr

_______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160921/660de7da/attachment.html>


More information about the llvm-dev mailing list