[clang] 07740dd - [clang-format] [PR44476] Add space between template and attribute

via cfe-commits cfe-commits at lists.llvm.org
Tue May 19 08:44:39 PDT 2020


Author: mydeveloperday
Date: 2020-05-19T16:43:50+01:00
New Revision: 07740dd08b572af26d9fdeed2615f3a6996bee22

URL: https://github.com/llvm/llvm-project/commit/07740dd08b572af26d9fdeed2615f3a6996bee22
DIFF: https://github.com/llvm/llvm-project/commit/07740dd08b572af26d9fdeed2615f3a6996bee22.diff

LOG: [clang-format] [PR44476] Add space between template and attribute

Summary:
https://bugs.llvm.org/show_bug.cgi?id=44476

```template <typename T> [[nodiscard]] int a() { return 1; }```

gets incorrectly formatted to be

```template <typename T>[[nodiscard]] int a() { return 1; }```

This revision ensure there is a space between the template and the attribute

Reviewed By: JakeMerdichAMD

Subscribers: cfe-commits

Tags: #clang, #clang-format

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

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 043859a2f5c0..2a2c4d754732 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2896,6 +2896,11 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     // No whitespace in x(/*foo=*/1), except for JavaScript.
     return Style.Language == FormatStyle::LK_JavaScript ||
            !Left.TokenText.endswith("=*/");
+
+  // Space between template and attribute.
+  // e.g. template <typename T> [[nodiscard]] ...
+  if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
+    return true;
   if (Right.is(tok::l_paren)) {
     if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
         (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d128f4b610c9..8da99f2f7c82 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8005,6 +8005,8 @@ TEST_F(FormatTest, UnderstandsSquareAttributes) {
   verifyFormat("@[ [NSArray class] ];");
   verifyFormat("@[ [foo enum] ];");
 
+  verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
+
   // Make sure we do not parse attributes as lambda introducers.
   FormatStyle MultiLineFunctions = getLLVMStyle();
   MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;


        


More information about the cfe-commits mailing list