Coverage Report

Created: 2017-09-21 03:39

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