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

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 31 07:16:36 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:

I discussed with @sam-mccall as well, we explored these options:

1. Perform a global analysis on the CFG to make all these cases work perfect;
2. Treat all coroutine statements as invalid dead statements, we never diagnose on these statements;
3. Treat the current behaivor as intended, as technically these code are never executed during the runtime;

For 1), performing such global analysis (capturing all coroutine stmts in CFG, and figuring out all side effect of removing them) is sophisticated, and this model is not easy for human to understand and follow (as removing a coroutine statement may affect the whole analysis result).

For 3), users can suppress these diagnostics via the diagnostic [pragma](https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas) (the newly-introduced [`[[suppress]]`](https://clang.llvm.org/docs/AttributeReference.html#suppress) is promising, but doesn't work for clang diagnostic).

2\) is a simple model and solution. The tradeoff is that we might have some false negatives on some cases (as listed above), but I think it is fine. 

Please take a look on the new change, and let me know what you think.

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


More information about the cfe-commits mailing list