From 3a9370cbca9145696eafdc7c8b9849371eecfe01 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 4 Feb 2013 07:21:18 +0000 Subject: 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. llvm-svn: 174307 --- clang/lib/Format/Format.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'clang/lib/Format/Format.cpp') diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 6cfa0d88239..6d227cca627 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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 Queue; Queue.insert(std::pair( - 0, QueueItem(InitialState, Edge(false, (const LineState *) 0)))); + 0, QueueItem(InitialState, Edge(false, (const LineState *)0)))); std::map 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( 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 { -- cgit v1.2.3