From 149f6eaed9818057b83d8f0b17bb5e8764a13143 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Wed, 5 Oct 2016 05:59:29 +0000 Subject: [PATCH] Re-commit "Use StringRef in Support/Darf APIs (NFC)" This reverts commit r283285 and re-commit r283275 with a fix for format("%s", Str); where Str is a StringRef. llvm-svn: 283298 --- .../Plugins/SymbolFile/DWARF/DWARFDefines.cpp | 114 ++++++++++----------- llvm/include/llvm/Support/Dwarf.h | 52 +++++----- llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp | 10 +- llvm/lib/CodeGen/AsmPrinter/DIE.cpp | 8 +- .../DWARF/DWARFAbbreviationDeclaration.cpp | 12 +-- llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp | 6 +- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 6 +- llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp | 6 +- llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp | 31 +++--- llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp | 4 +- llvm/lib/IR/AsmWriter.cpp | 13 ++- llvm/lib/Support/Dwarf.cpp | 104 ++++++++++--------- llvm/unittests/Support/DwarfTest.cpp | 24 ++--- 13 files changed, 203 insertions(+), 187 deletions(-) diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDefines.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDefines.cpp index 7103e98..f302434 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFDefines.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFDefines.cpp @@ -21,53 +21,53 @@ const char *DW_TAG_value_to_name(uint32_t val) { if (val == 0) return "NULL"; - const char *llvmstr = llvm::dwarf::TagString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::TagString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_TAG constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_CHILDREN_value_to_name(uint8_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::ChildrenString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::ChildrenString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_CHILDREN constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_AT_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::AttributeString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::AttributeString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_AT constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_FORM_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::FormEncodingString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::FormEncodingString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_FORM constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_OP_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::OperationEncodingString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::OperationEncodingString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } DRC_class DW_OP_value_to_class(uint32_t val) { @@ -383,145 +383,145 @@ DRC_class DW_OP_value_to_class(uint32_t val) { const char *DW_ATE_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::AttributeEncodingString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::AttributeEncodingString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_ATE constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_ACCESS_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::AccessibilityString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::AccessibilityString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_ACCESS constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_VIS_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::VisibilityString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::VisibilityString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_VIS constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_VIRTUALITY_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::VirtualityString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::VirtualityString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_VIRTUALITY constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_LANG_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::LanguageString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::LanguageString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_LANG constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_ID_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::CaseString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::CaseString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_ID constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_CC_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::ConventionString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::ConventionString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_CC constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_INL_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::InlineCodeString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::InlineCodeString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_INL constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_ORD_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::ArrayOrderString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::ArrayOrderString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_ORD constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_DSC_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::DiscriminantString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::DiscriminantString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_DSC constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_LNS_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::LNStandardString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::LNStandardString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_LNS constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_LNE_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::LNExtendedString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::LNExtendedString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_LNE constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_MACINFO_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::MacinfoString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::MacinfoString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_MACINFO constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } const char *DW_CFA_value_to_name(uint32_t val) { static char invalid[100]; - const char *llvmstr = llvm::dwarf::CallFrameString(val); - if (llvmstr == NULL) { + llvm::StringRef llvmstr = llvm::dwarf::CallFrameString(val); + if (llvmstr.empty()) { snprintf(invalid, sizeof(invalid), "Unknown DW_CFA constant: 0x%x", val); return invalid; } - return llvmstr; + return llvmstr.data(); } DW_TAG_CategoryEnum get_tag_category(uint16_t tag) { diff --git a/llvm/include/llvm/Support/Dwarf.h b/llvm/include/llvm/Support/Dwarf.h index 4569c9e..fa7c648 100644 --- a/llvm/include/llvm/Support/Dwarf.h +++ b/llvm/include/llvm/Support/Dwarf.h @@ -606,31 +606,31 @@ enum GDBIndexEntryLinkage { /// known. /// /// @{ -const char *TagString(unsigned Tag); -const char *ChildrenString(unsigned Children); -const char *AttributeString(unsigned Attribute); -const char *FormEncodingString(unsigned Encoding); -const char *OperationEncodingString(unsigned Encoding); -const char *AttributeEncodingString(unsigned Encoding); -const char *DecimalSignString(unsigned Sign); -const char *EndianityString(unsigned Endian); -const char *AccessibilityString(unsigned Access); -const char *VisibilityString(unsigned Visibility); -const char *VirtualityString(unsigned Virtuality); -const char *LanguageString(unsigned Language); -const char *CaseString(unsigned Case); -const char *ConventionString(unsigned Convention); -const char *InlineCodeString(unsigned Code); -const char *ArrayOrderString(unsigned Order); -const char *DiscriminantString(unsigned Discriminant); -const char *LNStandardString(unsigned Standard); -const char *LNExtendedString(unsigned Encoding); -const char *MacinfoString(unsigned Encoding); -const char *CallFrameString(unsigned Encoding); -const char *ApplePropertyString(unsigned); -const char *AtomTypeString(unsigned Atom); -const char *GDBIndexEntryKindString(GDBIndexEntryKind Kind); -const char *GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage); +StringRef TagString(unsigned Tag); +StringRef ChildrenString(unsigned Children); +StringRef AttributeString(unsigned Attribute); +StringRef FormEncodingString(unsigned Encoding); +StringRef OperationEncodingString(unsigned Encoding); +StringRef AttributeEncodingString(unsigned Encoding); +StringRef DecimalSignString(unsigned Sign); +StringRef EndianityString(unsigned Endian); +StringRef AccessibilityString(unsigned Access); +StringRef VisibilityString(unsigned Visibility); +StringRef VirtualityString(unsigned Virtuality); +StringRef LanguageString(unsigned Language); +StringRef CaseString(unsigned Case); +StringRef ConventionString(unsigned Convention); +StringRef InlineCodeString(unsigned Code); +StringRef ArrayOrderString(unsigned Order); +StringRef DiscriminantString(unsigned Discriminant); +StringRef LNStandardString(unsigned Standard); +StringRef LNExtendedString(unsigned Encoding); +StringRef MacinfoString(unsigned Encoding); +StringRef CallFrameString(unsigned Encoding); +StringRef ApplePropertyString(unsigned); +StringRef AtomTypeString(unsigned Atom); +StringRef GDBIndexEntryKindString(GDBIndexEntryKind Kind); +StringRef GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage); /// @} /// \defgroup DwarfConstantsParsing Dwarf constants parsing functions @@ -654,7 +654,7 @@ unsigned getMacinfo(StringRef MacinfoString); /// \brief Returns the symbolic string representing Val when used as a value /// for attribute Attr. -const char *AttributeValueString(uint16_t Attr, unsigned Val); +StringRef AttributeValueString(uint16_t Attr, unsigned Val); /// \brief Decsribes an entry of the various gnu_pub* debug sections. /// diff --git a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp index 36c4714..5b254c8 100644 --- a/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp +++ b/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp @@ -74,7 +74,8 @@ void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { // filenames), so just print a few useful things. for (DICompileUnit *CU : Finder.compile_units()) { O << "Compile unit: "; - if (const char *Lang = dwarf::LanguageString(CU->getSourceLanguage())) + auto Lang = dwarf::LanguageString(CU->getSourceLanguage()); + if (!Lang.empty()) O << Lang; else O << "unknown-language(" << CU->getSourceLanguage() << ")"; @@ -105,14 +106,15 @@ void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { printFile(O, T->getFilename(), T->getDirectory(), T->getLine()); if (auto *BT = dyn_cast(T)) { O << " "; - if (const char *Encoding = - dwarf::AttributeEncodingString(BT->getEncoding())) + auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding()); + if (!Encoding.empty()) O << Encoding; else O << "unknown-encoding(" << BT->getEncoding() << ')'; } else { O << ' '; - if (const char *Tag = dwarf::TagString(T->getTag())) + auto Tag = dwarf::TagString(T->getTag()); + if (!Tag.empty()) O << Tag; else O << "unknown-tag(" << T->getTag() << ")"; diff --git a/llvm/lib/CodeGen/AsmPrinter/DIE.cpp b/llvm/lib/CodeGen/AsmPrinter/DIE.cpp index c5b7277..75b9a16 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DIE.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DIE.cpp @@ -63,10 +63,10 @@ void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { /// void DIEAbbrev::Emit(const AsmPrinter *AP) const { // Emit its Dwarf tag type. - AP->EmitULEB128(Tag, dwarf::TagString(Tag)); + AP->EmitULEB128(Tag, dwarf::TagString(Tag).data()); // Emit whether it has children DIEs. - AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children)); + AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data()); // For each attribute description. for (unsigned i = 0, N = Data.size(); i < N; ++i) { @@ -74,11 +74,11 @@ void DIEAbbrev::Emit(const AsmPrinter *AP) const { // Emit attribute type. AP->EmitULEB128(AttrData.getAttribute(), - dwarf::AttributeString(AttrData.getAttribute())); + dwarf::AttributeString(AttrData.getAttribute()).data()); // Emit form type. AP->EmitULEB128(AttrData.getForm(), - dwarf::FormEncodingString(AttrData.getForm())); + dwarf::FormEncodingString(AttrData.getForm()).data()); } // Mark end of abbreviation. diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp index 9314c9e..afae00e 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAbbreviationDeclaration.cpp @@ -62,23 +62,23 @@ DWARFAbbreviationDeclaration::extract(DataExtractor Data, uint32_t* OffsetPtr) { } void DWARFAbbreviationDeclaration::dump(raw_ostream &OS) const { - const char *tagString = TagString(getTag()); + auto tagString = TagString(getTag()); OS << '[' << getCode() << "] "; - if (tagString) + if (!tagString.empty()) OS << tagString; else OS << format("DW_TAG_Unknown_%x", getTag()); OS << "\tDW_CHILDREN_" << (hasChildren() ? "yes" : "no") << '\n'; for (const AttributeSpec &Spec : AttributeSpecs) { OS << '\t'; - const char *attrString = AttributeString(Spec.Attr); - if (attrString) + auto attrString = AttributeString(Spec.Attr); + if (!attrString.empty()) OS << attrString; else OS << format("DW_AT_Unknown_%x", Spec.Attr); OS << '\t'; - const char *formString = FormEncodingString(Spec.Form); - if (formString) + auto formString = FormEncodingString(Spec.Form); + if (!formString.empty()) OS << formString; else OS << format("DW_FORM_Unknown_%x", Spec.Form); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp index 8ae0543..9aa3a2b 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp @@ -61,12 +61,14 @@ void DWARFAcceleratorTable::dump(raw_ostream &OS) const { SmallVector AtomForms; for (const auto &Atom: HdrData.Atoms) { OS << format("Atom[%d] Type: ", i++); - if (const char *TypeString = dwarf::AtomTypeString(Atom.first)) + auto TypeString = dwarf::AtomTypeString(Atom.first); + if (!TypeString.empty()) OS << TypeString; else OS << format("DW_ATOM_Unknown_0x%x", Atom.first); OS << " Form: "; - if (const char *FormString = dwarf::FormEncodingString(Atom.second)) + auto FormString = dwarf::FormEncodingString(Atom.second); + if (!FormString.empty()) OS << FormString; else OS << format("DW_FORM_Unknown_0x%x", Atom.second); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 1a0b7e5..c2602ba 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -54,8 +54,10 @@ static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data, OS << format("0x%8.8x ", dieRef); if (GnuStyle) { PubIndexEntryDescriptor desc(pubNames.getU8(&offset)); - OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage)) - << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind)) + OS << format("%-8s", + dwarf::GDBIndexEntryLinkageString(desc.Linkage).data()) + << ' ' + << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind).data()) << ' '; } OS << '\"' << pubNames.getCStr(&offset) << "\"\n"; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp index 4253401..046559e 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp @@ -394,13 +394,15 @@ static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, OperandType Type = OpTypes[Opcode][OperandIdx]; switch (Type) { - case OT_Unset: + case OT_Unset: { OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to"; - if (const char *OpcodeName = CallFrameString(Opcode)) + auto OpcodeName = CallFrameString(Opcode); + if (!OpcodeName.empty()) OS << " " << OpcodeName; else OS << format(" Opcode %x", Opcode); break; + } case OT_None: break; case OT_Address: diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp index 62d5e66..c43456b 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugInfoEntry.cpp @@ -45,12 +45,12 @@ void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u, if (abbrCode) { if (AbbrevDecl) { - const char *tagString = TagString(getTag()); - if (tagString) - WithColor(OS, syntax::Tag).get().indent(indent) << tagString; - else - WithColor(OS, syntax::Tag).get().indent(indent) << - format("DW_TAG_Unknown_%x", getTag()); + auto tagString = TagString(getTag()); + if (!tagString.empty()) + WithColor(OS, syntax::Tag).get().indent(indent) << tagString; + else + WithColor(OS, syntax::Tag).get().indent(indent) + << format("DW_TAG_Unknown_%x", getTag()); OS << format(" [%u] %c\n", abbrCode, AbbrevDecl->hasChildren() ? '*' : ' '); @@ -83,7 +83,8 @@ static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) { uint64_t Shift = countTrailingZeros(Val); assert(Shift < 64 && "undefined behavior"); uint64_t Bit = 1ULL << Shift; - if (const char *PropName = ApplePropertyString(Bit)) + auto PropName = ApplePropertyString(Bit); + if (!PropName.empty()) OS << PropName; else OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit); @@ -116,14 +117,14 @@ void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, const char BaseIndent[] = " "; OS << BaseIndent; OS.indent(indent+2); - const char *attrString = AttributeString(attr); - if (attrString) + auto attrString = AttributeString(attr); + if (!attrString.empty()) WithColor(OS, syntax::Attribute) << attrString; else WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr); - const char *formString = FormEncodingString(form); - if (formString) + auto formString = FormEncodingString(form); + if (!formString.empty()) OS << " [" << formString << ']'; else OS << format(" [DW_FORM_Unknown_%x]", form); @@ -134,8 +135,8 @@ void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, return; OS << "\t("; - - const char *Name = nullptr; + + StringRef Name; std::string File; auto Color = syntax::Enumerator; if (attr == DW_AT_decl_file || attr == DW_AT_call_file) { @@ -146,12 +147,12 @@ void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, u->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) { File = '"' + File + '"'; - Name = File.c_str(); + Name = File; } } else if (Optional Val = formValue.getAsUnsignedConstant()) Name = AttributeValueString(attr, *Val); - if (Name) + if (!Name.empty()) WithColor(OS, Color) << Name; else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) OS << *formValue.getAsUnsignedConstant(); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp index 63d22d8..1635808 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp @@ -42,8 +42,8 @@ void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const { << format(" opcode_base: %u\n", OpcodeBase); for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i) - OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i + 1), - StandardOpcodeLengths[i]); + OS << format("standard_opcode_lengths[%s] = %u\n", + LNStandardString(i + 1).data(), StandardOpcodeLengths[i]); if (!IncludeDirectories.empty()) for (uint32_t i = 0; i < IncludeDirectories.size(); ++i) diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index fa67425..ac7c915 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -1418,7 +1418,8 @@ struct MDFieldPrinter { void MDFieldPrinter::printTag(const DINode *N) { Out << FS << "tag: "; - if (const char *Tag = dwarf::TagString(N->getTag())) + auto Tag = dwarf::TagString(N->getTag()); + if (!Tag.empty()) Out << Tag; else Out << N->getTag(); @@ -1426,7 +1427,8 @@ void MDFieldPrinter::printTag(const DINode *N) { void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) { Out << FS << "type: "; - if (const char *Type = dwarf::MacinfoString(N->getMacinfoType())) + auto Type = dwarf::MacinfoString(N->getMacinfoType()); + if (!Type.empty()) Out << Type; else Out << N->getMacinfoType(); @@ -1509,7 +1511,8 @@ void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value, return; Out << FS << Name << ": "; - if (const char *S = toString(Value)) + auto S = toString(Value); + if (!S.empty()) Out << S; else Out << Value; @@ -1843,8 +1846,8 @@ static void writeDIExpression(raw_ostream &Out, const DIExpression *N, FieldSeparator FS; if (N->isValid()) { for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) { - const char *OpStr = dwarf::OperationEncodingString(I->getOp()); - assert(OpStr && "Expected valid opcode"); + auto OpStr = dwarf::OperationEncodingString(I->getOp()); + assert(!OpStr.empty() && "Expected valid opcode"); Out << FS << OpStr; for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A) diff --git a/llvm/lib/Support/Dwarf.cpp b/llvm/lib/Support/Dwarf.cpp index e7c1ecf..65050fd 100644 --- a/llvm/lib/Support/Dwarf.cpp +++ b/llvm/lib/Support/Dwarf.cpp @@ -18,9 +18,10 @@ using namespace llvm; using namespace dwarf; -const char *llvm::dwarf::TagString(unsigned Tag) { +StringRef llvm::dwarf::TagString(unsigned Tag) { switch (Tag) { - default: return nullptr; + default: + return StringRef(); #define HANDLE_DW_TAG(ID, NAME) \ case DW_TAG_##NAME: \ return "DW_TAG_" #NAME; @@ -35,15 +36,15 @@ unsigned llvm::dwarf::getTag(StringRef TagString) { .Default(DW_TAG_invalid); } -const char *llvm::dwarf::ChildrenString(unsigned Children) { +StringRef llvm::dwarf::ChildrenString(unsigned Children) { switch (Children) { case DW_CHILDREN_no: return "DW_CHILDREN_no"; case DW_CHILDREN_yes: return "DW_CHILDREN_yes"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::AttributeString(unsigned Attribute) { +StringRef llvm::dwarf::AttributeString(unsigned Attribute) { switch (Attribute) { case DW_AT_sibling: return "DW_AT_sibling"; case DW_AT_location: return "DW_AT_location"; @@ -221,10 +222,10 @@ const char *llvm::dwarf::AttributeString(unsigned Attribute) { case DW_AT_GNU_pubtypes: return "DW_AT_GNU_pubtypes"; case DW_AT_GNU_discriminator: return "DW_AT_GNU_discriminator"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::FormEncodingString(unsigned Encoding) { +StringRef llvm::dwarf::FormEncodingString(unsigned Encoding) { switch (Encoding) { case DW_FORM_addr: return "DW_FORM_addr"; case DW_FORM_block2: return "DW_FORM_block2"; @@ -260,12 +261,13 @@ const char *llvm::dwarf::FormEncodingString(unsigned Encoding) { case DW_FORM_GNU_ref_alt: return "DW_FORM_GNU_ref_alt"; case DW_FORM_GNU_strp_alt: return "DW_FORM_GNU_strp_alt"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::OperationEncodingString(unsigned Encoding) { +StringRef llvm::dwarf::OperationEncodingString(unsigned Encoding) { switch (Encoding) { - default: return nullptr; + default: + return StringRef(); #define HANDLE_DW_OP(ID, NAME) \ case DW_OP_##NAME: \ return "DW_OP_" #NAME; @@ -280,9 +282,10 @@ unsigned llvm::dwarf::getOperationEncoding(StringRef OperationEncodingString) { .Default(0); } -const char *llvm::dwarf::AttributeEncodingString(unsigned Encoding) { +StringRef llvm::dwarf::AttributeEncodingString(unsigned Encoding) { switch (Encoding) { - default: return nullptr; + default: + return StringRef(); #define HANDLE_DW_ATE(ID, NAME) \ case DW_ATE_##NAME: \ return "DW_ATE_" #NAME; @@ -297,7 +300,7 @@ unsigned llvm::dwarf::getAttributeEncoding(StringRef EncodingString) { .Default(0); } -const char *llvm::dwarf::DecimalSignString(unsigned Sign) { +StringRef llvm::dwarf::DecimalSignString(unsigned Sign) { switch (Sign) { case DW_DS_unsigned: return "DW_DS_unsigned"; case DW_DS_leading_overpunch: return "DW_DS_leading_overpunch"; @@ -305,10 +308,10 @@ const char *llvm::dwarf::DecimalSignString(unsigned Sign) { case DW_DS_leading_separate: return "DW_DS_leading_separate"; case DW_DS_trailing_separate: return "DW_DS_trailing_separate"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::EndianityString(unsigned Endian) { +StringRef llvm::dwarf::EndianityString(unsigned Endian) { switch (Endian) { case DW_END_default: return "DW_END_default"; case DW_END_big: return "DW_END_big"; @@ -316,32 +319,32 @@ const char *llvm::dwarf::EndianityString(unsigned Endian) { case DW_END_lo_user: return "DW_END_lo_user"; case DW_END_hi_user: return "DW_END_hi_user"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::AccessibilityString(unsigned Access) { +StringRef llvm::dwarf::AccessibilityString(unsigned Access) { switch (Access) { // Accessibility codes case DW_ACCESS_public: return "DW_ACCESS_public"; case DW_ACCESS_protected: return "DW_ACCESS_protected"; case DW_ACCESS_private: return "DW_ACCESS_private"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::VisibilityString(unsigned Visibility) { +StringRef llvm::dwarf::VisibilityString(unsigned Visibility) { switch (Visibility) { case DW_VIS_local: return "DW_VIS_local"; case DW_VIS_exported: return "DW_VIS_exported"; case DW_VIS_qualified: return "DW_VIS_qualified"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::VirtualityString(unsigned Virtuality) { +StringRef llvm::dwarf::VirtualityString(unsigned Virtuality) { switch (Virtuality) { default: - return nullptr; + return StringRef(); #define HANDLE_DW_VIRTUALITY(ID, NAME) \ case DW_VIRTUALITY_##NAME: \ return "DW_VIRTUALITY_" #NAME; @@ -357,10 +360,10 @@ unsigned llvm::dwarf::getVirtuality(StringRef VirtualityString) { .Default(DW_VIRTUALITY_invalid); } -const char *llvm::dwarf::LanguageString(unsigned Language) { +StringRef llvm::dwarf::LanguageString(unsigned Language) { switch (Language) { default: - return nullptr; + return StringRef(); #define HANDLE_DW_LANG(ID, NAME) \ case DW_LANG_##NAME: \ return "DW_LANG_" #NAME; @@ -375,20 +378,20 @@ unsigned llvm::dwarf::getLanguage(StringRef LanguageString) { .Default(0); } -const char *llvm::dwarf::CaseString(unsigned Case) { +StringRef llvm::dwarf::CaseString(unsigned Case) { switch (Case) { case DW_ID_case_sensitive: return "DW_ID_case_sensitive"; case DW_ID_up_case: return "DW_ID_up_case"; case DW_ID_down_case: return "DW_ID_down_case"; case DW_ID_case_insensitive: return "DW_ID_case_insensitive"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::ConventionString(unsigned CC) { +StringRef llvm::dwarf::ConventionString(unsigned CC) { switch (CC) { default: - return nullptr; + return StringRef(); #define HANDLE_DW_CC(ID, NAME) \ case DW_CC_##NAME: \ return "DW_CC_" #NAME; @@ -403,33 +406,33 @@ unsigned llvm::dwarf::getCallingConvention(StringRef CCString) { .Default(0); } -const char *llvm::dwarf::InlineCodeString(unsigned Code) { +StringRef llvm::dwarf::InlineCodeString(unsigned Code) { switch (Code) { case DW_INL_not_inlined: return "DW_INL_not_inlined"; case DW_INL_inlined: return "DW_INL_inlined"; case DW_INL_declared_not_inlined: return "DW_INL_declared_not_inlined"; case DW_INL_declared_inlined: return "DW_INL_declared_inlined"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::ArrayOrderString(unsigned Order) { +StringRef llvm::dwarf::ArrayOrderString(unsigned Order) { switch (Order) { case DW_ORD_row_major: return "DW_ORD_row_major"; case DW_ORD_col_major: return "DW_ORD_col_major"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::DiscriminantString(unsigned Discriminant) { +StringRef llvm::dwarf::DiscriminantString(unsigned Discriminant) { switch (Discriminant) { case DW_DSC_label: return "DW_DSC_label"; case DW_DSC_range: return "DW_DSC_range"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::LNStandardString(unsigned Standard) { +StringRef llvm::dwarf::LNStandardString(unsigned Standard) { switch (Standard) { case DW_LNS_copy: return "DW_LNS_copy"; case DW_LNS_advance_pc: return "DW_LNS_advance_pc"; @@ -444,10 +447,10 @@ const char *llvm::dwarf::LNStandardString(unsigned Standard) { case DW_LNS_set_epilogue_begin: return "DW_LNS_set_epilogue_begin"; case DW_LNS_set_isa: return "DW_LNS_set_isa"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::LNExtendedString(unsigned Encoding) { +StringRef llvm::dwarf::LNExtendedString(unsigned Encoding) { switch (Encoding) { // Line Number Extended Opcode Encodings case DW_LNE_end_sequence: return "DW_LNE_end_sequence"; @@ -457,10 +460,10 @@ const char *llvm::dwarf::LNExtendedString(unsigned Encoding) { case DW_LNE_lo_user: return "DW_LNE_lo_user"; case DW_LNE_hi_user: return "DW_LNE_hi_user"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::MacinfoString(unsigned Encoding) { +StringRef llvm::dwarf::MacinfoString(unsigned Encoding) { switch (Encoding) { // Macinfo Type Encodings case DW_MACINFO_define: return "DW_MACINFO_define"; @@ -470,7 +473,7 @@ const char *llvm::dwarf::MacinfoString(unsigned Encoding) { case DW_MACINFO_vendor_ext: return "DW_MACINFO_vendor_ext"; case DW_MACINFO_invalid: return "DW_MACINFO_invalid"; } - return nullptr; + return StringRef(); } unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString) { @@ -483,7 +486,7 @@ unsigned llvm::dwarf::getMacinfo(StringRef MacinfoString) { .Default(DW_MACINFO_invalid); } -const char *llvm::dwarf::CallFrameString(unsigned Encoding) { +StringRef llvm::dwarf::CallFrameString(unsigned Encoding) { switch (Encoding) { case DW_CFA_nop: return "DW_CFA_nop"; case DW_CFA_advance_loc: return "DW_CFA_advance_loc"; @@ -517,10 +520,10 @@ const char *llvm::dwarf::CallFrameString(unsigned Encoding) { case DW_CFA_lo_user: return "DW_CFA_lo_user"; case DW_CFA_hi_user: return "DW_CFA_hi_user"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::ApplePropertyString(unsigned Prop) { +StringRef llvm::dwarf::ApplePropertyString(unsigned Prop) { switch (Prop) { case DW_APPLE_PROPERTY_readonly: return "DW_APPLE_PROPERTY_readonly"; @@ -553,10 +556,10 @@ const char *llvm::dwarf::ApplePropertyString(unsigned Prop) { case DW_APPLE_PROPERTY_class: return "DW_APPLE_PROPERTY_class"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::AtomTypeString(unsigned AT) { +StringRef llvm::dwarf::AtomTypeString(unsigned AT) { switch (AT) { case dwarf::DW_ATOM_null: return "DW_ATOM_null"; @@ -569,10 +572,10 @@ const char *llvm::dwarf::AtomTypeString(unsigned AT) { case DW_ATOM_type_flags: return "DW_ATOM_type_flags"; } - return nullptr; + return StringRef(); } -const char *llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind) { +StringRef llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind) { switch (Kind) { case GIEK_NONE: return "NONE"; @@ -594,7 +597,8 @@ const char *llvm::dwarf::GDBIndexEntryKindString(GDBIndexEntryKind Kind) { llvm_unreachable("Unknown GDBIndexEntryKind value"); } -const char *llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage) { +StringRef +llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage) { switch (Linkage) { case GIEL_EXTERNAL: return "EXTERNAL"; @@ -604,7 +608,7 @@ const char *llvm::dwarf::GDBIndexEntryLinkageString(GDBIndexEntryLinkage Linkage llvm_unreachable("Unknown GDBIndexEntryLinkage value"); } -const char *llvm::dwarf::AttributeValueString(uint16_t Attr, unsigned Val) { +StringRef llvm::dwarf::AttributeValueString(uint16_t Attr, unsigned Val) { switch (Attr) { case DW_AT_accessibility: return AccessibilityString(Val); @@ -632,5 +636,5 @@ const char *llvm::dwarf::AttributeValueString(uint16_t Attr, unsigned Val) { return DiscriminantString(Val); } - return nullptr; + return StringRef(); } diff --git a/llvm/unittests/Support/DwarfTest.cpp b/llvm/unittests/Support/DwarfTest.cpp index 74fcc98..148ea27 100644 --- a/llvm/unittests/Support/DwarfTest.cpp +++ b/llvm/unittests/Support/DwarfTest.cpp @@ -17,13 +17,13 @@ namespace { TEST(DwarfTest, TagStringOnInvalid) { // This is invalid, so it shouldn't be stringified. - EXPECT_EQ(nullptr, TagString(DW_TAG_invalid)); + EXPECT_EQ(StringRef(), TagString(DW_TAG_invalid)); // These aren't really tags: they describe ranges within tags. They // shouldn't be stringified either. - EXPECT_EQ(nullptr, TagString(DW_TAG_lo_user)); - EXPECT_EQ(nullptr, TagString(DW_TAG_hi_user)); - EXPECT_EQ(nullptr, TagString(DW_TAG_user_base)); + EXPECT_EQ(StringRef(), TagString(DW_TAG_lo_user)); + EXPECT_EQ(StringRef(), TagString(DW_TAG_hi_user)); + EXPECT_EQ(StringRef(), TagString(DW_TAG_user_base)); } TEST(DwarfTest, getTag) { @@ -58,12 +58,12 @@ TEST(DwarfTest, getOperationEncoding) { TEST(DwarfTest, LanguageStringOnInvalid) { // This is invalid, so it shouldn't be stringified. - EXPECT_EQ(nullptr, LanguageString(0)); + EXPECT_EQ(StringRef(), LanguageString(0)); // These aren't really tags: they describe ranges within tags. They // shouldn't be stringified either. - EXPECT_EQ(nullptr, LanguageString(DW_LANG_lo_user)); - EXPECT_EQ(nullptr, LanguageString(DW_LANG_hi_user)); + EXPECT_EQ(StringRef(), LanguageString(DW_LANG_lo_user)); + EXPECT_EQ(StringRef(), LanguageString(DW_LANG_hi_user)); } TEST(DwarfTest, getLanguage) { @@ -85,12 +85,12 @@ TEST(DwarfTest, getLanguage) { TEST(DwarfTest, AttributeEncodingStringOnInvalid) { // This is invalid, so it shouldn't be stringified. - EXPECT_EQ(nullptr, AttributeEncodingString(0)); + EXPECT_EQ(StringRef(), AttributeEncodingString(0)); // These aren't really tags: they describe ranges within tags. They // shouldn't be stringified either. - EXPECT_EQ(nullptr, AttributeEncodingString(DW_ATE_lo_user)); - EXPECT_EQ(nullptr, AttributeEncodingString(DW_ATE_hi_user)); + EXPECT_EQ(StringRef(), AttributeEncodingString(DW_ATE_lo_user)); + EXPECT_EQ(StringRef(), AttributeEncodingString(DW_ATE_hi_user)); } TEST(DwarfTest, getAttributeEncoding) { @@ -122,8 +122,8 @@ TEST(DwarfTest, VirtualityString) { VirtualityString(DW_VIRTUALITY_max)); // Invalid numbers shouldn't be stringified. - EXPECT_EQ(nullptr, VirtualityString(DW_VIRTUALITY_max + 1)); - EXPECT_EQ(nullptr, VirtualityString(DW_VIRTUALITY_max + 77)); + EXPECT_EQ(StringRef(), VirtualityString(DW_VIRTUALITY_max + 1)); + EXPECT_EQ(StringRef(), VirtualityString(DW_VIRTUALITY_max + 77)); } TEST(DwarfTest, getVirtuality) { -- 2.7.4