1 //=== unittests/CodeGen/IncrementalProcessingTest.cpp - IncrementalCodeGen ===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InterpreterTestFixture.h" 10 11 #include "clang/AST/ASTConsumer.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/AST/RecursiveASTVisitor.h" 14 #include "clang/Basic/TargetInfo.h" 15 #include "clang/CodeGen/ModuleBuilder.h" 16 #include "clang/Frontend/CompilerInstance.h" 17 #include "clang/Interpreter/Interpreter.h" 18 #include "clang/Lex/Preprocessor.h" 19 #include "clang/Parse/Parser.h" 20 #include "clang/Sema/Sema.h" 21 22 #include "llvm/ExecutionEngine/Orc/LLJIT.h" 23 #include "llvm/IR/LLVMContext.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/TargetParser/Host.h" 27 #include "llvm/TargetParser/Triple.h" 28 #include "gtest/gtest.h" 29 30 #include <memory> 31 32 #if defined(_AIX) || defined(__MVS__) 33 #define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT 34 #endif 35 36 using namespace llvm; 37 using namespace clang; 38 39 namespace { 40 41 class IncrementalProcessingTest : public InterpreterTestBase {}; 42 43 // Incremental processing produces several modules, all using the same "main 44 // file". Make sure CodeGen can cope with that, e.g. for static initializers. 45 const char TestProgram1[] = "extern \"C\" int funcForProg1() { return 17; }\n" 46 "struct EmitCXXGlobalInitFunc1 {\n" 47 " EmitCXXGlobalInitFunc1() {}\n" 48 "} test1;"; 49 50 const char TestProgram2[] = "extern \"C\" int funcForProg2() { return 42; }\n" 51 "struct EmitCXXGlobalInitFunc2 {\n" 52 " EmitCXXGlobalInitFunc2() {}\n" 53 "} test2;"; 54 55 const Function *getGlobalInit(llvm::Module *M) { 56 for (const auto &Func : *M) 57 if (Func.hasName() && Func.getName().starts_with("_GLOBAL__sub_I_")) 58 return &Func; 59 60 return nullptr; 61 } 62 63 TEST_F(IncrementalProcessingTest, EmitCXXGlobalInitFunc) { 64 std::vector<const char *> ClangArgv = {"-Xclang", "-emit-llvm-only"}; 65 auto CB = clang::IncrementalCompilerBuilder(); 66 CB.SetCompilerArgs(ClangArgv); 67 auto CI = cantFail(CB.CreateCpp()); 68 auto Interp = llvm::cantFail(Interpreter::create(std::move(CI))); 69 70 std::array<clang::PartialTranslationUnit *, 2> PTUs; 71 72 PTUs[0] = &llvm::cantFail(Interp->Parse(TestProgram1)); 73 ASSERT_TRUE(PTUs[0]->TheModule); 74 ASSERT_TRUE(PTUs[0]->TheModule->getFunction("funcForProg1")); 75 76 PTUs[1] = &llvm::cantFail(Interp->Parse(TestProgram2)); 77 ASSERT_TRUE(PTUs[1]->TheModule); 78 ASSERT_TRUE(PTUs[1]->TheModule->getFunction("funcForProg2")); 79 // First code should not end up in second module: 80 ASSERT_FALSE(PTUs[1]->TheModule->getFunction("funcForProg1")); 81 82 // Make sure global inits exist and are unique: 83 const Function *GlobalInit1 = getGlobalInit(PTUs[0]->TheModule.get()); 84 ASSERT_TRUE(GlobalInit1); 85 86 const Function *GlobalInit2 = getGlobalInit(PTUs[1]->TheModule.get()); 87 ASSERT_TRUE(GlobalInit2); 88 89 ASSERT_FALSE(GlobalInit1->getName() == GlobalInit2->getName()); 90 } 91 92 } // end anonymous namespace 93