Restructuring of token annotation for formatting.

This combines several changes:
* Calculation token type (e.g. for * and &) in the AnnotatingParser.
* Calculate the scope binding strength in the AnnotatingParser.
* Let <> and [] scopes bind stronger than () and {} scopes.
* Add minimal debugging output.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174307 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 6cfa0d8..6d227cc 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -47,6 +47,7 @@
   LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
   LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
+  LLVMStyle.PenaltyExcessCharacter = 1000000;
   return LLVMStyle;
 }
 
@@ -65,6 +66,7 @@
   GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
   GoogleStyle.AllowShortIfStatementsOnASingleLine = false;
   GoogleStyle.ObjCSpaceBeforeProtocolList = false;
+  GoogleStyle.PenaltyExcessCharacter = 1000000;
   return GoogleStyle;
 }
 
@@ -75,11 +77,6 @@
   return ChromiumStyle;
 }
 
-struct OptimizationParameters {
-  unsigned PenaltyIndentLevel;
-  unsigned PenaltyExcessCharacter;
-};
-
 /// \brief Manages the whitespaces around tokens and their replacements.
 ///
 /// This includes special handling for certain constructs, e.g. the alignment of
@@ -216,8 +213,6 @@
       : Style(Style), SourceMgr(SourceMgr), Line(Line),
         FirstIndent(FirstIndent), RootToken(RootToken),
         Whitespaces(Whitespaces) {
-    Parameters.PenaltyIndentLevel = 20;
-    Parameters.PenaltyExcessCharacter = 1000000;
   }
 
   /// \brief Formats an \c UnwrappedLine.
@@ -594,15 +589,17 @@
     // Insert start element into queue.
     std::multimap<unsigned, QueueItem> Queue;
     Queue.insert(std::pair<unsigned, QueueItem>(
-        0, QueueItem(InitialState, Edge(false, (const LineState *) 0))));
+        0, QueueItem(InitialState, Edge(false, (const LineState *)0))));
     std::map<LineState, Edge> Seen;
 
     // While not empty, take first element and follow edges.
     while (!Queue.empty()) {
       unsigned Penalty = Queue.begin()->first;
       QueueItem Item = Queue.begin()->second;
-      if (Item.first.NextToken == NULL)
+      if (Item.first.NextToken == NULL) {
+        DEBUG(llvm::errs() << "\n---\nPenalty for line: " << Penalty << "\n");
         break;
+      }
       Queue.erase(Queue.begin());
 
       if (Seen.find(Item.first) != Seen.end())
@@ -626,9 +623,16 @@
     Edge *CurrentEdge = &Queue.begin()->second.second;
     while (CurrentEdge->second != NULL) {
       LineState CurrentState = *CurrentEdge->second;
+      if (CurrentEdge->first) {
+        DEBUG(llvm::errs() << "Penalty for splitting before "
+                           << CurrentState.NextToken->FormatTok.Tok.getName()
+                           << ": " << CurrentState.NextToken->SplitPenalty
+                           << "\n");
+      }
       addTokenToState(CurrentEdge->first, false, CurrentState);
       CurrentEdge = &Seen[*CurrentEdge->second];
     }
+    DEBUG(llvm::errs() << "---\n");
 
     // Return the column after the last token of the solution.
     return Queue.begin()->second.first.Column;
@@ -647,12 +651,11 @@
       return;
     LineState State(OldState);
     if (NewLine)
-      Penalty += Parameters.PenaltyIndentLevel * State.Stack.size() +
-                 State.NextToken->SplitPenalty;
+      Penalty += State.NextToken->SplitPenalty;
     addTokenToState(NewLine, true, State);
     if (State.Column > getColumnLimit()) {
       unsigned ExcessCharacters = State.Column - getColumnLimit();
-      Penalty += Parameters.PenaltyExcessCharacter * ExcessCharacters;
+      Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
     }
     Queue.insert(std::pair<unsigned, QueueItem>(
         Penalty, QueueItem(State, Edge(NewLine, &OldState))));
@@ -673,7 +676,6 @@
     return true;
   }
 
-
   /// \brief Returns \c true, if a line break after \p State is mandatory.
   bool mustBreak(const LineState &State) {
     if (State.NextToken->MustBreakBefore)
@@ -701,8 +703,6 @@
   const unsigned FirstIndent;
   const AnnotatedToken &RootToken;
   WhitespaceManager &Whitespaces;
-
-  OptimizationParameters Parameters;
 };
 
 class LexerBasedFormatTokenSource : public FormatTokenSource {