2011-10-04 17:24:48 +00:00
|
|
|
//===-- llvm/Support/GCOV.h - LLVM coverage tool ----------------*- C++ -*-===//
|
2011-09-28 18:50:00 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-10-04 17:24:48 +00:00
|
|
|
// This header provides the interface to read and write coverage files that
|
|
|
|
// use 'gcov' format.
|
2011-09-28 18:50:00 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_SUPPORT_GCOV_H
|
|
|
|
#define LLVM_SUPPORT_GCOV_H
|
2011-09-28 18:50:00 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class GCOVFunction;
|
|
|
|
class GCOVBlock;
|
|
|
|
class GCOVLines;
|
|
|
|
class FileInfo;
|
|
|
|
|
2012-08-31 17:31:28 +00:00
|
|
|
namespace GCOV {
|
|
|
|
enum GCOVFormat {
|
|
|
|
InvalidGCOV,
|
|
|
|
GCNO_402,
|
|
|
|
GCNO_404,
|
|
|
|
GCDA_402,
|
|
|
|
GCDA_404
|
|
|
|
};
|
|
|
|
} // end GCOV namespace
|
2011-09-28 18:50:00 +00:00
|
|
|
|
2011-09-29 16:52:53 +00:00
|
|
|
/// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
|
2011-09-28 18:50:00 +00:00
|
|
|
/// read operations.
|
|
|
|
class GCOVBuffer {
|
|
|
|
public:
|
|
|
|
GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
|
|
|
|
|
|
|
|
/// readGCOVFormat - Read GCOV signature at the beginning of buffer.
|
2012-08-31 17:31:28 +00:00
|
|
|
GCOV::GCOVFormat readGCOVFormat() {
|
2011-09-28 18:50:00 +00:00
|
|
|
StringRef Magic = Buffer->getBuffer().slice(0, 12);
|
|
|
|
Cursor = 12;
|
|
|
|
if (Magic == "oncg*404MVLL")
|
2012-08-31 17:31:28 +00:00
|
|
|
return GCOV::GCNO_404;
|
2011-09-28 18:50:00 +00:00
|
|
|
else if (Magic == "oncg*204MVLL")
|
2012-08-31 17:31:28 +00:00
|
|
|
return GCOV::GCNO_402;
|
2011-09-28 18:50:00 +00:00
|
|
|
else if (Magic == "adcg*404MVLL")
|
2012-08-31 17:31:28 +00:00
|
|
|
return GCOV::GCDA_404;
|
2011-09-28 18:50:00 +00:00
|
|
|
else if (Magic == "adcg*204MVLL")
|
2012-08-31 17:31:28 +00:00
|
|
|
return GCOV::GCDA_402;
|
2011-09-28 18:50:00 +00:00
|
|
|
|
|
|
|
Cursor = 0;
|
2012-08-31 17:31:28 +00:00
|
|
|
return GCOV::InvalidGCOV;
|
2011-09-28 18:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// readFunctionTag - If cursor points to a function tag then increment the
|
|
|
|
/// cursor and return true otherwise return false.
|
|
|
|
bool readFunctionTag() {
|
|
|
|
StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
if (Tag.empty() ||
|
2012-07-19 00:01:00 +00:00
|
|
|
Tag[0] != '\0' || Tag[1] != '\0' ||
|
|
|
|
Tag[2] != '\0' || Tag[3] != '\1') {
|
2011-09-28 18:50:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cursor += 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// readBlockTag - If cursor points to a block tag then increment the
|
|
|
|
/// cursor and return true otherwise return false.
|
|
|
|
bool readBlockTag() {
|
|
|
|
StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
if (Tag.empty() ||
|
2012-07-19 00:01:00 +00:00
|
|
|
Tag[0] != '\0' || Tag[1] != '\0' ||
|
|
|
|
Tag[2] != '\x41' || Tag[3] != '\x01') {
|
2011-09-28 18:50:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cursor += 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// readEdgeTag - If cursor points to an edge tag then increment the
|
|
|
|
/// cursor and return true otherwise return false.
|
|
|
|
bool readEdgeTag() {
|
|
|
|
StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
if (Tag.empty() ||
|
2012-07-19 00:01:00 +00:00
|
|
|
Tag[0] != '\0' || Tag[1] != '\0' ||
|
|
|
|
Tag[2] != '\x43' || Tag[3] != '\x01') {
|
2011-09-28 18:50:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cursor += 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// readLineTag - If cursor points to a line tag then increment the
|
|
|
|
/// cursor and return true otherwise return false.
|
|
|
|
bool readLineTag() {
|
|
|
|
StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
if (Tag.empty() ||
|
2012-07-19 00:01:00 +00:00
|
|
|
Tag[0] != '\0' || Tag[1] != '\0' ||
|
|
|
|
Tag[2] != '\x45' || Tag[3] != '\x01') {
|
2011-09-28 18:50:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cursor += 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// readArcTag - If cursor points to an gcda arc tag then increment the
|
|
|
|
/// cursor and return true otherwise return false.
|
|
|
|
bool readArcTag() {
|
|
|
|
StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
if (Tag.empty() ||
|
2012-07-19 00:01:00 +00:00
|
|
|
Tag[0] != '\0' || Tag[1] != '\0' ||
|
|
|
|
Tag[2] != '\xa1' || Tag[3] != '\1') {
|
2011-09-28 18:50:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Cursor += 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t readInt() {
|
|
|
|
uint32_t Result;
|
|
|
|
StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
|
|
|
|
assert (Str.empty() == false && "Unexpected memory buffer end!");
|
|
|
|
Cursor += 4;
|
2012-09-05 22:26:57 +00:00
|
|
|
Result = *(const uint32_t *)(Str.data());
|
2011-09-28 18:50:00 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t readInt64() {
|
|
|
|
uint64_t Lo = readInt();
|
|
|
|
uint64_t Hi = readInt();
|
|
|
|
uint64_t Result = Lo | (Hi << 32);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef readString() {
|
|
|
|
uint32_t Len = readInt() * 4;
|
|
|
|
StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+Len);
|
|
|
|
Cursor += Len;
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getCursor() const { return Cursor; }
|
|
|
|
private:
|
|
|
|
MemoryBuffer *Buffer;
|
|
|
|
uint64_t Cursor;
|
|
|
|
};
|
|
|
|
|
2011-09-29 16:52:53 +00:00
|
|
|
/// GCOVFile - Collects coverage information for one pair of coverage file
|
|
|
|
/// (.gcno and .gcda).
|
2011-09-28 18:50:00 +00:00
|
|
|
class GCOVFile {
|
|
|
|
public:
|
2011-09-29 16:48:44 +00:00
|
|
|
GCOVFile() {}
|
2011-09-28 18:50:00 +00:00
|
|
|
~GCOVFile();
|
|
|
|
bool read(GCOVBuffer &Buffer);
|
|
|
|
void dump();
|
|
|
|
void collectLineCounts(FileInfo &FI);
|
|
|
|
private:
|
|
|
|
SmallVector<GCOVFunction *, 16> Functions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// GCOVFunction - Collects function information.
|
|
|
|
class GCOVFunction {
|
|
|
|
public:
|
|
|
|
GCOVFunction() : Ident(0), LineNumber(0) {}
|
|
|
|
~GCOVFunction();
|
2012-08-31 17:31:28 +00:00
|
|
|
bool read(GCOVBuffer &Buffer, GCOV::GCOVFormat Format);
|
2011-09-28 18:50:00 +00:00
|
|
|
void dump();
|
|
|
|
void collectLineCounts(FileInfo &FI);
|
|
|
|
private:
|
|
|
|
uint32_t Ident;
|
|
|
|
uint32_t LineNumber;
|
|
|
|
StringRef Name;
|
|
|
|
StringRef Filename;
|
|
|
|
SmallVector<GCOVBlock *, 16> Blocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// GCOVBlock - Collects block information.
|
|
|
|
class GCOVBlock {
|
|
|
|
public:
|
|
|
|
GCOVBlock(uint32_t N) : Number(N), Counter(0) {}
|
|
|
|
~GCOVBlock();
|
|
|
|
void addEdge(uint32_t N) { Edges.push_back(N); }
|
|
|
|
void addLine(StringRef Filename, uint32_t LineNo);
|
|
|
|
void addCount(uint64_t N) { Counter = N; }
|
|
|
|
void dump();
|
|
|
|
void collectLineCounts(FileInfo &FI);
|
|
|
|
private:
|
|
|
|
uint32_t Number;
|
|
|
|
uint64_t Counter;
|
|
|
|
SmallVector<uint32_t, 16> Edges;
|
|
|
|
StringMap<GCOVLines *> Lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// GCOVLines - A wrapper around a vector of int to keep track of line nos.
|
|
|
|
class GCOVLines {
|
|
|
|
public:
|
|
|
|
~GCOVLines() { Lines.clear(); }
|
|
|
|
void add(uint32_t N) { Lines.push_back(N); }
|
|
|
|
void collectLineCounts(FileInfo &FI, StringRef Filename, uint32_t Count);
|
|
|
|
void dump();
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallVector<uint32_t, 4> Lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SmallVector<uint32_t, 16> LineCounts;
|
|
|
|
class FileInfo {
|
|
|
|
public:
|
|
|
|
void addLineCount(StringRef Filename, uint32_t Line, uint32_t Count);
|
|
|
|
void print();
|
|
|
|
private:
|
|
|
|
StringMap<LineCounts> LineInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|