Coverage Report

Created: 2018-01-17 21:32

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