forked from OSchip/llvm-project
[clang-format] Handle PointerAlignment in `if` and `switch` statements with initializers (C++17) the same way as in `for` loops.
Reviewed By: MyDeveloperDay, owenpan Differential Revision: https://reviews.llvm.org/D119650
This commit is contained in:
parent
9cb9445979
commit
25282bd6c4
|
@ -26,6 +26,13 @@ namespace format {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
/// Returns \c true if the line starts with a token that can start a statement
|
||||||
|
/// with an initializer.
|
||||||
|
static bool startsWithInitStatement(const AnnotatedLine &Line) {
|
||||||
|
return Line.startsWith(tok::kw_for) || Line.startsWith(tok::kw_if) ||
|
||||||
|
Line.startsWith(tok::kw_switch);
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns \c true if the token can be used as an identifier in
|
/// Returns \c true if the token can be used as an identifier in
|
||||||
/// an Objective-C \c \@selector, \c false otherwise.
|
/// an Objective-C \c \@selector, \c false otherwise.
|
||||||
///
|
///
|
||||||
|
@ -1135,7 +1142,7 @@ private:
|
||||||
else if (Contexts.back().InInheritanceList)
|
else if (Contexts.back().InInheritanceList)
|
||||||
Tok->setType(TT_InheritanceComma);
|
Tok->setType(TT_InheritanceComma);
|
||||||
else if (Contexts.back().FirstStartOfName &&
|
else if (Contexts.back().FirstStartOfName &&
|
||||||
(Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
|
(Contexts.size() == 1 || startsWithInitStatement(Line))) {
|
||||||
Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
|
Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
|
||||||
Line.IsMultiVariableDeclStmt = true;
|
Line.IsMultiVariableDeclStmt = true;
|
||||||
}
|
}
|
||||||
|
@ -3090,7 +3097,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
|
||||||
FormatStyle::PAS_Left ||
|
FormatStyle::PAS_Left ||
|
||||||
(Line.IsMultiVariableDeclStmt &&
|
(Line.IsMultiVariableDeclStmt &&
|
||||||
(Left.NestingLevel == 0 ||
|
(Left.NestingLevel == 0 ||
|
||||||
(Left.NestingLevel == 1 && Line.First->is(tok::kw_for)))));
|
(Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
|
||||||
}
|
}
|
||||||
if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
|
if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
|
||||||
(!Left.is(TT_PointerOrReference) ||
|
(!Left.is(TT_PointerOrReference) ||
|
||||||
|
|
|
@ -8396,6 +8396,12 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
|
||||||
Style);
|
Style);
|
||||||
verifyFormat("vector<int*> a, b;", Style);
|
verifyFormat("vector<int*> a, b;", Style);
|
||||||
verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
|
verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
|
||||||
|
verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
|
||||||
|
verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
|
||||||
|
verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
|
||||||
|
Style);
|
||||||
|
verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", Style);
|
||||||
|
verifyFormat("/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", Style);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
|
TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
|
||||||
|
|
Loading…
Reference in New Issue