提问人:Danny 提问时间:2/1/2023 最后编辑:Danny 更新时间:2/3/2023 访问量:671
Clang tidy 无法正常工作,用于静态成员变量
Clang tidy doesn't work properly for static member variable
问:
我正在为我的项目运行 clang-tidy。以下是我的 .clang-tidy 文件中的相关命名选项:
- key: readability-identifier-naming.ClassMemberCase # e.g., int myClassMember_
value: camelBack
- key: readability-identifier-naming.ClassMemberSuffix # e.g., int myClassMember_
value: _
- key: readability-identifier-naming.GlobalConstantCase # e.g., int MyGlobalConstant (please don't make globals)
value: CamelCase
- key: readability-identifier-naming.IgnoreMainLikeFunctions # Doesn't apply clang checks to main() args
value: 1
- key: readability-identifier-naming.MacroDefinitionCase # e.g., #define MY_MACRO="PleaseDon'tUseMacrosAnywhere"
value: UPPER_CASE
- key: readability-identifier-naming.MacroDefinitionIgnoredRegexp
value: '^[A-Z]+(_[A-Z]+)*_$'
- key: readability-identifier-naming.MemberCase # e.g., int myMember_ = 42;
value: CamelCase
- key: readability-identifier-naming.MemberSuffix # e.g., int myMember_ = 42;
value: _
- key: readability-identifier-naming.ParameterCase # e.g., void MyFunction(int parameter, int anotherParam);
value: camelBack
- key: readability-identifier-naming.StaticConstantCase # e.g., static const std::string s_myStaticConstant = "I hope this works!"
value: camelBack
- key: readability-identifier-naming.StaticConstantPrefix # e.g., static const std::string s_myStaticConstant = "I hope this works!"
value: s_
- key: readability-identifier-naming.StaticVariableCase # e.g., static std::string s_myStaticVar = "I hope this works!"
value: camelBack
- key: readability-identifier-naming.StaticVariablePrefix # e.g., static std::string s_myStaticVar = "I hope this works!"
value: s_
- key: readability-identifier-naming.StructCase # e.g., struct MyStruct { ... };
value: CamelCase
- key: readability-identifier-naming.VariableCase # e.g., int myVariable = 10;
value: camelBack
不幸的是,clang tidy 将我的静态类成员变量变成了 .似乎类成员的选项覆盖了静态变量的选项:sMyVariable_
s_myVariable_
warning: invalid case style for class member 's_myVariable_' [readability-identifier-naming]
int MyClass::s_myVariable_ = 1;
有没有办法使静态命名规则优先于成员命名规则?谢谢!
示例代码:
class MyClass{
static int s_myVariable_; // Clang-tidy doesn't like this
};
答:
4赞
Marc
2/3/2023
#1
我也很难在文档中找到这一点。静态成员变量 readability-identifier-naming 样式似乎是使用 ClassMemberCase、ClassMemberPrefix 和 ClassMemberSuffix 定义的。
对于您想要的格式
class MyClass{
static int s_myVariable_; // Clang-tidy doesn't like this
};
.clang-tidy 文件的字段将包含以下键/值:CheckOptions
- { key: readability-identifier-naming.ClassMemberPrefix, value: s_ }
- { key: readability-identifier-naming.ClassMemberCase, value: camelBack }
- { key: readability-identifier-naming.ClassMemberSuffix, value: _ }
评论
0赞
Danny
2/8/2023
嗨,马克,这难道不会给所有类成员加上一个 ,甚至是非静态的吗?这是不可取的。s_
0赞
Marc
2/8/2023
它不适合我。据我从上面链接的文档中可以看出,仅影响静态成员变量。还有其他样式由 、 、 等定义。这会影响其他作用域内的成员变量。我不确定这些是否以某种方式覆盖,或者它们是否完全是单独的样式,但定义它们应该有助于获得您正在寻找的格式。ClassMemberX
MemberCaseX
PrivateMemberX
ProtectedMemberX
ClassMemberX
0赞
Danny
2/23/2023
嗨,马克,这似乎对我来说效果很好,除了成员是静态 constexpr 的情况。你对此有什么建议吗?
0赞
Marc
2/25/2023
我会查看 / 或 / 'xConstantCase' 字段之一。它们对我来说都设置为“k”和“CamelCase”,所以我不确定哪一个涵盖静态 constexpr。ConstexprVariablePrefix
ConstexprVariableCase
xConstantPrefix
评论
int s_myVariable_