зеркало из https://github.com/microsoft/clang-1.git
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:
Родитель
bcca7e4841
Коммит
f9955d309d
|
@ -85,6 +85,12 @@ static bool isTrailingComment(const AnnotatedToken &Tok) {
|
||||||
(Tok.Children.empty() || Tok.Children[0].MustBreakBefore);
|
(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
|
// Returns the length of everything up to the first possible line break after
|
||||||
// the ), ], } or > matching \c Tok.
|
// the ), ], } or > matching \c Tok.
|
||||||
static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
|
static unsigned getLengthToMatchingParen(const AnnotatedToken &Tok) {
|
||||||
|
@ -127,7 +133,7 @@ public:
|
||||||
// Align comment with other comments.
|
// Align comment with other comments.
|
||||||
if (Tok.Parent != NULL || !Comments.empty()) {
|
if (Tok.Parent != NULL || !Comments.empty()) {
|
||||||
if (Style.ColumnLimit >=
|
if (Style.ColumnLimit >=
|
||||||
Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
|
Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
|
||||||
StoredComment Comment;
|
StoredComment Comment;
|
||||||
Comment.Tok = Tok.FormatTok;
|
Comment.Tok = Tok.FormatTok;
|
||||||
Comment.Spaces = Spaces;
|
Comment.Spaces = Spaces;
|
||||||
|
@ -229,8 +235,7 @@ private:
|
||||||
const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
|
const char *TokenStart = SourceMgr.getCharacterData(Tok.Tok.getLocation());
|
||||||
while (Line.rtrim().size() > ColumnLimit) {
|
while (Line.rtrim().size() > ColumnLimit) {
|
||||||
// Try to break at the last whitespace before the column limit.
|
// Try to break at the last whitespace before the column limit.
|
||||||
size_t SpacePos =
|
size_t SpacePos = Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
|
||||||
Line.find_last_of(WhiteSpaceChars, ColumnLimit + 1);
|
|
||||||
if (SpacePos == StringRef::npos) {
|
if (SpacePos == StringRef::npos) {
|
||||||
// Try to find any whitespace in the line.
|
// Try to find any whitespace in the line.
|
||||||
SpacePos = Line.find_first_of(WhiteSpaceChars);
|
SpacePos = Line.find_first_of(WhiteSpaceChars);
|
||||||
|
@ -573,7 +578,7 @@ private:
|
||||||
if (VariablePos != Other.VariablePos)
|
if (VariablePos != Other.VariablePos)
|
||||||
return VariablePos < Other.VariablePos;
|
return VariablePos < Other.VariablePos;
|
||||||
if (LineContainsContinuedForLoopSection !=
|
if (LineContainsContinuedForLoopSection !=
|
||||||
Other.LineContainsContinuedForLoopSection)
|
Other.LineContainsContinuedForLoopSection)
|
||||||
return LineContainsContinuedForLoopSection;
|
return LineContainsContinuedForLoopSection;
|
||||||
if (ParenLevel != Other.ParenLevel)
|
if (ParenLevel != Other.ParenLevel)
|
||||||
return ParenLevel < Other.ParenLevel;
|
return ParenLevel < Other.ParenLevel;
|
||||||
|
@ -621,7 +626,8 @@ private:
|
||||||
State.Column = State.Stack.back().FirstLessLess;
|
State.Column = State.Stack.back().FirstLessLess;
|
||||||
} else if (State.ParenLevel != 0 &&
|
} else if (State.ParenLevel != 0 &&
|
||||||
(Previous.isOneOf(tok::equal, tok::coloncolon) ||
|
(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
|
// 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
|
// continued. Don't do that on the top level, as we already indent 4
|
||||||
// there.
|
// there.
|
||||||
|
@ -709,7 +715,7 @@ private:
|
||||||
if (Current.Type == TT_ObjCSelectorName &&
|
if (Current.Type == TT_ObjCSelectorName &&
|
||||||
State.Stack.back().ColonPos == 0) {
|
State.Stack.back().ColonPos == 0) {
|
||||||
if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
|
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().ColonPos =
|
||||||
State.Stack.back().Indent + Current.LongestObjCSelectorName;
|
State.Stack.back().Indent + Current.LongestObjCSelectorName;
|
||||||
else
|
else
|
||||||
|
@ -729,7 +735,7 @@ private:
|
||||||
// Treat the condition inside an if as if it was a second function
|
// Treat the condition inside an if as if it was a second function
|
||||||
// parameter, i.e. let nested calls have an indent of 4.
|
// parameter, i.e. let nested calls have an indent of 4.
|
||||||
State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
|
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.
|
// Top-level spaces are exempt as that mostly leads to better results.
|
||||||
State.Stack.back().LastSpace = State.Column;
|
State.Stack.back().LastSpace = State.Column;
|
||||||
else if ((Previous.Type == TT_BinaryOperator ||
|
else if ((Previous.Type == TT_BinaryOperator ||
|
||||||
|
@ -1283,8 +1289,7 @@ public:
|
||||||
while (IndentForLevel.size() <= TheLine.Level)
|
while (IndentForLevel.size() <= TheLine.Level)
|
||||||
IndentForLevel.push_back(-1);
|
IndentForLevel.push_back(-1);
|
||||||
IndentForLevel.resize(TheLine.Level + 1);
|
IndentForLevel.resize(TheLine.Level + 1);
|
||||||
bool WasMoved =
|
bool WasMoved = PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
|
||||||
PreviousLineWasTouched && FirstTok.NewlinesBefore == 0;
|
|
||||||
if (TheLine.First.is(tok::eof)) {
|
if (TheLine.First.is(tok::eof)) {
|
||||||
if (PreviousLineWasTouched) {
|
if (PreviousLineWasTouched) {
|
||||||
unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
|
unsigned NewLines = std::min(FirstTok.NewlinesBefore, 1u);
|
||||||
|
@ -1298,8 +1303,8 @@ public:
|
||||||
if (static_cast<int>(Indent) + Offset >= 0)
|
if (static_cast<int>(Indent) + Offset >= 0)
|
||||||
Indent += Offset;
|
Indent += Offset;
|
||||||
if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
|
if (!FirstTok.WhiteSpaceStart.isValid() || StructuralError) {
|
||||||
Indent = LevelIndent = SourceMgr.getSpellingColumnNumber(
|
Indent = LevelIndent =
|
||||||
FirstTok.Tok.getLocation()) - 1;
|
SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
|
||||||
} else {
|
} else {
|
||||||
formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
|
formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
|
||||||
PreviousEndOfLineColumn);
|
PreviousEndOfLineColumn);
|
||||||
|
|
|
@ -204,7 +204,7 @@ private:
|
||||||
isUnaryOperator(*Parent) || Parent->Type == TT_ObjCForIn ||
|
isUnaryOperator(*Parent) || Parent->Type == TT_ObjCForIn ||
|
||||||
Parent->Type == TT_CastRParen ||
|
Parent->Type == TT_CastRParen ||
|
||||||
getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
|
getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
|
||||||
prec::Unknown);
|
prec::Unknown);
|
||||||
ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
|
ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
|
||||||
Contexts.back().IsExpression = true;
|
Contexts.back().IsExpression = true;
|
||||||
bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
|
bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
|
||||||
|
@ -329,7 +329,7 @@ private:
|
||||||
Tok->Type = TT_ObjCMethodExpr;
|
Tok->Type = TT_ObjCMethodExpr;
|
||||||
Tok->Parent->Type = TT_ObjCSelectorName;
|
Tok->Parent->Type = TT_ObjCSelectorName;
|
||||||
if (Tok->Parent->FormatTok.TokenLength >
|
if (Tok->Parent->FormatTok.TokenLength >
|
||||||
Contexts.back().LongestObjCSelectorName)
|
Contexts.back().LongestObjCSelectorName)
|
||||||
Contexts.back().LongestObjCSelectorName =
|
Contexts.back().LongestObjCSelectorName =
|
||||||
Tok->Parent->FormatTok.TokenLength;
|
Tok->Parent->FormatTok.TokenLength;
|
||||||
if (Contexts.back().FirstObjCSelectorName == NULL)
|
if (Contexts.back().FirstObjCSelectorName == NULL)
|
||||||
|
|
|
@ -71,9 +71,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool eof() {
|
bool eof() { return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; }
|
||||||
return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
|
|
||||||
}
|
|
||||||
|
|
||||||
FormatToken createEOF() {
|
FormatToken createEOF() {
|
||||||
FormatToken FormatTok;
|
FormatToken FormatTok;
|
||||||
|
@ -133,8 +131,7 @@ bool UnwrappedLineParser::parse() {
|
||||||
DEBUG(llvm::dbgs() << "----\n");
|
DEBUG(llvm::dbgs() << "----\n");
|
||||||
readToken();
|
readToken();
|
||||||
bool Error = parseFile();
|
bool Error = parseFile();
|
||||||
for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
|
for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
|
||||||
E = Lines.end();
|
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
Callback.consumeUnwrappedLine(*I);
|
Callback.consumeUnwrappedLine(*I);
|
||||||
}
|
}
|
||||||
|
@ -149,7 +146,7 @@ bool UnwrappedLineParser::parse() {
|
||||||
bool UnwrappedLineParser::parseFile() {
|
bool UnwrappedLineParser::parseFile() {
|
||||||
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
||||||
/*MustBeDeclaration=*/ true);
|
/*MustBeDeclaration=*/ true);
|
||||||
bool Error = parseLevel(/*HasOpeningBrace=*/false);
|
bool Error = parseLevel(/*HasOpeningBrace=*/ false);
|
||||||
// Make sure to format the remaining tokens.
|
// Make sure to format the remaining tokens.
|
||||||
flushComments(true);
|
flushComments(true);
|
||||||
addUnwrappedLine();
|
addUnwrappedLine();
|
||||||
|
@ -200,14 +197,14 @@ bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
|
||||||
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
||||||
MustBeDeclaration);
|
MustBeDeclaration);
|
||||||
Line->Level += AddLevels;
|
Line->Level += AddLevels;
|
||||||
parseLevel(/*HasOpeningBrace=*/true);
|
parseLevel(/*HasOpeningBrace=*/ true);
|
||||||
|
|
||||||
if (!FormatTok.Tok.is(tok::r_brace)) {
|
if (!FormatTok.Tok.is(tok::r_brace)) {
|
||||||
Line->Level -= AddLevels;
|
Line->Level -= AddLevels;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextToken(); // Munch the closing brace.
|
nextToken(); // Munch the closing brace.
|
||||||
Line->Level -= AddLevels;
|
Line->Level -= AddLevels;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -673,8 +670,7 @@ void UnwrappedLineParser::parseRecord() {
|
||||||
// The actual identifier can be a nested name specifier, and in macros
|
// The actual identifier can be a nested name specifier, and in macros
|
||||||
// it is often token-pasted.
|
// it is often token-pasted.
|
||||||
while (FormatTok.Tok.is(tok::identifier) ||
|
while (FormatTok.Tok.is(tok::identifier) ||
|
||||||
FormatTok.Tok.is(tok::coloncolon) ||
|
FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash))
|
||||||
FormatTok.Tok.is(tok::hashhash))
|
|
||||||
nextToken();
|
nextToken();
|
||||||
|
|
||||||
// Note that parsing away template declarations here leads to incorrectly
|
// Note that parsing away template declarations here leads to incorrectly
|
||||||
|
@ -723,12 +719,12 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() {
|
||||||
|
|
||||||
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
|
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
|
||||||
nextToken();
|
nextToken();
|
||||||
nextToken(); // interface name
|
nextToken(); // interface name
|
||||||
|
|
||||||
// @interface can be followed by either a base class, or a category.
|
// @interface can be followed by either a base class, or a category.
|
||||||
if (FormatTok.Tok.is(tok::colon)) {
|
if (FormatTok.Tok.is(tok::colon)) {
|
||||||
nextToken();
|
nextToken();
|
||||||
nextToken(); // base class name
|
nextToken(); // base class name
|
||||||
} else if (FormatTok.Tok.is(tok::l_paren))
|
} else if (FormatTok.Tok.is(tok::l_paren))
|
||||||
// Skip category, if present.
|
// Skip category, if present.
|
||||||
parseParens();
|
parseParens();
|
||||||
|
@ -749,7 +745,7 @@ void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
|
||||||
|
|
||||||
void UnwrappedLineParser::parseObjCProtocol() {
|
void UnwrappedLineParser::parseObjCProtocol() {
|
||||||
nextToken();
|
nextToken();
|
||||||
nextToken(); // protocol name
|
nextToken(); // protocol name
|
||||||
|
|
||||||
if (FormatTok.Tok.is(tok::less))
|
if (FormatTok.Tok.is(tok::less))
|
||||||
parseObjCProtocolList();
|
parseObjCProtocolList();
|
||||||
|
@ -791,9 +787,7 @@ void UnwrappedLineParser::addUnwrappedLine() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UnwrappedLineParser::eof() const {
|
bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); }
|
||||||
return FormatTok.Tok.is(tok::eof);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
|
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
|
||||||
bool JustComments = Line->Tokens.empty();
|
bool JustComments = Line->Tokens.empty();
|
||||||
|
@ -829,8 +823,8 @@ void UnwrappedLineParser::readToken() {
|
||||||
FormatTok.IsFirst)) {
|
FormatTok.IsFirst)) {
|
||||||
// If there is an unfinished unwrapped line, we flush the preprocessor
|
// If there is an unfinished unwrapped line, we flush the preprocessor
|
||||||
// directives only after that unwrapped line was finished later.
|
// directives only after that unwrapped line was finished later.
|
||||||
bool SwitchToPreprocessorLines = !Line->Tokens.empty() &&
|
bool SwitchToPreprocessorLines =
|
||||||
CurrentLines == &Lines;
|
!Line->Tokens.empty() && CurrentLines == &Lines;
|
||||||
ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
|
ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
|
||||||
parsePPDirective();
|
parsePPDirective();
|
||||||
}
|
}
|
||||||
|
|
|
@ -323,8 +323,8 @@ TEST_F(FormatTest, FormatsForLoop) {
|
||||||
verifyFormat(
|
verifyFormat(
|
||||||
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
|
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
|
||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
|
||||||
" ++aaaaaaaaaaa) {\n}");
|
" ++aaaaaaaaaaa) {\n}");
|
||||||
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
|
verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
|
||||||
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
|
" aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
|
||||||
|
@ -1446,7 +1446,7 @@ TEST_F(FormatTest, BreaksAsHighAsPossible) {
|
||||||
" f();\n"
|
" f();\n"
|
||||||
"}");
|
"}");
|
||||||
verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
|
verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
|
||||||
" Intervals[i - 1].getRange().getLast()) {\n}");
|
" Intervals[i - 1].getRange().getLast()) {\n}");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, BreaksDesireably) {
|
TEST_F(FormatTest, BreaksDesireably) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче