Skip to content

Commit 2b5d03a

Browse files
committed
llvm-cov: move the gcov code into a separate file.
The gcov compatible code is moved to its own file and llvm-cov is updated to be a wrapper that always calls the gcov main function. llvm-svn: 214107
1 parent b9f46ee commit 2b5d03a

File tree

3 files changed

+158
-135
lines changed

3 files changed

+158
-135
lines changed

llvm/tools/llvm-cov/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ set(LLVM_LINK_COMPONENTS core support )
22

33
add_llvm_tool(llvm-cov
44
llvm-cov.cpp
5+
gcov.cpp
56
)

llvm/tools/llvm-cov/gcov.cpp

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

llvm/tools/llvm-cov/llvm-cov.cpp

+4-135
Original file line numberDiff line numberDiff line change
@@ -11,140 +11,9 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

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/ManagedStatic.h"
20-
#include "llvm/Support/MemoryObject.h"
21-
#include "llvm/Support/Path.h"
22-
#include "llvm/Support/PrettyStackTrace.h"
23-
#include "llvm/Support/Signals.h"
24-
#include <system_error>
25-
using namespace llvm;
14+
/// \brief The main function for the gcov compatible coverage tool
15+
int gcov_main(int argc, const char **argv);
2616

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

0 commit comments

Comments
 (0)