};
class DiagnosticLocation {
- StringRef Filename;
+ DIFile *File = nullptr;
unsigned Line = 0;
unsigned Column = 0;
DiagnosticLocation(const DebugLoc &DL);
DiagnosticLocation(const DISubprogram *SP);
- bool isValid() const { return !Filename.empty(); }
- StringRef getFilename() const { return Filename; }
+ bool isValid() const { return File; }
+ /// Return the full path to the file.
+ std::string getAbsolutePath() const;
+ /// Return the file name relative to the compilation directory.
+ StringRef getRelativePath() const;
unsigned getLine() const { return Line; }
unsigned getColumn() const { return Column; }
};
const std::string getLocationStr() const;
/// Return location information for this diagnostic in three parts:
- /// the source file name, line number and column.
- void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
+ /// the relative source file path, line number and column.
+ void getLocation(StringRef &RelativePath, unsigned &Line,
+ unsigned &Column) const;
+ /// Return the absolute path tot the file.
+ std::string getAbsolutePath() const;
+
const Function &getFunction() const { return Fn; }
DiagnosticLocation getLocation() const { return Loc; }
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
-#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ScopedPrinter.h"
+#include "llvm/Support/raw_ostream.h"
#include <atomic>
#include <cassert>
#include <memory>
DiagnosticLocation::DiagnosticLocation(const DebugLoc &DL) {
if (!DL)
return;
- Filename = DL->getFilename();
+ File = DL->getFile();
Line = DL->getLine();
Column = DL->getColumn();
}
DiagnosticLocation::DiagnosticLocation(const DISubprogram *SP) {
if (!SP)
return;
- Filename = SP->getFilename();
+
+ File = SP->getFile();
Line = SP->getScopeLine();
Column = 0;
}
-void DiagnosticInfoWithLocationBase::getLocation(StringRef *Filename,
- unsigned *Line,
- unsigned *Column) const {
- *Filename = Loc.getFilename();
- *Line = Loc.getLine();
- *Column = Loc.getColumn();
+StringRef DiagnosticLocation::getRelativePath() const {
+ return File->getFilename();
+}
+
+std::string DiagnosticLocation::getAbsolutePath() const {
+ StringRef Name = File->getFilename();
+ if (sys::path::is_absolute(Name))
+ return Name;
+
+ SmallString<128> Path;
+ sys::path::append(Path, File->getDirectory(), Name);
+ return sys::path::remove_leading_dotslash(Path).str();
+}
+
+std::string DiagnosticInfoWithLocationBase::getAbsolutePath() const {
+ return Loc.getAbsolutePath();
+}
+
+void DiagnosticInfoWithLocationBase::getLocation(StringRef &RelativePath,
+ unsigned &Line,
+ unsigned &Column) const {
+ RelativePath = Loc.getRelativePath();
+ Line = Loc.getLine();
+ Column = Loc.getColumn();
}
const std::string DiagnosticInfoWithLocationBase::getLocationStr() const {
unsigned Line = 0;
unsigned Column = 0;
if (isLocationAvailable())
- getLocation(&Filename, &Line, &Column);
+ getLocation(Filename, Line, Column);
return (Filename + ":" + Twine(Line) + ":" + Twine(Column)).str();
}
static void mapping(IO &io, DiagnosticLocation &DL) {
assert(io.outputting() && "input not yet implemented");
- StringRef File = DL.getFilename();
+ StringRef File = DL.getRelativePath();
unsigned Line = DL.getLine();
unsigned Col = DL.getColumn();
return SP->getName();
}
+/// Extract a filename for a DISubprogram.
+///
+/// Prefer relative paths in the coverage notes. Clang also may split
+/// up absolute paths into a directory and filename component. When
+/// the relative path doesn't exist, reconstruct the absolute path.
+SmallString<128> getFilename(const DISubprogram *SP) {
+ SmallString<128> Path;
+ StringRef RelPath = SP->getFilename();
+ if (sys::fs::exists(RelPath))
+ Path = RelPath;
+ else
+ sys::path::append(Path, SP->getDirectory(), SP->getFilename());
+ return Path;
+}
+
namespace {
class GCOVRecord {
protected:
}
private:
- StringRef Filename;
+ std::string Filename;
SmallVector<uint32_t, 32> Lines;
};
void writeOut() {
writeBytes(FunctionTag, 4);
+ SmallString<128> Filename = getFilename(SP);
uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) +
- 1 + lengthOfGCOVString(SP->getFilename()) + 1;
+ 1 + lengthOfGCOVString(Filename) + 1;
if (UseCfgChecksum)
++BlockLen;
write(BlockLen);
if (UseCfgChecksum)
write(CfgChecksum);
writeGCOVString(getFunctionName(SP));
- writeGCOVString(SP->getFilename());
+ writeGCOVString(Filename);
write(SP->getLine());
// Emit count of blocks.
if (FilterRe.empty() && ExcludeRe.empty()) {
return true;
}
- const StringRef Filename = F.getSubprogram()->getFilename();
+ SmallString<128> Filename = getFilename(F.getSubprogram());
auto It = InstrumentedFiles.find(Filename);
if (It != InstrumentedFiles.end()) {
return It->second;
// Add the function line number to the lines of the entry block
// to have a counter for the function definition.
uint32_t Line = SP->getLine();
- Func.getBlock(&EntryBlock).getFile(SP->getFilename()).addLine(Line);
+ auto Filename = getFilename(SP);
+ Func.getBlock(&EntryBlock).getFile(Filename).addLine(Line);
for (auto &BB : F) {
GCOVBlock &Block = Func.getBlock(&BB);
if (SP != getDISubprogram(Loc.getScope()))
continue;
- GCOVLines &Lines = Block.getFile(SP->getFilename());
+ GCOVLines &Lines = Block.getFile(Filename);
Lines.addLine(Loc.getLine());
}
Line = 0;
!llvm.module.flags = !{!2, !3}
!0 = distinct !DICompileUnit(language: DW_LANG_OpenCL, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
-!1 = !DIFile(filename: "foo.cl", directory: "/dev/null")
+!1 = !DIFile(filename: "foo.cl", directory: ".")
!2 = !{i32 2, !"Dwarf Version", i32 4}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !DILocation(line: 1, column: 42, scope: !5)