0

a.cpp:

#include <iostream>

void func(const int& a, const int b, int c) {
    std::cout << a + b + c << std::endl;
}

int main() {
    func(1, 2, 3);
    return 0;
}

I want to specify casing for parameters a and b, but not c with clang-tidy. How to do this?

I've also opened an issue on the llvm project https://github.com/llvm/llvm-project/issues/64220 but maybe someone here has a quick fix.

relevant: https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html

clang-tidy -checks='readability-identifier-naming' -config="{CheckOptions: [ {key: readability-identifier-naming.ParameterCase, value: UPPER_CASE} ]}" a.cpp -- -std=c++17

catches all parameters.

clang-tidy -checks='readability-identifier-naming' -config="{CheckOptions: [ {key: readability-identifier-naming.ConstantParameterCase, value: UPPER_CASE} ]}" a.cpp -- -std=c++17

catches only parameter b, not a. I want to catch both a, b.

clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 15.0.7
  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: skylake

1 Answers1

1

To summarize the request, you want a configuration for the clang-tidy check called readability-identifier-naming that will require the use of uppercase letters for parameters of type const int & and const int but not int.

In the current release of Clang+LLVM, 16.0.6, this is not possible. The ParameterCase option applies to all three, while the ConstantParameterCase option only applies to const int, not const int &, since the latter is a reference to a const-qualified type, but is not const-qualified itself (reference types cannot be const-qualified).

There is no more specific option that applies to a const int & parameter than ParameterCase, as can be seen from the source code of IdentifierNamingCheck::findStyleKind, excerpted below:

  • Parameters are always ParmVarDecl AST objects.
  • It (const int &) is neither constexpr nor const.
  • It is not a parameter pack.
  • It is not Type::isAnyPointerType(), as that is true of C/C++ pointers and Objective-C pointers, but not references.
  • Thus only remaining applicable style is simply SK_Parameter, which corresponds to the ParameterCase option (for letter case rules).

This could of course change in a future version if your Issue #64220 is acted upon. You might want to clarify in that issue that you (now) understand it is not possible in the current version, and you are making an enhancement request.

Source code excerpt:

StyleKind IdentifierNamingCheck::findStyleKind(
    const NamedDecl *D,
    ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
    bool IgnoreMainLikeFunctions) const {
...
 if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
    if (isParamInMainLikeFunction(*Decl, IgnoreMainLikeFunctions))
      return SK_Invalid;
    QualType Type = Decl->getType();

    if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
      return SK_ConstexprVariable;

    if (!Type.isNull() && Type.isConstQualified()) {
      if (Type.getTypePtr()->isAnyPointerType() &&
          NamingStyles[SK_ConstantPointerParameter])
        return SK_ConstantPointerParameter;

      if (NamingStyles[SK_ConstantParameter])
        return SK_ConstantParameter;

      if (NamingStyles[SK_Constant])
        return SK_Constant;
    }

    if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
      return SK_ParameterPack;

    if (!Type.isNull() && Type.getTypePtr()->isAnyPointerType() &&
        NamingStyles[SK_PointerParameter])
      return SK_PointerParameter;

    if (NamingStyles[SK_Parameter])
      return SK_Parameter;

    return SK_Invalid;
  }
...
Scott McPeak
  • 8,803
  • 2
  • 40
  • 79