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