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 "clang/AST/ASTConsumer.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecursiveASTVisitor.h"
12#include "clang/Basic/TargetInfo.h"
13#include "clang/CodeGen/ModuleBuilder.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Interpreter/Interpreter.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Parse/Parser.h"
18#include "clang/Sema/Sema.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/TargetParser/Host.h"
23#include "llvm/TargetParser/Triple.h"
24#include "gtest/gtest.h"
25
26#include <memory>
27
28using namespace llvm;
29using namespace clang;
30
31namespace {
32
33// Incremental processing produces several modules, all using the same "main
34// file". Make sure CodeGen can cope with that, e.g. for static initializers.
35const char TestProgram1[] = "extern \"C\" int funcForProg1() { return 17; }\n"
36 "struct EmitCXXGlobalInitFunc1 {\n"
37 " EmitCXXGlobalInitFunc1() {}\n"
38 "} test1;";
39
40const char TestProgram2[] = "extern \"C\" int funcForProg2() { return 42; }\n"
41 "struct EmitCXXGlobalInitFunc2 {\n"
42 " EmitCXXGlobalInitFunc2() {}\n"
43 "} test2;";
44
45const Function *getGlobalInit(llvm::Module *M) {
46 for (const auto &Func : *M)
47 if (Func.hasName() && Func.getName().starts_with(Prefix: "_GLOBAL__sub_I_"))
48 return &Func;
49
50 return nullptr;
51}
52
53TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
54 std::vector<const char *> ClangArgv = {"-Xclang", "-emit-llvm-only"};
55 auto CB = clang::IncrementalCompilerBuilder();
56 CB.SetCompilerArgs(ClangArgv);
57 auto CI = cantFail(ValOrErr: CB.CreateCpp());
58 auto Interp = llvm::cantFail(ValOrErr: Interpreter::create(CI: std::move(CI)));
59
60 std::array<clang::PartialTranslationUnit *, 2> PTUs;
61
62 PTUs[0] = &llvm::cantFail(ValOrErr: Interp->Parse(Code: TestProgram1));
63 ASSERT_TRUE(PTUs[0]->TheModule);
64 ASSERT_TRUE(PTUs[0]->TheModule->getFunction("funcForProg1"));
65
66 PTUs[1] = &llvm::cantFail(ValOrErr: Interp->Parse(Code: TestProgram2));
67 ASSERT_TRUE(PTUs[1]->TheModule);
68 ASSERT_TRUE(PTUs[1]->TheModule->getFunction("funcForProg2"));
69 // First code should not end up in second module:
70 ASSERT_FALSE(PTUs[1]->TheModule->getFunction("funcForProg1"));
71
72 // Make sure global inits exist and are unique:
73 const Function *GlobalInit1 = getGlobalInit(M: PTUs[0]->TheModule.get());
74 ASSERT_TRUE(GlobalInit1);
75
76 const Function *GlobalInit2 = getGlobalInit(M: PTUs[1]->TheModule.get());
77 ASSERT_TRUE(GlobalInit2);
78
79 ASSERT_FALSE(GlobalInit1->getName() == GlobalInit2->getName());
80}
81
82} // end anonymous namespace
83

source code of clang/unittests/Interpreter/IncrementalProcessingTest.cpp