#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryObject.h"
#include "llvm/Support/Path.h"
-#include "llvm/Support/system_error.h"
#include <algorithm>
+#include <system_error>
using namespace llvm;
//===----------------------------------------------------------------------===//
return OS;
}
+namespace {
+class LineConsumer {
+ std::unique_ptr<MemoryBuffer> Buffer;
+ StringRef Remaining;
+public:
+ LineConsumer(StringRef Filename) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
+ MemoryBuffer::getFileOrSTDIN(Filename);
+ if (std::error_code EC = BufferOrErr.getError()) {
+ errs() << Filename << ": " << EC.message() << "\n";
+ Remaining = "";
+ } else {
+ Buffer = std::move(BufferOrErr.get());
+ 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) {
std::unique_ptr<raw_ostream>
FileInfo::openCoveragePath(StringRef CoveragePath) {
if (Options.NoOutput)
- return make_unique<raw_null_ostream>();
-
- std::string ErrorInfo;
- auto OS = make_unique<raw_fd_ostream>(CoveragePath.str().c_str(), ErrorInfo,
- sys::fs::F_Text);
- if (!ErrorInfo.empty()) {
- errs() << ErrorInfo << "\n";
- return make_unique<raw_null_ostream>();
+ return llvm::make_unique<raw_null_ostream>();
+
+ std::error_code EC;
+ auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC,
+ sys::fs::F_Text);
+ if (EC) {
+ errs() << EC.message() << "\n";
+ return llvm::make_unique<raw_null_ostream>();
}
return std::move(OS);
}
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;