Path: blob/main/contrib/llvm-project/llvm/tools/llvm-cov/gcov.cpp
35231 views
//===- gcov.cpp - GCOV compatible LLVM coverage tool ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// llvm-cov is a command line tools to analyze and report coverage information.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ProfileData/GCOV.h"13#include "llvm/ADT/SmallString.h"14#include "llvm/Support/CommandLine.h"15#include "llvm/Support/Errc.h"16#include "llvm/Support/FileSystem.h"17#include "llvm/Support/Path.h"18#include <system_error>19using namespace llvm;2021static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,22const std::string &InputGCNO,23const std::string &InputGCDA, bool DumpGCOV,24const GCOV::Options &Options) {25SmallString<128> CoverageFileStem(ObjectDir);26if (CoverageFileStem.empty()) {27// If no directory was specified with -o, look next to the source file.28CoverageFileStem = sys::path::parent_path(SourceFile);29sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));30} else if (sys::fs::is_directory(ObjectDir))31// A directory name was given. Use it and the source file name.32sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));33else34// A file was given. Ignore the source file and look next to this file.35sys::path::replace_extension(CoverageFileStem, "");3637std::string GCNO =38InputGCNO.empty() ? std::string(CoverageFileStem) + ".gcno" : InputGCNO;39std::string GCDA =40InputGCDA.empty() ? std::string(CoverageFileStem) + ".gcda" : InputGCDA;41GCOVFile GF;4243// Open .gcda and .gcda without requiring a NUL terminator. The concurrent44// modification may nullify the NUL terminator condition.45ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff =46MemoryBuffer::getFileOrSTDIN(GCNO, /*IsText=*/false,47/*RequiresNullTerminator=*/false);48if (std::error_code EC = GCNO_Buff.getError()) {49errs() << GCNO << ": " << EC.message() << "\n";50return;51}52GCOVBuffer GCNO_GB(GCNO_Buff.get().get());53if (!GF.readGCNO(GCNO_GB)) {54errs() << "Invalid .gcno File!\n";55return;56}5758ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =59MemoryBuffer::getFileOrSTDIN(GCDA, /*IsText=*/false,60/*RequiresNullTerminator=*/false);61if (std::error_code EC = GCDA_Buff.getError()) {62if (EC != errc::no_such_file_or_directory) {63errs() << GCDA << ": " << EC.message() << "\n";64return;65}66// Clear the filename to make it clear we didn't read anything.67GCDA = "-";68} else {69GCOVBuffer gcda_buf(GCDA_Buff.get().get());70if (!gcda_buf.readGCDAFormat())71errs() << GCDA << ":not a gcov data file\n";72else if (!GF.readGCDA(gcda_buf))73errs() << "Invalid .gcda File!\n";74}7576if (DumpGCOV)77GF.print(errs());7879gcovOneInput(Options, SourceFile, GCNO, GCDA, GF);80}8182int gcovMain(int argc, const char *argv[]) {83cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,84cl::desc("SOURCEFILE"));8586cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),87cl::desc("Display all basic blocks"));88cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));8990cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),91cl::desc("Display branch probabilities"));92cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));9394cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),95cl::desc("Display branch counts instead "96"of percentages (requires -b)"));97cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));9899cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),100cl::desc("Prefix filenames with the main file"));101cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));102103cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),104cl::desc("Show coverage for each function"));105cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));106107// Supported by gcov 4.9~8. gcov 9 (GCC r265587) removed --intermediate-format108// and -i was changed to mean --json-format. We consider this format still109// useful and support -i.110cl::opt<bool> Intermediate(111"intermediate-format", cl::init(false),112cl::desc("Output .gcov in intermediate text format"));113cl::alias IntermediateA("i", cl::desc("Alias for --intermediate-format"),114cl::Grouping, cl::NotHidden,115cl::aliasopt(Intermediate));116117cl::opt<bool> Demangle("demangled-names", cl::init(false),118cl::desc("Demangle function names"));119cl::alias DemangleA("m", cl::desc("Alias for --demangled-names"),120cl::Grouping, cl::NotHidden, cl::aliasopt(Demangle));121122cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),123cl::desc("Do not output any .gcov files"));124cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));125126cl::opt<std::string> ObjectDir(127"o", cl::value_desc("DIR|FILE"), cl::init(""),128cl::desc("Find objects in DIR or based on FILE's path"));129cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));130cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));131132cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),133cl::desc("Preserve path components"));134cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));135136cl::opt<bool> RelativeOnly(137"r", cl::Grouping,138cl::desc("Only dump files with relative paths or absolute paths with the "139"prefix specified by -s"));140cl::alias RelativeOnlyA("relative-only", cl::aliasopt(RelativeOnly));141cl::opt<std::string> SourcePrefix("s", cl::desc("Source prefix to elide"));142cl::alias SourcePrefixA("source-prefix", cl::aliasopt(SourcePrefix));143144cl::opt<bool> UseStdout("t", cl::Grouping, cl::init(false),145cl::desc("Print to stdout"));146cl::alias UseStdoutA("stdout", cl::aliasopt(UseStdout));147148cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),149cl::desc("Display unconditional branch info "150"(requires -b)"));151cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));152153cl::opt<bool> HashFilenames("x", cl::Grouping, cl::init(false),154cl::desc("Hash long pathnames"));155cl::alias HashFilenamesA("hash-filenames", cl::aliasopt(HashFilenames));156157158cl::OptionCategory DebugCat("Internal and debugging options");159cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),160cl::desc("Dump the gcov file to stderr"));161cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),162cl::desc("Override inferred gcno file"));163cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),164cl::desc("Override inferred gcda file"));165166cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");167168GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,169PreservePaths, UncondBranch, Intermediate, LongNames,170Demangle, NoOutput, RelativeOnly, UseStdout,171HashFilenames, SourcePrefix);172173for (const auto &SourceFile : SourceFiles)174reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,175Options);176return 0;177}178179180