[clang] 4cafc37 - [clang-format] Add 'friend' to QualifierOrder
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 22 02:04:16 PST 2022
Author: Micah Weston
Date: 2022-12-22T02:02:09-08:00
New Revision: 4cafc3727b35151e8f676adab11524bb21140939
URL: https://github.com/llvm/llvm-project/commit/4cafc3727b35151e8f676adab11524bb21140939
DIFF: https://github.com/llvm/llvm-project/commit/4cafc3727b35151e8f676adab11524bb21140939.diff
LOG: [clang-format] Add 'friend' to QualifierOrder
For cases of defining friend functions, qualifier ordering can
allow multiple positions for the 'friend' token.
Closes #59450.
Differential Revision: https://reviews.llvm.org/D139801
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h
clang/lib/Format/QualifierAlignmentFixer.cpp
clang/unittests/Format/QualifierFixerTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 969fce82781c2..27e93ff504d83 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3712,6 +3712,7 @@ the configuration (without a prefix: ``Auto``).
* const
* inline
* static
+ * friend
* constexpr
* volatile
* restrict
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index b7fbe4b97c6db..8b11ae662c707 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2970,6 +2970,7 @@ struct FormatStyle {
/// * const
/// * inline
/// * static
+ /// * friend
/// * constexpr
/// * volatile
/// * restrict
diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 6ee0d6da76a48..cef8b36ff7582 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -414,6 +414,7 @@ tok::TokenKind LeftRightQualifierAlignmentFixer::getTokenFromQualifier(
.Case("inline", tok::kw_inline)
.Case("constexpr", tok::kw_constexpr)
.Case("restrict", tok::kw_restrict)
+ .Case("friend", tok::kw_friend)
.Default(tok::identifier);
}
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index b01aff53150da..875ad8353e017 100755
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -133,6 +133,8 @@ TEST_F(QualifierFixerTest, RotateTokens) {
tok::kw_static);
EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("restrict"),
tok::kw_restrict);
+ EXPECT_EQ(LeftRightQualifierAlignmentFixer::getTokenFromQualifier("friend"),
+ tok::kw_friend);
}
TEST_F(QualifierFixerTest, FailQualifierInvalidConfiguration) {
@@ -196,8 +198,8 @@ TEST_F(QualifierFixerTest, QualifierRight) {
TEST_F(QualifierFixerTest, QualifiersCustomOrder) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Left;
- Style.QualifierOrder = {"inline", "constexpr", "static",
- "const", "volatile", "type"};
+ Style.QualifierOrder = {"friend", "inline", "constexpr", "static",
+ "const", "volatile", "type"};
verifyFormat("const volatile int a;", "const volatile int a;", Style);
verifyFormat("const volatile int a;", "volatile const int a;", Style);
@@ -216,6 +218,15 @@ TEST_F(QualifierFixerTest, QualifiersCustomOrder) {
verifyFormat("constexpr static LPINT Bar;", "static constexpr LPINT Bar;",
Style);
verifyFormat("const const int a;", "const int const a;", Style);
+
+ verifyFormat(
+ "friend constexpr auto operator<=>(const foo &, const foo &) = default;",
+ "constexpr friend auto operator<=>(const foo &, const foo &) = default;",
+ Style);
+ verifyFormat(
+ "friend constexpr bool operator==(const foo &, const foo &) = default;",
+ "constexpr bool friend operator==(const foo &, const foo &) = default;",
+ Style);
}
TEST_F(QualifierFixerTest, LeftRightQualifier) {
@@ -723,9 +734,10 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
ConfiguredTokens.push_back(tok::kw_inline);
ConfiguredTokens.push_back(tok::kw_restrict);
ConfiguredTokens.push_back(tok::kw_constexpr);
+ ConfiguredTokens.push_back(tok::kw_friend);
- auto Tokens =
- annotate("const static inline auto restrict int double long constexpr");
+ auto Tokens = annotate(
+ "const static inline auto restrict int double long constexpr friend");
EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
Tokens[0], ConfiguredTokens));
@@ -745,6 +757,8 @@ TEST_F(QualifierFixerTest, IsQualifierType) {
Tokens[7], ConfiguredTokens));
EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
Tokens[8], ConfiguredTokens));
+ EXPECT_TRUE(LeftRightQualifierAlignmentFixer::isQualifierOrType(
+ Tokens[9], ConfiguredTokens));
auto NotTokens = annotate("for while do Foo Bar ");
More information about the cfe-commits
mailing list