mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-04-18 06:10:37 +00:00
Prefer not to break after assignments.
This addresses llvm.org/PR14830. Before: unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(), SI->getPointerAddressSpace()); CharSourceRange LineRange = CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(), TheLine.Tokens.back().Tok.getLocation()); After: unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(), SI->getPointerAddressSpace()); CharSourceRange LineRange = CharSourceRange::getTokenRange( TheLine.Tokens.front().Tok.getLocation(), TheLine.Tokens.back().Tok.getLocation()); This required rudimentary changes to static initializer lists, but we are not yet formatting them in a reasonable way. That will be done in a subsequent patch. llvm-svn: 171731
This commit is contained in:
parent
a055aab506
commit
206df73417
@ -257,7 +257,12 @@ private:
|
||||
|
||||
if (Newline) {
|
||||
unsigned WhitespaceStartColumn = State.Column;
|
||||
if (Current.Tok.is(tok::string_literal) &&
|
||||
if (Previous.Tok.is(tok::l_brace)) {
|
||||
// FIXME: This does not work with nested static initializers.
|
||||
// Implement a better handling for static initializers and similar
|
||||
// constructs.
|
||||
State.Column = Line.Level * 2 + 2;
|
||||
} else if (Current.Tok.is(tok::string_literal) &&
|
||||
Previous.Tok.is(tok::string_literal)) {
|
||||
State.Column = State.Column - Previous.TokenLength;
|
||||
} else if (Current.Tok.is(tok::lessless) &&
|
||||
@ -306,17 +311,19 @@ private:
|
||||
if (!DryRun)
|
||||
replaceWhitespace(Current, 0, Spaces);
|
||||
|
||||
// FIXME: Look into using this alignment at other ParenLevels.
|
||||
if (ParenLevel == 0 && (getPrecedence(Previous) == prec::Assignment ||
|
||||
Previous.Tok.is(tok::kw_return)))
|
||||
if (Line.Tokens[0].Tok.isNot(tok::kw_for) &&
|
||||
(getPrecedence(Previous) == prec::Assignment ||
|
||||
Previous.Tok.is(tok::kw_return)))
|
||||
State.Indent[ParenLevel] = State.Column + Spaces;
|
||||
if (Previous.Tok.is(tok::l_paren) ||
|
||||
Annotations[Index - 1].Type == TT_TemplateOpener)
|
||||
State.Indent[ParenLevel] = State.Column;
|
||||
|
||||
// Top-level spaces are exempt as that mostly leads to better results.
|
||||
// Top-level spaces that are not part of assignments are exempt as that
|
||||
// mostly leads to better results.
|
||||
State.Column += Spaces;
|
||||
if (Spaces > 0 && ParenLevel != 0)
|
||||
if (Spaces > 0 &&
|
||||
(ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
|
||||
State.LastSpace[ParenLevel] = State.Column;
|
||||
}
|
||||
moveStateToNextToken(State);
|
||||
@ -375,7 +382,13 @@ private:
|
||||
if (Left.Tok.is(tok::l_paren))
|
||||
return 20;
|
||||
|
||||
prec::Level Level = getPrecedence(Line.Tokens[Index]);
|
||||
prec::Level Level = getPrecedence(Left);
|
||||
|
||||
// Breaking after an assignment leads to a bad result as the two sides of
|
||||
// the assignment are visually very close together.
|
||||
if (Level == prec::Assignment)
|
||||
return 50;
|
||||
|
||||
if (Level != prec::Unknown)
|
||||
return Level;
|
||||
|
||||
|
@ -384,8 +384,8 @@ TEST_F(FormatTest, StaticInitializers) {
|
||||
|
||||
// FIXME: Format like enums if the static initializer does not fit on a line.
|
||||
verifyFormat(
|
||||
"static SomeClass WithALoooooooooooooooooooongName = { 100000000,\n"
|
||||
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
|
||||
"static SomeClass WithALoooooooooooooooooooongName = {\n"
|
||||
" 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
|
||||
@ -641,23 +641,34 @@ TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
|
||||
" ccccccccccccccccccccccccc) {\n}");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) {
|
||||
verifyFormat(
|
||||
"unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n"
|
||||
" SI->getAlignment(),\n"
|
||||
" SI->getPointerAddressSpaceee());\n");
|
||||
verifyFormat(
|
||||
"CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
|
||||
" Line.Tokens.front().Tok.getLocation(),\n"
|
||||
" Line.Tokens.back().Tok.getLocation());");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, AlignsAfterAssignments) {
|
||||
verifyFormat(
|
||||
"int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
verifyFormat(
|
||||
"Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
verifyFormat(
|
||||
"Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
verifyFormat(
|
||||
"int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa);");
|
||||
verifyFormat(
|
||||
"double LooooooooooooooooooooooooongResult =\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
"double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaa +\n"
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaa;");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, AlignsAfterReturn) {
|
||||
@ -713,8 +724,10 @@ TEST_F(FormatTest, UnderstandsEquals) {
|
||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
|
||||
"}");
|
||||
|
||||
verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
||||
" 100000000 + 100000000) {\n}");
|
||||
verifyFormat(
|
||||
"if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n"
|
||||
" 10000000) {\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user