[clang] 49d3118 - [clang-format] Missing space after cast in a macro
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 6 00:11:25 PST 2022
Author: mydeveloperday
Date: 2022-01-06T08:07:03Z
New Revision: 49d311874edc928831ccaddd621801a4dbee580d
URL: https://github.com/llvm/llvm-project/commit/49d311874edc928831ccaddd621801a4dbee580d
DIFF: https://github.com/llvm/llvm-project/commit/49d311874edc928831ccaddd621801a4dbee580d.diff
LOG: [clang-format] Missing space after cast in a macro
https://github.com/llvm/llvm-project/issues/52979
Though SpaceAfterCStyleCast is set to true, clang-format 13 does not add a space after (void *) here:
```
```
This patch addresses that
Fixes: #52979
Reviewed By: curdeius, HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D116592
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index ec9bfdb0b2a76..5241685630a5d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1883,9 +1883,10 @@ class AnnotatingParser {
FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
if (LeftOfParens) {
- // If there is a closing parenthesis left of the current parentheses,
- // look past it as these might be chained casts.
- if (LeftOfParens->is(tok::r_paren)) {
+ // If there is a closing parenthesis left of the current
+ // parentheses, look past it as these might be chained casts.
+ if (LeftOfParens->is(tok::r_paren) &&
+ LeftOfParens->isNot(TT_CastRParen)) {
if (!LeftOfParens->MatchingParen ||
!LeftOfParens->MatchingParen->Previous)
return false;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d90c3d3a291f6..85ce3171bbc89 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10150,6 +10150,15 @@ TEST_F(FormatTest, FormatsCasts) {
" (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
" bbbbbbbbbbbbbbbbbbbbbb);");
+ verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
+ verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
+ verifyFormat("#define CONF_BOOL(x) (bool)(x)");
+ verifyFormat("bool *y = (bool *)(void *)(x);");
+ verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
+ verifyFormat("bool *y = (bool *)(void *)(int)(x);");
+ verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
+ verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
+
// These are not casts.
verifyFormat("void f(int *) {}");
verifyFormat("f(foo)->b;");
@@ -14661,6 +14670,11 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
" break;\n"
"}",
Spaces);
+ verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
+ verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
+ verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
+ verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
+ verifyFormat("bool *y = ( bool * ) (x);", Spaces);
// Run subset of tests again with:
Spaces.SpacesInCStyleCastParentheses = false;
@@ -14680,6 +14694,11 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
+ verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
+ verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
+ verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
+ verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
+ verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
Spaces.ColumnLimit = 80;
Spaces.IndentWidth = 4;
Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
More information about the cfe-commits
mailing list