Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[DebugInfo] Unify ChecksumKind and Checksum value in DIFile
Browse files Browse the repository at this point in the history
Rather than encode the absence of a checksum with a Kind variant, instead put
both the kind and value in a struct and wrap it in an Optional.

Differential Revision: http://reviews.llvm.org/D43043



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324928 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
scott-linder committed Feb 12, 2018
1 parent 66f9706 commit c091aea
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 122 deletions.
9 changes: 4 additions & 5 deletions include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ namespace llvm {
/// Create a file descriptor to hold debugging information for a file.
/// \param Filename File name.
/// \param Directory Directory.
/// \param CSKind Checksum kind (e.g. CSK_None, CSK_MD5, CSK_SHA1, etc.).
/// \param Checksum Checksum data.
DIFile *createFile(StringRef Filename, StringRef Directory,
DIFile::ChecksumKind CSKind = DIFile::CSK_None,
StringRef Checksum = StringRef());
/// \param Checksum Checksum kind (e.g. CSK_MD5, CSK_SHA1, etc.) and value.
DIFile *
createFile(StringRef Filename, StringRef Directory,
Optional<DIFile::ChecksumInfo<StringRef>> Checksum = None);

/// Create debugging information entry for a macro.
/// \param Parent Macro parent (could be nullptr).
Expand Down
74 changes: 52 additions & 22 deletions include/llvm/IR/DebugInfoMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,63 +499,93 @@ class DIFile : public DIScope {
friend class MDNode;

public:
// These values must be explictly set, as they end up in the final object
// file.
/// Which algorithm (e.g. MD5) a checksum was generated with.
///
/// The encoding is explicit because it is used directly in Bitcode. The
/// value 0 is reserved to indicate the absence of a checksum in Bitcode.
enum ChecksumKind {
CSK_None = 0,
// The first variant was originally CSK_None, encoded as 0. The new
// internal representation removes the need for this by wrapping the
// ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
// encoding is reserved.
CSK_MD5 = 1,
CSK_SHA1 = 2,
CSK_Last = CSK_SHA1 // Should be last enumeration.
};

/// A single checksum, represented by a \a Kind and a \a Value (a string).
template <typename T>
struct ChecksumInfo {
/// The kind of checksum which \a Value encodes.
ChecksumKind Kind;
/// The string value of the checksum.
T Value;

ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) { }
~ChecksumInfo() = default;
bool operator==(const ChecksumInfo<T> &X) const {
return Kind == X.Kind && Value == X.Value;
}
bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
};

private:
ChecksumKind CSKind;
Optional<ChecksumInfo<MDString *>> Checksum;

DIFile(LLVMContext &C, StorageType Storage, ChecksumKind CSK,
DIFile(LLVMContext &C, StorageType Storage,
Optional<ChecksumInfo<MDString *>> CS,
ArrayRef<Metadata *> Ops)
: DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops),
CSKind(CSK) {}
Checksum(CS) {}
~DIFile() = default;

static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
StringRef Directory, ChecksumKind CSK, StringRef CS,
StringRef Directory,
Optional<ChecksumInfo<StringRef>> CS,
StorageType Storage, bool ShouldCreate = true) {
Optional<ChecksumInfo<MDString *>> MDChecksum;
if (CS)
MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
return getImpl(Context, getCanonicalMDString(Context, Filename),
getCanonicalMDString(Context, Directory), CSK,
getCanonicalMDString(Context, CS), Storage, ShouldCreate);
getCanonicalMDString(Context, Directory), MDChecksum,
Storage, ShouldCreate);
}
static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
MDString *Directory, ChecksumKind CSK, MDString *CS,
MDString *Directory,
Optional<ChecksumInfo<MDString *>> CS,
StorageType Storage, bool ShouldCreate = true);

TempDIFile cloneImpl() const {
return getTemporary(getContext(), getFilename(), getDirectory(),
getChecksumKind(), getChecksum());
getChecksum());
}

public:
DEFINE_MDNODE_GET(DIFile, (StringRef Filename, StringRef Directory,
ChecksumKind CSK = CSK_None,
StringRef CS = StringRef()),
(Filename, Directory, CSK, CS))
Optional<ChecksumInfo<StringRef>> CS = None),
(Filename, Directory, CS))
DEFINE_MDNODE_GET(DIFile, (MDString * Filename, MDString *Directory,
ChecksumKind CSK = CSK_None,
MDString *CS = nullptr),
(Filename, Directory, CSK, CS))
Optional<ChecksumInfo<MDString *>> CS = None),
(Filename, Directory, CS))

TempDIFile clone() const { return cloneImpl(); }

StringRef getFilename() const { return getStringOperand(0); }
StringRef getDirectory() const { return getStringOperand(1); }
StringRef getChecksum() const { return getStringOperand(2); }
ChecksumKind getChecksumKind() const { return CSKind; }
StringRef getChecksumKindAsString() const;
Optional<ChecksumInfo<StringRef>> getChecksum() const {
Optional<ChecksumInfo<StringRef>> StringRefChecksum;
if (Checksum)
StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
return StringRefChecksum;
}

MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
MDString *getRawChecksum() const { return getOperandAs<MDString>(2); }
Optional<ChecksumInfo<MDString *>> getRawChecksum() const { return Checksum; }

static ChecksumKind getChecksumKind(StringRef CSKindStr);
static StringRef getChecksumKindAsString(ChecksumKind CSKind);
static Optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);

static bool classof(const Metadata *MD) {
return MD->getMetadataID() == DIFileKind;
Expand Down
23 changes: 16 additions & 7 deletions lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3612,7 +3612,6 @@ struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
};

struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
ChecksumKindField() : ImplTy(DIFile::CSK_None) {}
ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
};

Expand Down Expand Up @@ -3976,13 +3975,14 @@ bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
template <>
bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
ChecksumKindField &Result) {
if (Lex.getKind() != lltok::ChecksumKind)
Optional<DIFile::ChecksumKind> CSKind =
DIFile::getChecksumKind(Lex.getStrVal());

if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
return TokError(
"invalid checksum kind" + Twine(" '") + Lex.getStrVal() + "'");

DIFile::ChecksumKind CSKind = DIFile::getChecksumKind(Lex.getStrVal());

Result.assign(CSKind);
Result.assign(*CSKind);
Lex.Lex();
return false;
}
Expand Down Expand Up @@ -4247,16 +4247,25 @@ bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
/// checksumkind: CSK_MD5,
/// checksum: "000102030405060708090a0b0c0d0e0f")
bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
// The default constructed value for checksumkind is required, but will never
// be used, as the parser checks if the field was actually Seen before using
// the Val.
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
REQUIRED(filename, MDStringField, ); \
REQUIRED(directory, MDStringField, ); \
OPTIONAL(checksumkind, ChecksumKindField, ); \
OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
OPTIONAL(checksum, MDStringField, );
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS

Optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
if (checksumkind.Seen && checksum.Seen)
OptChecksum.emplace(checksumkind.Val, checksum.Val);
else if (checksumkind.Seen || checksum.Seen)
return Lex.Error("'checksumkind' and 'checksum' must be provided together");

Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val,
checksumkind.Val, checksum.Val));
OptChecksum));
return false;
}

Expand Down
13 changes: 10 additions & 3 deletions lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,13 +1354,20 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
return error("Invalid record");

IsDistinct = Record[0];
Optional<DIFile::ChecksumInfo<MDString *>> Checksum;
// The BitcodeWriter writes null bytes into Record[3:4] when the Checksum
// is not present. This matches up with the old internal representation,
// and the old encoding for CSK_None in the ChecksumKind. The new
// representation reserves the value 0 in the ChecksumKind to continue to
// encode None in a backwards-compatible way.
if (Record.size() == 5 && Record[3] && Record[4])
Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]),
getMDString(Record[4]));
MetadataList.assignValue(
GET_OR_DISTINCT(
DIFile,
(Context, getMDString(Record[1]), getMDString(Record[2]),
Record.size() == 3 ? DIFile::CSK_None
: static_cast<DIFile::ChecksumKind>(Record[3]),
Record.size() == 3 ? nullptr : getMDString(Record[4]))),
Checksum)),
NextMetadataNo);
NextMetadataNo++;
break;
Expand Down
11 changes: 9 additions & 2 deletions lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,15 @@ void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
Record.push_back(N->isDistinct());
Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
Record.push_back(N->getChecksumKind());
Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()));
if (N->getRawChecksum()) {
Record.push_back(N->getRawChecksum()->Kind);
Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
} else {
// Maintain backwards compatibility with the old internal representation of
// CSK_None in ChecksumKind by writing nulls here when Checksum is None.
Record.push_back(0);
Record.push_back(VE.getMetadataOrNullID(nullptr));
}

Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
Record.clear();
Expand Down
21 changes: 14 additions & 7 deletions lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
auto Insertion = FileIdMap.insert(std::make_pair(FullPath, NextId));
if (Insertion.second) {
// We have to compute the full filepath and emit a .cv_file directive.
std::string Checksum = fromHex(F->getChecksum());
void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
memcpy(CKMem, Checksum.data(), Checksum.size());
ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
Checksum.size());
DIFile::ChecksumKind ChecksumKind = F->getChecksumKind();
ArrayRef<uint8_t> ChecksumAsBytes;
FileChecksumKind CSKind = FileChecksumKind::None;
if (F->getChecksum()) {
std::string Checksum = fromHex(F->getChecksum()->Value);
void *CKMem = OS.getContext().allocate(Checksum.size(), 1);
memcpy(CKMem, Checksum.data(), Checksum.size());
ChecksumAsBytes = ArrayRef<uint8_t>(
reinterpret_cast<const uint8_t *>(CKMem), Checksum.size());
switch (F->getChecksum()->Kind) {
case DIFile::CSK_MD5: CSKind = FileChecksumKind::MD5; break;
case DIFile::CSK_SHA1: CSKind = FileChecksumKind::SHA1; break;
}
}
bool Success = OS.EmitCVFileDirective(NextId, FullPath, ChecksumAsBytes,
static_cast<unsigned>(ChecksumKind));
static_cast<unsigned>(CSKind));
(void)Success;
assert(Success && ".cv_file directive failed");
}
Expand Down
7 changes: 4 additions & 3 deletions lib/CodeGen/AsmPrinter/DwarfUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,16 @@ void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,

MD5::MD5Result *DwarfUnit::getMD5AsBytes(const DIFile *File) {
assert(File);
if (File->getChecksumKind() != DIFile::CSK_MD5)
Optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum();
if (!Checksum || Checksum->Kind != DIFile::CSK_MD5)
return nullptr;

// Convert the string checksum to an MD5Result for the streamer.
// The verifier validates the checksum so we assume it's okay.
// An MD5 checksum is 16 bytes.
std::string Checksum = fromHex(File->getChecksum());
std::string ChecksumString = fromHex(Checksum->Value);
void *CKMem = Asm->OutStreamer->getContext().allocate(16, 1);
memcpy(CKMem, Checksum.data(), 16);
memcpy(CKMem, ChecksumString.data(), 16);
return reinterpret_cast<MD5::MD5Result *>(CKMem);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ struct MDFieldPrinter {

void printTag(const DINode *N);
void printMacinfoType(const DIMacroNode *N);
void printChecksumKind(const DIFile *N);
void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
void printString(StringRef Name, StringRef Value,
bool ShouldSkipEmpty = true);
void printMetadata(StringRef Name, const Metadata *MD,
Expand Down Expand Up @@ -1488,11 +1488,10 @@ void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
Out << N->getMacinfoType();
}

void MDFieldPrinter::printChecksumKind(const DIFile *N) {
if (N->getChecksumKind() == DIFile::CSK_None)
// Skip CSK_None checksum kind.
return;
Out << FS << "checksumkind: " << N->getChecksumKindAsString();
void MDFieldPrinter::printChecksum(
const DIFile::ChecksumInfo<StringRef> &Checksum) {
Out << FS << "checksumkind: " << Checksum.getKindAsString();
printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
}

void MDFieldPrinter::printString(StringRef Name, StringRef Value,
Expand Down Expand Up @@ -1721,8 +1720,9 @@ static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
/* ShouldSkipEmpty */ false);
Printer.printString("directory", N->getDirectory(),
/* ShouldSkipEmpty */ false);
Printer.printChecksumKind(N);
Printer.printString("checksum", N->getChecksum(), /* ShouldSkipEmpty */ true);
// Print all values for checksum together, or not at all.
if (N->getChecksum())
Printer.printChecksum(*N->getChecksum());
Out << ")";
}

Expand Down
4 changes: 2 additions & 2 deletions lib/IR/DIBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ DIImportedEntity *DIBuilder::createImportedDeclaration(DIScope *Context,
}

DIFile *DIBuilder::createFile(StringRef Filename, StringRef Directory,
DIFile::ChecksumKind CSKind, StringRef Checksum) {
return DIFile::get(VMContext, Filename, Directory, CSKind, Checksum);
Optional<DIFile::ChecksumInfo<StringRef>> CS) {
return DIFile::get(VMContext, Filename, Directory, CS);
}

DIMacro *DIBuilder::createMacro(DIMacroFile *Parent, unsigned LineNumber,
Expand Down
37 changes: 19 additions & 18 deletions lib/IR/DebugInfoMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,34 +394,36 @@ DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,

// FIXME: Implement this string-enum correspondence with a .def file and macros,
// so that the association is explicit rather than implied.
static const char *ChecksumKindName[DIFile::CSK_Last + 1] = {
"CSK_None",
static const char *ChecksumKindName[DIFile::CSK_Last] = {
"CSK_MD5",
"CSK_SHA1"
};

DIFile::ChecksumKind DIFile::getChecksumKind(StringRef CSKindStr) {
return StringSwitch<DIFile::ChecksumKind>(CSKindStr)
.Case("CSK_MD5", DIFile::CSK_MD5)
.Case("CSK_SHA1", DIFile::CSK_SHA1)
.Default(DIFile::CSK_None);
StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) {
assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
// The first space was originally the CSK_None variant, which is now
// obsolete, but the space is still reserved in ChecksumKind, so we account
// for it here.
return ChecksumKindName[CSKind - 1];
}

StringRef DIFile::getChecksumKindAsString() const {
assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
return ChecksumKindName[CSKind];
Optional<DIFile::ChecksumKind> DIFile::getChecksumKind(StringRef CSKindStr) {
return StringSwitch<Optional<DIFile::ChecksumKind>>(CSKindStr)
.Case("CSK_MD5", DIFile::CSK_MD5)
.Case("CSK_SHA1", DIFile::CSK_SHA1)
.Default(None);
}

DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
MDString *Directory, DIFile::ChecksumKind CSKind,
MDString *Checksum, StorageType Storage,
bool ShouldCreate) {
MDString *Directory,
Optional<DIFile::ChecksumInfo<MDString *>> CS,
StorageType Storage, bool ShouldCreate) {
assert(isCanonical(Filename) && "Expected canonical MDString");
assert(isCanonical(Directory) && "Expected canonical MDString");
assert(isCanonical(Checksum) && "Expected canonical MDString");
DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CSKind, Checksum));
Metadata *Ops[] = {Filename, Directory, Checksum};
DEFINE_GETIMPL_STORE(DIFile, (CSKind), Ops);
assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString");
DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS));
Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr};
DEFINE_GETIMPL_STORE(DIFile, (CS), Ops);
}

DICompileUnit *DICompileUnit::getImpl(
Expand Down Expand Up @@ -901,4 +903,3 @@ DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
Metadata *Ops[] = { File, Elements };
DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
}

Loading

0 comments on commit c091aea

Please sign in to comment.