[clang] [llvm] [clang-tools-extra] [Format] Fix isStartOfName to recognize attributes (PR #76804)
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 15 05:53:19 PST 2024
https://github.com/ilya-biryukov updated https://github.com/llvm/llvm-project/pull/76804
>From d8598a382fb1496a96d6ff8317c06cf73606ad84 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Wed, 3 Jan 2024 11:07:17 +0100
Subject: [PATCH 1/4] [Format] Fix isStartOfName to recognize attributes
This addresses a problem with formatting attributes in a different way
as before. For some context:
- 199fc973ced20016b04ba540cf63a1d4914fa513 changed `isStartOfName` to
fix problems inside macro directives (judging by the added tests), but
this behavior changed formatting of attribute macros in an undesirable
way.
- efeb546865c233dfa7706ee0316c676de9f69897 changed Google format style
to fix some widely used attributes.
Instead of changing the format style, this commit specializes behavior
introduced in 199fc973ced20016b04ba540cf63a1d4914fa513 to macro
directives. This seems to work well in both cases.
Also update the test with two `GUARDED_BY` directives. While the
formatting after efeb546865c233dfa7706ee0316c676de9f69897 seems better,
this case is rare enough to not warrant the extra complexity. We are
reverting it back to the state it had before
efeb546865c233dfa7706ee0316c676de9f69897.
---
clang/lib/Format/Format.cpp | 2 --
clang/lib/Format/TokenAnnotator.cpp | 3 ++-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f798d555bf9929..38974f578fe1d2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1698,8 +1698,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
/*BasedOnStyle=*/"google",
},
};
- GoogleStyle.AttributeMacros.push_back("GUARDED_BY");
- GoogleStyle.AttributeMacros.push_back("ABSL_GUARDED_BY");
GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.Standard = FormatStyle::LS_Auto;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3ac3aa3c5e3a22..94fe5b21cfc6e6 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2209,7 +2209,8 @@ class AnnotatingParser {
(!NextNonComment && !Line.InMacroBody) ||
(NextNonComment &&
(NextNonComment->isPointerOrReference() ||
- NextNonComment->isOneOf(tok::identifier, tok::string_literal)))) {
+ (Line.InPragmaDirective &&
+ NextNonComment->isOneOf(tok::identifier, tok::string_literal))))) {
return false;
}
>From 6ba6f35e97af2299bbd5364fd535f31962a7eb4c Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Wed, 3 Jan 2024 11:22:26 +0100
Subject: [PATCH 2/4] Update tests
---
clang/unittests/Format/FormatTest.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 881993ede17c3d..fbbeddc29cfb5f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8498,9 +8498,6 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
" __attribute__((unused));");
Style = getGoogleStyle();
- ASSERT_THAT(Style.AttributeMacros,
- testing::AllOf(testing::Contains("GUARDED_BY"),
- testing::Contains("ABSL_GUARDED_BY")));
verifyFormat(
"bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -10093,11 +10090,11 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
getGoogleStyleWithColumns(40));
verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
" ABSL_GUARDED_BY(mutex1)\n"
- " ABSL_GUARDED_BY(mutex2);",
+ " ABSL_GUARDED_BY(mutex2);",
getGoogleStyleWithColumns(40));
verifyFormat("Tttttt f(int a, int b)\n"
" ABSL_GUARDED_BY(mutex1)\n"
- " ABSL_GUARDED_BY(mutex2);",
+ " ABSL_GUARDED_BY(mutex2);",
getGoogleStyleWithColumns(40));
// * typedefs
verifyGoogleFormat("typedef ATTR(X) char x;");
>From be3b337199fdc71718872e56aed19d89fa4447a1 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <iu.biryukov at gmail.com>
Date: Fri, 12 Jan 2024 15:28:56 +0100
Subject: [PATCH 3/4] Update clang/lib/Format/TokenAnnotator.cpp
Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
clang/lib/Format/TokenAnnotator.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 94fe5b21cfc6e6..7d030db45243a9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2209,8 +2209,8 @@ class AnnotatingParser {
(!NextNonComment && !Line.InMacroBody) ||
(NextNonComment &&
(NextNonComment->isPointerOrReference() ||
- (Line.InPragmaDirective &&
- NextNonComment->isOneOf(tok::identifier, tok::string_literal))))) {
+ NextNonComment->is(tok::string_literal) ||
+ (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
return false;
}
>From 806099685fc424513ad6aa0a954d43f89ed44b1e Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Fri, 12 Jan 2024 15:30:40 +0100
Subject: [PATCH 4/4] Remove the unused #include of gmock
---
clang/unittests/Format/FormatTest.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fbbeddc29cfb5f..5ae6d1a082ebe1 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "FormatTestBase.h"
-#include "gmock/gmock.h"
#define DEBUG_TYPE "format-test"
More information about the cfe-commits
mailing list