Skip to content

Commit 5723fce

Browse files
[Format] Fix isStartOfName to recognize attributes (#76804)
This addresses a problem with formatting attributes. Some context: - eaff083 changed `isStartOfName` to fix problems inside `#pragma`s, but this behavior changed formatting of attribute macros in an undesirable way. - efeb546 changed Google format style to fix some widely used attributes. Instead of changing the format style, this commit specializes behavior introduced in eaff083 to `#pragma`s. This seems to work well in both cases. Also update the test with two `GUARDED_BY` directives. While the formatting after efeb546 seems better, this case is rare enough to not warrant the extra complexity. We are reverting it back to the state it had before efeb546. --------- Co-authored-by: Owen Pan <[email protected]>
1 parent dc01b59 commit 5723fce

File tree

3 files changed

+4
-9
lines changed

3 files changed

+4
-9
lines changed

clang/lib/Format/Format.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
17011701
/*BasedOnStyle=*/"google",
17021702
},
17031703
};
1704-
GoogleStyle.AttributeMacros.push_back("GUARDED_BY");
1705-
GoogleStyle.AttributeMacros.push_back("ABSL_GUARDED_BY");
17061704

17071705
GoogleStyle.SpacesBeforeTrailingComments = 2;
17081706
GoogleStyle.Standard = FormatStyle::LS_Auto;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,8 @@ class AnnotatingParser {
22092209
(!NextNonComment && !Line.InMacroBody) ||
22102210
(NextNonComment &&
22112211
(NextNonComment->isPointerOrReference() ||
2212-
NextNonComment->isOneOf(tok::identifier, tok::string_literal)))) {
2212+
NextNonComment->is(tok::string_literal) ||
2213+
(Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
22132214
return false;
22142215
}
22152216

clang/unittests/Format/FormatTest.cpp

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

99
#include "FormatTestBase.h"
10-
#include "gmock/gmock.h"
1110

1211
#define DEBUG_TYPE "format-test"
1312

@@ -8563,9 +8562,6 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
85638562
" __attribute__((unused));");
85648563

85658564
Style = getGoogleStyle();
8566-
ASSERT_THAT(Style.AttributeMacros,
8567-
testing::AllOf(testing::Contains("GUARDED_BY"),
8568-
testing::Contains("ABSL_GUARDED_BY")));
85698565

85708566
verifyFormat(
85718567
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -10158,11 +10154,11 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
1015810154
getGoogleStyleWithColumns(40));
1015910155
verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
1016010156
" ABSL_GUARDED_BY(mutex1)\n"
10161-
" ABSL_GUARDED_BY(mutex2);",
10157+
" ABSL_GUARDED_BY(mutex2);",
1016210158
getGoogleStyleWithColumns(40));
1016310159
verifyFormat("Tttttt f(int a, int b)\n"
1016410160
" ABSL_GUARDED_BY(mutex1)\n"
10165-
" ABSL_GUARDED_BY(mutex2);",
10161+
" ABSL_GUARDED_BY(mutex2);",
1016610162
getGoogleStyleWithColumns(40));
1016710163
// * typedefs
1016810164
verifyGoogleFormat("typedef ATTR(X) char x;");

0 commit comments

Comments
 (0)