breakToken -> replaceWhitespaceInToken

Hi klimek,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D949?vs=2337&id=2353#toc

Files:
  lib/Format/BreakableToken.cpp
  lib/Format/BreakableToken.h
  lib/Format/WhitespaceManager.cpp
  lib/Format/WhitespaceManager.h
  unittests/Format/FormatTest.cpp
Index: lib/Format/BreakableToken.cpp
===================================================================
--- lib/Format/BreakableToken.cpp
+++ lib/Format/BreakableToken.cpp
@@ -16,6 +16,7 @@
 #define DEBUG_TYPE "format-token-breaker"
 
 #include "BreakableToken.h"
+#include "clang/Basic/CharInfo.h"
 #include "clang/Format/Format.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
@@ -122,9 +123,9 @@
                                            unsigned TailOffset, Split Split,
                                            bool InPPDirective,
                                            WhitespaceManager &Whitespaces) {
-  Whitespaces.breakToken(Tok, Prefix.size() + TailOffset + Split.first,
-                         Split.second, Postfix, Prefix, InPPDirective,
-                         StartColumn);
+  Whitespaces.replaceWhitespaceInToken(
+      Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
+      Prefix, InPPDirective, 1, StartColumn);
 }
 
 BreakableSingleLineToken::BreakableSingleLineToken(const FormatToken &Tok,
@@ -164,15 +165,42 @@
                                            encoding::Encoding Encoding)
     : BreakableSingleLineToken(Token, StartColumn,
                                getLineCommentPrefix(Token.TokenText), "",
-                               Encoding) {}
+                               Encoding) {
+  OriginalPrefix = Prefix;
+  if (Token.TokenText.size() > Prefix.size() &&
+      isAlphanumeric(Token.TokenText[Prefix.size()])) {
+    if (Prefix == "//")
+      Prefix = "// ";
+    else if (Prefix == "///")
+      Prefix = "/// ";
+  }
+}
 
 BreakableToken::Split
 BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
                                unsigned ColumnLimit) const {
   return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
                          ColumnLimit, Encoding);
 }
 
+void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
+                                       Split Split, bool InPPDirective,
+                                       WhitespaceManager &Whitespaces) {
+  Whitespaces.replaceWhitespaceInToken(
+      Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
+      Postfix, Prefix, InPPDirective, 1, StartColumn);
+}
+
+void
+BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex,
+                                              unsigned InPPDirective,
+                                              WhitespaceManager &Whitespaces) {
+  if (OriginalPrefix != Prefix) {
+    Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
+                                         false, 0, 1);
+  }
+}
+
 BreakableBlockComment::BreakableBlockComment(
     const FormatStyle &Style, const FormatToken &Token, unsigned StartColumn,
     unsigned OriginalStartColumn, bool FirstInLine, encoding::Encoding Encoding)
@@ -299,8 +327,9 @@
       Text.data() - Tok.TokenText.data() + Split.first;
   unsigned CharsToRemove = Split.second;
   assert(IndentAtLineBreak >= Decoration.size());
-  Whitespaces.breakToken(Tok, BreakOffsetInToken, CharsToRemove, "", Prefix,
-                         InPPDirective, IndentAtLineBreak - Decoration.size());
+  Whitespaces.replaceWhitespaceInToken(Tok, BreakOffsetInToken, CharsToRemove,
+                                       "", Prefix, InPPDirective, 1,
+                                       IndentAtLineBreak - Decoration.size());
 }
 
 void
@@ -331,9 +360,9 @@
       Lines[LineIndex].data() - Tok.TokenText.data() -
       LeadingWhitespace[LineIndex];
   assert(StartOfLineColumn[LineIndex] >= Prefix.size());
-  Whitespaces.breakToken(
+  Whitespaces.replaceWhitespaceInToken(
       Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
-      InPPDirective, StartOfLineColumn[LineIndex] - Prefix.size());
+      InPPDirective, 1, StartOfLineColumn[LineIndex] - Prefix.size());
 }
 
 unsigned
Index: lib/Format/BreakableToken.h
===================================================================
--- lib/Format/BreakableToken.h
+++ lib/Format/BreakableToken.h
@@ -116,6 +116,9 @@
 };
 
 class BreakableLineComment : public BreakableSingleLineToken {
+  // The prefix without an additional space if one was added.
+  StringRef OriginalPrefix;
+
 public:
   /// \brief Creates a breakable token for a line comment.
   ///
@@ -126,6 +129,11 @@
 
   virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
                          unsigned ColumnLimit) const;
+  virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
+                           bool InPPDirective, WhitespaceManager &Whitespaces);
+  virtual void replaceWhitespaceBefore(unsigned LineIndex,
+                                       unsigned InPPDirective,
+                                       WhitespaceManager &Whitespaces);
 };
 
 class BreakableBlockComment : public BreakableToken {
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -56,16 +56,15 @@
              InPPDirective && !Tok.IsFirst));
 }
 
-void WhitespaceManager::breakToken(const FormatToken &Tok, unsigned Offset,
-                                   unsigned ReplaceChars,
-                                   StringRef PreviousPostfix,
-                                   StringRef CurrentPrefix, bool InPPDirective,
-                                   unsigned Spaces) {
+void WhitespaceManager::replaceWhitespaceInToken(
+    const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
+    StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
+    unsigned NewLines, unsigned Spaces) {
   Changes.push_back(Change(
       true, SourceRange(Tok.getStartOfNonWhitespace().getLocWithOffset(Offset),
                         Tok.getStartOfNonWhitespace().getLocWithOffset(
                             Offset + ReplaceChars)),
-      Spaces, Spaces, 1, PreviousPostfix, CurrentPrefix,
+      Spaces, Spaces, NewLines, PreviousPostfix, CurrentPrefix,
       // FIXME: Unify token adjustment, so we don't split it between
       // BreakableToken and the WhitespaceManager. That would also allow us to
       // correctly store a tok::TokenKind instead of rolling our own enum.
Index: lib/Format/WhitespaceManager.h
===================================================================
--- lib/Format/WhitespaceManager.h
+++ lib/Format/WhitespaceManager.h
@@ -52,21 +52,26 @@
   /// was not called.
   void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
 
-  /// \brief Inserts a line break into the middle of a token.
+  /// \brief Inserts or replaces whitespace in the middle of a token.
   ///
-  /// Will break at \p Offset inside \p Tok, putting \p PreviousPostfix before
-  /// the line break and \p CurrentPrefix before the rest of the token starts in
-  /// the next line.
+  /// Inserts \p PreviousPostfix, \p NewLines, \p Spaces and \p CurrentPrefix
+  /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
+  /// characters.
   ///
-  /// \p InPPDirective and \p Spaces are used to generate the correct line
-  /// break.
-  void breakToken(const FormatToken &Tok, unsigned Offset,
-                  unsigned ReplaceChars, StringRef PreviousPostfix,
-                  StringRef CurrentPrefix, bool InPPDirective, unsigned Spaces);
+  /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
+  /// used to align backslashes correctly.
+  void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
+                                unsigned ReplaceChars,
+                                StringRef PreviousPostfix,
+                                StringRef CurrentPrefix, bool InPPDirective,
+                                unsigned NewLines, unsigned Spaces);
 
   /// \brief Returns all the \c Replacements created during formatting.
   const tooling::Replacements &generateReplacements();
 
+  /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
+  void storeReplacement(const SourceRange &Range, StringRef Text);
+
 private:
   /// \brief Represents a change before a token, a break inside a token,
   /// or the layout of an unchanged token (or whitespace within).
@@ -149,8 +154,6 @@
   /// \brief Fill \c Replaces with the replacements for all effective changes.
   void generateChanges();
 
-  /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
-  void storeReplacement(const SourceRange &Range, StringRef Text);
   std::string getNewLineText(unsigned NewLines, unsigned Spaces);
   std::string getNewLineText(unsigned NewLines, unsigned Spaces,
                              unsigned PreviousEndOfTokenColumn,
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -860,10 +860,15 @@
   EXPECT_EQ("//    Don't_touch_leading_whitespace",
             format("//    Don't_touch_leading_whitespace",
                    getLLVMStyleWithColumns(20)));
-  EXPECT_EQ(
-      "//Don't add leading\n"
-      "//whitespace",
-      format("//Don't add leading whitespace", getLLVMStyleWithColumns(20)));
+  EXPECT_EQ("// Add leading\n"
+            "// whitespace",
+            format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
+  EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
+  EXPECT_EQ("// Even if it makes the line exceed the column\n"
+            "// limit",
+            format("//Even if it makes the line exceed the column limit",
+                   getLLVMStyleWithColumns(51)));
+  EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
   EXPECT_EQ("// A comment before\n"
             "// a macro\n"
             "// definition\n"
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to