From b6a8fd5475014e1a00a5bf3135a5e5ef84f96af0 Mon Sep 17 00:00:00 2001 From: ykiko Date: Sun, 13 Oct 2024 18:32:03 +0800 Subject: [PATCH] Basic logic for building PCM. --- include/Compiler/Compiler.h | 2 +- src/Compiler/Compiler.cpp | 34 +++++++++++++++++-------- tests/AST/Compiler.cpp | 50 ++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/include/Compiler/Compiler.h b/include/Compiler/Compiler.h index 0af8ebf4..666051c1 100644 --- a/include/Compiler/Compiler.h +++ b/include/Compiler/Compiler.h @@ -34,7 +34,7 @@ public: llvm::StringRef filepath); /// build PreCompiledModule. - void buildPCM(); + void buildPCM(llvm::StringRef filename, llvm::StringRef content, llvm::ArrayRef args); /// build AST. void buildAST(); diff --git a/src/Compiler/Compiler.cpp b/src/Compiler/Compiler.cpp index 49bef4d8..a79a5c0a 100644 --- a/src/Compiler/Compiler.cpp +++ b/src/Compiler/Compiler.cpp @@ -113,18 +113,9 @@ void Compiler::buildPCH(llvm::StringRef filename, llvm::StringRef content, llvm: std::terminate(); } - instance->getASTContext().getTranslationUnitDecl()->dump(); + // instance->getASTContext().getTranslationUnitDecl()->dump(); action.EndSourceFile(); - - // auto err = instance->ExecuteAction(action); - // - //// - // - // if(!err) { - // llvm::outs() << "Error has occured!!"; - // std::terminate(); - //} } void Compiler::applyPCH(clang::CompilerInvocation& invocation, @@ -146,4 +137,27 @@ void Compiler::applyPCH(clang::CompilerInvocation& invocation, PreprocessorOpts.UsePredefines = false; } +void Compiler::buildPCM(llvm::StringRef filename, llvm::StringRef content, llvm::ArrayRef args) { + auto invocation = createInvocation(filename, content, args); + + auto instance = createInstance(std::move(invocation)); + + auto& frontend = instance->getFrontendOpts(); + frontend.OutputFile = "/home/ykiko/C++/clice2/build/cache/xxx.pcm"; + + auto action = clang::GenerateReducedModuleInterfaceAction(); + + if(!action.BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0])) { + llvm::errs() << "Failed to begin source file\n"; + std::terminate(); + } + + if(auto error = action.Execute()) { + llvm::errs() << "Failed to execute action: " << error << "\n"; + std::terminate(); + } + + action.EndSourceFile(); +} + } // namespace clice diff --git a/tests/AST/Compiler.cpp b/tests/AST/Compiler.cpp index 87b27668..ceaab01d 100644 --- a/tests/AST/Compiler.cpp +++ b/tests/AST/Compiler.cpp @@ -29,7 +29,7 @@ TEST(clice, ModuleScanner) { }); } -TEST(clice, Compiler) { +TEST(clice, PCH) { const char* code = R"( #include @@ -69,5 +69,53 @@ int main(){ instance->getASTContext().getTranslationUnitDecl()->dump(); } +TEST(clice, PCM) { + const char* mod = R"( +export module M; + +export constexpr int f() { + return 42; +} +)"; + + const char* code = R"( +import M; + +int main() { + constexpr int x = f(); + return x; +} +)"; + + std::vector compileArgs = { + "clang++", + "-std=c++20", + "main.cpp", + "-resource-dir", + "/home/ykiko/C++/clice2/build/lib/clang/20", + }; + + Compiler compiler; + compiler.buildPCM("main.cpp", mod, compileArgs); + + auto invocation = createInvocation("main.cpp", code, compileArgs); + // compiler.applyPCH(*invocation, "main.cpp", code, "/home/ykiko/C++/clice2/build/cache/xxx.pch"); + invocation->getHeaderSearchOpts().PrebuiltModuleFiles.emplace("M", "/home/ykiko/C++/clice2/build/cache/xxx.pcm"); + auto instance = createInstance(std::move(invocation)); + auto action = std::make_unique(); + + if(!action->BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0])) { + llvm::errs() << "Failed to begin source file\n"; + std::terminate(); + } + + if(auto error = action->Execute()) { + llvm::errs() << "Failed to execute action: " << error << "\n"; + std::terminate(); + } + + instance->getASTContext().getTranslationUnitDecl()->dump(); +} + } // namespace