MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: curdeius, HazardyKnusperkeks, owenpan, lahwaacz.
MyDeveloperDay added projects: clang, clang-format.
MyDeveloperDay requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116494

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -21172,6 +21172,22 @@
   verifyFormat("A< A< int > >();", Spaces);
   verifyFormat("A<A<int > >();", Spaces);
   verifyFormat("A< A< int>>();", Spaces);
+
+  Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
+  EXPECT_EQ("// clang-format off\n"
+            "foo<<<1, 1>>>();\n"
+            "// clang-format on\n",
+            format("// clang-format off\n"
+                   "foo<<<1, 1>>>();\n"
+                   "// clang-format on\n",
+                   Spaces));
+  EXPECT_EQ("// clang-format off\n"
+            "foo< < <1, 1> > >();\n"
+            "// clang-format on\n",
+            format("// clang-format off\n"
+                   "foo< < <1, 1> > >();\n"
+                   "// clang-format on\n",
+                   Spaces));
 }
 
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3294,6 +3294,10 @@
     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.
 


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -21172,6 +21172,22 @@
   verifyFormat("A< A< int > >();", Spaces);
   verifyFormat("A<A<int > >();", Spaces);
   verifyFormat("A< A< int>>();", Spaces);
+
+  Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
+  EXPECT_EQ("// clang-format off\n"
+            "foo<<<1, 1>>>();\n"
+            "// clang-format on\n",
+            format("// clang-format off\n"
+                   "foo<<<1, 1>>>();\n"
+                   "// clang-format on\n",
+                   Spaces));
+  EXPECT_EQ("// clang-format off\n"
+            "foo< < <1, 1> > >();\n"
+            "// clang-format on\n",
+            format("// clang-format off\n"
+                   "foo< < <1, 1> > >();\n"
+                   "// clang-format on\n",
+                   Spaces));
 }
 
 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3294,6 +3294,10 @@
     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.
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to