Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/TestModuleFileExtension.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- TestModuleFileExtension.cpp - Module Extension Tester -------------===//
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
#include "TestModuleFileExtension.h"
9
#include "clang/Frontend/FrontendDiagnostic.h"
10
#include "clang/Serialization/ASTReader.h"
11
#include "llvm/ADT/Hashing.h"
12
#include "llvm/Bitstream/BitstreamWriter.h"
13
#include "llvm/Support/raw_ostream.h"
14
#include <cstdio>
15
using namespace clang;
16
using namespace clang::serialization;
17
18
12
TestModuleFileExtension::Writer::~Writer() { }
19
20
void TestModuleFileExtension::Writer::writeExtensionContents(
21
       Sema &SemaRef,
22
12
       llvm::BitstreamWriter &Stream) {
23
12
  using namespace llvm;
24
12
25
12
  // Write an abbreviation for this record.
26
12
  auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
27
12
  Abv->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID));
28
12
  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of characters
29
12
  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // message
30
12
  auto Abbrev = Stream.EmitAbbrev(std::move(Abv));
31
12
32
12
  // Write a message into the extension block.
33
12
  SmallString<64> Message;
34
12
  {
35
12
    auto Ext = static_cast<TestModuleFileExtension *>(getExtension());
36
12
    raw_svector_ostream OS(Message);
37
12
    OS << "Hello from " << Ext->BlockName << " v" << Ext->MajorVersion << "."
38
12
       << Ext->MinorVersion;
39
12
  }
40
12
  uint64_t Record[] = {FIRST_EXTENSION_RECORD_ID, Message.size()};
41
12
  Stream.EmitRecordWithBlob(Abbrev, Record, Message);
42
12
}
43
44
TestModuleFileExtension::Reader::Reader(ModuleFileExtension *Ext,
45
                                        const llvm::BitstreamCursor &InStream)
46
  : ModuleFileExtensionReader(Ext), Stream(InStream)
47
13
{
48
13
  // Read the extension block.
49
13
  SmallVector<uint64_t, 4> Record;
50
26
  while (true) {
51
26
    llvm::Expected<llvm::BitstreamEntry> MaybeEntry =
52
26
        Stream.advanceSkippingSubblocks();
53
26
    if (!MaybeEntry)
54
0
      (void)MaybeEntry.takeError();
55
26
    llvm::BitstreamEntry Entry = MaybeEntry.get();
56
26
57
26
    switch (Entry.Kind) {
58
26
    case llvm::BitstreamEntry::SubBlock:
59
13
    case llvm::BitstreamEntry::EndBlock:
60
13
    case llvm::BitstreamEntry::Error:
61
13
      return;
62
13
63
13
    case llvm::BitstreamEntry::Record:
64
13
      break;
65
13
    }
66
13
67
13
    Record.clear();
68
13
    StringRef Blob;
69
13
    Expected<unsigned> MaybeRecCode =
70
13
        Stream.readRecord(Entry.ID, Record, &Blob);
71
13
    if (!MaybeRecCode)
72
0
      fprintf(stderr, "Failed reading rec code: %s\n",
73
0
              toString(MaybeRecCode.takeError()).c_str());
74
13
    switch (MaybeRecCode.get()) {
75
13
    case FIRST_EXTENSION_RECORD_ID: {
76
13
      StringRef Message = Blob.substr(0, Record[0]);
77
13
      fprintf(stderr, "Read extension block message: %s\n",
78
13
              Message.str().c_str());
79
13
      break;
80
13
    }
81
13
    }
82
13
  }
83
13
}
84
85
13
TestModuleFileExtension::Reader::~Reader() { }
86
87
16
TestModuleFileExtension::~TestModuleFileExtension() { }
88
89
ModuleFileExtensionMetadata
90
40
TestModuleFileExtension::getExtensionMetadata() const {
91
40
  return { BlockName, MajorVersion, MinorVersion, UserInfo };
92
40
}
93
94
llvm::hash_code TestModuleFileExtension::hashExtension(
95
18
                  llvm::hash_code Code) const {
96
18
  if (Hashed) {
97
8
    Code = llvm::hash_combine(Code, BlockName);
98
8
    Code = llvm::hash_combine(Code, MajorVersion);
99
8
    Code = llvm::hash_combine(Code, MinorVersion);
100
8
    Code = llvm::hash_combine(Code, UserInfo);
101
8
  }
102
18
103
18
  return Code;
104
18
}
105
106
std::unique_ptr<ModuleFileExtensionWriter>
107
12
TestModuleFileExtension::createExtensionWriter(ASTWriter &) {
108
12
  return std::unique_ptr<ModuleFileExtensionWriter>(new Writer(this));
109
12
}
110
111
std::unique_ptr<ModuleFileExtensionReader>
112
TestModuleFileExtension::createExtensionReader(
113
  const ModuleFileExtensionMetadata &Metadata,
114
  ASTReader &Reader, serialization::ModuleFile &Mod,
115
  const llvm::BitstreamCursor &Stream)
116
15
{
117
15
  assert(Metadata.BlockName == BlockName && "Wrong block name");
118
15
  if (std::make_pair(Metadata.MajorVersion, Metadata.MinorVersion) !=
119
15
        std::make_pair(MajorVersion, MinorVersion)) {
120
2
    Reader.getDiags().Report(Mod.ImportLoc,
121
2
                             diag::err_test_module_file_extension_version)
122
2
      << BlockName << Metadata.MajorVersion << Metadata.MinorVersion
123
2
      << MajorVersion << MinorVersion;
124
2
    return nullptr;
125
2
  }
126
13
127
13
  return std::unique_ptr<ModuleFileExtensionReader>(
128
13
                                                    new TestModuleFileExtension::Reader(this, Stream));
129
13
}