Skip to content

Commit efeb546

Browse files
[clang-format] Add common attribute macros to Google style (#76239)
We have found that 199fc97 regresses formatting of our codebases because we do not properly configure the names of attribute macros. `GUARDED_BY` and `ABSL_GUARDED_BY` are very commoon in Google codebases so it is reasonable to include them by default to avoid the need for extra configuration in every Google repository.
1 parent f5ab0bb commit efeb546

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,9 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
16981698
/*BasedOnStyle=*/"google",
16991699
},
17001700
};
1701+
GoogleStyle.AttributeMacros.push_back("GUARDED_BY");
1702+
GoogleStyle.AttributeMacros.push_back("ABSL_GUARDED_BY");
1703+
17011704
GoogleStyle.SpacesBeforeTrailingComments = 2;
17021705
GoogleStyle.Standard = FormatStyle::LS_Auto;
17031706

clang/unittests/Format/FormatTest.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "FormatTestBase.h"
10+
#include "gmock/gmock.h"
1011

1112
#define DEBUG_TYPE "format-test"
1213

@@ -8497,7 +8498,10 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
84978498
" __attribute__((unused));");
84988499

84998500
Style = getGoogleStyle();
8500-
Style.AttributeMacros.push_back("GUARDED_BY");
8501+
ASSERT_THAT(Style.AttributeMacros,
8502+
testing::AllOf(testing::Contains("GUARDED_BY"),
8503+
testing::Contains("ABSL_GUARDED_BY")));
8504+
85018505
verifyFormat(
85028506
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
85038507
" GUARDED_BY(aaaaaaaaaaaa);",
@@ -8514,6 +8518,23 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
85148518
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
85158519
" aaaaaaaaaaaaaaaaaaaaaaaaa;",
85168520
Style);
8521+
8522+
verifyFormat(
8523+
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8524+
" ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8525+
Style);
8526+
verifyFormat(
8527+
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8528+
" ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8529+
Style);
8530+
verifyFormat(
8531+
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8532+
" aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8533+
Style);
8534+
verifyFormat(
8535+
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8536+
" aaaaaaaaaaaaaaaaaaaaaaaaa;",
8537+
Style);
85178538
}
85188539

85198540
TEST_F(FormatTest, FunctionAnnotations) {
@@ -10072,11 +10093,11 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
1007210093
getGoogleStyleWithColumns(40));
1007310094
verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
1007410095
" ABSL_GUARDED_BY(mutex1)\n"
10075-
" ABSL_GUARDED_BY(mutex2);",
10096+
" ABSL_GUARDED_BY(mutex2);",
1007610097
getGoogleStyleWithColumns(40));
1007710098
verifyFormat("Tttttt f(int a, int b)\n"
1007810099
" ABSL_GUARDED_BY(mutex1)\n"
10079-
" ABSL_GUARDED_BY(mutex2);",
10100+
" ABSL_GUARDED_BY(mutex2);",
1008010101
getGoogleStyleWithColumns(40));
1008110102
// * typedefs
1008210103
verifyGoogleFormat("typedef ATTR(X) char x;");

0 commit comments

Comments
 (0)