Index: clang/lib/Format/FormatTokenLexer.h =================================================================== --- clang/lib/Format/FormatTokenLexer.h +++ clang/lib/Format/FormatTokenLexer.h @@ -56,6 +56,7 @@ bool tryMergeCSharpNullConditional(); bool tryTransformCSharpForEach(); bool tryMergeForEach(); + bool tryTransformTryUsageForC(); bool tryMergeTokens(ArrayRef Kinds, TokenType NewType); Index: clang/lib/Format/FormatTokenLexer.cpp =================================================================== --- clang/lib/Format/FormatTokenLexer.cpp +++ clang/lib/Format/FormatTokenLexer.cpp @@ -76,6 +76,8 @@ return; if (tryMergeForEach()) return; + if (Style.isCpp() && tryTransformTryUsageForC()) + return; if (Style.isCSharp()) { if (tryMergeCSharpKeywordVariables()) @@ -383,6 +385,26 @@ 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) Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -2664,6 +2664,18 @@ 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"