r373165 - [clang-format] Reference qualifiers in member templates causing extra indentation.

Paul Hoad via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 29 06:45:38 PDT 2019


Author: paulhoad
Date: Sun Sep 29 06:45:38 2019
New Revision: 373165

URL: http://llvm.org/viewvc/llvm-project?rev=373165&view=rev
Log:
[clang-format] Reference qualifiers in member templates causing extra indentation.

Summary:
The following code

```
struct f {
  template <class T>
  void bar() && noexcept {}
};
```

will be formatted to the following with LLVM style, and
`AlwaysBreakTemplateDeclarations: Yes`

```
struct f {
  template <class T>
      void bar() && noexcept {}
};
```

The indentation of the `void bar()` line is wrong.

Reviewers: klimek, owenpan, krasimir, timwoj, MyDeveloperDay

Reviewed By: klimek, MyDeveloperDay

Subscribers: MyDeveloperDay, ilya-biryukov, llvm-commits, cfe-commits

Patch By: AndWass

Tags: #clang-format, #clang, #llvm

Differential Revision: https://reviews.llvm.org/D68072

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=373165&r1=373164&r2=373165&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun Sep 29 06:45:38 2019
@@ -65,7 +65,7 @@ public:
   AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
                    const AdditionalKeywords &Keywords)
       : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
-        Keywords(Keywords) {
+        TrailingReturnFound(false), Keywords(Keywords) {
     Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
     resetTokenMetadata(CurrentToken);
   }
@@ -1389,7 +1389,11 @@ private:
     } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
                Current.NestingLevel == 0) {
       Current.Type = TT_TrailingReturnArrow;
-    } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
+      TrailingReturnFound = true;
+    } else if (Current.is(tok::star) ||
+               (Current.isOneOf(tok::amp, tok::ampamp) &&
+                (Current.NestingLevel != 0 || !Line.MightBeFunctionDecl ||
+                 TrailingReturnFound))) {
       Current.Type = determineStarAmpUsage(Current,
                                            Contexts.back().CanBeExpression &&
                                                Contexts.back().IsExpression,
@@ -1412,6 +1416,8 @@ private:
         Current.Type = TT_ConditionalExpr;
       }
     } else if (Current.isBinaryOperator() &&
+               !(Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
+                 Current.isOneOf(tok::amp, tok::ampamp)) &&
                (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
                (!Current.is(tok::greater) &&
                 Style.Language != FormatStyle::LK_TextProto)) {
@@ -1486,10 +1492,12 @@ private:
       // colon after this, this is the only place which annotates the identifier
       // as a selector.)
       Current.Type = TT_SelectorName;
-    } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
+    } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::amp,
+                               tok::ampamp) &&
                Current.Previous &&
                !Current.Previous->isOneOf(tok::equal, tok::at) &&
-               Line.MightBeFunctionDecl && Contexts.size() == 1) {
+               Line.MightBeFunctionDecl && !TrailingReturnFound &&
+               Contexts.size() == 1) {
       // Line.MightBeFunctionDecl can only be true after the parentheses of a
       // function declaration have been found.
       Current.Type = TT_TrailingAnnotation;
@@ -1767,6 +1775,7 @@ private:
   AnnotatedLine &Line;
   FormatToken *CurrentToken;
   bool AutoFound;
+  bool TrailingReturnFound;
   const AdditionalKeywords &Keywords;
 
   // Set of "<" tokens that do not open a template parameter list. If parseAngle
@@ -2680,6 +2689,14 @@ bool TokenAnnotator::spaceRequiredBetwee
       Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at))
     // Objective-C dictionary literal -> no space before closing brace.
     return false;
+  if (Right.Type == TT_TrailingAnnotation &&
+      Right.isOneOf(tok::amp, tok::ampamp) &&
+      Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
+      (!Right.Next || Right.Next->is(tok::semi)))
+    // Match const and volatile ref-qualifiers without any additional
+    // qualifiers such as
+    // void Fn() const &;
+    return Style.PointerAlignment != FormatStyle::PAS_Left;
   return true;
 }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=373165&r1=373164&r2=373165&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Sep 29 06:45:38 2019
@@ -6901,6 +6901,74 @@ TEST_F(FormatTest, UnderstandsFunctionRe
                Spaces);
   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
+
+  FormatStyle BreakTemplate = getLLVMStyle();
+  BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int &foo(const std::string &str) & noexcept {}\n"
+               "};",
+               BreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int &foo(const std::string &str) && noexcept {}\n"
+               "};",
+               BreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int &foo(const std::string &str) const & noexcept {}\n"
+               "};",
+               BreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int &foo(const std::string &str) const & noexcept {}\n"
+               "};",
+               BreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  auto foo(const std::string &str) && noexcept -> int & {}\n"
+               "};",
+               BreakTemplate);
+
+  FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
+  AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
+      FormatStyle::BTDS_Yes;
+  AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int& foo(const std::string& str) & noexcept {}\n"
+               "};",
+               AlignLeftBreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int& foo(const std::string& str) && noexcept {}\n"
+               "};",
+               AlignLeftBreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int& foo(const std::string& str) const & noexcept {}\n"
+               "};",
+               AlignLeftBreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  int& foo(const std::string& str) const & noexcept {}\n"
+               "};",
+               AlignLeftBreakTemplate);
+
+  verifyFormat("struct f {\n"
+               "  template <class T>\n"
+               "  auto foo(const std::string& str) && noexcept -> int& {}\n"
+               "};",
+               AlignLeftBreakTemplate);
 }
 
 TEST_F(FormatTest, UnderstandsNewAndDelete) {




More information about the cfe-commits mailing list