typedef DenseMap<uint32_t, BlockVector> BlockLines;
struct LineData {
+ LineData() : LastLine(0) {}
BlockLines Blocks;
FunctionLines Functions;
+ uint32_t LastLine;
};
struct GCOVCoverage {
Options(Options), LineInfo(), RunCount(0), ProgramCount(0) {}
void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block) {
+ if (Line > LineInfo[Filename].LastLine)
+ LineInfo[Filename].LastLine = Line;
LineInfo[Filename].Blocks[Line-1].push_back(Block);
}
void addFunctionLine(StringRef Filename, uint32_t Line,
const GCOVFunction *Function) {
- LineInfo[Filename].Functions[Line-1].push_back(Function);
+ if (Line > LineInfo[Filename].LastLine)
+ LineInfo[Filename].LastLine = Line;
+ LineInfo[Filename].Functions[Line-1].push_back(Function);
}
void setRunCount(uint32_t Runs) { RunCount = Runs; }
void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
return OS;
}
+namespace {
+class LineConsumer {
+ std::unique_ptr<MemoryBuffer> Buffer;
+ StringRef Remaining;
+public:
+ LineConsumer(StringRef Filename) {
+ if (error_code EC = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) {
+ errs() << Filename << ": " << EC.message() << "\n";
+ Remaining = "";
+ } else
+ Remaining = Buffer->getBuffer();
+ }
+ bool empty() { return Remaining.empty(); }
+ void printNext(raw_ostream &OS, uint32_t LineNum) {
+ StringRef Line;
+ if (empty())
+ Line = "/*EOF*/";
+ else
+ std::tie(Line, Remaining) = Remaining.split("\n");
+ OS << format("%5u:", LineNum) << Line << "\n";
+ }
+};
+}
+
/// Convert a path to a gcov filename. If PreservePaths is true, this
/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
E = LineInfo.end(); I != E; ++I) {
StringRef Filename = I->first();
- std::unique_ptr<MemoryBuffer> Buff;
- if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
- errs() << Filename << ": " << ec.message() << "\n";
- return;
- }
- StringRef AllLines = Buff->getBuffer();
+ auto AllLines = LineConsumer(Filename);
std::string CoveragePath = getCoveragePath(Filename, MainFilename);
std::unique_ptr<raw_ostream> S = openCoveragePath(CoveragePath);
const LineData &Line = I->second;
GCOVCoverage FileCoverage(Filename);
- for (uint32_t LineIndex = 0; !AllLines.empty(); ++LineIndex) {
+ for (uint32_t LineIndex = 0;
+ LineIndex < Line.LastLine || !AllLines.empty(); ++LineIndex) {
if (Options.BranchInfo) {
FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
if (FuncsIt != Line.Functions.end())
if (BlocksIt == Line.Blocks.end()) {
// No basic blocks are on this line. Not an executable line of code.
OS << " -:";
- std::pair<StringRef, StringRef> P = AllLines.split('\n');
- OS << format("%5u:", LineIndex+1) << P.first << "\n";
- AllLines = P.second;
+ AllLines.printNext(OS, LineIndex + 1);
} else {
const BlockVector &Blocks = BlocksIt->second;
}
++FileCoverage.LogicalLines;
- std::pair<StringRef, StringRef> P = AllLines.split('\n');
- OS << format("%5u:", LineIndex+1) << P.first << "\n";
- AllLines = P.second;
+ AllLines.printNext(OS, LineIndex + 1);
uint32_t BlockNo = 0;
uint32_t EdgeNo = 0;
# With gcov output disabled
RUN: llvm-cov -n test.c | diff -u test_no_output.output -
+# Missing source files. This test is fragile, as it depends on being
+# run before we copy some sources into place in the next test.
+RUN: llvm-cov test_paths.cpp 2>/dev/null | diff -u test_missing.output -
+RUN: diff -aub test_missing.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_missing.h.gcov test.h.gcov
+
# Preserve paths. This mangles the output filenames.
RUN: mkdir -p %t/srcdir/nested_dir
RUN: cp test.cpp test.h %t/srcdir