Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
5.63k
      ShouldCacheASTInMemory(ShouldCacheASTInMemory) {
35
5.63k
  this->Buffer->IsComplete = false;
36
5.63k
}
37
38
5.63k
PCHGenerator::~PCHGenerator() {
39
5.63k
}
40
41
5.63k
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
42
  // Don't create a PCH if there were fatal failures during module loading.
43
5.63k
  if (PP.getModuleLoader().HadFatalFailure)
44
0
    return;
45
46
5.63k
  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
47
5.63k
  if (hasErrors && 
!AllowASTWithErrors108
)
48
69
    return;
49
50
5.56k
  Module *Module = nullptr;
51
5.56k
  if (PP.getLangOpts().isCompilingModule()) {
52
2.14k
    Module = PP.getHeaderSearchInfo().lookupModule(
53
2.14k
        PP.getLangOpts().CurrentModule, SourceLocation(),
54
2.14k
        /*AllowSearch*/ false);
55
2.14k
    if (!Module) {
56
0
      assert(hasErrors && "emitting module but current module doesn't exist");
57
0
      return;
58
0
    }
59
2.14k
  }
60
61
  // Errors that do not prevent the PCH from being written should not cause the
62
  // overall compilation to fail either.
63
5.56k
  if (AllowASTWithErrors)
64
170
    PP.getDiagnostics().getClient()->clear();
65
66
  // Emit the PCH file to the Buffer.
67
5.56k
  assert(SemaPtr && "No Sema?");
68
0
  Buffer->Signature =
69
5.56k
      Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
70
                      // For serialization we are lenient if the errors were
71
                      // only warn-as-error kind.
72
5.56k
                      PP.getDiagnostics().hasUncompilableErrorOccurred(),
73
5.56k
                      ShouldCacheASTInMemory);
74
75
5.56k
  Buffer->IsComplete = true;
76
5.56k
}
77
78
5.65k
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
79
5.65k
  return &Writer;
80
5.65k
}
81
82
5.53k
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
83
5.53k
  return &Writer;
84
5.53k
}