From b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Thu, 1 May 2025 07:31:30 -0700 Subject: [clang][frontend] Require invocation to construct `CompilerInstance` (#137668) This PR makes it so that `CompilerInvocation` needs to be provided to `CompilerInstance` on construction. There are a couple of benefits in my view: * Making it impossible to mis-use some `CompilerInstance` APIs. For example there are cases, where `createDiagnostics()` was called before `setInvocation()`, causing the `DiagnosticEngine` to use the default-constructed `DiagnosticOptions` instead of the intended ones. * This shrinks `CompilerInstance`'s state space. * This makes it possible to access **the** invocation in `CompilerInstance`'s constructor (to be used in a follow-up). --- clang/unittests/Serialization/ModuleCacheTest.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'clang/unittests/Serialization/ModuleCacheTest.cpp') diff --git a/clang/unittests/Serialization/ModuleCacheTest.cpp b/clang/unittests/Serialization/ModuleCacheTest.cpp index 6ceee1c..38003e9 100644 --- a/clang/unittests/Serialization/ModuleCacheTest.cpp +++ b/clang/unittests/Serialization/ModuleCacheTest.cpp @@ -119,9 +119,8 @@ TEST_F(ModuleCacheTest, CachedModuleNewPath) { std::shared_ptr Invocation = createInvocationAndEnableFree(Args, CIOpts); ASSERT_TRUE(Invocation); - CompilerInstance Instance; + CompilerInstance Instance(std::move(Invocation)); Instance.setDiagnostics(Diags.get()); - Instance.setInvocation(Invocation); SyntaxOnlyAction Action; ASSERT_TRUE(Instance.ExecuteAction(Action)); ASSERT_FALSE(Diags->hasErrorOccurred()); @@ -142,10 +141,10 @@ TEST_F(ModuleCacheTest, CachedModuleNewPath) { std::shared_ptr Invocation2 = createInvocationAndEnableFree(Args2, CIOpts); ASSERT_TRUE(Invocation2); - CompilerInstance Instance2(Instance.getPCHContainerOperations(), + CompilerInstance Instance2(std::move(Invocation2), + Instance.getPCHContainerOperations(), &Instance.getModuleCache()); Instance2.setDiagnostics(Diags.get()); - Instance2.setInvocation(Invocation2); SyntaxOnlyAction Action2; ASSERT_FALSE(Instance2.ExecuteAction(Action2)); ASSERT_TRUE(Diags->hasErrorOccurred()); @@ -169,9 +168,8 @@ TEST_F(ModuleCacheTest, CachedModuleNewPathAllowErrors) { std::shared_ptr Invocation = createInvocationAndEnableFree(Args, CIOpts); ASSERT_TRUE(Invocation); - CompilerInstance Instance; + CompilerInstance Instance(std::move(Invocation)); Instance.setDiagnostics(Diags.get()); - Instance.setInvocation(Invocation); SyntaxOnlyAction Action; ASSERT_TRUE(Instance.ExecuteAction(Action)); ASSERT_FALSE(Diags->hasErrorOccurred()); @@ -186,10 +184,10 @@ TEST_F(ModuleCacheTest, CachedModuleNewPathAllowErrors) { std::shared_ptr Invocation2 = createInvocationAndEnableFree(Args2, CIOpts); ASSERT_TRUE(Invocation2); - CompilerInstance Instance2(Instance.getPCHContainerOperations(), + CompilerInstance Instance2(std::move(Invocation2), + Instance.getPCHContainerOperations(), &Instance.getModuleCache()); Instance2.setDiagnostics(Diags.get()); - Instance2.setInvocation(Invocation2); SyntaxOnlyAction Action2; ASSERT_FALSE(Instance2.ExecuteAction(Action2)); ASSERT_TRUE(Diags->hasErrorOccurred()); -- cgit v1.1