r178537 - Alternative handling of comments adjacent to preprocessor directives.

Alexander Kornienko alexfh at google.com
Tue Apr 2 06:04:06 PDT 2013


Author: alexfh
Date: Tue Apr  2 08:04:06 2013
New Revision: 178537

URL: http://llvm.org/viewvc/llvm-project?rev=178537&view=rev
Log:
Alternative handling of comments adjacent to preprocessor directives.

Summary: Store comments in ScopedLineState

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits

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

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=178537&r1=178536&r2=178537&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue Apr  2 08:04:06 2013
@@ -100,6 +100,7 @@ public:
     Parser.Line.reset(new UnwrappedLine());
     Parser.Line->Level = PreBlockLine->Level;
     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
+    Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
   }
 
   ~ScopedLineState() {
@@ -111,6 +112,7 @@ public:
     Parser.MustBreakBeforeNextToken = true;
     if (SwitchToPreprocessorLines)
       Parser.CurrentLines = &Parser.Lines;
+    Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
   }
 
 private:
@@ -118,6 +120,7 @@ private:
   const bool SwitchToPreprocessorLines;
 
   UnwrappedLine *PreBlockLine;
+  SmallVector<FormatToken, 1> CommentsBeforeNextToken;
 };
 
 UnwrappedLineParser::UnwrappedLineParser(
@@ -822,7 +825,6 @@ void UnwrappedLineParser::readToken() {
     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 =

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=178537&r1=178536&r2=178537&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Apr  2 08:04:06 2013
@@ -708,6 +708,15 @@ TEST_F(FormatTest, SplitsLongCxxComments
                    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"





More information about the cfe-commits mailing list