Skip to content

Commit c16417f

Browse files
committed
[llvm-cov gcov] Add --demangled-names (-m)
gcov 4.9 introduced the option.
1 parent 4ce84b0 commit c16417f

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

llvm/include/llvm/ProfileData/GCOV.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ enum GCOVVersion { V304, V407, V408, V800, V900 };
4747
/// A struct for passing gcov options between functions.
4848
struct Options {
4949
Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L,
50-
bool N, bool R, bool T, bool X, std::string SourcePrefix)
50+
bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
5151
: AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
5252
PreservePaths(P), UncondBranch(U), Intermediate(I), LongFileNames(L),
53-
NoOutput(N), RelativeOnly(R), UseStdout(T), HashFilenames(X),
54-
SourcePrefix(std::move(SourcePrefix)) {}
53+
Demangle(M), NoOutput(N), RelativeOnly(R), UseStdout(T),
54+
HashFilenames(X), SourcePrefix(std::move(SourcePrefix)) {}
5555

5656
bool AllBlocks;
5757
bool BranchInfo;
@@ -61,6 +61,7 @@ struct Options {
6161
bool UncondBranch;
6262
bool Intermediate;
6363
bool LongFileNames;
64+
bool Demangle;
6465
bool NoOutput;
6566
bool RelativeOnly;
6667
bool UseStdout;
@@ -232,7 +233,7 @@ class GCOVFunction {
232233

233234
GCOVFunction(GCOVFile &file) : file(file) {}
234235

235-
StringRef getName() const { return Name; }
236+
StringRef getName(bool demangle) const;
236237
StringRef getFilename() const;
237238
uint64_t getEntryCount() const;
238239
GCOVBlock &getExitBlock() const;
@@ -255,6 +256,7 @@ class GCOVFunction {
255256
uint32_t endColumn = 0;
256257
uint8_t artificial = 0;
257258
StringRef Name;
259+
mutable SmallString<0> demangled;
258260
unsigned srcIdx;
259261
SmallVector<std::unique_ptr<GCOVBlock>, 0> blocks;
260262
SmallVector<std::unique_ptr<GCOVArc>, 0> arcs, treeArcs;

llvm/lib/ProfileData/GCOV.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ProfileData/GCOV.h"
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/Config/llvm-config.h"
17+
#include "llvm/Demangle/Demangle.h"
1718
#include "llvm/Support/Debug.h"
1819
#include "llvm/Support/FileSystem.h"
1920
#include "llvm/Support/Format.h"
@@ -316,6 +317,26 @@ bool GCOVArc::onTree() const { return flags & GCOV_ARC_ON_TREE; }
316317
//===----------------------------------------------------------------------===//
317318
// GCOVFunction implementation.
318319

320+
StringRef GCOVFunction::getName(bool demangle) const {
321+
if (!demangle)
322+
return Name;
323+
if (demangled.empty()) {
324+
do {
325+
if (Name.startswith("_Z")) {
326+
int status = 0;
327+
// Name is guaranteed to be NUL-terminated.
328+
char *res = itaniumDemangle(Name.data(), nullptr, nullptr, &status);
329+
if (status == 0) {
330+
demangled = res;
331+
free(res);
332+
break;
333+
}
334+
}
335+
demangled = Name;
336+
} while (0);
337+
}
338+
return demangled;
339+
}
319340
StringRef GCOVFunction::getFilename() const { return file.filenames[srcIdx]; }
320341

321342
/// getEntryCount - Get the number of times the function was called by
@@ -785,7 +806,7 @@ void Context::printSourceToIntermediate(const SourceInfo &si,
785806
for (const auto &fs : si.startLineToFunctions)
786807
for (const GCOVFunction *f : fs)
787808
os << "function:" << f->startLine << ',' << f->getEntryCount() << ','
788-
<< f->Name << '\n';
809+
<< f->getName(options.Demangle) << '\n';
789810
for (size_t lineNum = 1, size = si.lines.size(); lineNum < size; ++lineNum) {
790811
const LineInfo &line = si.lines[lineNum];
791812
if (line.blocks.empty())
@@ -832,7 +853,7 @@ void Context::print(StringRef filename, StringRef gcno, StringRef gcda,
832853

833854
raw_ostream &os = llvm::outs();
834855
for (GCOVFunction &f : make_pointee_range(file.functions)) {
835-
Summary summary(f.Name);
856+
Summary summary(f.getName(options.Demangle));
836857
collectFunction(f, summary);
837858
if (options.FuncCoverage && !options.UseStdout) {
838859
os << "Function '" << summary.Name << "'\n";
@@ -900,8 +921,9 @@ void Context::printFunctionDetails(const GCOVFunction &f,
900921
if (b.number != 0 && &b != &exitBlock && b.getCount())
901922
++blocksExec;
902923

903-
os << "function " << f.getName() << " called " << entryCount << " returned "
904-
<< formatPercentage(exitCount, entryCount) << "% blocks executed "
924+
os << "function " << f.getName(options.Demangle) << " called " << entryCount
925+
<< " returned " << formatPercentage(exitCount, entryCount)
926+
<< "% blocks executed "
905927
<< formatPercentage(blocksExec, f.blocks.size() - 2) << "%\n";
906928
}
907929

llvm/lib/ProfileData/LLVMBuild.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ subdirectories = Coverage
2121
type = Library
2222
name = ProfileData
2323
parent = Libraries
24-
required_libraries = Core Support
24+
required_libraries = Core Support Demangle
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test --demangled-names (-m).
2+
RUN: rm -rf %t && mkdir %t && cd %t
3+
RUN: cp %S/Inputs/test.cpp %S/Inputs/test.gcno %S/Inputs/test.gcda .
4+
5+
RUN: llvm-cov gcov -b -f -m test.gcda | FileCheck %s
6+
RUN: llvm-cov gcov -b -f --demangled-names test.gcda | FileCheck %s
7+
RUN: FileCheck %s --check-prefix=BRANCH < test.cpp.gcov
8+
9+
CHECK: Function 'A::B()'
10+
BRANCH: function A::B() called

llvm/tools/llvm-cov/gcov.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ int gcovMain(int argc, const char *argv[]) {
115115
cl::Grouping, cl::NotHidden,
116116
cl::aliasopt(Intermediate));
117117

118+
cl::opt<bool> Demangle("demangled-names", cl::init(false),
119+
cl::desc("Demangle function names"));
120+
cl::alias DemangleA("m", cl::desc("Alias for --demangled-names"),
121+
cl::Grouping, cl::NotHidden, cl::aliasopt(Demangle));
122+
118123
cl::opt<bool> NoOutput("n", cl::Grouping, cl::init(false),
119124
cl::desc("Do not output any .gcov files"));
120125
cl::alias NoOutputA("no-output", cl::aliasopt(NoOutput));
@@ -163,8 +168,8 @@ int gcovMain(int argc, const char *argv[]) {
163168

164169
GCOV::Options Options(AllBlocks, BranchProb, BranchCount, FuncSummary,
165170
PreservePaths, UncondBranch, Intermediate, LongNames,
166-
NoOutput, RelativeOnly, UseStdout, HashFilenames,
167-
SourcePrefix);
171+
Demangle, NoOutput, RelativeOnly, UseStdout,
172+
HashFilenames, SourcePrefix);
168173

169174
for (const auto &SourceFile : SourceFiles)
170175
reportCoverage(SourceFile, ObjectDir, InputGCNO, InputGCDA, DumpGCOV,

0 commit comments

Comments
 (0)