From 0b57cec536236d46e3dba9bd041533462f33dbb7 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Fri, 20 Dec 2019 19:53:05 +0000 Subject: Move all sources from the llvm project into contrib/llvm-project. This uses the new layout of the upstream repository, which was recently migrated to GitHub, and converted into a "monorepo". That is, most of the earlier separate sub-projects with their own branches and tags were consolidated into one top-level directory, and are now branched and tagged together. Updating the vendor area to match this layout is next. --- .../llvm/tools/llvm-cov/TestingSupport.cpp | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 contrib/llvm-project/llvm/tools/llvm-cov/TestingSupport.cpp (limited to 'contrib/llvm-project/llvm/tools/llvm-cov/TestingSupport.cpp') diff --git a/contrib/llvm-project/llvm/tools/llvm-cov/TestingSupport.cpp b/contrib/llvm-project/llvm/tools/llvm-cov/TestingSupport.cpp new file mode 100644 index 000000000000..3ee318c9c640 --- /dev/null +++ b/contrib/llvm-project/llvm/tools/llvm-cov/TestingSupport.cpp @@ -0,0 +1,102 @@ +//===- TestingSupport.cpp - Convert objects files into test files --------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/ObjectFile.h" +#include "llvm/ProfileData/InstrProf.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/LEB128.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; +using namespace object; + +int convertForTestingMain(int argc, const char *argv[]) { + cl::opt InputSourceFile(cl::Positional, cl::Required, + cl::desc("")); + + cl::opt OutputFilename( + "o", cl::Required, + cl::desc( + "File with the profile data obtained after an instrumented run")); + + cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n"); + + auto ObjErr = llvm::object::ObjectFile::createObjectFile(InputSourceFile); + if (!ObjErr) { + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(ObjErr.takeError(), OS); + OS.flush(); + errs() << "error: " << Buf; + return 1; + } + ObjectFile *OF = ObjErr.get().getBinary(); + auto BytesInAddress = OF->getBytesInAddress(); + if (BytesInAddress != 8) { + errs() << "error: 64 bit binary expected\n"; + return 1; + } + + // Look for the sections that we are interested in. + int FoundSectionCount = 0; + SectionRef ProfileNames, CoverageMapping; + auto ObjFormat = OF->getTripleObjectFormat(); + for (const auto &Section : OF->sections()) { + StringRef Name; + if (Section.getName(Name)) + return 1; + if (Name == llvm::getInstrProfSectionName(IPSK_name, ObjFormat, + /*AddSegmentInfo=*/false)) { + ProfileNames = Section; + } else if (Name == llvm::getInstrProfSectionName( + IPSK_covmap, ObjFormat, /*AddSegmentInfo=*/false)) { + CoverageMapping = Section; + } else + continue; + ++FoundSectionCount; + } + if (FoundSectionCount != 2) + return 1; + + // Get the contents of the given sections. + uint64_t ProfileNamesAddress = ProfileNames.getAddress(); + StringRef CoverageMappingData; + StringRef ProfileNamesData; + if (Expected E = CoverageMapping.getContents()) + CoverageMappingData = *E; + else { + consumeError(E.takeError()); + return 1; + } + if (Expected E = ProfileNames.getContents()) + ProfileNamesData = *E; + else { + consumeError(E.takeError()); + return 1; + } + + int FD; + if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) { + errs() << "error: " << Err.message() << "\n"; + return 1; + } + + raw_fd_ostream OS(FD, true); + OS << "llvmcovmtestdata"; + encodeULEB128(ProfileNamesData.size(), OS); + encodeULEB128(ProfileNamesAddress, OS); + OS << ProfileNamesData; + // Coverage mapping data is expected to have an alignment of 8. + for (unsigned Pad = OffsetToAlignment(OS.tell(), 8); Pad; --Pad) + OS.write(uint8_t(0)); + OS << CoverageMappingData; + + return 0; +} -- cgit v1.2.3