[clang] [clang-format] Google Style: disable DerivePointerAlignment. (PR #149602)
James Y Knight via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 21 08:01:56 PDT 2025
https://github.com/jyknight updated https://github.com/llvm/llvm-project/pull/149602
>From 296b9b160564882e209544ed7a2db0adc62d3246 Mon Sep 17 00:00:00 2001
From: James Y Knight <jyknight at google.com>
Date: Fri, 18 Jul 2025 17:44:38 -0400
Subject: [PATCH 1/2] [clang-format] Google Style: disable
DerivePointerAlignment.
The Google C++ Style Guide is being changed to specify that spaces
should go after the asterisk/ampersand, rather than permitting either
before or after on a file-by-file basis.
ObjC style is not being modified.
---
clang/lib/Format/Format.cpp | 3 ++-
clang/unittests/Format/FormatTest.cpp | 22 +++++++++++++---------
2 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 62feb3db0ed5e..c8462891f8290 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1753,7 +1753,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.AttributeMacros.push_back("absl_nullable");
GoogleStyle.AttributeMacros.push_back("absl_nullability_unknown");
GoogleStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
- GoogleStyle.DerivePointerAlignment = true;
+ GoogleStyle.DerivePointerAlignment = false;
GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
GoogleStyle.IncludeStyle.IncludeCategories = {{"^<ext/.*\\.h>", 2, 0, false},
{"^<.*\\.h>", 1, 0, false},
@@ -1862,6 +1862,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
} else if (Language == FormatStyle::LK_ObjC) {
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
GoogleStyle.ColumnLimit = 100;
+ GoogleStyle.DerivePointerAlignment = true;
// "Regroup" doesn't work well for ObjC yet (main header heuristic,
// relationship between ObjC standard library headers and other heades,
// #imports, etc.)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 0bc1c6d45656e..195d045573b4d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8571,10 +8571,10 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
"operator<<(const SomeLooooooooooooooooooooooooogType &other);");
verifyGoogleFormat(
"SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
- " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
+ " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
verifyGoogleFormat(
"SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
- " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
+ " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
@@ -8583,7 +8583,7 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
verifyGoogleFormat(
"typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
"aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
+ " bool* aaaaaaaaaaaaaaaaaa, bool* aa) {}");
verifyGoogleFormat("template <typename T>\n"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
"aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
@@ -12287,6 +12287,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyGoogleFormat("int const* a = &b;");
+ verifyFormat("int const* a = &b;", "int const *a = &b;", getGoogleStyle());
verifyGoogleFormat("**outparam = 1;");
verifyGoogleFormat("*outparam = a * b;");
verifyGoogleFormat("int main(int argc, char** argv) {}");
@@ -12891,27 +12892,30 @@ TEST_F(FormatTest, UnderstandsEllipsis) {
}
TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
+ FormatStyle Style = getLLVMStyle();
+ Style.DerivePointerAlignment = true;
+
verifyFormat("int *a;\n"
"int *a;\n"
"int *a;",
"int *a;\n"
"int* a;\n"
"int *a;",
- getGoogleStyle());
+ Style);
verifyFormat("int* a;\n"
"int* a;\n"
"int* a;",
"int* a;\n"
"int* a;\n"
"int *a;",
- getGoogleStyle());
+ Style);
verifyFormat("int *a;\n"
"int *a;\n"
"int *a;",
"int *a;\n"
"int * a;\n"
"int * a;",
- getGoogleStyle());
+ Style);
verifyFormat("auto x = [] {\n"
" int *a;\n"
" int *a;\n"
@@ -12920,7 +12924,7 @@ TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
"auto x=[]{int *a;\n"
"int * a;\n"
"int * a;};",
- getGoogleStyle());
+ Style);
}
TEST_F(FormatTest, UnderstandsRvalueReferences) {
@@ -13056,7 +13060,7 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("virtual void foo(char &) const;");
verifyFormat("virtual void foo(int *a, char *) const;");
verifyFormat("int a = sizeof(int *) + b;");
- verifyGoogleFormat("int a = alignof(int *) + b;");
+ verifyGoogleFormat("int a = alignof(int*) + b;");
verifyFormat("bool b = f(g<int>) && c;");
verifyFormat("typedef void (*f)(int i) func;");
verifyFormat("void operator++(int) noexcept;");
@@ -25419,7 +25423,7 @@ TEST_F(FormatTest, AtomicQualifier) {
verifyFormat("struct foo {\n"
" int a1;\n"
" _Atomic(a) a2;\n"
- " _Atomic(_Atomic(int) *const) a3;\n"
+ " _Atomic(_Atomic(int)* const) a3;\n"
"};",
Google);
verifyFormat("_Atomic(uint64_t) a;");
>From 9d86ca8094a3f2901c7b8d62889ab783e806fc59 Mon Sep 17 00:00:00 2001
From: James Y Knight <jyknight at google.com>
Date: Mon, 21 Jul 2025 11:01:39 -0400
Subject: [PATCH 2/2] Review comments
---
clang/lib/Format/Format.cpp | 1 -
clang/unittests/Format/FormatTest.cpp | 4 ++--
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c8462891f8290..8059f47fc61b1 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1753,7 +1753,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.AttributeMacros.push_back("absl_nullable");
GoogleStyle.AttributeMacros.push_back("absl_nullability_unknown");
GoogleStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
- GoogleStyle.DerivePointerAlignment = false;
GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
GoogleStyle.IncludeStyle.IncludeCategories = {{"^<ext/.*\\.h>", 2, 0, false},
{"^<.*\\.h>", 1, 0, false},
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 195d045573b4d..eaef541ca43a1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12287,7 +12287,6 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyGoogleFormat("int const* a = &b;");
- verifyFormat("int const* a = &b;", "int const *a = &b;", getGoogleStyle());
verifyGoogleFormat("**outparam = 1;");
verifyGoogleFormat("*outparam = a * b;");
verifyGoogleFormat("int main(int argc, char** argv) {}");
@@ -12892,7 +12891,8 @@ TEST_F(FormatTest, UnderstandsEllipsis) {
}
TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
- FormatStyle Style = getLLVMStyle();
+ auto Style = getGoogleStyle();
+ EXPECT_FALSE(Style.DerivePointerAlignment);
Style.DerivePointerAlignment = true;
verifyFormat("int *a;\n"
More information about the cfe-commits
mailing list