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;
|
return;
|
||||||
if (tryMergeForEach())
|
if (tryMergeForEach())
|
||||||
return;
|
return;
|
||||||
|
if (Style.isCpp() && tryTransformTryUsageForC())
|
||||||
|
return;
|
||||||
|
|
||||||
if (Style.isCSharp()) {
|
if (Style.isCSharp()) {
|
||||||
if (tryMergeCSharpKeywordVariables())
|
if (tryMergeCSharpKeywordVariables())
|
||||||
|
@ -383,6 +385,26 @@ bool FormatTokenLexer::tryMergeForEach() {
|
||||||
return true;
|
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() {
|
bool FormatTokenLexer::tryMergeLessLess() {
|
||||||
// Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
|
// Merge X,less,less,Y into X,lessless,Y unless X or Y is less.
|
||||||
if (Tokens.size() < 3)
|
if (Tokens.size() < 3)
|
||||||
|
|
|
@ -56,6 +56,7 @@ private:
|
||||||
bool tryMergeCSharpNullConditional();
|
bool tryMergeCSharpNullConditional();
|
||||||
bool tryTransformCSharpForEach();
|
bool tryTransformCSharpForEach();
|
||||||
bool tryMergeForEach();
|
bool tryMergeForEach();
|
||||||
|
bool tryTransformTryUsageForC();
|
||||||
|
|
||||||
bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
|
bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
|
||||||
|
|
||||||
|
|
|
@ -2664,6 +2664,18 @@ TEST_F(FormatTest, FormatTryCatch) {
|
||||||
verifyIncompleteFormat("try {} catch (");
|
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) {
|
TEST_F(FormatTest, FormatSEHTryCatch) {
|
||||||
verifyFormat("__try {\n"
|
verifyFormat("__try {\n"
|
||||||
" int a = b * c;\n"
|
" int a = b * c;\n"
|
||||||
|
|
Loading…
Reference in New Issue