clang-tidyで命名規則のチェック(&自動修正)


clang-tidy、C/C++のプログラミングでは、とても便利な静的解析ツールです。
-fix-fix-errorsオプションをつけて実行すれば、ある程度自動修正してくれる賢さも嬉しいです。

命名規則のチェックや、自動修正に対応していることに気づいたので紹介します。
種類ごとにキャピタライズ(CamelCase,lower_case)や、プレフィクス/サフィックスルールを決められます。

いちおうドキュメントもありますが、指定できるオプションは記載されていませんでした。テストを直接見るのが確実です。

clang-tidyは、.clang-tidyファイルに設定を書いておくと、デフォルトでそれを使ってくれます。
個別の設定をいちいち引数で指定するのは面倒なので、設定ファイルにまとめて書いておくのがおすすめです。

.clang-tidy
Checks: 'readability-identifier-naming'
HeaderFilterRegex: '.*'
CheckOptions:
  - key:             readability-identifier-naming.ClassCase
    value:           CamelCase
  - key:             readability-identifier-naming.EnumCase
    value:           CamelCase
  - key:             readability-identifier-naming.FunctionCase
    value:           camelBack
  - key:             readability-identifier-naming.MemberCase
    value:           lower_case
  - key:             readability-identifier-naming.MemberSuffix
    value:           _
  - key:             readability-identifier-naming.ParameterCase
    value:           lower_case
  - key:             readability-identifier-naming.UnionCase
    value:           CamelCase
  - key:             readability-identifier-naming.VariableCase
    value:           lower_case

参考メモ

ソースコードより。

対象の種類

複数に当てはまる場合、上のが優先されます。*は複数ヶ所に反映されるもの。なかなか混沌としています。

  1. Typedef
  2. TypeAlias
  3. 名前空間
    1. InlineNamespace
    2. Namespace
  4. Enum*
  5. Enum定数
    1. EnumConstant
    2. Constant*
  6. クラス系
    1. AbstractClass
    2. Struct
    3. Class
    4. Union
    5. Enum*
  7. メンバ
    1. ConstantMember
    2. Constant*
    3. PrivateMember
    4. ProtectedMember
    5. PublicMember
    6. Member
  8. 引数
    1. ConstexprVariable*
    2. ConstantParameter
    3. Constant*
    4. ParameterPack
    5. Parameter
  9. 変数
    1. const系
      1. ConstexprVariable*
      2. ClassConstant
      3. GlobalConstant
      4. StaticConstant
      5. LocalConstant
      6. Constant*
    2. ClassMember
    3. GlobalVariable
    4. StaticVariable
    5. LocalVariable
    6. Variable
  10. メソッド
    1. ConstexprMethod
    2. ConstexprFunction*
    3. ClassMethod
    4. VirtualMethod
    5. PrivateMethod
    6. ProtectedMethod
    7. PublicMethod
    8. Method
    9. Function*
  11. 関数
    1. ConstexprFunction*
    2. GlobalFunction
    3. Function*
  12. テンプレート引数(型)
    1. TypeTemplateParameter
    2. TemplateParameter*
  13. テンプレート引数(型以外)
    1. ValueTemplateParameter
    2. TemplateParameter*
  14. テンプレート引数(テンプレート)
    1. TemplateTemplateParameter
    2. TemplateParameter*
  15. 非対応ぽい
    1. MacroDefinition
    2. PureFunction
    3. PureMethod
    4. TemplateUsing
    5. Using

キャピタライズの種類

  • aNy_CasE
  • lower_case
  • UPPER_CASE
  • camelBack
  • CamelCase
  • Camel_Snake_Case(アップデートで追加?1
  • camel_Snake_Back(アップデートで追加?1