使用Clang-format格式化具有LTTNG跟踪定义的文件时,默认设置会使ctf_enum_value缩进非常难看:
TRACEPOINT_ENUM(com_fafa,
fafa_enum,
TP_ENUM_VALUES(ctf_enum_value("AAA", AAA)
ctf_enum_value("BBB", BBB)
ctf_enum_value("CCC", CCC)))
是否有任何选项可以让clang-format对齐ctf_enum_values,如下所示?
TRACEPOINT_ENUM(com_fafa,
fafa_enum,
TP_ENUM_VALUES(ctf_enum_value("AAA", AAA)
ctf_enum_value("BBB", BBB)
ctf_enum_value("CCC", CCC)))
我想问题是这些列表中没有逗号,clang-format可能不喜欢...
在下面的回答中指出使用“ContinuationIndentWidth:0”在某些情况下效果很好,但对于其他情况,它会使情况变得更糟,例如你可能会得到这个:
TRACEPOINT_EVENT(
com_fafa,
L_ERROR_fafa,
TP_ARGS(const enum fafa_type, e_code_, const int, msg_type_, const char*, file_line_),
TP_FIELDS(ctf_string(file_line, file_line_) ctf_integer(int, e_code, e_code_) ctf_integer(int, msg_type, msg_type_)
ctf_integer_nowrite(int, u_trace_id, -1) ctf_string_nowrite(e_msg, "")))
答案 0 :(得分:0)
自LTTng 2.10.1起,ctf_enum_value()
被翻译为this:
#define ctf_enum_value(_string, _value) \
{ \
.start = { \
.value = lttng_is_signed_type(__typeof__(_value)) ? \
(long long) (_value) : (_value), \
.signedness = lttng_is_signed_type(__typeof__(_value)), \
}, \
.end = { \
.value = lttng_is_signed_type(__typeof__(_value)) ? \
(long long) (_value) : (_value), \
.signedness = lttng_is_signed_type(__typeof__(_value)), \
}, \
.string = (_string), \
},
如您所见,逗号包含在输出中。这是因为否则,逗号将被解释为TP_ENUM_VALUES()
宏参数分隔符,这将限制您可以放入TP_ENUM_VALUES()
的枚举值的数量。
我认为宏是ClangFormat的一个问题,因为它们不一定能被解析为C / C ++。您可能会遇到其他宏的更多问题,并且必须对其中一些宏进行手动格式化。
与此同时,我的解决方案是将ClangFormat ContinuationIndentWidth
选项设置为0,例如:
clang-format -style '{BasedOnStyle: llvm, ContinuationIndentWidth: 0}' my-tp.h
根据您的输入,此命令将打印:
TRACEPOINT_ENUM(com_fafa, fafa_enum,
TP_ENUM_VALUES(ctf_enum_value("AAA", AAA)
ctf_enum_value("BBB", BBB)
ctf_enum_value("CCC", CCC)))