MyDeveloperDay created this revision. MyDeveloperDay added reviewers: krasimir, mitchell-stellar, JakeMerdichAMD, curdeius, lefticus. MyDeveloperDay added projects: clang, clang-format.
https://twitter.com/lefticus/status/1262392152950288384?s=20 Jason Turner's (@lefticus) most recent C++ weekly explains the usage of [[likely]] and [[unlikely]] in an 'if/else' context in C++ 20 clang-format leaves the code a little messy afterwards.. if (argc > 5) [[unlikely]] { // ... } else if (argc < 0) [[likely]] { // ... } else [[likely]] { // ... } try to improve the situation if (argc > 5) [[unlikely]] { // ... } else if (argc < 0) [[likely]] { // ... } else [[likely]] { // ... } Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D80144 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -15849,6 +15849,36 @@ verifyFormat("operator&&(int(&&)(), class Foo);", Style); } +TEST_F(FormatTest, LikelyUnlikely) { + FormatStyle Style = getLLVMStyle(); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "}", + Style); + + verifyFormat("if (argc > 5) [[likely]] {\n" + " return 29;\n" + "}", + Style); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "} else [[likely]] {\n" + " return 42;\n" + "}\n", + Style); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "} else if (argc > 10) [[likely]] {\n" + " return 99;\n" + "} else {\n" + " return 42;\n" + "}\n", + Style); +} + } // namespace } // namespace format } // namespace clang Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1954,6 +1954,9 @@ nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); + // handle [[likely]] / [[unlikely]] + if (FormatTok->is(tok::l_square)) + parseSquare(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); @@ -1970,6 +1973,9 @@ } if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); + // handle [[likely]] / [[unlikely]] + if (FormatTok->is(tok::l_square)) + parseSquare(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false);
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -15849,6 +15849,36 @@ verifyFormat("operator&&(int(&&)(), class Foo);", Style); } +TEST_F(FormatTest, LikelyUnlikely) { + FormatStyle Style = getLLVMStyle(); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "}", + Style); + + verifyFormat("if (argc > 5) [[likely]] {\n" + " return 29;\n" + "}", + Style); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "} else [[likely]] {\n" + " return 42;\n" + "}\n", + Style); + + verifyFormat("if (argc > 5) [[unlikely]] {\n" + " return 29;\n" + "} else if (argc > 10) [[likely]] {\n" + " return 99;\n" + "} else {\n" + " return 42;\n" + "}\n", + Style); +} + } // namespace } // namespace format } // namespace clang Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1954,6 +1954,9 @@ nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); + // handle [[likely]] / [[unlikely]] + if (FormatTok->is(tok::l_square)) + parseSquare(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); @@ -1970,6 +1973,9 @@ } if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); + // handle [[likely]] / [[unlikely]] + if (FormatTok->is(tok::l_square)) + parseSquare(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits