[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
This commit is contained in:
mydeveloperday 2021-08-14 12:05:21 +01:00
parent 0391165134
commit fe866327c1
2 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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: }
}