21
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-01-30

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. 名前空間
  4. InlineNamespace
  5. Namespace
  6. Enum*
  7. Enum定数
  8. EnumConstant
  9. Constant*
  10. クラス系
  11. AbstractClass
  12. Struct
  13. Class
  14. Union
  15. Enum*
  16. メンバ
  17. ConstantMember
  18. Constant*
  19. PrivateMember
  20. ProtectedMember
  21. PublicMember
  22. Member
  23. 引数
  24. ConstexprVariable*
  25. ConstantParameter
  26. Constant*
  27. ParameterPack
  28. Parameter
  29. 変数
  30. const系
    1. ConstexprVariable*
    2. ClassConstant
    3. GlobalConstant
    4. StaticConstant
    5. LocalConstant
    6. Constant*
  31. ClassMember
  32. GlobalVariable
  33. StaticVariable
  34. LocalVariable
  35. Variable
  36. メソッド
  37. ConstexprMethod
  38. ConstexprFunction*
  39. ClassMethod
  40. VirtualMethod
  41. PrivateMethod
  42. ProtectedMethod
  43. PublicMethod
  44. Method
  45. Function*
  46. 関数
  47. ConstexprFunction*
  48. GlobalFunction
  49. Function*
  50. テンプレート引数(型)
  51. TypeTemplateParameter
  52. TemplateParameter*
  53. テンプレート引数(型以外)
  54. ValueTemplateParameter
  55. TemplateParameter*
  56. テンプレート引数(テンプレート)
  57. TemplateTemplateParameter
  58. TemplateParameter*
  59. 非対応ぽい
  60. MacroDefinition
  61. PureFunction
  62. PureMethod
  63. TemplateUsing
  64. Using

キャピタライズの種類

  • aNy_CasE
  • lower_case
  • UPPER_CASE
  • camelBack
  • CamelCase
  • Camel_Snake_Case(アップデートで追加?1
  • camel_Snake_Back(アップデートで追加?1
  1. https://reviews.llvm.org/D21472 2

21
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?