[clang] [clang][Sema] Re-use existing BinaryOperator if possible (PR #90625)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 30 09:53:53 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Youngsuk Kim (JOE1994)

<details>
<summary>Changes</summary>

First round of Sema checks were run at initial parsing step. Creating a new BinaryOperator instance (with the re-built LHS or RHS) will trigger another round of Sema checks, which can lead to duplicate diagnostic warning messages.

All we want here is to replace the LHS or RHS with a NonOdrUse version. Don't create a new BinaryOperator, but simply replace the LHS or RHS of the given BinaryOperator.

Fixes #<!-- -->45783

---
Full diff: https://github.com/llvm/llvm-project/pull/90625.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+3-4) 
- (modified) clang/test/CXX/drs/dr7xx.cpp (-2) 


``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 50f92c496a539a..27733cd857d964 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19707,18 +19707,17 @@ static ExprResult rebuildPotentialResultsAsNonOdrUsed(Sema &S, Expr *E,
       ExprResult Sub = Rebuild(LHS);
       if (!Sub.isUsable())
         return Sub;
-      LHS = Sub.get();
+      BO->setLHS(Sub.get());
     //   -- If e is a comma expression, ...
     } else if (BO->getOpcode() == BO_Comma) {
       ExprResult Sub = Rebuild(RHS);
       if (!Sub.isUsable())
         return Sub;
-      RHS = Sub.get();
+      BO->setRHS(Sub.get());
     } else {
       break;
     }
-    return S.BuildBinOp(nullptr, BO->getOperatorLoc(), BO->getOpcode(),
-                        LHS, RHS);
+    return ExprResult(BO);
   }
 
   //   -- If e has the form (e1)...
diff --git a/clang/test/CXX/drs/dr7xx.cpp b/clang/test/CXX/drs/dr7xx.cpp
index 69ee6d6d4e6ae0..0300dae08d6d31 100644
--- a/clang/test/CXX/drs/dr7xx.cpp
+++ b/clang/test/CXX/drs/dr7xx.cpp
@@ -28,10 +28,8 @@ namespace cwg712 { // cwg712: partial
         use(a);
         use((a));
         use(cond ? a : a);
-        // FIXME: should only warn once
         use((cond, a));
         // expected-warning at -1 {{left operand of comma operator has no effect}}
-        // expected-warning at -2 {{left operand of comma operator has no effect}}
 
         (void)a;
         // expected-error at -1 {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME

``````````

</details>


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


More information about the cfe-commits mailing list