Coverage Report

Created: 2024-01-17 10:31

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