[clang] 4ad8913 - [NFC] Add checks for self-assignment.

Sindhu Chittireddy via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 24 09:21:10 PDT 2023


Author: Sindhu Chittireddy
Date: 2023-08-24T09:20:58-07:00
New Revision: 4ad89131e0de9368bc41395d770e1923366deba1

URL: https://github.com/llvm/llvm-project/commit/4ad89131e0de9368bc41395d770e1923366deba1
DIFF: https://github.com/llvm/llvm-project/commit/4ad89131e0de9368bc41395d770e1923366deba1.diff

LOG: [NFC] Add checks for self-assignment.

Differential Revision: https://reviews.llvm.org/D155776

Added: 
    

Modified: 
    clang/lib/AST/APValue.cpp
    clang/lib/CodeGen/CGDebugInfo.h
    clang/lib/Interpreter/Value.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 5b0a5e256e411a..ef424215182280 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -390,11 +390,13 @@ APValue &APValue::operator=(const APValue &RHS) {
 }
 
 APValue &APValue::operator=(APValue &&RHS) {
-  if (Kind != None && Kind != Indeterminate)
-    DestroyDataAndMakeUninit();
-  Kind = RHS.Kind;
-  Data = RHS.Data;
-  RHS.Kind = None;
+  if (this != &RHS) {
+    if (Kind != None && Kind != Indeterminate)
+      DestroyDataAndMakeUninit();
+    Kind = RHS.Kind;
+    Data = RHS.Data;
+    RHS.Kind = None;
+  }
   return *this;
 }
 

diff  --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index b47a4934f0b1ba..ae12485850ca77 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -835,8 +835,10 @@ class ApplyDebugLocation {
 
   // Define copy assignment operator.
   ApplyDebugLocation &operator=(ApplyDebugLocation &&Other) {
-    CGF = Other.CGF;
-    Other.CGF = nullptr;
+    if (this != &Other) {
+      CGF = Other.CGF;
+      Other.CGF = nullptr;
+    }
     return *this;
   }
 

diff  --git a/clang/lib/Interpreter/Value.cpp b/clang/lib/Interpreter/Value.cpp
index 6d0eaf1b82e108..1d6b2da087e9fb 100644
--- a/clang/lib/Interpreter/Value.cpp
+++ b/clang/lib/Interpreter/Value.cpp
@@ -201,16 +201,17 @@ Value &Value::operator=(const Value &RHS) {
 }
 
 Value &Value::operator=(Value &&RHS) noexcept {
-  if (IsManuallyAlloc)
-    ValueStorage::getFromPayload(getPtr())->Release();
+  if (this != &RHS) {
+    if (IsManuallyAlloc)
+      ValueStorage::getFromPayload(getPtr())->Release();
 
-  Interp = std::exchange(RHS.Interp, nullptr);
-  OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
-  ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
-  IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
-
-  Data = RHS.Data;
+    Interp = std::exchange(RHS.Interp, nullptr);
+    OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
+    ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
+    IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
 
+    Data = RHS.Data;
+  }
   return *this;
 }
 


        


More information about the cfe-commits mailing list