Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Serialization/GeneratePCH.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
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 file defines the PCHGenerator, which as a SemaConsumer that generates
10
//  a PCH file.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/ASTContext.h"
15
#include "clang/Lex/HeaderSearch.h"
16
#include "clang/Lex/Preprocessor.h"
17
#include "clang/Sema/SemaConsumer.h"
18
#include "clang/Serialization/ASTWriter.h"
19
#include "llvm/Bitstream/BitstreamWriter.h"
20
21
using namespace clang;
22
23
PCHGenerator::PCHGenerator(
24
    const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
25
    StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
26
    ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
27
    bool AllowASTWithErrors, bool IncludeTimestamps,
28
    bool ShouldCacheASTInMemory)
29
    : PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
30
      SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
31
      Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
32
             IncludeTimestamps),
33
      AllowASTWithErrors(AllowASTWithErrors),
34
3.59k
      ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
35
3.59k
  this->Buffer->IsComplete = false;
36
3.59k
}
37
38
3.59k
PCHGenerator::~PCHGenerator() {
39
3.59k
}
40
41
3.59k
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
42
3.59k
  // Don't create a PCH if there were fatal failures during module loading.
43
3.59k
  if (PP.getModuleLoader().HadFatalFailure)
44
0
    return;
45
3.59k
46
3.59k
  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
47
3.59k
  if (hasErrors && 
!AllowASTWithErrors46
)
48
34
    return;
49
3.55k
50
3.55k
  Module *Module = nullptr;
51
3.55k
  if (PP.getLangOpts().isCompilingModule()) {
52
1.48k
    Module = PP.getHeaderSearchInfo().lookupModule(
53
1.48k
        PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
54
1.48k
    if (!Module) {
55
0
      assert(hasErrors && "emitting module but current module doesn't exist");
56
0
      return;
57
0
    }
58
3.55k
  }
59
3.55k
60
3.55k
  // Emit the PCH file to the Buffer.
61
3.55k
  assert(SemaPtr && "No Sema?");
62
3.55k
  Buffer->Signature =
63
3.55k
      Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
64
3.55k
                      // For serialization we are lenient if the errors were
65
3.55k
                      // only warn-as-error kind.
66
3.55k
                      PP.getDiagnostics().hasUncompilableErrorOccurred(),
67
3.55k
                      ShouldCacheASTInMemory);
68
3.55k
69
3.55k
  Buffer->IsComplete = true;
70
3.55k
}
71
72
3.61k
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
73
3.61k
  return &Writer;
74
3.61k
}
75
76
3.55k
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
77
3.55k
  return &Writer;
78
3.55k
}