aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-02-04 07:21:18 +0000
committerDaniel Jasper <djasper@google.com>2013-02-04 07:21:18 +0000
commit0178673f541685cf5067814dfeee2644078e39a9 (patch)
tree23dbeb42fb6c9e03f277b901bd59a206c2a9dbee /lib/Format/Format.cpp
parent22c0cbee5bb2150841e0046354fd37ba22de747a (diff)
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
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp30
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 {