[clang] [Analyzer][CFG] Correctly handle rebuilt default arg and default init expression (PR #91879)

via cfe-commits cfe-commits at lists.llvm.org
Sun May 12 09:53:52 PDT 2024


================
@@ -2433,6 +2429,30 @@ CFGBlock *CFGBuilder::VisitChildren(Stmt *S) {
   return B;
 }
 
+CFGBlock *CFGBuilder::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *Arg,
+                                             AddStmtChoice asc) {
+  if (Arg->hasRewrittenInit()) {
+    if (asc.alwaysAdd(*this, Arg)) {
+      autoCreateBlock();
+      appendStmt(Block, Arg);
+    }
+    return VisitStmt(Arg->getExpr(), asc);
+  }
+  return VisitStmt(Arg, asc);
----------------
yronglin wrote:

Thanks a lot for your review!

> I think it'd be useful to keep some of the old comment here: we can't add the default argument if it's not rewritten because we could end up with the same expression appearing multiple times.

Agree 100%, keep the old comment here can make things more clear. 

> Actually, are we safe from that even if the default argument is rewritten? Do we guarantee to recreate all subexpressions in that case?

Not exactly, The rebuild implementation is the following, it's ignores `LambdaExpr`, `BlockExpr` and `CXXThisExpr`, And `EnsureImmediateInvocationInDefaultArgs`’s name may need to be refine.
https://github.com/llvm/llvm-project/blob/78b3a00418ce6da0426a261a64a77608d0264fe5/clang/lib/Sema/SemaExpr.cpp#L5707-L5723 

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


More information about the cfe-commits mailing list