[PATCH] D105479: [clang-tidy] [PR50069] readability-braces-around-statements doesn't work well with [[likely]] [[unlikely]]

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 6 07:00:41 PDT 2021


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: alexfh, njames93, aaron.ballman.
MyDeveloperDay added projects: clang-tools-extra, clang.
Herald added subscribers: jdoerfert, xazax.hun.
MyDeveloperDay requested review of this revision.

https://bugs.llvm.org/show_bug.cgi?id=50069

When clang-tidy sees:

  if (true) [[unlikely]] {
      ...
  }

It thinks the braces are missing and add them again.

  if (true)  { [[unlikely]] {
      ...
    }
  }

This revision aims to prevent that incorrect code generation


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105479

Files:
  clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy  -std=c++20-or-later %s readability-braces-around-statements %t
+
+void test(bool b) {
+  if (b) {
+    return;
+  }
+  if (b) [[likely]] {
+    // CHECK-FIXES-NOT: if (b) { {{[[][[]}}likely{{[]][]]}} {
+    return;
+  }
+  if (b) [[unlikely]] {
+    // CHECK-FIXES-NOT: if (b) { {{[[][[]}}unlikely{{[]][]]}} {
+    return;
+  }
+}
Index: clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
@@ -193,7 +193,7 @@
   // token, the check inserts "\n}" right before that token.
   // 3) Otherwise the check finds the end of line (possibly after some block or
   // line comments) and inserts "\n}" right before that EOL.
-  if (!S || isa<CompoundStmt>(S)) {
+  if (!S || isa<CompoundStmt>(S) || isa<AttributedStmt>(S)) {
     // Already inside braces.
     return false;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105479.356709.patch
Type: text/x-patch
Size: 1341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210706/0c4433a6/attachment.bin>


More information about the cfe-commits mailing list