diff options
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r-- | lib/Format/Format.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6cfa0d8823..6d227cca62 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -47,6 +47,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.ObjCSpaceBeforeProtocolList = true; + LLVMStyle.PenaltyExcessCharacter = 1000000; return LLVMStyle; } @@ -65,6 +66,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; GoogleStyle.AllowShortIfStatementsOnASingleLine = false; GoogleStyle.ObjCSpaceBeforeProtocolList = false; + GoogleStyle.PenaltyExcessCharacter = 1000000; return GoogleStyle; } @@ -75,11 +77,6 @@ FormatStyle getChromiumStyle() { 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 @@ public: : 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 @@ private: // 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 @@ private: 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 @@ private: 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 @@ private: 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 @@ private: const unsigned FirstIndent; const AnnotatedToken &RootToken; WhitespaceManager &Whitespaces; - - OptimizationParameters Parameters; }; class LexerBasedFormatTokenSource : public FormatTokenSource { |