[PATCH] Even better way to handle comments adjacent to preprocessor directives.

Alexander Kornienko alexfh at google.com
Wed Apr 3 05:40:56 PDT 2013


  Rephrased the comment.

Hi klimek,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D617?vs=1492&id=1493#toc

BRANCH
  svn

ARCANIST PROJECT
  clang

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

Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -100,7 +100,6 @@
     Parser.Line.reset(new UnwrappedLine());
     Parser.Line->Level = PreBlockLine->Level;
     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
-    Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
   }
 
   ~ScopedLineState() {
@@ -112,15 +111,13 @@
     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(
@@ -830,6 +827,10 @@
       bool SwitchToPreprocessorLines =
           !Line->Tokens.empty() && CurrentLines == &Lines;
       ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
+      // Comments stored before the preprocessor directive need to be output
+      // before the preprocessor directive, at the same level as the
+      // preprocessor directive, as we consider them to apply to the directive.
+      flushComments(FormatTok.NewlinesBefore > 0);
       parsePPDirective();
     }
     if (!FormatTok.Tok.is(tok::comment))
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -592,6 +592,14 @@
   verifyGoogleFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81 cols with this comment");
+  EXPECT_EQ("D(a, {\n"
+            "  // test\n"
+            "  int a;\n"
+            "});",
+            format("D(a, {\n"
+                   "// test\n"
+                   "int a;\n"
+                   "});"));
 }
 
 TEST_F(FormatTest, CanFormatCommentsLocally) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D617.2.patch
Type: text/x-patch
Size: 2056 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130403/3e8ac4d7/attachment.bin>


More information about the cfe-commits mailing list