[PATCH] D115865: [clang-format] add support for branch attribute macros

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 16 01:55:08 PST 2021


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: curdeius, HazardyKnusperkeks, owenpan.
MyDeveloperDay added projects: clang, clang-format.
MyDeveloperDay requested review of this revision.

https://github.com/llvm/llvm-project/issues/49184

clang-format doesn't handle the use of AttributeMacros where `[[unlikely]]` / `[[likely]]` could be used in `if` statements

  if (StartIndex == End)
      BRANCH_UNLIKELY
  return npos;

Give better support via

  if (StartIndex == End) [[unlikely]]
    return npos;
  
  if (StartIndex == End) [[unlikely]] {
    return npos;
  }
  
  if (StartIndex == End) UNLIKELY
    return npos;
  
  if (StartIndex == End) UNLIKELY {
    return npos;
  }

Fixes #49184


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115865

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
@@ -22180,6 +22180,42 @@
                "  return 29;\n"
                "}",
                Style);
+
+  verifyFormat("if (argc > 5) [[unlikely]]\n"
+               "  return 29;\n",
+               Style);
+  verifyFormat("if (argc > 5) [[likely]]\n"
+               "  return 29;\n",
+               Style);
+
+  Style.AttributeMacros.push_back("UNLIKELY");
+  Style.AttributeMacros.push_back("LIKELY");
+  verifyFormat("if (argc > 5) UNLIKELY\n"
+               "  return 29;\n",
+               Style);
+
+  verifyFormat("if (argc > 5) UNLIKELY {\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 LIKELY {\n"
+               "  return 42;\n"
+               "}\n",
+               Style);
+  verifyFormat("if (argc > 5) [[unlikely]] {\n"
+               "  return 29;\n"
+               "} else LIKELY {\n"
+               "  return 42;\n"
+               "}\n",
+               Style);
 }
 
 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2166,6 +2166,9 @@
     nextToken();
   if (FormatTok->Tok.is(tok::l_paren))
     parseParens();
+  // handle  AttributeMacro  if (x) UNLIKELY
+  if (FormatTok->is(TT_AttributeMacro))
+    nextToken();
   // handle [[likely]] / [[unlikely]]
   if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute())
     parseSquare();
@@ -2185,6 +2188,9 @@
   }
   if (FormatTok->Tok.is(tok::kw_else)) {
     nextToken();
+    // handle  AttributeMacro  else UNLIKELY
+    if (FormatTok->is(TT_AttributeMacro))
+      nextToken();
     // handle [[likely]] / [[unlikely]]
     if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute())
       parseSquare();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115865.394791.patch
Type: text/x-patch
Size: 2347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211216/373fc66a/attachment.bin>


More information about the cfe-commits mailing list