forked from OSchip/llvm-project
[clang-format] [PR46159] Linux kernel 'C' code uses 'try' as a variable name, allow clang-format to handle such cases
Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D80940
This commit is contained in:
parent
3a1b07506c
commit
6a0484f04b
|
@ -76,6 +76,8 @@ void FormatTokenLexer::tryMergePreviousTokens() {
|
|||
return;
|
||||
if (tryMergeForEach())
|
||||
return;
|
||||
if (Style.isCpp() && tryTransformTryUsageForC())
|
||||
return;
|
||||
|
||||
if (Style.isCSharp()) {
|
||||
if (tryMergeCSharpKeywordVariables())
|
||||
|
@ -383,6 +385,26 @@ bool FormatTokenLexer::tryMergeForEach() {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool FormatTokenLexer::tryTransformTryUsageForC() {
|
||||
if (Tokens.size() < 2)
|
||||
return false;
|
||||
auto &Try = *(Tokens.end() - 2);
|
||||
if (!Try->is(tok::kw_try))
|
||||
return false;
|
||||
auto &Next = *(Tokens.end() - 1);
|
||||
if (Next->isOneOf(tok::l_brace, tok::colon))
|
||||
return false;
|
||||
|
||||
if (Tokens.size() > 2) {
|
||||
auto &At = *(Tokens.end() - 3);
|
||||
if (At->is(tok::at))
|
||||
return false;
|
||||
}
|
||||
|
||||
Try->Tok.setKind(tok::identifier);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FormatTokenLexer::tryMergeLessLess() {
|
||||
// Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
|
||||
if (Tokens.size() < 3)
|
||||
|
|
|
@ -56,6 +56,7 @@ private:
|
|||
bool tryMergeCSharpNullConditional();
|
||||
bool tryTransformCSharpForEach();
|
||||
bool tryMergeForEach();
|
||||
bool tryTransformTryUsageForC();
|
||||
|
||||
bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
|
||||
|
||||
|
|
|
@ -2664,6 +2664,18 @@ TEST_F(FormatTest, FormatTryCatch) {
|
|||
verifyIncompleteFormat("try {} catch (");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, FormatTryAsAVariable) {
|
||||
verifyFormat("int try;");
|
||||
verifyFormat("int try, size;");
|
||||
verifyFormat("try = foo();");
|
||||
verifyFormat("if (try < size) {\n return true;\n}");
|
||||
|
||||
verifyFormat("int catch;");
|
||||
verifyFormat("int catch, size;");
|
||||
verifyFormat("catch = foo();");
|
||||
verifyFormat("if (catch < size) {\n return true;\n}");
|
||||
}
|
||||
|
||||
TEST_F(FormatTest, FormatSEHTryCatch) {
|
||||
verifyFormat("__try {\n"
|
||||
" int a = b * c;\n"
|
||||
|
|
Loading…
Reference in New Issue