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

Manuel Klimek klimek at google.com
Wed Jan 16 06:55:28 PST 2013


Author: klimek
Date: Wed Jan 16 08:55:28 2013
New Revision: 172616

URL: http://llvm.org/viewvc/llvm-project?rev=172616&view=rev
Log:
Add debugging support for split penalties.

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.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=172616&r1=172615&r2=172616&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Jan 16 08:55:28 2013
@@ -16,15 +16,21 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "clang/Format/Format.h"
+#define DEBUG_TYPE "format-formatter"
+
 #include "UnwrappedLineParser.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/OperatorPrecedence.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Format/Format.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/Support/Debug.h"
 #include <string>
 
+// Uncomment to get debug output from tests:
+// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
+
 namespace clang {
 namespace format {
 
@@ -246,6 +252,10 @@
     State.LineContainsContinuedForLoopSection = false;
     State.StartOfLineLevel = 1;
 
+    DEBUG({
+      DebugTokenState(*State.NextToken);
+    });
+
     // The first token has already been indented and thus consumed.
     moveStateToNextToken(State);
 
@@ -262,6 +272,18 @@
       } else {
         unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
         unsigned Break = calcPenalty(State, true, NoBreak);
+        DEBUG({
+          if (Break < NoBreak)
+            llvm::errs() << "\n";
+          else
+            llvm::errs() << " ";
+          llvm::errs() << "<";
+          DebugPenalty(Break, Break < NoBreak);
+          llvm::errs() << "/";
+          DebugPenalty(NoBreak, !(Break < NoBreak));
+          llvm::errs() << "> ";
+          DebugTokenState(*State.NextToken);
+        });
         addTokenToState(Break < NoBreak, false, State);
         if (State.NextToken != NULL &&
             State.NextToken->Parent->Type == TT_CtorInitializerColon) {
@@ -271,10 +293,28 @@
         }
       }
     }
+    DEBUG(llvm::errs() << "\n");
     return State.Column;
   }
 
 private:
+  void DebugTokenState(const AnnotatedToken &AnnotatedTok) {
+    const Token &Tok = AnnotatedTok.FormatTok.Tok;
+    llvm::errs()
+        << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
+                     Tok.getLength());
+    llvm::errs();
+  }
+
+  void DebugPenalty(unsigned Penalty, bool Winner) {
+    llvm::errs().changeColor(Winner ? raw_ostream::GREEN : raw_ostream::RED);
+    if (Penalty == UINT_MAX)
+      llvm::errs() << "MAX";
+    else
+      llvm::errs() << Penalty;
+    llvm::errs().resetColor();
+  }
+
   struct ParenState {
     ParenState(unsigned Indent, unsigned LastSpace)
         : Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=172616&r1=172615&r2=172616&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Jan 16 08:55:28 2013
@@ -20,7 +20,6 @@
 
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/Support/raw_ostream.h"
 #include "UnwrappedLineParser.h"
 
 // Uncomment to get debug output from tests:

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172616&r1=172615&r2=172616&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 16 08:55:28 2013
@@ -7,10 +7,16 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "format-test"
+
 #include "clang/Format/Format.h"
-#include "../Tooling/RewriterTestContext.h"
 #include "clang/Lex/Lexer.h"
 #include "gtest/gtest.h"
+#include "llvm/Support/Debug.h"
+#include "../Tooling/RewriterTestContext.h"
+
+// Uncomment to get debug output from tests:
+// #define DEBUG_WITH_TYPE(T, X) do { X; } while(0)
 
 namespace clang {
 namespace format {
@@ -19,6 +25,7 @@
 protected:
   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
                      const FormatStyle &Style) {
+    DEBUG(llvm::errs() << "---\n");
     RewriterTestContext Context;
     FileID ID = Context.createInMemoryFile("input.cc", Code);
     SourceLocation Start =
@@ -32,6 +39,7 @@
                                              Ranges,
                                              new IgnoringDiagConsumer());
     EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
+    DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n");
     return Context.getRewrittenText(ID);
   }
 





More information about the cfe-commits mailing list