[cfe-commits] r171713 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Mon Jan 7 00:54:53 PST 2013


Author: klimek
Date: Mon Jan  7 02:54:53 2013
New Revision: 171713

URL: http://llvm.org/viewvc/llvm-project?rev=171713&view=rev
Log:
Fix layouting of single-line-comments preceded by an escaped newline.

Previously, we'd format
  int i;\
  // comment
as
  int i; // comment

The problem is that the escaped newline is part of the next token, and
thus the raw token text of the comment doesn't start with "//".

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171713&r1=171712&r2=171713&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan  7 02:54:53 2013
@@ -520,8 +520,8 @@
 class TokenAnnotator {
 public:
   TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
-                 SourceManager &SourceMgr)
-      : Line(Line), Style(Style), SourceMgr(SourceMgr) {
+                 SourceManager &SourceMgr, Lexer &Lex)
+      : Line(Line), Style(Style), SourceMgr(SourceMgr), Lex(Lex) {
   }
 
   /// \brief A parser that gathers additional information about tokens.
@@ -865,10 +865,9 @@
       } else if (isBinaryOperator(Line.Tokens[i])) {
         Annotation.Type = TokenAnnotation::TT_BinaryOperator;
       } else if (Tok.Tok.is(tok::comment)) {
-        // FIXME: Use Lexer::getSpelling(Tok, SourceMgr, LangOpts, bool*);
-        StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
-                       Tok.Tok.getLength());
-        if (Data.startswith("//"))
+        std::string Data(
+            Lexer::getSpelling(Tok.Tok, SourceMgr, Lex.getLangOpts()));
+        if (StringRef(Data).startswith("//"))
           Annotation.Type = TokenAnnotation::TT_LineComment;
         else
           Annotation.Type = TokenAnnotation::TT_BlockComment;
@@ -1012,6 +1011,7 @@
   const UnwrappedLine &Line;
   FormatStyle Style;
   SourceManager &SourceMgr;
+  Lexer &Lex;
   std::vector<TokenAnnotation> Annotations;
 };
 
@@ -1142,7 +1142,7 @@
                                               LineRange.getBegin()))
         continue;
 
-      TokenAnnotator Annotator(TheLine, Style, SourceMgr);
+      TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
       if (!Annotator.annotate())
         break;
       UnwrappedLineFormatter Formatter(

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171713&r1=171712&r2=171713&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan  7 02:54:53 2013
@@ -290,6 +290,9 @@
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;  // Trailing comment");
+
+  EXPECT_EQ("int i;  // single line trailing comment",
+            format("int i;\\\n// single line trailing comment"));
 }
 
 TEST_F(FormatTest, UnderstandsMultiLineComments) {





More information about the cfe-commits mailing list