From c7ac782942f15cc38858965b435b9e10db38b757 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Fri, 25 Jul 2025 11:55:50 -0400 Subject: [PATCH] [clang-format] Google Style: disable DerivePointerAlignment. (#149602) The [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions) 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. The new requirement is: > When referring to a pointer or reference (variable declarations or > definitions, arguments, return types, template parameters, etc.), > you must not place a space before the asterisk/ampersand. Use a > space to separate the type from the declared name (if present). The [Google ObjC style](https://google.github.io/styleguide/objcguide.html) is silent on this matter, but the de-facto style is not being modified at this time. So, keep DerivePointerAlignment enabled for ObjC language mode. (cherry picked from commit 9281797a577b7954521fb9192d41e457ca2ca42e) --- clang/lib/Format/Format.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0e92c93ea1dde..513fcfcd41258 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1749,7 +1749,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 = true; GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; GoogleStyle.IncludeStyle.IncludeCategories = {{"^", 2, 0, false}, {"^<.*\\.h>", 1, 0, false}, @@ -1858,6 +1857,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 f9eedcb8f12af..e161f61e8864e 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::aaaaaaaaaaa\n" "aaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" - " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); + " bool* aaaaaaaaaaaaaaaaaa, bool* aa) {}"); verifyGoogleFormat("template \n" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "aaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaa(\n" @@ -12891,27 +12891,31 @@ TEST_F(FormatTest, UnderstandsEllipsis) { } TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { + auto Style = getGoogleStyle(); + EXPECT_FALSE(Style.DerivePointerAlignment); + 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) && c;"); verifyFormat("typedef void (*f)(int i) func;"); verifyFormat("void operator++(int) noexcept;"); @@ -25425,7 +25429,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;");