[clang] 6a0484f - [clang-format] [PR46159] Linux kernel 'C' code uses 'try' as a variable name, allow clang-format to handle such cases
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 12:47:37 PDT 2020
Author: mydeveloperday
Date: 2020-06-03T20:44:45+01:00
New Revision: 6a0484f04b628870fb7cd0757a72d73294ff5346
URL: https://github.com/llvm/llvm-project/commit/6a0484f04b628870fb7cd0757a72d73294ff5346
DIFF: https://github.com/llvm/llvm-project/commit/6a0484f04b628870fb7cd0757a72d73294ff5346.diff
LOG: [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
Added:
Modified:
clang/lib/Format/FormatTokenLexer.cpp
clang/lib/Format/FormatTokenLexer.h
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index b6cbebdcbe7f..f64ab389d02f 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -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)
diff --git a/clang/lib/Format/FormatTokenLexer.h b/clang/lib/Format/FormatTokenLexer.h
index be11020270a3..219cd46f98ed 100644
--- a/clang/lib/Format/FormatTokenLexer.h
+++ b/clang/lib/Format/FormatTokenLexer.h
@@ -56,6 +56,7 @@ class FormatTokenLexer {
bool tryMergeCSharpNullConditional();
bool tryTransformCSharpForEach();
bool tryMergeForEach();
+ bool tryTransformTryUsageForC();
bool tryMergeTokens(ArrayRef<tok::TokenKind> Kinds, TokenType NewType);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index eea0b364d97c..004ae412ea6d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
More information about the cfe-commits
mailing list