[cfe-dev] clang-format, recognizing alternative binary operator tokens
Bobby Moretti
bobmoretti at gmail.com
Sat May 3 14:52:53 PDT 2014
Hello,
clang-format does a really nice job breaking lines with boolean binary
operators, e.g., && and ||.
The C++ standard provides alternative keywords for these operators
(and, or). I think clang-format should be able to treat these
alternative keywords the same as the corresponding binary operators.
However, with these alternative keywords, clang-format makes
completely different whitespace choices. For example, using
clang-format 3.5 from svn r207601, with the default LLVM style options
void foo(void) {
if (call_some_function() >
0 and(some_other_function_result() ==
0 or yet_another_result or(something_else == 1))) {
}
if (call_some_function() > 0 &&
(some_other_function_result() == 0 || yet_another_result ||
(something_else == 1))) {
}
}
I did some investigation to see what might be causing this behavior.
It boils down to the LangOptions returned by getFormattingOptions() in
Format.cpp line 1848. If I add
LangOpts.CXXOperatorNames = 1;
to that function, then clang-format outputs the much more pleasing
void foo(void) {
if (call_some_function() > 0 and
(some_other_function_result() == 0 or yet_another_result or
(something_else == 1))) {
}
if (call_some_function() > 0 &&
(some_other_function_result() == 0 || yet_another_result ||
(something_else == 1))) {
}
}
Of course, this should probably not always be enabled.
One option would be to only enable this option if LS_Cpp03 or LS_Cpp11
were chosen. But C users #including <iso646.h> might want this option,
and not all C++ users would want it (since for example, MSVC++ does
not recognize these unless using standards compliant mode).
So another option would be to add a format style option to selectively
enable this behavior.
Would such a change to clang-format be welcome? If so, what would be
the preferred implementation?
Thanks,
--
Bobby Moretti
bobmoretti at gmail.com
More information about the cfe-dev
mailing list