[PATCH] D100473: [clang] Implement CompilerInvocation copy assignment

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 14 05:40:05 PDT 2021


jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, dexonsmith.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch implements the copy assignment for `CompilerInvocation`.

Eventually, the deep-copy operation will be moved into a `clone()` method (D100460 <https://reviews.llvm.org/D100460>), but until then, this is necessary for basic ergonomics.

Depends on D100455 <https://reviews.llvm.org/D100455>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100473

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


Index: clang/unittests/Frontend/CompilerInvocationTest.cpp
===================================================================
--- clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -97,7 +97,7 @@
   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 @@
   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.
 
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -125,6 +125,17 @@
       PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())),
       AnalyzerOpts(new AnalyzerOptions(*X.getAnalyzerOpts())) {}
 
+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() = default;
 
 //===----------------------------------------------------------------------===//
Index: clang/include/clang/Frontend/CompilerInvocation.h
===================================================================
--- clang/include/clang/Frontend/CompilerInvocation.h
+++ clang/include/clang/Frontend/CompilerInvocation.h
@@ -91,8 +91,7 @@
 
   CompilerInvocationRefBase();
   CompilerInvocationRefBase(const CompilerInvocationRefBase &X);
-  CompilerInvocationRefBase &
-  operator=(const CompilerInvocationRefBase &) = delete;
+  CompilerInvocationRefBase &operator=(CompilerInvocationRefBase X);
   ~CompilerInvocationRefBase();
 
   LangOptions *getLangOpts() { return LangOpts.get(); }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100473.337422.patch
Type: text/x-patch
Size: 2515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210414/64c07081/attachment-0001.bin>


More information about the cfe-commits mailing list