Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/llvm-cov/gcov.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- gcov.cpp - GCOV compatible LLVM coverage tool ----------------------===//
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
// llvm-cov is a command line tools to analyze and report coverage information.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/ProfileData/GCOV.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/Support/CommandLine.h"
16
#include "llvm/Support/Errc.h"
17
#include "llvm/Support/FileSystem.h"
18
#include "llvm/Support/Path.h"
19
#include <system_error>
20
using namespace llvm;
21
22
static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
23
                           const std::string &InputGCNO,
24
                           const std::string &InputGCDA, bool DumpGCOV,
25
31
                           const GCOV::Options &Options) {
26
31
  SmallString<128> CoverageFileStem(ObjectDir);
27
31
  if (CoverageFileStem.empty()) {
28
28
    // If no directory was specified with -o, look next to the source file.
29
28
    CoverageFileStem = sys::path::parent_path(SourceFile);
30
28
    sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
31
28
  } else 
if (3
sys::fs::is_directory(ObjectDir)3
)
32
1
    // A directory name was given. Use it and the source file name.
33
1
    sys::path::append(CoverageFileStem, sys::path::stem(SourceFile));
34
2
  else
35
2
    // A file was given. Ignore the source file and look next to this file.
36
2
    sys::path::replace_extension(CoverageFileStem, "");
37
31
38
31
  std::string GCNO = InputGCNO.empty()
39
31
                         ? 
std::string(CoverageFileStem.str()) + ".gcno"28
40
31
                         : 
InputGCNO3
;
41
31
  std::string GCDA = InputGCDA.empty()
42
31
                         ? 
std::string(CoverageFileStem.str()) + ".gcda"26
43
31
                         : 
InputGCDA5
;
44
31
  GCOVFile GF;
45
31
46
31
  ErrorOr<std::unique_ptr<MemoryBuffer>> GCNO_Buff =
47
31
      MemoryBuffer::getFileOrSTDIN(GCNO);
48
31
  if (std::error_code EC = GCNO_Buff.getError()) {
49
0
    errs() << GCNO << ": " << EC.message() << "\n";
50
0
    return;
51
0
  }
52
31
  GCOVBuffer GCNO_GB(GCNO_Buff.get().get());
53
31
  if (!GF.readGCNO(GCNO_GB)) {
54
1
    errs() << "Invalid .gcno File!\n";
55
1
    return;
56
1
  }
57
30
58
30
  ErrorOr<std::unique_ptr<MemoryBuffer>> GCDA_Buff =
59
30
      MemoryBuffer::getFileOrSTDIN(GCDA);
60
30
  if (std::error_code EC = GCDA_Buff.getError()) {
61
7
    if (EC != errc::no_such_file_or_directory) {
62
0
      errs() << GCDA << ": " << EC.message() << "\n";
63
0
      return;
64
0
    }
65
7
    // Clear the filename to make it clear we didn't read anything.
66
7
    GCDA = "-";
67
23
  } else {
68
23
    GCOVBuffer GCDA_GB(GCDA_Buff.get().get());
69
23
    if (!GF.readGCDA(GCDA_GB)) {
70
2
      errs() << "Invalid .gcda File!\n";
71
2
      return;
72
2
    }
73
28
  }
74
28
75
28
  if (DumpGCOV)
76
6
    GF.print(errs());
77
28
78
28
  FileInfo FI(Options);
79
28
  GF.collectLineCounts(FI);
80
28
  FI.print(llvm::outs(), SourceFile, GCNO, GCDA);
81
28
}
82
83
31
int gcovMain(int argc, const char *argv[]) {
84
31
  cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
85
31
                                    cl::desc("SOURCEFILE"));
86
31
87
31
  cl::opt<bool> AllBlocks("a", cl::Grouping, cl::init(false),
88
31
                          cl::desc("Display all basic blocks"));
89
31
  cl::alias AllBlocksA("all-blocks", cl::aliasopt(AllBlocks));
90
31
91
31
  cl::opt<bool> BranchProb("b", cl::Grouping, cl::init(false),
92
31
                           cl::desc("Display branch probabilities"));
93
31
  cl::alias BranchProbA("branch-probabilities", cl::aliasopt(BranchProb));
94
31
95
31
  cl::opt<bool> BranchCount("c", cl::Grouping, cl::init(false),
96
31
                            cl::desc("Display branch counts instead "
97
31
                                     "of percentages (requires -b)"));
98
31
  cl::alias BranchCountA("branch-counts", cl::aliasopt(BranchCount));
99
31
100
31
  cl::opt<bool> LongNames("l", cl::Grouping, cl::init(false),
101
31
                          cl::desc("Prefix filenames with the main file"));
102
31
  cl::alias LongNamesA("long-file-names", cl::aliasopt(LongNames));
103
31
104
31
  cl::opt<bool> FuncSummary("f", cl::Grouping, cl::init(false),
105
31
                            cl::desc("Show coverage for each function"));
106
31
  cl::alias FuncSummaryA("function-summaries", cl::aliasopt(FuncSummary));
107
31
108
31
  cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
109
31
                         cl::desc("Do not output any .gcov files"));
110
31
  cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
111
31
112
31
  cl::opt<std::string> ObjectDir(
113
31
      "o", cl::value_desc("DIR|FILE"), cl::init(""),
114
31
      cl::desc("Find objects in DIR or based on FILE's path"));
115
31
  cl::alias ObjectDirA("object-directory", cl::aliasopt(ObjectDir));
116
31
  cl::alias ObjectDirB("object-file", cl::aliasopt(ObjectDir));
117
31
118
31
  cl::opt<bool> PreservePaths("p", cl::Grouping, cl::init(false),
119
31
                              cl::desc("Preserve path components"));
120
31
  cl::alias PreservePathsA("preserve-paths", cl::aliasopt(PreservePaths));
121
31
122
31
  cl::opt<bool> UncondBranch("u", cl::Grouping, cl::init(false),
123
31
                             cl::desc("Display unconditional branch info "
124
31
                                      "(requires -b)"));
125
31
  cl::alias UncondBranchA("unconditional-branches", cl::aliasopt(UncondBranch));
126
31
127
31
  cl::opt<bool> HashFilenames("x", cl::Grouping, cl::init(false),
128
31
                              cl::desc("Hash long pathnames"));
129
31
  cl::alias HashFilenamesA("hash-filenames", cl::aliasopt(HashFilenames));
130
31
131
31
132
31
  cl::OptionCategory DebugCat("Internal and debugging options");
133
31
  cl::opt<bool> DumpGCOV("dump", cl::init(false), cl::cat(DebugCat),
134
31
                         cl::desc("Dump the gcov file to stderr"));
135
31
  cl::opt<std::string> InputGCNO("gcno", cl::cat(DebugCat), cl::init(""),
136
31
                                 cl::desc("Override inferred gcno file"));
137
31
  cl::opt<std::string> InputGCDA("gcda", cl::cat(DebugCat), cl::init(""),
138
31
                                 cl::desc("Override inferred gcda file"));
139
31
140
31
  cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
141
31
142
31
  GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
143
31
                        PreservePaths, UncondBranch, LongNames, NoOutput,
144
31
                        HashFilenames);
145
31
146
31
  for (const auto &SourceFile : SourceFiles)
147
31
    reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,
148
31
                   Options);
149
31
  return 0;
150
31
}