clang-format: Allow ternary expressions inside template parameters if
the template parameters aren't inside an expression context. This fixes llvm.org/PR23270. llvm-svn: 236603
This commit is contained in:
parent
e92bf6f141
commit
112b50e6b6
|
@ -47,6 +47,11 @@ private:
|
||||||
FormatToken *Left = CurrentToken->Previous;
|
FormatToken *Left = CurrentToken->Previous;
|
||||||
Left->ParentBracket = Contexts.back().ContextKind;
|
Left->ParentBracket = Contexts.back().ContextKind;
|
||||||
ScopedContextCreator ContextCreator(*this, tok::less, 10);
|
ScopedContextCreator ContextCreator(*this, tok::less, 10);
|
||||||
|
|
||||||
|
// If this angle is in the context of an expression, we need to be more
|
||||||
|
// hesitant to detect it as opening template parameters.
|
||||||
|
bool InExprContext = Contexts.back().IsExpression;
|
||||||
|
|
||||||
Contexts.back().IsExpression = false;
|
Contexts.back().IsExpression = false;
|
||||||
// If there's a template keyword before the opening angle bracket, this is a
|
// If there's a template keyword before the opening angle bracket, this is a
|
||||||
// template parameter, not an argument.
|
// template parameter, not an argument.
|
||||||
|
@ -70,8 +75,8 @@ private:
|
||||||
next();
|
next();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
|
if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
|
||||||
tok::colon, tok::question))
|
(CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext))
|
||||||
return false;
|
return false;
|
||||||
// If a && or || is found and interpreted as a binary operator, this set
|
// If a && or || is found and interpreted as a binary operator, this set
|
||||||
// of angles is likely part of something like "a < b && c > d". If the
|
// of angles is likely part of something like "a < b && c > d". If the
|
||||||
|
|
|
@ -5144,6 +5144,8 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
|
||||||
verifyFormat("f<int>();");
|
verifyFormat("f<int>();");
|
||||||
verifyFormat("template <typename T> void f() {}");
|
verifyFormat("template <typename T> void f() {}");
|
||||||
verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
|
verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
|
||||||
|
verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
|
||||||
|
"sizeof(char)>::type>;");
|
||||||
|
|
||||||
// Not template parameters.
|
// Not template parameters.
|
||||||
verifyFormat("return a < b && c > d;");
|
verifyFormat("return a < b && c > d;");
|
||||||
|
|
Loading…
Reference in New Issue