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/Frontend/OutputStreamTest.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'clang/unittests/Frontend/OutputStreamTest.cpp') diff --git a/clang/unittests/Frontend/OutputStreamTest.cpp b/clang/unittests/Frontend/OutputStreamTest.cpp index 27d2d7f..0eda3a1 100644 --- a/clang/unittests/Frontend/OutputStreamTest.cpp +++ b/clang/unittests/Frontend/OutputStreamTest.cpp @@ -31,14 +31,13 @@ TEST(FrontendOutputTests, TestOutputStream) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = EmitBC; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; + CompilerInstance Compiler(std::move(Invocation)); SmallVector IRBuffer; std::unique_ptr IRStream( new raw_svector_ostream(IRBuffer)); Compiler.setOutputStream(std::move(IRStream)); - Compiler.setInvocation(std::move(Invocation)); Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); bool Success = ExecuteCompilerInvocation(&Compiler); @@ -56,13 +55,12 @@ TEST(FrontendOutputTests, TestVerboseOutputStreamShared) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = EmitBC; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; + CompilerInstance Compiler(std::move(Invocation)); std::string VerboseBuffer; raw_string_ostream VerboseStream(VerboseBuffer); Compiler.setOutputStream(std::make_unique()); - Compiler.setInvocation(std::move(Invocation)); IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); Compiler.createDiagnostics( *llvm::vfs::getRealFileSystem(), @@ -87,13 +85,12 @@ TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = EmitBC; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; + CompilerInstance Compiler(std::move(Invocation)); std::unique_ptr VerboseStream = std::make_unique(VerboseBuffer); Compiler.setOutputStream(std::make_unique()); - Compiler.setInvocation(std::move(Invocation)); IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); Compiler.createDiagnostics( *llvm::vfs::getRealFileSystem(), -- cgit v1.1