aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-03-13 14:41:29 +0000
committerAlexander Kornienko <alexfh@google.com>2013-03-13 14:41:29 +0000
commite74de28ec3692986f3467a7f160a0e293277fa7e (patch)
treebe1f39eacf5470438d7dacdfbb9c42e6c27072b9 /lib/Format/Format.cpp
parentbb3699543e60594af7b5cbdb3b2e9acb816b3687 (diff)
Added AnnotatedToken::isOneOf + a few other refactorings
Summary: <subj> Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D536 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@176951 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp142
1 files changed, 65 insertions, 77 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 74adebe88d..67ca928a1a 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -499,9 +499,8 @@ private:
State.Stack.back().FirstLessLess != 0) {
State.Column = State.Stack.back().FirstLessLess;
} else if (State.ParenLevel != 0 &&
- (Previous.is(tok::equal) || Previous.is(tok::coloncolon) ||
- Current.is(tok::period) || Current.is(tok::arrow) ||
- Current.is(tok::question))) {
+ (Previous.isOneOf(tok::equal, tok::coloncolon) ||
+ Current.isOneOf(tok::period, tok::arrow, tok::question))) {
// Indent and extra 4 spaces after if we know the current expression is
// continued. Don't do that on the top level, as we already indent 4
// there.
@@ -534,7 +533,7 @@ private:
if (Current.is(tok::question))
State.Stack.back().BreakBeforeParameter = true;
- if ((Previous.is(tok::comma) || Previous.is(tok::semi)) &&
+ if (Previous.isOneOf(tok::comma, tok::semi) &&
!State.Stack.back().AvoidBinPacking)
State.Stack.back().BreakBeforeParameter = false;
@@ -562,7 +561,7 @@ private:
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
State.Stack[i].BreakBeforeParameter = true;
}
- if (Current.is(tok::period) || Current.is(tok::arrow))
+ if (Current.isOneOf(tok::period, tok::arrow))
State.Stack.back().BreakBeforeParameter = true;
// If we break after {, we should also break before the corresponding }.
@@ -600,7 +599,7 @@ private:
}
if (Current.Type != TT_LineComment &&
- (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
+ (Previous.isOneOf(tok::l_paren, tok::l_brace) ||
State.NextToken->Parent->Type == TT_TemplateOpener))
State.Stack.back().Indent = State.Column + Spaces;
if (Previous.is(tok::comma) && !isTrailingComment(Current))
@@ -622,8 +621,7 @@ private:
else if (Previous.Type == TT_InheritanceColon)
State.Stack.back().Indent = State.Column;
else if (Previous.ParameterCount > 1 &&
- (Previous.is(tok::l_paren) || Previous.is(tok::l_square) ||
- Previous.is(tok::l_brace) ||
+ (Previous.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
Previous.Type == TT_TemplateOpener))
// If this function has multiple parameters, indent nested calls from
// the start of the first parameter.
@@ -645,7 +643,7 @@ private:
State.Stack.back().FirstLessLess = State.Column;
if (Current.is(tok::question))
State.Stack.back().QuestionColumn = State.Column;
- if ((Current.is(tok::period) || Current.is(tok::arrow)) &&
+ if (Current.isOneOf(tok::period, tok::arrow) &&
Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
State.Stack.back().StartOfFunctionCall =
Current.LastInChainOfCalls ? 0 : State.Column;
@@ -665,8 +663,7 @@ private:
// If we encounter an opening (, [, { or <, we add a level to our stacks to
// prepare for the following tokens.
- if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
- Current.is(tok::l_brace) ||
+ if (Current.isOneOf(tok::l_paren, tok::l_square, tok::l_brace) ||
State.NextToken->Type == TT_TemplateOpener) {
unsigned NewIndent;
bool AvoidBinPacking;
@@ -695,7 +692,7 @@ private:
// If we encounter a closing ), ], } or >, we can remove a level from our
// stacks.
- if (Current.is(tok::r_paren) || Current.is(tok::r_square) ||
+ if (Current.isOneOf(tok::r_paren, tok::r_square) ||
(Current.is(tok::r_brace) && State.NextToken != &RootToken) ||
State.NextToken->Type == TT_TemplateCloser) {
State.Stack.pop_back();
@@ -985,8 +982,7 @@ private:
if (State.NextToken->Parent->is(tok::semi) &&
State.LineContainsContinuedForLoopSection)
return true;
- if ((State.NextToken->Parent->is(tok::comma) ||
- State.NextToken->Parent->is(tok::semi) ||
+ if ((State.NextToken->Parent->isOneOf(tok::comma, tok::semi) ||
State.NextToken->is(tok::question) ||
State.NextToken->Type == TT_ConditionalExpr) &&
State.Stack.back().BreakBeforeParameter &&
@@ -1126,43 +1122,6 @@ public:
virtual ~Formatter() {}
- void deriveLocalStyle() {
- unsigned CountBoundToVariable = 0;
- unsigned CountBoundToType = 0;
- bool HasCpp03IncompatibleFormat = false;
- for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
- if (AnnotatedLines[i].First.Children.empty())
- continue;
- AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
- while (!Tok->Children.empty()) {
- if (Tok->Type == TT_PointerOrReference) {
- bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
- bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
- if (SpacesBefore && !SpacesAfter)
- ++CountBoundToVariable;
- else if (!SpacesBefore && SpacesAfter)
- ++CountBoundToType;
- }
-
- if (Tok->Type == TT_TemplateCloser &&
- Tok->Parent->Type == TT_TemplateCloser &&
- Tok->FormatTok.WhiteSpaceLength == 0)
- HasCpp03IncompatibleFormat = true;
- Tok = &Tok->Children[0];
- }
- }
- if (Style.DerivePointerBinding) {
- if (CountBoundToType > CountBoundToVariable)
- Style.PointerBindsToType = true;
- else if (CountBoundToType < CountBoundToVariable)
- Style.PointerBindsToType = false;
- }
- if (Style.Standard == FormatStyle::LS_Auto) {
- Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
- : FormatStyle::LS_Cpp03;
- }
- }
-
tooling::Replacements format() {
LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
@@ -1183,16 +1142,16 @@ public:
E = AnnotatedLines.end();
I != E; ++I) {
const AnnotatedLine &TheLine = *I;
+ const FormatToken &FirstTok = TheLine.First.FormatTok;
int Offset = getIndentOffset(TheLine.First);
while (IndentForLevel.size() <= TheLine.Level)
IndentForLevel.push_back(-1);
IndentForLevel.resize(TheLine.Level + 1);
bool WasMoved =
- PreviousLineWasTouched && TheLine.First.FormatTok.NewlinesBefore == 0;
+ PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
if (TheLine.First.is(tok::eof)) {
if (PreviousLineWasTouched) {
- unsigned NewLines =
- std::min(TheLine.First.FormatTok.NewlinesBefore, 1u);
+ unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
Whitespaces.replaceWhitespace(TheLine.First, NewLines, /*Indent*/ 0,
/*WhitespaceStartColumn*/ 0, Style);
}
@@ -1202,10 +1161,9 @@ public:
unsigned Indent = LevelIndent;
if (static_cast<int>(Indent) + Offset >= 0)
Indent += Offset;
- if (!TheLine.First.FormatTok.WhiteSpaceStart.isValid() ||
- StructuralError) {
+ if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
- TheLine.First.FormatTok.Tok.getLocation()) - 1;
+ FirstTok.Tok.getLocation()) - 1;
} else {
formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
PreviousEndOfLineColumn);
@@ -1219,10 +1177,9 @@ public:
IndentForLevel[TheLine.Level] = LevelIndent;
PreviousLineWasTouched = true;
} else {
- if (TheLine.First.FormatTok.NewlinesBefore > 0 ||
- TheLine.First.FormatTok.IsFirst) {
- unsigned Indent = SourceMgr.getSpellingColumnNumber(
- TheLine.First.FormatTok.Tok.getLocation()) - 1;
+ if (FirstTok.NewlinesBefore > 0 || FirstTok.IsFirst) {
+ unsigned Indent =
+ SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
unsigned LevelIndent = Indent;
if (static_cast<int>(LevelIndent) - Offset >= 0)
LevelIndent -= Offset;
@@ -1236,11 +1193,10 @@ public:
// If we did not reformat this unwrapped line, the column at the end of
// the last token is unchanged - thus, we can calculate the end of the
// last token.
+ SourceLocation LastLoc = TheLine.Last->FormatTok.Tok.getLocation();
PreviousEndOfLineColumn =
- SourceMgr.getSpellingColumnNumber(
- TheLine.Last->FormatTok.Tok.getLocation()) +
- Lex.MeasureTokenLength(TheLine.Last->FormatTok.Tok.getLocation(),
- SourceMgr, Lex.getLangOpts()) - 1;
+ SourceMgr.getSpellingColumnNumber(LastLoc) +
+ Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
PreviousLineWasTouched = false;
}
}
@@ -1248,6 +1204,43 @@ public:
}
private:
+ void deriveLocalStyle() {
+ unsigned CountBoundToVariable = 0;
+ unsigned CountBoundToType = 0;
+ bool HasCpp03IncompatibleFormat = false;
+ for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
+ if (AnnotatedLines[i].First.Children.empty())
+ continue;
+ AnnotatedToken *Tok = &AnnotatedLines[i].First.Children[0];
+ while (!Tok->Children.empty()) {
+ if (Tok->Type == TT_PointerOrReference) {
+ bool SpacesBefore = Tok->FormatTok.WhiteSpaceLength > 0;
+ bool SpacesAfter = Tok->Children[0].FormatTok.WhiteSpaceLength > 0;
+ if (SpacesBefore && !SpacesAfter)
+ ++CountBoundToVariable;
+ else if (!SpacesBefore && SpacesAfter)
+ ++CountBoundToType;
+ }
+
+ if (Tok->Type == TT_TemplateCloser &&
+ Tok->Parent->Type == TT_TemplateCloser &&
+ Tok->FormatTok.WhiteSpaceLength == 0)
+ HasCpp03IncompatibleFormat = true;
+ Tok = &Tok->Children[0];
+ }
+ }
+ if (Style.DerivePointerBinding) {
+ if (CountBoundToType > CountBoundToVariable)
+ Style.PointerBindsToType = true;
+ else if (CountBoundToType < CountBoundToVariable)
+ Style.PointerBindsToType = false;
+ }
+ if (Style.Standard == FormatStyle::LS_Auto) {
+ Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
+ : FormatStyle::LS_Cpp03;
+ }
+ }
+
/// \brief Get the indent of \p Level from \p IndentForLevel.
///
/// \p IndentForLevel must contain the indent for the level \c l
@@ -1267,8 +1260,7 @@ private:
/// characters to the left from their level.
int getIndentOffset(const AnnotatedToken &RootToken) {
bool IsAccessModifier = false;
- if (RootToken.is(tok::kw_public) || RootToken.is(tok::kw_protected) ||
- RootToken.is(tok::kw_private))
+ if (RootToken.isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
IsAccessModifier = true;
else if (RootToken.is(tok::at) && !RootToken.Children.empty() &&
(RootToken.Children[0].isObjCAtKeyword(tok::objc_public) ||
@@ -1361,15 +1353,11 @@ private:
// we're not in a control flow statement and the last token is an opening
// brace.
AnnotatedLine &Line = *I;
- bool AllowedTokens =
- Line.First.isNot(tok::kw_if) && Line.First.isNot(tok::kw_while) &&
- Line.First.isNot(tok::kw_do) && Line.First.isNot(tok::r_brace) &&
- Line.First.isNot(tok::kw_else) && Line.First.isNot(tok::kw_try) &&
- Line.First.isNot(tok::kw_catch) && Line.First.isNot(tok::kw_for) &&
- // This gets rid of all ObjC @ keywords and methods.
- Line.First.isNot(tok::at) && Line.First.isNot(tok::minus) &&
- Line.First.isNot(tok::plus);
- if (!AllowedTokens)
+ if (Line.First.isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
+ tok::kw_else, tok::kw_try, tok::kw_catch,
+ tok::kw_for,
+ // This gets rid of all ObjC @ keywords and methods.
+ tok::at, tok::minus, tok::plus))
return;
AnnotatedToken *Tok = &(I + 1)->First;
@@ -1391,7 +1379,7 @@ private:
if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
return;
do {
- if (Tok->is(tok::l_brace) || Tok->is(tok::r_brace))
+ if (Tok->isOneOf(tok::l_brace, tok::r_brace))
return;
Tok = Tok->Children.empty() ? NULL : &Tok->Children.back();
} while (Tok != NULL);