Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
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/CodeGen/ObjectFilePCHContainerOperations.h"
10
#include "CGDebugInfo.h"
11
#include "CodeGenModule.h"
12
#include "clang/AST/ASTContext.h"
13
#include "clang/AST/DeclObjC.h"
14
#include "clang/AST/Expr.h"
15
#include "clang/AST/RecursiveASTVisitor.h"
16
#include "clang/Basic/CodeGenOptions.h"
17
#include "clang/Basic/Diagnostic.h"
18
#include "clang/Basic/TargetInfo.h"
19
#include "clang/CodeGen/BackendUtil.h"
20
#include "clang/Frontend/CompilerInstance.h"
21
#include "clang/Lex/HeaderSearch.h"
22
#include "clang/Lex/Preprocessor.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/Bitstream/BitstreamReader.h"
25
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
26
#include "llvm/IR/Constants.h"
27
#include "llvm/IR/DataLayout.h"
28
#include "llvm/IR/LLVMContext.h"
29
#include "llvm/IR/Module.h"
30
#include "llvm/Object/COFF.h"
31
#include "llvm/Object/ObjectFile.h"
32
#include "llvm/Support/Path.h"
33
#include "llvm/Support/TargetRegistry.h"
34
#include <memory>
35
#include <utility>
36
37
using namespace clang;
38
39
#define DEBUG_TYPE "pchcontainer"
40
41
namespace {
42
class PCHContainerGenerator : public ASTConsumer {
43
  DiagnosticsEngine &Diags;
44
  const std::string MainFileName;
45
  const std::string OutputFileName;
46
  ASTContext *Ctx;
47
  ModuleMap &MMap;
48
  const HeaderSearchOptions &HeaderSearchOpts;
49
  const PreprocessorOptions &PreprocessorOpts;
50
  CodeGenOptions CodeGenOpts;
51
  const TargetOptions TargetOpts;
52
  const LangOptions LangOpts;
53
  std::unique_ptr<llvm::LLVMContext> VMContext;
54
  std::unique_ptr<llvm::Module> M;
55
  std::unique_ptr<CodeGen::CodeGenModule> Builder;
56
  std::unique_ptr<raw_pwrite_stream> OS;
57
  std::shared_ptr<PCHBuffer> Buffer;
58
59
  /// Visit every type and emit debug info for it.
60
  struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
61
    clang::CodeGen::CGDebugInfo &DI;
62
    ASTContext &Ctx;
63
    DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
64
299k
        : DI(DI), Ctx(Ctx) {}
65
66
    /// Determine whether this type can be represented in DWARF.
67
796k
    static bool CanRepresent(const Type *Ty) {
68
796k
      return !Ty->isDependentType() && 
!Ty->isUndeducedType()431k
;
69
796k
    }
70
71
13
    bool VisitImportDecl(ImportDecl *D) {
72
13
      if (!D->getImportedOwningModule())
73
13
        DI.EmitImportDecl(*D);
74
13
      return true;
75
13
    }
76
77
598k
    bool VisitTypeDecl(TypeDecl *D) {
78
598k
      // TagDecls may be deferred until after all decls have been merged and we
79
598k
      // know the complete type. Pure forward declarations will be skipped, but
80
598k
      // they don't need to be emitted into the module anyway.
81
598k
      if (auto *TD = dyn_cast<TagDecl>(D))
82
219k
        if (!TD->isCompleteDefinition())
83
19.8k
          return true;
84
578k
85
578k
      QualType QualTy = Ctx.getTypeDeclType(D);
86
578k
      if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
87
247k
        DI.getOrCreateStandaloneType(QualTy, D->getLocation());
88
578k
      return true;
89
578k
    }
90
91
5.57k
    bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
92
5.57k
      QualType QualTy(D->getTypeForDecl(), 0);
93
5.57k
      if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
94
5.57k
        DI.getOrCreateStandaloneType(QualTy, D->getLocation());
95
5.57k
      return true;
96
5.57k
    }
97
98
436k
    bool VisitFunctionDecl(FunctionDecl *D) {
99
436k
      if (isa<CXXMethodDecl>(D))
100
240k
        // This is not yet supported. Constructing the `this' argument
101
240k
        // mandates a CodeGenFunction.
102
240k
        return true;
103
195k
104
195k
      SmallVector<QualType, 16> ArgTypes;
105
195k
      for (auto i : D->parameters())
106
449k
        ArgTypes.push_back(i->getType());
107
195k
      QualType RetTy = D->getReturnType();
108
195k
      QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
109
195k
                                          FunctionProtoType::ExtProtoInfo());
110
195k
      if (CanRepresent(FnTy.getTypePtr()))
111
161k
        DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
112
195k
      return true;
113
195k
    }
114
115
18.7k
    bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
116
18.7k
      if (!D->getClassInterface())
117
2.21k
        return true;
118
16.5k
119
16.5k
      bool selfIsPseudoStrong, selfIsConsumed;
120
16.5k
      SmallVector<QualType, 16> ArgTypes;
121
16.5k
      ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
122
16.5k
                                        selfIsPseudoStrong, selfIsConsumed));
123
16.5k
      ArgTypes.push_back(Ctx.getObjCSelType());
124
16.5k
      for (auto i : D->parameters())
125
24.1k
        ArgTypes.push_back(i->getType());
126
16.5k
      QualType RetTy = D->getReturnType();
127
16.5k
      QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
128
16.5k
                                          FunctionProtoType::ExtProtoInfo());
129
16.5k
      if (CanRepresent(FnTy.getTypePtr()))
130
16.5k
        DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
131
16.5k
      return true;
132
16.5k
    }
133
  };
134
135
public:
136
  PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
137
                        const std::string &OutputFileName,
138
                        std::unique_ptr<raw_pwrite_stream> OS,
139
                        std::shared_ptr<PCHBuffer> Buffer)
140
      : Diags(CI.getDiagnostics()), MainFileName(MainFileName),
141
        OutputFileName(OutputFileName), Ctx(nullptr),
142
        MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
143
        HeaderSearchOpts(CI.getHeaderSearchOpts()),
144
        PreprocessorOpts(CI.getPreprocessorOpts()),
145
        TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()),
146
315
        OS(std::move(OS)), Buffer(std::move(Buffer)) {
147
315
    // The debug info output isn't affected by CodeModel and
148
315
    // ThreadModel, but the backend expects them to be nonempty.
149
315
    CodeGenOpts.CodeModel = "default";
150
315
    CodeGenOpts.ThreadModel = "single";
151
315
    CodeGenOpts.DebugTypeExtRefs = true;
152
315
    // When building a module MainFileName is the name of the modulemap file.
153
315
    CodeGenOpts.MainFileName =
154
315
        LangOpts.CurrentModule.empty() ? 
MainFileName10
:
LangOpts.CurrentModule305
;
155
315
    CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
156
315
    CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning());
157
315
    CodeGenOpts.DebugPrefixMap =
158
315
        CI.getInvocation().getCodeGenOpts().DebugPrefixMap;
159
315
  }
160
161
315
  ~PCHContainerGenerator() override = default;
162
163
315
  void Initialize(ASTContext &Context) override {
164
315
    assert(!Ctx && "initialized multiple times");
165
315
166
315
    Ctx = &Context;
167
315
    VMContext.reset(new llvm::LLVMContext());
168
315
    M.reset(new llvm::Module(MainFileName, *VMContext));
169
315
    M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
170
315
    Builder.reset(new CodeGen::CodeGenModule(
171
315
        *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
172
315
173
315
    // Prepare CGDebugInfo to emit debug info for a clang module.
174
315
    auto *DI = Builder->getModuleDebugInfo();
175
315
    StringRef ModuleName = llvm::sys::path::filename(MainFileName);
176
315
    DI->setPCHDescriptor({ModuleName, "", OutputFileName,
177
315
                          ASTFileSignature{{{~0U, ~0U, ~0U, ~0U, ~1U}}}});
178
315
    DI->setModuleMap(MMap);
179
315
  }
180
181
174k
  bool HandleTopLevelDecl(DeclGroupRef D) override {
182
174k
    if (Diags.hasErrorOccurred())
183
0
      return true;
184
174k
185
174k
    // Collect debug info for all decls in this group.
186
174k
    for (auto *I : D)
187
191k
      if (!I->isFromASTFile()) {
188
188k
        DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
189
188k
        DTV.TraverseDecl(I);
190
188k
      }
191
174k
    return true;
192
174k
  }
193
194
1.12k
  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
195
1.12k
    HandleTopLevelDecl(D);
196
1.12k
  }
197
198
147k
  void HandleTagDeclDefinition(TagDecl *D) override {
199
147k
    if (Diags.hasErrorOccurred())
200
0
      return;
201
147k
202
147k
    if (D->isFromASTFile())
203
1
      return;
204
147k
205
147k
    // Anonymous tag decls are deferred until we are building their declcontext.
206
147k
    if (D->getName().empty())
207
33.5k
      return;
208
113k
209
113k
    // Defer tag decls until their declcontext is complete.
210
113k
    auto *DeclCtx = D->getDeclContext();
211
520k
    while (DeclCtx) {
212
409k
      if (auto *D = dyn_cast<TagDecl>(DeclCtx))
213
3.74k
        if (!D->isCompleteDefinition())
214
2.88k
          return;
215
407k
      DeclCtx = DeclCtx->getParent();
216
407k
    }
217
113k
218
113k
    DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
219
110k
    DTV.TraverseDecl(D);
220
110k
    Builder->UpdateCompletedType(D);
221
110k
  }
222
223
89.6k
  void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
224
89.6k
    if (Diags.hasErrorOccurred())
225
0
      return;
226
89.6k
227
89.6k
    if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
228
88.7k
      Builder->getModuleDebugInfo()->completeRequiredType(RD);
229
89.6k
  }
230
231
82.6k
  void HandleImplicitImportDecl(ImportDecl *D) override {
232
82.6k
    if (!D->getImportedOwningModule())
233
82.6k
      Builder->getModuleDebugInfo()->EmitImportDecl(*D);
234
82.6k
  }
235
236
  /// Emit a container holding the serialized AST.
237
315
  void HandleTranslationUnit(ASTContext &Ctx) override {
238
315
    assert(M && VMContext && Builder);
239
315
    // Delete these on function exit.
240
315
    std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
241
315
    std::unique_ptr<llvm::Module> M = std::move(this->M);
242
315
    std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
243
315
244
315
    if (Diags.hasErrorOccurred())
245
0
      return;
246
315
247
315
    M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
248
315
    M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
249
315
250
315
    // PCH files don't have a signature field in the control block,
251
315
    // but LLVM detects DWO CUs by looking for a non-zero DWO id.
252
315
    // We use the lower 64 bits for debug info.
253
315
    uint64_t Signature =
254
315
        Buffer->Signature
255
315
            ? 
(uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0]305
256
315
            : 
~1ULL10
;
257
315
    Builder->getModuleDebugInfo()->setDwoId(Signature);
258
315
259
315
    // Finalize the Builder.
260
315
    if (Builder)
261
315
      Builder->Release();
262
315
263
315
    // Ensure the target exists.
264
315
    std::string Error;
265
315
    auto Triple = Ctx.getTargetInfo().getTriple();
266
315
    if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
267
0
      llvm::report_fatal_error(Error);
268
315
269
315
    // Emit the serialized Clang AST into its own section.
270
315
    assert(Buffer->IsComplete && "serialization did not complete");
271
315
    auto &SerializedAST = Buffer->Data;
272
315
    auto Size = SerializedAST.size();
273
315
    auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
274
315
    auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
275
315
    auto *Data = llvm::ConstantDataArray::getString(
276
315
        *VMContext, StringRef(SerializedAST.data(), Size),
277
315
        /*AddNull=*/false);
278
315
    auto *ASTSym = new llvm::GlobalVariable(
279
315
        *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
280
315
        "__clang_ast");
281
315
    // The on-disk hashtable needs to be aligned.
282
315
    ASTSym->setAlignment(llvm::Align(8));
283
315
284
315
    // Mach-O also needs a segment name.
285
315
    if (Triple.isOSBinFormatMachO())
286
311
      ASTSym->setSection("__CLANG,__clangast");
287
4
    // COFF has an eight character length limit.
288
4
    else if (Triple.isOSBinFormatCOFF())
289
2
      ASTSym->setSection("clangast");
290
2
    else
291
2
      ASTSym->setSection("__clangast");
292
315
293
315
    LLVM_DEBUG({
294
315
      // Print the IR for the PCH container to the debug output.
295
315
      llvm::SmallString<0> Buffer;
296
315
      clang::EmitBackendOutput(
297
315
          Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts,
298
315
          Ctx.getTargetInfo().getDataLayout(), M.get(),
299
315
          BackendAction::Backend_EmitLL,
300
315
          std::make_unique<llvm::raw_svector_ostream>(Buffer));
301
315
      llvm::dbgs() << Buffer;
302
315
    });
303
315
304
315
    // Use the LLVM backend to emit the pch container.
305
315
    clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
306
315
                             LangOpts, Ctx.getTargetInfo().getDataLayout(),
307
315
                             M.get(), BackendAction::Backend_EmitObj,
308
315
                             std::move(OS));
309
315
310
315
    // Free the memory for the temporary buffer.
311
315
    llvm::SmallVector<char, 0> Empty;
312
315
    SerializedAST = std::move(Empty);
313
315
  }
314
};
315
316
} // anonymous namespace
317
318
std::unique_ptr<ASTConsumer>
319
ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
320
    CompilerInstance &CI, const std::string &MainFileName,
321
    const std::string &OutputFileName,
322
    std::unique_ptr<llvm::raw_pwrite_stream> OS,
323
315
    std::shared_ptr<PCHBuffer> Buffer) const {
324
315
  return std::make_unique<PCHContainerGenerator>(
325
315
      CI, MainFileName, OutputFileName, std::move(OS), Buffer);
326
315
}
327
328
StringRef
329
20.6k
ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
330
20.6k
  StringRef PCH;
331
20.6k
  auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
332
20.6k
  if (OFOrErr) {
333
17.9k
    auto &OF = OFOrErr.get();
334
17.9k
    bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
335
17.9k
    // Find the clang AST section in the container.
336
35.9k
    for (auto &Section : OF->sections()) {
337
35.9k
      StringRef Name;
338
35.9k
      if (Expected<StringRef> NameOrErr = Section.getName())
339
35.9k
        Name = *NameOrErr;
340
0
      else
341
0
        consumeError(NameOrErr.takeError());
342
35.9k
343
35.9k
      if ((!IsCOFF && 
Name == "__clangast"35.9k
) ||
(18.0k
IsCOFF18.0k
&&
Name == "clangast"8
)) {
344
17.9k
        if (Expected<StringRef> E = Section.getContents())
345
17.9k
          return *E;
346
0
        else {
347
0
          handleAllErrors(E.takeError(), [&](const llvm::ErrorInfoBase &EIB) {
348
0
            EIB.log(llvm::errs());
349
0
          });
350
0
          return "";
351
0
        }
352
17.9k
      }
353
35.9k
    }
354
17.9k
  }
355
20.6k
  
handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) 2.66k
{
356
2.66k
    if (EIB.convertToErrorCode() ==
357
2.66k
        llvm::object::object_error::invalid_file_type)
358
2.66k
      // As a fallback, treat the buffer as a raw AST.
359
2.66k
      PCH = Buffer.getBuffer();
360
0
    else
361
0
      EIB.log(llvm::errs());
362
2.66k
  });
363
2.66k
  return PCH;
364
20.6k
}