diff options
author | Jan Svoboda <jan_svoboda@apple.com> | 2025-05-01 07:31:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-01 07:31:30 -0700 |
commit | b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17 (patch) | |
tree | 91fa427a131edc9cd16c9bef896a64083442218e /clang/unittests/Frontend | |
parent | 0009a1783490a8ff69251a0ec7df1891a427cfb0 (diff) | |
download | llvm-b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17.zip llvm-b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17.tar.gz llvm-b69dcb873476cd8e7d3f6f9ffd5b6d0bbe1a3a17.tar.bz2 |
[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).
Diffstat (limited to 'clang/unittests/Frontend')
-rw-r--r-- | clang/unittests/Frontend/CodeGenActionTest.cpp | 9 | ||||
-rw-r--r-- | clang/unittests/Frontend/CompilerInstanceTest.cpp | 3 | ||||
-rw-r--r-- | clang/unittests/Frontend/FrontendActionTest.cpp | 18 | ||||
-rw-r--r-- | clang/unittests/Frontend/OutputStreamTest.cpp | 9 |
4 files changed, 13 insertions, 26 deletions
diff --git a/clang/unittests/Frontend/CodeGenActionTest.cpp b/clang/unittests/Frontend/CodeGenActionTest.cpp index d855302..90818b7 100644 --- a/clang/unittests/Frontend/CodeGenActionTest.cpp +++ b/clang/unittests/Frontend/CodeGenActionTest.cpp @@ -51,8 +51,7 @@ TEST(CodeGenTest, TestNullCodeGen) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = EmitLLVM; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; - Compiler.setInvocation(std::move(Invocation)); + CompilerInstance Compiler(std::move(Invocation)); Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); EXPECT_TRUE(Compiler.hasDiagnostics()); @@ -69,8 +68,7 @@ TEST(CodeGenTest, CodeGenFromIRMemBuffer) { FrontendInputFile(*MemBuffer, Language::LLVM_IR)); Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; - Compiler.setInvocation(std::move(Invocation)); + CompilerInstance Compiler(std::move(Invocation)); Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); EXPECT_TRUE(Compiler.hasDiagnostics()); @@ -97,11 +95,10 @@ TEST(CodeGenTest, DebugInfoCWDCodeGen) { Invocation->getFrontendOpts().ProgramAction = EmitLLVM; Invocation->getTargetOpts().Triple = "x86_64-unknown-linux-gnu"; Invocation->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); - CompilerInstance Compiler; + CompilerInstance Compiler(std::move(Invocation)); SmallString<256> IRBuffer; Compiler.setOutputStream(std::make_unique<raw_svector_ostream>(IRBuffer)); - Compiler.setInvocation(std::move(Invocation)); Compiler.createDiagnostics(*VFS); Compiler.createFileManager(std::move(VFS)); diff --git a/clang/unittests/Frontend/CompilerInstanceTest.cpp b/clang/unittests/Frontend/CompilerInstanceTest.cpp index 07329eb2..6e9a6f5 100644 --- a/clang/unittests/Frontend/CompilerInstanceTest.cpp +++ b/clang/unittests/Frontend/CompilerInstanceTest.cpp @@ -66,9 +66,8 @@ TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { FAIL() << "could not create compiler invocation"; // Create a minimal CompilerInstance which should use the VFS we specified // in the CompilerInvocation (as we don't explicitly set our own). - CompilerInstance Instance; + CompilerInstance Instance(std::move(CInvok)); Instance.setDiagnostics(Diags.get()); - Instance.setInvocation(CInvok); Instance.createFileManager(); // Check if the virtual file exists which means that our VFS is used by the diff --git a/clang/unittests/Frontend/FrontendActionTest.cpp b/clang/unittests/Frontend/FrontendActionTest.cpp index a6bb767..4e04078 100644 --- a/clang/unittests/Frontend/FrontendActionTest.cpp +++ b/clang/unittests/Frontend/FrontendActionTest.cpp @@ -90,8 +90,7 @@ TEST(ASTFrontendAction, Sanity) { FrontendInputFile("test.cc", Language::CXX)); invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance compiler; - compiler.setInvocation(std::move(invocation)); + CompilerInstance compiler(std::move(invocation)); compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); TestASTFrontendAction test_action; @@ -110,8 +109,7 @@ TEST(ASTFrontendAction, IncrementalParsing) { FrontendInputFile("test.cc", Language::CXX)); invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance compiler; - compiler.setInvocation(std::move(invocation)); + CompilerInstance compiler(std::move(invocation)); compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true); @@ -137,8 +135,7 @@ TEST(ASTFrontendAction, LateTemplateIncrementalParsing) { FrontendInputFile("test.cc", Language::CXX)); invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance compiler; - compiler.setInvocation(std::move(invocation)); + CompilerInstance compiler(std::move(invocation)); compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true, @@ -183,8 +180,7 @@ TEST(PreprocessorFrontendAction, EndSourceFile) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; - Compiler.setInvocation(std::move(Invocation)); + CompilerInstance Compiler(std::move(Invocation)); Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); TestPPCallbacks *Callbacks = new TestPPCallbacks; @@ -244,8 +240,7 @@ TEST(ASTFrontendAction, ExternalSemaSource) { FrontendInputFile("test.cc", Language::CXX)); Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly; Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; - CompilerInstance Compiler; - Compiler.setInvocation(std::move(Invocation)); + CompilerInstance Compiler(std::move(Invocation)); auto *TDC = new TypoDiagnosticConsumer; Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem(), TDC, /*ShouldOwnClient=*/true); @@ -278,8 +273,7 @@ TEST(GeneratePCHFrontendAction, CacheGeneratedPCH) { Invocation->getFrontendOpts().OutputFile = PCHFilename.str().str(); Invocation->getFrontendOpts().ProgramAction = frontend::GeneratePCH; Invocation->getTargetOpts().Triple = "x86_64-apple-darwin19.0.0"; - CompilerInstance Compiler; - Compiler.setInvocation(std::move(Invocation)); + CompilerInstance Compiler(std::move(Invocation)); Compiler.createDiagnostics(*llvm::vfs::getRealFileSystem()); GeneratePCHAction TestAction; 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<char, 256> IRBuffer; std::unique_ptr<raw_pwrite_stream> 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<raw_null_ostream>()); - Compiler.setInvocation(std::move(Invocation)); IntrusiveRefCntPtr<DiagnosticOptions> 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<raw_ostream> VerboseStream = std::make_unique<raw_string_ostream>(VerboseBuffer); Compiler.setOutputStream(std::make_unique<raw_null_ostream>()); - Compiler.setInvocation(std::move(Invocation)); IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); Compiler.createDiagnostics( *llvm::vfs::getRealFileSystem(), |