[clang-tools-extra] fe86632 - [clang-tidy] [PR50069] readability-braces-around-statements doesn't work well with [[likely]] [[unlikely]]

via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 14 04:05:54 PDT 2021


Author: mydeveloperday
Date: 2021-08-14T12:05:21+01:00
New Revision: fe866327c1f98a327767e80290dd08cedeadbfd6

URL: https://github.com/llvm/llvm-project/commit/fe866327c1f98a327767e80290dd08cedeadbfd6
DIFF: https://github.com/llvm/llvm-project/commit/fe866327c1f98a327767e80290dd08cedeadbfd6.diff

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

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

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D105479

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

Modified: 
    clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
index fe25f7a7ccbcc..7dc519c152828 100644
--- a/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp
@@ -186,6 +186,10 @@ BracesAroundStatementsCheck::findRParenLoc(const IfOrWhileStmt *S,
 bool BracesAroundStatementsCheck::checkStmt(
     const MatchFinder::MatchResult &Result, const Stmt *S,
     SourceLocation InitialLoc, SourceLocation EndLocHint) {
+
+  while (const auto *AS = dyn_cast<AttributedStmt>(S))
+    S = AS->getSubStmt();
+
   // 1) If there's a corresponding "else" or "while", the check inserts "} "
   // right before that token.
   // 2) If there's a multi-line block comment starting on the same line after

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp
new file mode 100644
index 0000000000000..e799614a1f7b0
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-braces-around-statements-attributes.cpp
@@ -0,0 +1,24 @@
+// 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;
+  }
+
+  if (b) [[likely]]
+    // CHECK-FIXES: if (b) {{[[][[]}}likely{{[]][]]}} {
+    return;
+  // CHECK-FIXES: }
+  if (b) [[unlikely]]
+    // CHECK-FIXES: if (b) {{[[][[]}}unlikely{{[]][]]}} {
+    return;
+  // CHECK-FIXES: }
+}


        


More information about the cfe-commits mailing list