Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/TestingSupport.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- TestingSupport.cpp - Convert objects files into test files --------===//
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 "llvm/Object/ObjectFile.h"
10
#include "llvm/ProfileData/InstrProf.h"
11
#include "llvm/Support/CommandLine.h"
12
#include "llvm/Support/LEB128.h"
13
#include "llvm/Support/raw_ostream.h"
14
#include <functional>
15
#include <system_error>
16
17
using namespace llvm;
18
using namespace object;
19
20
0
int convertForTestingMain(int argc, const char *argv[]) {
21
0
  cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
22
0
                                       cl::desc("<Source file>"));
23
0
24
0
  cl::opt<std::string> OutputFilename(
25
0
      "o", cl::Required,
26
0
      cl::desc(
27
0
          "File with the profile data obtained after an instrumented run"));
28
0
29
0
  cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
30
0
31
0
  auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
32
0
  if (!ObjErr) {
33
0
    std::string Buf;
34
0
    raw_string_ostream OS(Buf);
35
0
    logAllUnhandledErrors(ObjErr.takeError(), OS);
36
0
    OS.flush();
37
0
    errs() << "error: " << Buf;
38
0
    return 1;
39
0
  }
40
0
  ObjectFile *OF = ObjErr.get().getBinary();
41
0
  auto BytesInAddress = OF->getBytesInAddress();
42
0
  if (BytesInAddress != 8) {
43
0
    errs() << "error: 64 bit binary expected\n";
44
0
    return 1;
45
0
  }
46
0
47
0
  // Look for the sections that we are interested in.
48
0
  int FoundSectionCount = 0;
49
0
  SectionRef ProfileNames, CoverageMapping;
50
0
  auto ObjFormat = OF->getTripleObjectFormat();
51
0
  for (const auto &Section : OF->sections()) {
52
0
    StringRef Name;
53
0
    if (Section.getName(Name))
54
0
      return 1;
55
0
    if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat,
56
0
                                              /*AddSegmentInfo=*/false)) {
57
0
      ProfileNames = Section;
58
0
    } else if (Name == llvm::getInstrProfSectionName(
59
0
                           IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) {
60
0
      CoverageMapping = Section;
61
0
    } else
62
0
      continue;
63
0
    ++FoundSectionCount;
64
0
  }
65
0
  if (FoundSectionCount != 2)
66
0
    return 1;
67
0
68
0
  // Get the contents of the given sections.
69
0
  uint64_t ProfileNamesAddress = ProfileNames.getAddress();
70
0
  StringRef CoverageMappingData;
71
0
  StringRef ProfileNamesData;
72
0
  if (Expected<StringRef> E = CoverageMapping.getContents())
73
0
    CoverageMappingData = *E;
74
0
  else {
75
0
    consumeError(E.takeError());
76
0
    return 1;
77
0
  }
78
0
  if (Expected<StringRef> E = ProfileNames.getContents())
79
0
    ProfileNamesData = *E;
80
0
  else {
81
0
    consumeError(E.takeError());
82
0
    return 1;
83
0
  }
84
0
85
0
  int FD;
86
0
  if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
87
0
    errs() << "error: " << Err.message() << "\n";
88
0
    return 1;
89
0
  }
90
0
91
0
  raw_fd_ostream OS(FD, true);
92
0
  OS << "llvmcovmtestdata";
93
0
  encodeULEB128(ProfileNamesData.size(), OS);
94
0
  encodeULEB128(ProfileNamesAddress, OS);
95
0
  OS << ProfileNamesData;
96
0
  // Coverage mapping data is expected to have an alignment of 8.
97
0
  for (unsigned Pad = OffsetToAlignment(OS.tell(), 8); Pad; --Pad)
98
0
    OS.write(uint8_t(0));
99
0
  OS << CoverageMappingData;
100
0
101
0
  return 0;
102
0
}