[PATCH] clang-format: Change location of stashed token

Jacques Pienaar jpienaar at google.com
Tue Feb 24 14:57:30 PST 2015


Hi djasper, kcc,

LessLess and GreaterGreater are turned into two tokens via the stashed token approach. The location of the stashed token (the second Less or Greater) was assumed to be one past the first Less or Greater. This does not hold in cases such as
printf "\\\\\x0a<<<" | clang-format 
Where LessLess is considered a token of length 4 and the second Less (post splitting) starts at an offset of 3 and not 1 from the first one. This results in an error when attempting to insert a space between the first and second Less.

http://reviews.llvm.org/D7871

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

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -940,11 +940,13 @@
     FormatTok = new (Allocator.Allocate()) FormatToken;
     FormatTok->Tok = Tok;
     SourceLocation TokLocation =
-        FormatTok->Tok.getLocation().getLocWithOffset(1);
+        FormatTok->Tok.getLocation().getLocWithOffset(Tok.getLength() - 1);
+    FormatTok->Tok.setLocation(TokLocation);
     FormatTok->WhitespaceRange = SourceRange(TokLocation, TokLocation);
     FormatTok->TokenText = TokenText;
     FormatTok->ColumnWidth = 1;
-    FormatTok->OriginalColumn = OriginalColumn;
+    FormatTok->OriginalColumn = OriginalColumn + 1;
+
     return FormatTok;
   }
 
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -9753,6 +9753,7 @@
 
 TEST_F(FormatTest, MergeLessLessAtEnd) {
   verifyFormat("<<");
+  EXPECT_EQ("< < <", format("\\\n<<<"));
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                "aaallvm::outs() <<");
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7871.20630.patch
Type: text/x-patch
Size: 1261 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150224/fc598d7d/attachment.bin>


More information about the cfe-commits mailing list