diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2023-09-07 14:01:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-07 14:01:40 -0700 |
commit | e1cc299ec885d0b1607f162322fe23b772866acf (patch) | |
tree | 121476a1e4cc7c7fb0b95717455a61dc6132ccba /clang/unittests/Frontend/CompilerInvocationTest.cpp | |
parent | 0a4a8bec34e970a45d61775329293a2f562f521f (diff) | |
download | llvm-e1cc299ec885d0b1607f162322fe23b772866acf.zip llvm-e1cc299ec885d0b1607f162322fe23b772866acf.tar.gz llvm-e1cc299ec885d0b1607f162322fe23b772866acf.tar.bz2 |
[clang] Introduce copy-on-write `CompilerInvocation` (#65412)
This PR introduces new copy-on-write `CompilerInvocation` class
(`CowCompilerInvocation`), which will be used by the dependency scanner
to reduce the number of copies performed when generating command lines
for discovered modules.
Diffstat (limited to 'clang/unittests/Frontend/CompilerInvocationTest.cpp')
-rw-r--r-- | clang/unittests/Frontend/CompilerInvocationTest.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp index 36a2eda..7912253 100644 --- a/clang/unittests/Frontend/CompilerInvocationTest.cpp +++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -121,6 +121,46 @@ TEST(CompilerInvocationTest, DeepCopyAssignment) { ASSERT_EQ(A.getAnalyzerOpts().Config["Key"], "Old"); } +TEST(CompilerInvocationTest, CopyOnWriteConstructor) { + CowCompilerInvocation A; + A.getMutFrontendOpts().OutputFile = "x.o"; + + // B's FrontendOptions are initially shared with A. + CowCompilerInvocation B(A); + EXPECT_EQ(&A.getFrontendOpts(), &B.getFrontendOpts()); + + // Modifying A's FrontendOptions creates new copy, does not affect other opts. + A.getMutFrontendOpts().OutputFile = "y.o"; + EXPECT_NE(&A.getFrontendOpts(), &B.getFrontendOpts()); + EXPECT_EQ(&A.getCodeGenOpts(), &B.getCodeGenOpts()); + + // The new copy reflects the modification, old instance remains unchanged. + EXPECT_EQ(A.getFrontendOpts().OutputFile, "y.o"); + EXPECT_EQ(B.getFrontendOpts().OutputFile, "x.o"); +} + +TEST(CompilerInvocationTest, CopyOnWriteAssignment) { + CowCompilerInvocation A; + A.getMutFrontendOpts().OutputFile = "x.o"; + + // B's FrontendOptions are initially independent of A. + CowCompilerInvocation B; + EXPECT_NE(&A.getFrontendOpts(), &B.getFrontendOpts()); + + // B's FrontendOptions are shared with A after assignment. + B = A; + EXPECT_EQ(&A.getFrontendOpts(), &B.getFrontendOpts()); + + // Modifying A's FrontendOptions creates new copy, does not affect other opts. + A.getMutFrontendOpts().OutputFile = "y.o"; + EXPECT_NE(&A.getFrontendOpts(), &B.getFrontendOpts()); + EXPECT_EQ(&A.getCodeGenOpts(), &B.getCodeGenOpts()); + + // The new copy reflects the modification, old instance remains unchanged. + EXPECT_EQ(A.getFrontendOpts().OutputFile, "y.o"); + EXPECT_EQ(B.getFrontendOpts().OutputFile, "x.o"); +} + // Boolean option with a keypath that defaults to true. // The only flag with a negative spelling can set the keypath to false. |