[clang] 26bbb87 - [clang] Implement CompilerInvocation copy assignment

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 19 02:12:29 PDT 2021


Author: Jan Svoboda
Date: 2021-04-19T11:12:22+02:00
New Revision: 26bbb8700bb0ea0ce29e4158e5aa7999ab0d5386

URL: https://github.com/llvm/llvm-project/commit/26bbb8700bb0ea0ce29e4158e5aa7999ab0d5386
DIFF: https://github.com/llvm/llvm-project/commit/26bbb8700bb0ea0ce29e4158e5aa7999ab0d5386.diff

LOG: [clang] Implement CompilerInvocation copy assignment

This patch implements the copy assignment for `CompilerInvocation`.

Eventually, the deep-copy operation will be moved into a `clone()` method (D100460), but until then, this is necessary for basic ergonomics.

Depends on D100455.

Reviewed By: Bigcheese

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

Added: 
    

Modified: 
    clang/include/clang/Frontend/CompilerInvocation.h
    clang/lib/Frontend/CompilerInvocation.cpp
    clang/unittests/Frontend/CompilerInvocationTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Frontend/CompilerInvocation.h b/clang/include/clang/Frontend/CompilerInvocation.h
index bf2fbce3b899d..367efc5ad6464 100644
--- a/clang/include/clang/Frontend/CompilerInvocation.h
+++ b/clang/include/clang/Frontend/CompilerInvocation.h
@@ -91,8 +91,9 @@ class CompilerInvocationRefBase {
 
   CompilerInvocationRefBase();
   CompilerInvocationRefBase(const CompilerInvocationRefBase &X);
-  CompilerInvocationRefBase &
-  operator=(const CompilerInvocationRefBase &) = delete;
+  CompilerInvocationRefBase(CompilerInvocationRefBase &&X);
+  CompilerInvocationRefBase &operator=(CompilerInvocationRefBase X);
+  CompilerInvocationRefBase &operator=(CompilerInvocationRefBase &&X);
   ~CompilerInvocationRefBase();
 
   LangOptions *getLangOpts() { return LangOpts.get(); }

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index fd6ca5e721eb5..34319fe07452a 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -125,6 +125,23 @@ CompilerInvocationRefBase::CompilerInvocationRefBase(
       PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())),
       AnalyzerOpts(new AnalyzerOptions(*X.getAnalyzerOpts())) {}
 
+CompilerInvocationRefBase::CompilerInvocationRefBase(
+    CompilerInvocationRefBase &&X) = default;
+
+CompilerInvocationRefBase &
+CompilerInvocationRefBase::operator=(CompilerInvocationRefBase X) {
+  LangOpts.swap(X.LangOpts);
+  TargetOpts.swap(X.TargetOpts);
+  DiagnosticOpts.swap(X.DiagnosticOpts);
+  HeaderSearchOpts.swap(X.HeaderSearchOpts);
+  PreprocessorOpts.swap(X.PreprocessorOpts);
+  AnalyzerOpts.swap(X.AnalyzerOpts);
+  return *this;
+}
+
+CompilerInvocationRefBase &
+CompilerInvocationRefBase::operator=(CompilerInvocationRefBase &&X) = default;
+
 CompilerInvocationRefBase::~CompilerInvocationRefBase() = default;
 
 //===----------------------------------------------------------------------===//

diff  --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 5062a2e6cb8fe..0baf17f123acc 100644
--- a/clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -97,7 +97,7 @@ TEST(ContainsN, Two) {
   ASSERT_THAT(Array, ContainsN(StrEq("x"), 2));
 }
 
-// Copy constructor performs a deep copy of reference-counted pointers.
+// Copy constructor/assignment perform deep copy of reference-counted pointers.
 
 TEST(CompilerInvocationTest, DeepCopyConstructor) {
   CompilerInvocation A;
@@ -109,6 +109,17 @@ TEST(CompilerInvocationTest, DeepCopyConstructor) {
   ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
 }
 
+TEST(CompilerInvocationTest, DeepCopyAssignment) {
+  CompilerInvocation A;
+  A.getAnalyzerOpts()->Config["Key"] = "Old";
+
+  CompilerInvocation B;
+  B = A;
+  B.getAnalyzerOpts()->Config["Key"] = "New";
+
+  ASSERT_EQ(A.getAnalyzerOpts()->Config["Key"], "Old");
+}
+
 // Boolean option with a keypath that defaults to true.
 // The only flag with a negative spelling can set the keypath to false.
 


        


More information about the cfe-commits mailing list