[PATCH] Alternative handling of comments adjacent to preprocessor directives.

Alexander Kornienko alexfh at google.com
Tue Apr 2 05:29:53 PDT 2013


Hi klimek, djasper,

Store comments in ScopedLineState

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

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

Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -100,6 +100,7 @@
     Parser.Line.reset(new UnwrappedLine());
     Parser.Line->Level = PreBlockLine->Level;
     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
+    Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
   }
 
   ~ScopedLineState() {
@@ -111,13 +112,15 @@
     Parser.MustBreakBeforeNextToken = true;
     if (SwitchToPreprocessorLines)
       Parser.CurrentLines = &Parser.Lines;
+    Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
   }
 
 private:
   UnwrappedLineParser &Parser;
   const bool SwitchToPreprocessorLines;
 
   UnwrappedLine *PreBlockLine;
+  SmallVector<FormatToken, 1> CommentsBeforeNextToken;
 };
 
 UnwrappedLineParser::UnwrappedLineParser(
@@ -822,7 +825,6 @@
     while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
            ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
             FormatTok.IsFirst)) {
-      flushComments(FormatTok.NewlinesBefore > 0);
       // If there is an unfinished unwrapped line, we flush the preprocessor
       // directives only after that unwrapped line was finished later.
       bool SwitchToPreprocessorLines =
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -708,6 +708,15 @@
                    getLLVMStyleWithColumns(20)));
 }
 
+TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
+  EXPECT_EQ("namespace {}\n// Test\n#define A",
+            format("namespace {}\n   // Test\n#define A"));
+  EXPECT_EQ("namespace {}\n/* Test */\n#define A",
+            format("namespace {}\n   /* Test */\n#define A"));
+  EXPECT_EQ("namespace {}\n/* Test */ #define A",
+            format("namespace {}\n   /* Test */    #define A"));
+}
+
 TEST_F(FormatTest, SplitsLongLinesInComments) {
   EXPECT_EQ("/* This is a long\n"
             " * comment that\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D609.1.patch
Type: text/x-patch
Size: 2129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130402/c0596951/attachment.bin>


More information about the cfe-commits mailing list