Add extra indentation for multiline comparisons.

This seems to be generally more desired.

Before:
if (aaaaaaaa &&
    bbbbbbbb >
    cccccccc) {}
After:
if (aaaaaaaa &&
    bbbbbbbb >
        cccccccc) {}

Also: Some formatting cleanup on clang-format's files.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177514 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Jasper 2013-03-20 12:37:50 +00:00
Родитель bcca7e4841
Коммит f9955d309d
4 изменённых файлов: 33 добавлений и 34 удалений

Просмотреть файл

@ -85,6 +85,12 @@ static bool isTrailingComment(const AnnotatedToken &Tok) {
(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) {
@ -229,8 +235,7 @@ private:
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);
@ -621,7 +626,8 @@ private:
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.
@ -729,7 +735,7 @@ private:
// 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 ||
@ -1283,8 +1289,7 @@ public:
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);
@ -1298,8 +1303,8 @@ public:
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);

Просмотреть файл

@ -71,9 +71,7 @@ public:
}
private:
bool eof() {
return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
}
bool eof() { return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; }
FormatToken createEOF() {
FormatToken FormatTok;
@ -133,8 +131,7 @@ bool UnwrappedLineParser::parse() {
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);
}
@ -673,8 +670,7 @@ void UnwrappedLineParser::parseRecord() {
// 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
@ -791,9 +787,7 @@ void UnwrappedLineParser::addUnwrappedLine() {
}
}
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();
@ -829,8 +823,8 @@ void UnwrappedLineParser::readToken() {
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();
}

Просмотреть файл

@ -324,7 +324,7 @@ TEST_F(FormatTest, FormatsForLoop) {
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
" ++aaaaaaaaaaa) {\n}");
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"