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

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Wed May 1 08:34:15 PDT 2024


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

>From 0a1598d0e00cbbfd0320ea72491500957ddb6b52 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Tue, 30 Apr 2024 11:18:15 -0500
Subject: [PATCH] [clang][Sema] Re-use existing BinaryOperator if possible

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
---
 clang/lib/Sema/SemaExpr.cpp  | 7 +++----
 clang/test/CXX/drs/dr7xx.cpp | 2 --
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 0c37f43f75401b..0cca02c338954a 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



More information about the cfe-commits mailing list