]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-cov/TestingSupport.cpp
Update llvm to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-cov / TestingSupport.cpp
1 //===- TestingSupport.cpp - Convert objects files into test files --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Object/ObjectFile.h"
11 #include "llvm/ProfileData/InstrProf.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/LEB128.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <functional>
16 #include <system_error>
17
18 using namespace llvm;
19 using namespace object;
20
21 int convertForTestingMain(int argc, const char *argv[]) {
22   cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
23                                        cl::desc("<Source file>"));
24
25   cl::opt<std::string> OutputFilename(
26       "o", cl::Required,
27       cl::desc(
28           "File with the profile data obtained after an instrumented run"));
29
30   cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
31
32   auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile);
33   if (!ObjErr) {
34     std::string Buf;
35     raw_string_ostream OS(Buf);
36     logAllUnhandledErrors(ObjErr.takeError(), OS, "");
37     OS.flush();
38     errs() << "error: " << Buf;
39     return 1;
40   }
41   ObjectFile *OF = ObjErr.get().getBinary();
42   auto BytesInAddress = OF->getBytesInAddress();
43   if (BytesInAddress != 8) {
44     errs() << "error: 64 bit binary expected\n";
45     return 1;
46   }
47
48   // Look for the sections that we are interested in.
49   int FoundSectionCount = 0;
50   SectionRef ProfileNames, CoverageMapping;
51   for (const auto &Section : OF->sections()) {
52     StringRef Name;
53     if (Section.getName(Name))
54       return 1;
55     if (Name == llvm::getInstrProfNameSectionName(false)) {
56       ProfileNames = Section;
57     } else if (Name == llvm::getInstrProfCoverageSectionName(false)) {
58       CoverageMapping = Section;
59     } else
60       continue;
61     ++FoundSectionCount;
62   }
63   if (FoundSectionCount != 2)
64     return 1;
65
66   // Get the contents of the given sections.
67   uint64_t ProfileNamesAddress = ProfileNames.getAddress();
68   StringRef CoverageMappingData;
69   StringRef ProfileNamesData;
70   if (CoverageMapping.getContents(CoverageMappingData) ||
71       ProfileNames.getContents(ProfileNamesData))
72     return 1;
73
74   int FD;
75   if (auto Err =
76           sys::fs::openFileForWrite(OutputFilename, FD, sys::fs::F_None)) {
77     errs() << "error: " << Err.message() << "\n";
78     return 1;
79   }
80
81   raw_fd_ostream OS(FD, true);
82   OS << "llvmcovmtestdata";
83   encodeULEB128(ProfileNamesData.size(), OS);
84   encodeULEB128(ProfileNamesAddress, OS);
85   OS << ProfileNamesData;
86   // Coverage mapping data is expected to have an alignment of 8.
87   for (unsigned Pad = OffsetToAlignment(OS.tell(), 8); Pad; --Pad)
88     OS.write(uint8_t(0));
89   OS << CoverageMappingData;
90
91   return 0;
92 }