(Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
}
+static bool isComparison(const AnnotatedToken &Tok) {
+ prec::Level Precedence = getPrecedence(Tok);
+ return Tok.Type == TT_BinaryOperator &&
+ (Precedence == prec::Equality || Precedence == prec::Relational);
+}
+
// Returns the length of everything up to the first possible line break after
// the ), ], } or > matching \c Tok.
static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
// Align comment with other comments.
if (Tok.Parent != NULL || !Comments.empty()) {
if (Style.ColumnLimit >=
- Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
+ Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
StoredComment Comment;
Comment.Tok = Tok.FormatTok;
Comment.Spaces = Spaces;
const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
while (Line.rtrim().size() > ColumnLimit) {
// Try to break at the last whitespace before the column limit.
- size_t SpacePos =
- Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
+ size_t SpacePos = Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
if (SpacePos == StringRef::npos) {
// Try to find any whitespace in the line.
SpacePos = Line.find_first_of(WhiteSpaceChars);
if (VariablePos != Other.VariablePos)
return VariablePos < Other.VariablePos;
if (LineContainsContinuedForLoopSection !=
- Other.LineContainsContinuedForLoopSection)
+ Other.LineContainsContinuedForLoopSection)
return LineContainsContinuedForLoopSection;
if (ParenLevel != Other.ParenLevel)
return ParenLevel < Other.ParenLevel;
State.Column = State.Stack.back().FirstLessLess;
} else if (State.ParenLevel != 0 &&
(Previous.isOneOf(tok::equal, tok::coloncolon) ||
- Current.isOneOf(tok::period, tok::arrow, tok::question))) {
+ Current.isOneOf(tok::period, tok::arrow, tok::question) ||
+ isComparison(Previous))) {
// 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.
if (Current.Type == TT_ObjCSelectorName &&
State.Stack.back().ColonPos == 0) {
if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
- State.Column + Spaces + Current.FormatTok.TokenLength)
+ State.Column + Spaces + Current.FormatTok.TokenLength)
State.Stack.back().ColonPos =
State.Stack.back().Indent + Current.LongestObjCSelectorName;
else
// Treat the condition inside an if as if it was a second function
// parameter, i.e. let nested calls have an indent of 4.
State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
- else if (Previous.is(tok::comma) && State.ParenLevel != 0)
+ else if (Previous.is(tok::comma))
// Top-level spaces are exempt as that mostly leads to better results.
State.Stack.back().LastSpace = State.Column;
else if ((Previous.Type == TT_BinaryOperator ||
while (IndentForLevel.size() <= TheLine.Level)
IndentForLevel.push_back(-1);
IndentForLevel.resize(TheLine.Level + 1);
- bool WasMoved =
- PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
+ bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
if (TheLine.First.is(tok::eof)) {
if (PreviousLineWasTouched) {
unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
if (static_cast<int>(Indent) + Offset >= 0)
Indent += Offset;
if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
- Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
- FirstTok.Tok.getLocation()) - 1;
+ Indent = LevelIndent =
+ SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
} else {
formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
PreviousEndOfLineColumn);
isUnaryOperator(*Parent) || Parent->Type == TT_ObjCForIn ||
Parent->Type == TT_CastRParen ||
getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
- prec::Unknown);
+ prec::Unknown);
ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Contexts.back().IsExpression = true;
bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
Tok->Type = TT_ObjCMethodExpr;
Tok->Parent->Type = TT_ObjCSelectorName;
if (Tok->Parent->FormatTok.TokenLength >
- Contexts.back().LongestObjCSelectorName)
+ Contexts.back().LongestObjCSelectorName)
Contexts.back().LongestObjCSelectorName =
Tok->Parent->FormatTok.TokenLength;
if (Contexts.back().FirstObjCSelectorName == NULL)
}
private:
- bool eof() {
- return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
- }
+ bool eof() { return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; }
FormatToken createEOF() {
FormatToken FormatTok;
DEBUG(llvm::dbgs() << "----\n");
readToken();
bool Error = parseFile();
- for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
- E = Lines.end();
+ for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
I != E; ++I) {
Callback.consumeUnwrappedLine(*I);
}
bool UnwrappedLineParser::parseFile() {
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
/*MustBeDeclaration=*/ true);
- bool Error = parseLevel(/*HasOpeningBrace=*/false);
+ bool Error = parseLevel(/*HasOpeningBrace=*/ false);
// Make sure to format the remaining tokens.
flushComments(true);
addUnwrappedLine();
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
MustBeDeclaration);
Line->Level += AddLevels;
- parseLevel(/*HasOpeningBrace=*/true);
+ parseLevel(/*HasOpeningBrace=*/ true);
if (!FormatTok.Tok.is(tok::r_brace)) {
Line->Level -= AddLevels;
return true;
}
- nextToken(); // Munch the closing brace.
+ nextToken(); // Munch the closing brace.
Line->Level -= AddLevels;
return false;
}
// The actual identifier can be a nested name specifier, and in macros
// it is often token-pasted.
while (FormatTok.Tok.is(tok::identifier) ||
- FormatTok.Tok.is(tok::coloncolon) ||
- FormatTok.Tok.is(tok::hashhash))
+ FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash))
nextToken();
// Note that parsing away template declarations here leads to incorrectly
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
nextToken();
- nextToken(); // interface name
+ nextToken(); // interface name
// @interface can be followed by either a base class, or a category.
if (FormatTok.Tok.is(tok::colon)) {
nextToken();
- nextToken(); // base class name
+ nextToken(); // base class name
} else if (FormatTok.Tok.is(tok::l_paren))
// Skip category, if present.
parseParens();
void UnwrappedLineParser::parseObjCProtocol() {
nextToken();
- nextToken(); // protocol name
+ nextToken(); // protocol name
if (FormatTok.Tok.is(tok::less))
parseObjCProtocolList();
}
}
-bool UnwrappedLineParser::eof() const {
- return FormatTok.Tok.is(tok::eof);
-}
+bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); }
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
bool JustComments = Line->Tokens.empty();
FormatTok.IsFirst)) {
// If there is an unfinished unwrapped line, we flush the preprocessor
// directives only after that unwrapped line was finished later.
- bool SwitchToPreprocessorLines = !Line->Tokens.empty() &&
- CurrentLines == &Lines;
+ bool SwitchToPreprocessorLines =
+ !Line->Tokens.empty() && CurrentLines == &Lines;
ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
parsePPDirective();
}
verifyFormat(
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
" ++aaaaaaaaaaa) {\n}");
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
" f();\n"
"}");
verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
- " Intervals[i - 1].getRange().getLast()) {\n}");
+ " Intervals[i - 1].getRange().getLast()) {\n}");
}
TEST_F(FormatTest, BreaksDesireably) {