-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang-format] Add space after a word token #92741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Since I am by no means an expert on Clang, a few questions arose
|
@llvm/pr-subscribers-clang-format Author: Robin Caloudis (robincaloudis) ChangesCloses #92688 Full diff: https://github.com/llvm/llvm-project/pull/92741.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7c4c76a91f2c5..7786b85e8a1fc 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5280,7 +5280,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
// handled.
if (Left.is(tok::amp) && Right.is(tok::r_square))
return Style.SpacesInSquareBrackets;
- return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
+ return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
+ Right.is(TT_BinaryOperator);
}
// If the next token is a binary operator or a selector name, we have
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6f57f10e12e88..ca0edd7b22630 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24545,6 +24545,13 @@ TEST_F(FormatTest, STLWhileNotDefineChed) {
"#endif // while");
}
+TEST_F(FormatTest, BinaryOperatorAfterUnaryOperator) {
+ verifyFormat("void test(void) {\n"
+ " static void (*xor)(uint8_t *, size_t, uint8_t);\n"
+ " xor = resolve_xor_x86();\n"
+ "}");
+}
+
TEST_F(FormatTest, OperatorSpacing) {
FormatStyle Style = getLLVMStyle();
Style.PointerAlignment = FormatStyle::PAS_Right;
|
clang-format formats all C code as C++. Since
clang-format can't do it properly. @mydeveloperday, @HazardyKnusperkeks, and @rymiel may know more about why we didn't add |
@@ -5280,7 +5280,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, | |||
// handled. | |||
if (Left.is(tok::amp) && Right.is(tok::r_square)) | |||
return Style.SpacesInSquareBrackets; | |||
return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim); | |||
return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) || | |||
Right.is(TT_BinaryOperator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because if this code was in a .h and not a .c you wouldn't know what language you were in |
I thought we had some code like this around using a variable called "try" |
If I remember correctly, I was in favor of adding a C language.
There are certainly headers which are ambiguous, and we could add an option to set the language of such headers. But if we hit a |
@robincaloudis See #92880. |
Thanks @owenca, @mydeveloperday and @HazardyKnusperkeks for the explanation and insights! I'm closing this PR as @owenca found a much better solution. |
Closes #92688