/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===// |
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 | | // This builds an AST and converts it to LLVM Code. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/CodeGen/ModuleBuilder.h" |
14 | | #include "CGDebugInfo.h" |
15 | | #include "CodeGenModule.h" |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/DeclObjC.h" |
18 | | #include "clang/AST/Expr.h" |
19 | | #include "clang/Basic/CodeGenOptions.h" |
20 | | #include "clang/Basic/Diagnostic.h" |
21 | | #include "clang/Basic/TargetInfo.h" |
22 | | #include "llvm/ADT/StringRef.h" |
23 | | #include "llvm/IR/DataLayout.h" |
24 | | #include "llvm/IR/LLVMContext.h" |
25 | | #include "llvm/IR/Module.h" |
26 | | #include <memory> |
27 | | |
28 | | using namespace clang; |
29 | | using namespace CodeGen; |
30 | | |
31 | | namespace { |
32 | | class CodeGeneratorImpl : public CodeGenerator { |
33 | | DiagnosticsEngine &Diags; |
34 | | ASTContext *Ctx; |
35 | | const HeaderSearchOptions &HeaderSearchOpts; // Only used for debug info. |
36 | | const PreprocessorOptions &PreprocessorOpts; // Only used for debug info. |
37 | | const CodeGenOptions CodeGenOpts; // Intentionally copied in. |
38 | | |
39 | | unsigned HandlingTopLevelDecls; |
40 | | |
41 | | /// Use this when emitting decls to block re-entrant decl emission. It will |
42 | | /// emit all deferred decls on scope exit. Set EmitDeferred to false if decl |
43 | | /// emission must be deferred longer, like at the end of a tag definition. |
44 | | struct HandlingTopLevelDeclRAII { |
45 | | CodeGeneratorImpl &Self; |
46 | | bool EmitDeferred; |
47 | | HandlingTopLevelDeclRAII(CodeGeneratorImpl &Self, |
48 | | bool EmitDeferred = true) |
49 | 20.5M | : Self(Self), EmitDeferred(EmitDeferred) { |
50 | 20.5M | ++Self.HandlingTopLevelDecls; |
51 | 20.5M | } |
52 | 20.5M | ~HandlingTopLevelDeclRAII() { |
53 | 20.5M | unsigned Level = --Self.HandlingTopLevelDecls; |
54 | 20.5M | if (Level == 0 && EmitDeferred20.5M ) |
55 | 17.5M | Self.EmitDeferredDecls(); |
56 | 20.5M | } |
57 | | }; |
58 | | |
59 | | CoverageSourceInfo *CoverageInfo; |
60 | | |
61 | | protected: |
62 | | std::unique_ptr<llvm::Module> M; |
63 | | std::unique_ptr<CodeGen::CodeGenModule> Builder; |
64 | | |
65 | | private: |
66 | | SmallVector<FunctionDecl *, 8> DeferredInlineMemberFuncDefs; |
67 | | |
68 | | static llvm::StringRef ExpandModuleName(llvm::StringRef ModuleName, |
69 | 35.4k | const CodeGenOptions &CGO) { |
70 | 35.4k | if (ModuleName == "-" && !CGO.MainFileName.empty()274 ) |
71 | 82 | return CGO.MainFileName; |
72 | 35.3k | return ModuleName; |
73 | 35.4k | } |
74 | | |
75 | | public: |
76 | | CodeGeneratorImpl(DiagnosticsEngine &diags, llvm::StringRef ModuleName, |
77 | | const HeaderSearchOptions &HSO, |
78 | | const PreprocessorOptions &PPO, const CodeGenOptions &CGO, |
79 | | llvm::LLVMContext &C, |
80 | | CoverageSourceInfo *CoverageInfo = nullptr) |
81 | | : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO), |
82 | | PreprocessorOpts(PPO), CodeGenOpts(CGO), HandlingTopLevelDecls(0), |
83 | | CoverageInfo(CoverageInfo), |
84 | 35.4k | M(new llvm::Module(ExpandModuleName(ModuleName, CGO), C)) { |
85 | 35.4k | C.setDiscardValueNames(CGO.DiscardValueNames); |
86 | 35.4k | } |
87 | | |
88 | 35.3k | ~CodeGeneratorImpl() override { |
89 | | // There should normally not be any leftover inline method definitions. |
90 | 35.3k | assert(DeferredInlineMemberFuncDefs.empty() || |
91 | 35.3k | Diags.hasErrorOccurred()); |
92 | 35.3k | } |
93 | | |
94 | 4 | CodeGenModule &CGM() { |
95 | 4 | return *Builder; |
96 | 4 | } |
97 | | |
98 | 98.1k | llvm::Module *GetModule() { |
99 | 98.1k | return M.get(); |
100 | 98.1k | } |
101 | | |
102 | 1.42k | CGDebugInfo *getCGDebugInfo() { |
103 | 1.42k | return Builder->getModuleDebugInfo(); |
104 | 1.42k | } |
105 | | |
106 | 34.8k | llvm::Module *ReleaseModule() { |
107 | 34.8k | return M.release(); |
108 | 34.8k | } |
109 | | |
110 | 386k | const Decl *GetDeclForMangledName(StringRef MangledName) { |
111 | 386k | GlobalDecl Result; |
112 | 386k | if (!Builder->lookupRepresentativeDecl(MangledName, Result)) |
113 | 112k | return nullptr; |
114 | 274k | const Decl *D = Result.getCanonicalDecl().getDecl(); |
115 | 274k | if (auto FD = dyn_cast<FunctionDecl>(D)) { |
116 | 272k | if (FD->hasBody(FD)) |
117 | 248k | return FD; |
118 | 272k | } else if (auto 1.11k TD1.11k = dyn_cast<TagDecl>(D)) { |
119 | 0 | if (auto Def = TD->getDefinition()) |
120 | 0 | return Def; |
121 | 0 | } |
122 | 25.6k | return D; |
123 | 274k | } |
124 | | |
125 | 1 | llvm::StringRef GetMangledName(GlobalDecl GD) { |
126 | 1 | return Builder->getMangledName(GD); |
127 | 1 | } |
128 | | |
129 | 1 | llvm::Constant *GetAddrOfGlobal(GlobalDecl global, bool isForDefinition) { |
130 | 1 | return Builder->GetAddrOfGlobal(global, ForDefinition_t(isForDefinition)); |
131 | 1 | } |
132 | | |
133 | | llvm::Module *StartModule(llvm::StringRef ModuleName, |
134 | 32 | llvm::LLVMContext &C) { |
135 | 32 | assert(!M && "Replacing existing Module?"); |
136 | 0 | M.reset(new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C)); |
137 | 32 | Initialize(*Ctx); |
138 | 32 | return M.get(); |
139 | 32 | } |
140 | | |
141 | 35.2k | void Initialize(ASTContext &Context) override { |
142 | 35.2k | Ctx = &Context; |
143 | | |
144 | 35.2k | M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple()); |
145 | 35.2k | M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString()); |
146 | 35.2k | const auto &SDKVersion = Ctx->getTargetInfo().getSDKVersion(); |
147 | 35.2k | if (!SDKVersion.empty()) |
148 | 3.61k | M->setSDKVersion(SDKVersion); |
149 | 35.2k | if (const auto *TVT = Ctx->getTargetInfo().getDarwinTargetVariantTriple()) |
150 | 1 | M->setDarwinTargetVariantTriple(TVT->getTriple()); |
151 | 35.2k | if (auto TVSDKVersion = |
152 | 35.2k | Ctx->getTargetInfo().getDarwinTargetVariantSDKVersion()) |
153 | 1 | M->setDarwinTargetVariantSDKVersion(*TVSDKVersion); |
154 | 35.2k | Builder.reset(new CodeGen::CodeGenModule(Context, HeaderSearchOpts, |
155 | 35.2k | PreprocessorOpts, CodeGenOpts, |
156 | 35.2k | *M, Diags, CoverageInfo)); |
157 | | |
158 | 35.2k | for (auto &&Lib : CodeGenOpts.DependentLibraries) |
159 | 86 | Builder->AddDependentLib(Lib); |
160 | 35.2k | for (auto &&Opt : CodeGenOpts.LinkerOptions) |
161 | 1 | Builder->AppendLinkerOptions(Opt); |
162 | 35.2k | } |
163 | | |
164 | 576k | void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override { |
165 | 576k | if (Diags.hasErrorOccurred()) |
166 | 47 | return; |
167 | | |
168 | 576k | Builder->HandleCXXStaticMemberVarInstantiation(VD); |
169 | 576k | } |
170 | | |
171 | 17.4M | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
172 | 17.4M | if (Diags.hasErrorOccurred()) |
173 | 2.99k | return true; |
174 | | |
175 | 17.4M | HandlingTopLevelDeclRAII HandlingDecl(*this); |
176 | | |
177 | | // Make sure to emit all elements of a Decl. |
178 | 35.5M | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I18.0M ) |
179 | 18.0M | Builder->EmitTopLevelDecl(*I); |
180 | | |
181 | 17.4M | return true; |
182 | 17.4M | } |
183 | | |
184 | 17.5M | void EmitDeferredDecls() { |
185 | 17.5M | if (DeferredInlineMemberFuncDefs.empty()) |
186 | 17.4M | return; |
187 | | |
188 | | // Emit any deferred inline method definitions. Note that more deferred |
189 | | // methods may be added during this loop, since ASTConsumer callbacks |
190 | | // can be invoked if AST inspection results in declarations being added. |
191 | 30.7k | HandlingTopLevelDeclRAII HandlingDecl(*this); |
192 | 452k | for (unsigned I = 0; I != DeferredInlineMemberFuncDefs.size(); ++I421k ) |
193 | 421k | Builder->EmitTopLevelDecl(DeferredInlineMemberFuncDefs[I]); |
194 | 30.7k | DeferredInlineMemberFuncDefs.clear(); |
195 | 30.7k | } |
196 | | |
197 | 423k | void HandleInlineFunctionDefinition(FunctionDecl *D) override { |
198 | 423k | if (Diags.hasErrorOccurred()) |
199 | 1.19k | return; |
200 | | |
201 | 421k | assert(D->doesThisDeclarationHaveABody()); |
202 | | |
203 | | // We may want to emit this definition. However, that decision might be |
204 | | // based on computing the linkage, and we have to defer that in case we |
205 | | // are inside of something that will change the method's final linkage, |
206 | | // e.g. |
207 | | // typedef struct { |
208 | | // void bar(); |
209 | | // void foo() { bar(); } |
210 | | // } A; |
211 | 0 | DeferredInlineMemberFuncDefs.push_back(D); |
212 | | |
213 | | // Provide some coverage mapping even for methods that aren't emitted. |
214 | | // Don't do this for templated classes though, as they may not be |
215 | | // instantiable. |
216 | 421k | if (!D->getLexicalDeclContext()->isDependentContext()) |
217 | 103k | Builder->AddDeferredUnusedCoverageMapping(D); |
218 | 421k | } |
219 | | |
220 | | /// HandleTagDeclDefinition - This callback is invoked each time a TagDecl |
221 | | /// to (e.g. struct, union, enum, class) is completed. This allows the |
222 | | /// client hack on the type, which can occur at any point in the file |
223 | | /// (because these can be defined in declspecs). |
224 | 2.09M | void HandleTagDeclDefinition(TagDecl *D) override { |
225 | 2.09M | if (Diags.hasErrorOccurred()) |
226 | 1.34k | return; |
227 | | |
228 | | // Don't allow re-entrant calls to CodeGen triggered by PCH |
229 | | // deserialization to emit deferred decls. |
230 | 2.09M | HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); |
231 | | |
232 | 2.09M | Builder->UpdateCompletedType(D); |
233 | | |
234 | | // For MSVC compatibility, treat declarations of static data members with |
235 | | // inline initializers as definitions. |
236 | 2.09M | if (Ctx->getTargetInfo().getCXXABI().isMicrosoft()) { |
237 | 25.2k | for (Decl *Member : D->decls()) { |
238 | 25.2k | if (VarDecl *VD = dyn_cast<VarDecl>(Member)) { |
239 | 330 | if (Ctx->isMSStaticDataMemberInlineDefinition(VD) && |
240 | 330 | Ctx->DeclMustBeEmitted(VD)83 ) { |
241 | 38 | Builder->EmitGlobal(VD); |
242 | 38 | } |
243 | 330 | } |
244 | 25.2k | } |
245 | 6.37k | } |
246 | | // For OpenMP emit declare reduction functions, if required. |
247 | 2.09M | if (Ctx->getLangOpts().OpenMP) { |
248 | 32.5k | for (Decl *Member : D->decls()) { |
249 | 32.5k | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Member)) { |
250 | 16 | if (Ctx->DeclMustBeEmitted(DRD)) |
251 | 10 | Builder->EmitGlobal(DRD); |
252 | 32.5k | } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Member)) { |
253 | 0 | if (Ctx->DeclMustBeEmitted(DMD)) |
254 | 0 | Builder->EmitGlobal(DMD); |
255 | 0 | } |
256 | 32.5k | } |
257 | 6.57k | } |
258 | 2.09M | } |
259 | | |
260 | 984k | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
261 | 984k | if (Diags.hasErrorOccurred()) |
262 | 1.58k | return; |
263 | | |
264 | | // Don't allow re-entrant calls to CodeGen triggered by PCH |
265 | | // deserialization to emit deferred decls. |
266 | 982k | HandlingTopLevelDeclRAII HandlingDecl(*this, /*EmitDeferred=*/false); |
267 | | |
268 | 982k | if (CodeGen::CGDebugInfo *DI = Builder->getModuleDebugInfo()) |
269 | 926k | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
270 | 916k | DI->completeRequiredType(RD); |
271 | 982k | } |
272 | | |
273 | 35.2k | void HandleTranslationUnit(ASTContext &Ctx) override { |
274 | | // Release the Builder when there is no error. |
275 | 35.2k | if (!Diags.hasErrorOccurred() && Builder34.7k ) |
276 | 34.7k | Builder->Release(); |
277 | | |
278 | | // If there are errors before or when releasing the Builder, reset |
279 | | // the module to stop here before invoking the backend. |
280 | 35.2k | if (Diags.hasErrorOccurred()) { |
281 | 509 | if (Builder) |
282 | 509 | Builder->clear(); |
283 | 509 | M.reset(); |
284 | 509 | return; |
285 | 509 | } |
286 | 35.2k | } |
287 | | |
288 | 494 | void AssignInheritanceModel(CXXRecordDecl *RD) override { |
289 | 494 | if (Diags.hasErrorOccurred()) |
290 | 18 | return; |
291 | | |
292 | 476 | Builder->RefreshTypeCacheForClass(RD); |
293 | 476 | } |
294 | | |
295 | 4.23k | void CompleteTentativeDefinition(VarDecl *D) override { |
296 | 4.23k | if (Diags.hasErrorOccurred()) |
297 | 115 | return; |
298 | | |
299 | 4.11k | Builder->EmitTentativeDefinition(D); |
300 | 4.11k | } |
301 | | |
302 | 7 | void CompleteExternalDeclaration(VarDecl *D) override { |
303 | 7 | Builder->EmitExternalDeclaration(D); |
304 | 7 | } |
305 | | |
306 | 795 | void HandleVTable(CXXRecordDecl *RD) override { |
307 | 795 | if (Diags.hasErrorOccurred()) |
308 | 0 | return; |
309 | | |
310 | 795 | Builder->EmitVTable(RD); |
311 | 795 | } |
312 | | }; |
313 | | } |
314 | | |
315 | 0 | void CodeGenerator::anchor() { } |
316 | | |
317 | 4 | CodeGenModule &CodeGenerator::CGM() { |
318 | 4 | return static_cast<CodeGeneratorImpl*>(this)->CGM(); |
319 | 4 | } |
320 | | |
321 | 98.1k | llvm::Module *CodeGenerator::GetModule() { |
322 | 98.1k | return static_cast<CodeGeneratorImpl*>(this)->GetModule(); |
323 | 98.1k | } |
324 | | |
325 | 34.8k | llvm::Module *CodeGenerator::ReleaseModule() { |
326 | 34.8k | return static_cast<CodeGeneratorImpl*>(this)->ReleaseModule(); |
327 | 34.8k | } |
328 | | |
329 | 1.42k | CGDebugInfo *CodeGenerator::getCGDebugInfo() { |
330 | 1.42k | return static_cast<CodeGeneratorImpl*>(this)->getCGDebugInfo(); |
331 | 1.42k | } |
332 | | |
333 | 386k | const Decl *CodeGenerator::GetDeclForMangledName(llvm::StringRef name) { |
334 | 386k | return static_cast<CodeGeneratorImpl*>(this)->GetDeclForMangledName(name); |
335 | 386k | } |
336 | | |
337 | 1 | llvm::StringRef CodeGenerator::GetMangledName(GlobalDecl GD) { |
338 | 1 | return static_cast<CodeGeneratorImpl *>(this)->GetMangledName(GD); |
339 | 1 | } |
340 | | |
341 | | llvm::Constant *CodeGenerator::GetAddrOfGlobal(GlobalDecl global, |
342 | 1 | bool isForDefinition) { |
343 | 1 | return static_cast<CodeGeneratorImpl*>(this) |
344 | 1 | ->GetAddrOfGlobal(global, isForDefinition); |
345 | 1 | } |
346 | | |
347 | | llvm::Module *CodeGenerator::StartModule(llvm::StringRef ModuleName, |
348 | 32 | llvm::LLVMContext &C) { |
349 | 32 | return static_cast<CodeGeneratorImpl*>(this)->StartModule(ModuleName, C); |
350 | 32 | } |
351 | | |
352 | | CodeGenerator *clang::CreateLLVMCodeGen( |
353 | | DiagnosticsEngine &Diags, llvm::StringRef ModuleName, |
354 | | const HeaderSearchOptions &HeaderSearchOpts, |
355 | | const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, |
356 | 35.4k | llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo) { |
357 | 35.4k | return new CodeGeneratorImpl(Diags, ModuleName, HeaderSearchOpts, |
358 | 35.4k | PreprocessorOpts, CGO, C, CoverageInfo); |
359 | 35.4k | } |