Skip to content

Commit

Permalink
Allow retrieving source files relative to the compilation directory.
Browse files Browse the repository at this point in the history
Summary:
Dwarf stores source-file names the three parts:
<compilation_directory><include_directory><filename>

Prior to this change, the code only allowed retrieving either all
three as the absolute path, or just the filename.  But many
compile-command lines--especially those in hermetic build systems
don't specify an absolute path, nor just the filename, but rather the
path relative to the compilation directory. This features allows
retrieving them in that style.

Add tests for path printing styles.

Modify createBasicPrologue to handle include directories.

Subscribers: aprantl, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D73383
  • Loading branch information
Sterling-Augustine committed Feb 11, 2020
1 parent 9c1a88c commit 417375d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
7 changes: 6 additions & 1 deletion llvm/include/llvm/DebugInfo/DIContext.h
Expand Up @@ -133,7 +133,12 @@ enum class DINameKind { None, ShortName, LinkageName };
/// Controls which fields of DILineInfo container should be filled
/// with data.
struct DILineInfoSpecifier {
enum class FileLineInfoKind { None, Default, AbsoluteFilePath };
enum class FileLineInfoKind {
None,
Default,
RelativeFilePath,
AbsoluteFilePath
};
using FunctionNameKind = DINameKind;

FileLineInfoKind FLIKind;
Expand Down
15 changes: 10 additions & 5 deletions llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
Expand Up @@ -1082,7 +1082,7 @@ bool DWARFDebugLine::Prologue::getFileNameByIndex(
if (!Name)
return false;
StringRef FileName = *Name;
if (Kind != FileLineInfoKind::AbsoluteFilePath ||
if (Kind == FileLineInfoKind::Default ||
isPathAbsoluteOnWindowsOrPosix(FileName)) {
Result = std::string(FileName);
return true;
Expand All @@ -1099,12 +1099,17 @@ bool DWARFDebugLine::Prologue::getFileNameByIndex(
IncludeDir =
IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue();
}
// We may still need to append compilation directory of compile unit.
// We know that FileName is not absolute, the only way to have an
// absolute path at this point would be if IncludeDir is absolute.
if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
// For absolute paths only, include the compilation directory of compile unit.
// We know that FileName is not absolute, the only way to have an absolute
// path at this point would be if IncludeDir is absolute.
if (Kind == FileLineInfoKind::AbsoluteFilePath && !CompDir.empty() &&
!isPathAbsoluteOnWindowsOrPosix(IncludeDir))
sys::path::append(FilePath, Style, CompDir);

assert((Kind == FileLineInfoKind::AbsoluteFilePath ||
Kind == FileLineInfoKind::RelativeFilePath) &&
"invalid FileLineInfo Kind");

// sys::path::append skips empty strings.
sys::path::append(FilePath, Style, IncludeDir, FileName);
Result = std::string(FilePath.str());
Expand Down
47 changes: 47 additions & 0 deletions llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp
Expand Up @@ -917,4 +917,51 @@ TEST_F(DebugLineBasicFixture, ParserPrintsStandardOpcodesWhenRequested) {
EXPECT_TRUE(InOutput("0x0000003f: 0c DW_LNS_set_isa (66)\n")) << Output;
}

TEST_F(DebugLineBasicFixture, PrintPathsProperly) {
if (!setupGenerator(5))
return;

LineTable &LT = Gen->addLineTable();
DWARFDebugLine::Prologue P = LT.createBasicPrologue();
P.IncludeDirectories.push_back(
DWARFFormValue::createFromPValue(DW_FORM_string, "b dir"));
P.FileNames.push_back(DWARFDebugLine::FileNameEntry());
P.FileNames.back().Name =
DWARFFormValue::createFromPValue(DW_FORM_string, "b file");
P.FileNames.back().DirIdx = 1;
P.PrologueLength += 14;
LT.setPrologue(P);
generate();

auto ExpectedLineTable = Line.getOrParseLineTable(LineData, 0, *Context,
nullptr, RecordRecoverable);
EXPECT_THAT_EXPECTED(ExpectedLineTable, Succeeded());
std::string Result;
// DWARF 5 stores the compilation directory in two places: the Compilation
// Unit and the directory table entry 0, and implementations are free to use
// one or the other. This copy serves as the one stored in the CU.
StringRef CompDir = "a dir";
EXPECT_FALSE(
(*ExpectedLineTable)
->Prologue.getFileNameByIndex(
1, CompDir, DILineInfoSpecifier::FileLineInfoKind::None, Result));
EXPECT_TRUE((*ExpectedLineTable)
->Prologue.getFileNameByIndex(
1, CompDir,
DILineInfoSpecifier::FileLineInfoKind::Default, Result));
EXPECT_STREQ(Result.c_str(), "b file");
EXPECT_TRUE((*ExpectedLineTable)
->Prologue.getFileNameByIndex(
1, CompDir,
DILineInfoSpecifier::FileLineInfoKind::RelativeFilePath,
Result));
EXPECT_STREQ(Result.c_str(), "b dir/b file");
EXPECT_TRUE((*ExpectedLineTable)
->Prologue.getFileNameByIndex(
1, CompDir,
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
Result));
EXPECT_STREQ(Result.c_str(), "a dir/b dir/b file");
}

} // end anonymous namespace

0 comments on commit 417375d

Please sign in to comment.