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

Matthias Vallentin via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 21 12:48:11 PDT 2016


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.

    Matthias


More information about the llvm-dev mailing list