[clang] [coroutine] Suppress unreachable-code warning on coroutine statements. (PR #77454)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 04:44:54 PST 2024


================
@@ -60,6 +61,45 @@ static bool isTrivialDoWhile(const CFGBlock *B, const Stmt *S) {
   return false;
 }
 
+// Check if the block starts with a coroutine statement and see if the given
+// unreachable 'S' is the substmt of the coroutine statement.
+//
+// We suppress the unreachable warning for cases where an unreachable code is
+// a substmt of the coroutine statement, becase removing it will change the
+// function semantic if this is the only coroutine statement of the coroutine.
+static bool isInCoroutineStmt(const CFGBlock *Block, const Stmt* S) {
----------------
hokein wrote:

Some explanations to help understand the code:

- `Block` is an unreachable block in CFG;
- `S` is the first statement of the `Block`, which can be diagnosed as dead;

Given the code `task test() { std::abort(); co_return 1; }` (full [CFG](https://godbolt.org/z/5hG7as3GT)), the detected unreachable Block is `B1`, and `S` is a `DeclRefExpr` (substmt of the co_return stmt) which refers to the implicit `__promise` var decl.

```
 [B1]
   1: __promise
   2: [B1.1].return_value
   3: 1
   4: [B1.2]([B1.3])
   5: co_return [B1.3];   Preds (1): B2(Unreachable)
   Succs (1): B0
```

https://github.com/llvm/llvm-project/pull/77454


More information about the cfe-commits mailing list