[PATCH] Don't break string literals inside preprocessor directives.

Alexander Kornienko alexfh at google.com
Wed Oct 2 12:16:34 PDT 2013


Hi djasper,

This way we avoid breaking code which uses unknown preprocessor
directives with long string literals. The specific use case in
http://llvm.org/PR17035 isn't very common, but it seems to be a good idea to
avoid this kind of problem anyway.

http://llvm-reviews.chandlerc.com/D1813

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

Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -689,6 +689,13 @@
                       tok::utf8_string_literal, tok::utf16_string_literal,
                       tok::utf32_string_literal) &&
       Current.Type != TT_ImplicitStringLiteral) {
+    // Don't break string literals inside preprocessor directives.
+    // This way we avoid breaking code with unknown preprocessor directives
+    // that contain long string literals.
+    // FIXME: We may still want to break string literals inside macro
+    // definitions.
+    if (State.Line->Type == LT_PreprocessorDirective)
+      return 0;
     // Exempts unterminated string literals from line breaking. The user will
     // likely want to terminate the string before any line breaking is done.
     if (Current.IsUnterminatedLiteral)
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1849,6 +1849,13 @@
       "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
 }
 
+TEST_F(FormatTest, UnderstandsLineDirectives) {
+  verifyFormat("#1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
+               getLLVMStyleWithColumns(40));
+  verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
+               getLLVMStyleWithColumns(40));
+}
+
 TEST_F(FormatTest, LayoutUnknownPPDirective) {
   EXPECT_EQ("#123 \"A string literal\"",
             format("   #     123    \"A string literal\""));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1813.1.patch
Type: text/x-patch
Size: 1674 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131002/effeb32e/attachment.bin>


More information about the cfe-commits mailing list