[clang] cd2b050 - [clang-format] spacesRequiredBetween is not honouring clang-format off/on

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 3 03:19:33 PST 2022


Author: mydeveloperday
Date: 2022-01-03T11:19:02Z
New Revision: cd2b050fa4995b75b9c36fae16c0d9f105b67585

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

LOG: [clang-format] spacesRequiredBetween is not honouring clang-format off/on

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

It seems that clang-format off/on is not being honoured in regard to adding spaces.

My understanding of clang-format off/on is that it marks the token as finalized based on whether formatting is currently enabled or disabled.

This was causing a space to be added between the `<` and `<<`  in the Cuda kernel `foo<<<1, 1>>>();`

This if doesn't solve this actual issue but ensure that clang-format is at least honoured.

Reviewed By: curdeius, owenpan

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

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 505a7250572b..914997a54989 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3294,6 +3294,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
   };
 
+  // If the token is finalized don't touch it (as it could be in a
+  // clang-format-off section).
+  if (Left.Finalized)
+    return HasExistingWhitespace();
+
   if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
     return true; // Never ever merge two identifiers.
 

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7160c7a90073..7a7976c8b081 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21156,6 +21156,16 @@ TEST_F(FormatTest, SpacesInAngles) {
   verifyFormat("A< A< int > >();", Spaces);
   verifyFormat("A<A<int > >();", Spaces);
   verifyFormat("A< A< int>>();", Spaces);
+
+  Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
+  verifyFormat("// clang-format off\n"
+               "foo<<<1, 1>>>();\n"
+               "// clang-format on\n",
+               Spaces);
+  verifyFormat("// clang-format off\n"
+               "foo< < <1, 1> > >();\n"
+               "// clang-format on\n",
+               Spaces);
 }
 
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {


        


More information about the cfe-commits mailing list