1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| //=== unittests/CodeGen/IncrementalProcessingTest.cpp - IncrementalCodeGen ===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/Parser.h"
#include "clang/Sema/Sema.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
#include <memory>
using namespace llvm;
using namespace clang;
namespace {
// Incremental processing produces several modules, all using the same "main
// file". Make sure CodeGen can cope with that, e.g. for static initializers.
const char TestProgram1[] =
"extern \"C\" int funcForProg1() { return 17; }\n"
"struct EmitCXXGlobalInitFunc1 {\n"
" EmitCXXGlobalInitFunc1() {}\n"
"} test1;";
const char TestProgram2[] =
"extern \"C\" int funcForProg2() { return 42; }\n"
"struct EmitCXXGlobalInitFunc2 {\n"
" EmitCXXGlobalInitFunc2() {}\n"
"} test2;";
/// An incremental version of ParseAST().
static std::unique_ptr<llvm::Module>
IncrementalParseAST(CompilerInstance& CI, Parser& P,
CodeGenerator& CG, const char* code) {
static int counter = 0;
struct IncreaseCounterOnRet {
~IncreaseCounterOnRet() {
++counter;
}
} ICOR;
Sema& S = CI.getSema();
clang::SourceManager &SM = S.getSourceManager();
if (!code) {
// Main file
SM.setMainFileID(SM.createFileID(
llvm::MemoryBuffer::getMemBuffer(" "), clang::SrcMgr::C_User));
S.getPreprocessor().EnterMainSourceFile();
P.Initialize();
} else {
FileID FID = SM.createFileID(
llvm::MemoryBuffer::getMemBuffer(code), clang::SrcMgr::C_User);
SourceLocation MainStartLoc = SM.getLocForStartOfFile(SM.getMainFileID());
SourceLocation InclLoc = MainStartLoc.getLocWithOffset(counter);
S.getPreprocessor().EnterSourceFile(FID, 0, InclLoc);
}
ExternalASTSource *External = S.getASTContext().getExternalSource();
if (External)
External->StartTranslationUnit(&CG);
Parser::DeclGroupPtrTy ADecl;
for (bool AtEOF = P.ParseFirstTopLevelDecl(ADecl); !AtEOF;
AtEOF = P.ParseTopLevelDecl(ADecl)) {
// If we got a null return and something *was* parsed, ignore it. This
// is due to a top-level semicolon, an action override, or a parse error
// skipping something.
if (ADecl && !CG.HandleTopLevelDecl(ADecl.get()))
return nullptr;
}
// Process any TopLevelDecls generated by #pragma weak.
for (Decl *D : S.WeakTopLevelDecls())
CG.HandleTopLevelDecl(DeclGroupRef(D));
CG.HandleTranslationUnit(S.getASTContext());
std::unique_ptr<llvm::Module> M(CG.ReleaseModule());
// Switch to next module.
CG.StartModule("incremental-module-" + std::to_string(counter),
M->getContext());
return M;
}
const Function* getGlobalInit(llvm::Module& M) {
for (const auto& Func: M)
if (Func.hasName() && Func.getName().startswith("_GLOBAL__sub_I_"))
return &Func;
return nullptr;
}
TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
LLVMContext Context;
CompilerInstance compiler;
compiler.createDiagnostics();
compiler.getLangOpts().CPlusPlus = 1;
compiler.getLangOpts().CPlusPlus11 = 1;
compiler.getTargetOpts().Triple = llvm::Triple::normalize(
llvm::sys::getProcessTriple());
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
compiler.getDiagnostics(),
std::make_shared<clang::TargetOptions>(
compiler.getTargetOpts())));
compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor(clang::TU_Prefix);
compiler.getPreprocessor().enableIncrementalProcessing();
compiler.createASTContext();
CodeGenerator* CG =
CreateLLVMCodeGen(
compiler.getDiagnostics(),
"main-module",
compiler.getHeaderSearchOpts(),
compiler.getPreprocessorOpts(),
compiler.getCodeGenOpts(),
Context);
compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(CG));
compiler.createSema(clang::TU_Prefix, nullptr);
Sema& S = compiler.getSema();
std::unique_ptr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
/*SkipFunctionBodies*/ false));
Parser &P = *ParseOP.get();
std::array<std::unique_ptr<llvm::Module>, 3> M;
M[0] = IncrementalParseAST(compiler, P, *CG, nullptr);
ASSERT_TRUE(M[0]);
M[1] = IncrementalParseAST(compiler, P, *CG, TestProgram1);
ASSERT_TRUE(M[1]);
ASSERT_TRUE(M[1]->getFunction("funcForProg1"));
M[2] = IncrementalParseAST(compiler, P, *CG, TestProgram2);
ASSERT_TRUE(M[2]);
ASSERT_TRUE(M[2]->getFunction("funcForProg2"));
// First code should not end up in second module:
ASSERT_FALSE(M[2]->getFunction("funcForProg1"));
// Make sure global inits exist and are unique:
const Function* GlobalInit1 = getGlobalInit(*M[1]);
ASSERT_TRUE(GlobalInit1);
const Function* GlobalInit2 = getGlobalInit(*M[2]);
ASSERT_TRUE(GlobalInit2);
ASSERT_FALSE(GlobalInit1->getName() == GlobalInit2->getName());
}
} // end anonymous namespace
|