[PATCH] D139801: [clang-format] Adds 'friend' to QualifierOrder.

Micah Weston via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 11 14:35:38 PST 2022


red1bluelost created this revision.
red1bluelost added a reviewer: MyDeveloperDay.
Herald added a project: All.
red1bluelost requested review of this revision.
Herald added a project: clang.

For cases of defining friend functions, qualifier ordering can allow multiple
positions for the 'friend' token.

  c++
  struct S {
    int i;
  
    friend constexpr auto operator<=>(const S&, const S&) noexcept = default;
  };



  1. Changes
- Adds `friend` to `getTokenFromQualifier`
- Updates documentation in `Format.h` and `ClangFormatStyleOptions.rst`
- Adds tests to `QualifierFixerTest.cpp`

Testing
-------

Ran FormatTest with no errors.

GitHub Issue
------------

https://github.com/llvm/llvm-project/issues/59450


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139801

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/QualifierAlignmentFixer.cpp
  clang/unittests/Format/QualifierFixerTest.cpp


Index: clang/unittests/Format/QualifierFixerTest.cpp
===================================================================
--- clang/unittests/Format/QualifierFixerTest.cpp
+++ clang/unittests/Format/QualifierFixerTest.cpp
@@ -133,6 +133,8 @@
             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, 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 @@
   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 @@
   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 @@
       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 ");
 
Index: clang/lib/Format/QualifierAlignmentFixer.cpp
===================================================================
--- clang/lib/Format/QualifierAlignmentFixer.cpp
+++ clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -414,6 +414,7 @@
       .Case("inline", tok::kw_inline)
       .Case("constexpr", tok::kw_constexpr)
       .Case("restrict", tok::kw_restrict)
+      .Case("friend", tok::kw_friend)
       .Default(tok::identifier);
 }
 
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -2970,6 +2970,7 @@
   ///   * const
   ///   * inline
   ///   * static
+  ///   * friend
   ///   * constexpr
   ///   * volatile
   ///   * restrict
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -3712,6 +3712,7 @@
     * const
     * inline
     * static
+    * friend
     * constexpr
     * volatile
     * restrict


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139801.481952.patch
Type: text/x-patch
Size: 3732 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221211/b90cb80f/attachment.bin>


More information about the cfe-commits mailing list