[PATCH] D40928: Remove checkToString functions and use toString instead.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 17:30:00 PST 2017


LGTM

BTW, as a followup you can even convert the macro to a template
function, no?

Rui Ueyama via Phabricator <reviews at reviews.llvm.org> writes:

> ruiu created this revision.
> Herald added a subscriber: emaste.
>
> Remove checkToString functions and use toString instead.
>
>
> https://reviews.llvm.org/D40928
>
> Files:
>   lld/COFF/InputFiles.cpp
>   lld/ELF/InputFiles.cpp
>   lld/include/lld/Common/ErrorHandler.h
>
> Index: lld/include/lld/Common/ErrorHandler.h
> ===================================================================
> --- lld/include/lld/Common/ErrorHandler.h
> +++ lld/include/lld/Common/ErrorHandler.h
> @@ -102,12 +102,10 @@
>    return std::move(*E);
>  }
>  
> -inline std::string checkToString(const Twine &S) { return S.str(); }
> -inline std::string checkToString(std::string S) { return S; }
> -inline std::string checkToString(const char *S) { return S; }
> +inline std::string toString(const Twine &S) { return S.str(); }
>  
>  // To evaluate the second argument lazily, we use C macro.
> -#define CHECK(E, S) check2(E, [&] { return checkToString(S); })
> +#define CHECK(E, S) check2(E, [&] { return toString(S); })
>  
>  } // namespace lld
>  
> Index: lld/ELF/InputFiles.cpp
> ===================================================================
> --- lld/ELF/InputFiles.cpp
> +++ lld/ELF/InputFiles.cpp
> @@ -209,20 +209,19 @@
>  
>  template <class ELFT>
>  uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
> -  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX),
> -               toString(this));
> +  return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
>  }
>  
>  template <class ELFT>
>  void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
>                                     const Elf_Shdr *Symtab) {
>    FirstNonLocal = Symtab->sh_info;
> -  ELFSyms = CHECK(getObj().symbols(Symtab), toString(this));
> +  ELFSyms = CHECK(getObj().symbols(Symtab), this);
>    if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
>      fatal(toString(this) + ": invalid sh_info in symbol table");
>  
> -  StringTable = CHECK(getObj().getStringTableForSymtab(*Symtab, Sections),
> -                      toString(this));
> +  StringTable =
> +      CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
>  }
>  
>  template <class ELFT>
> @@ -254,12 +253,11 @@
>    // sh_info contains a symbol index, so we fetch a symbol and read its name.
>    if (this->ELFSyms.empty())
>      this->initSymtab(
> -        Sections,
> -        CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
> +        Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
>  
> -  const Elf_Sym *Sym = CHECK(
> -      object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), toString(this));
> -  StringRef Signature = CHECK(Sym->getName(this->StringTable), toString(this));
> +  const Elf_Sym *Sym =
> +      CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
> +  StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
>  
>    // As a special case, if a symbol is a section symbol and has no name,
>    // we use a section name as a signature.
> @@ -277,8 +275,8 @@
>  ArrayRef<typename ObjFile<ELFT>::Elf_Word>
>  ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
>    const ELFFile<ELFT> &Obj = this->getObj();
> -  ArrayRef<Elf_Word> Entries = CHECK(
> -      Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
> +  ArrayRef<Elf_Word> Entries =
> +      CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
>    if (Entries.empty() || Entries[0] != GRP_COMDAT)
>      fatal(toString(this) + ": unsupported SHT_GROUP format");
>    return Entries.slice(1);
> @@ -323,12 +321,11 @@
>      DenseSet<CachedHashStringRef> &ComdatGroups) {
>    const ELFFile<ELFT> &Obj = this->getObj();
>  
> -  ArrayRef<Elf_Shdr> ObjSections =
> -      CHECK(this->getObj().sections(), toString(this));
> +  ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
>    uint64_t Size = ObjSections.size();
>    this->Sections.resize(Size);
>    this->SectionStringTable =
> -      CHECK(Obj.getSectionStringTable(ObjSections), toString(this));
> +      CHECK(Obj.getSectionStringTable(ObjSections), this);
>  
>    for (size_t I = 0, E = ObjSections.size(); I < E; I++) {
>      if (this->Sections[I] == &InputSection::Discarded)
> @@ -374,8 +371,7 @@
>        this->initSymtab(ObjSections, &Sec);
>        break;
>      case SHT_SYMTAB_SHNDX:
> -      this->SymtabSHNDX =
> -          CHECK(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
> +      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
>        break;
>      case SHT_STRTAB:
>      case SHT_NULL:
> @@ -520,13 +516,12 @@
>  
>      size_t NumRelocations;
>      if (Sec.sh_type == SHT_RELA) {
> -      ArrayRef<Elf_Rela> Rels =
> -          CHECK(this->getObj().relas(&Sec), toString(this));
> +      ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
>        Target->FirstRelocation = Rels.begin();
>        NumRelocations = Rels.size();
>        Target->AreRelocsRela = true;
>      } else {
> -      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), toString(this));
> +      ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
>        Target->FirstRelocation = Rels.begin();
>        NumRelocations = Rels.size();
>        Target->AreRelocsRela = false;
> @@ -595,8 +590,7 @@
>  
>  template <class ELFT>
>  StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
> -  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable),
> -               toString(this));
> +  return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
>  }
>  
>  template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
> @@ -628,7 +622,7 @@
>  
>    if (Binding == STB_LOCAL) {
>      if (Sym->getType() == STT_FILE)
> -      SourceFile = CHECK(Sym->getName(this->StringTable), toString(this));
> +      SourceFile = CHECK(Sym->getName(this->StringTable), this);
>  
>      if (this->StringTable.size() <= Sym->st_name)
>        fatal(toString(this) + ": invalid symbol name offset");
> @@ -640,7 +634,7 @@
>      return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
>    }
>  
> -  StringRef Name = CHECK(Sym->getName(this->StringTable), toString(this));
> +  StringRef Name = CHECK(Sym->getName(this->StringTable), this);
>  
>    switch (Sym->st_shndx) {
>    case SHN_UNDEF:
> @@ -695,8 +689,7 @@
>                  Sym->getName());
>  
>    if (C.getParent()->isThin() && Tar)
> -    Tar->append(relativeToRoot(CHECK(C.getFullName(), toString(this))),
> -                Ret.getBuffer());
> +    Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), Ret.getBuffer());
>    if (C.getParent()->isThin())
>      return {Ret, 0};
>    return {Ret, C.getChildOffset()};
> @@ -712,7 +705,7 @@
>  template <class ELFT> void SharedFile<ELFT>::parseSoName() {
>    const Elf_Shdr *DynamicSec = nullptr;
>    const ELFFile<ELFT> Obj = this->getObj();
> -  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), toString(this));
> +  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
>  
>    // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
>    for (const Elf_Shdr &Sec : Sections) {
> @@ -726,8 +719,7 @@
>        DynamicSec = &Sec;
>        break;
>      case SHT_SYMTAB_SHNDX:
> -      this->SymtabSHNDX =
> -          CHECK(Obj.getSHNDXTable(Sec, Sections), toString(this));
> +      this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
>        break;
>      case SHT_GNU_versym:
>        this->VersymSec = &Sec;
> @@ -745,8 +737,7 @@
>    if (!DynamicSec)
>      return;
>    ArrayRef<Elf_Dyn> Arr =
> -      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
> -            toString(this));
> +      CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
>    for (const Elf_Dyn &Dyn : Arr) {
>      if (Dyn.d_tag == DT_SONAME) {
>        uint64_t Val = Dyn.getVal();
> @@ -805,8 +796,7 @@
>    const Elf_Versym *Versym = nullptr;
>    std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
>  
> -  ArrayRef<Elf_Shdr> Sections =
> -      CHECK(this->getObj().sections(), toString(this));
> +  ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
>  
>    // Add symbols to the symbol table.
>    Elf_Sym_Range Syms = this->getGlobalELFSyms();
> @@ -819,7 +809,7 @@
>      bool Hidden = VersymIndex & VERSYM_HIDDEN;
>      VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
>  
> -    StringRef Name = CHECK(Sym.getName(this->StringTable), toString(this));
> +    StringRef Name = CHECK(Sym.getName(this->StringTable), this);
>      if (Sym.isUndefined()) {
>        Undefs.push_back(Name);
>        continue;
> @@ -916,7 +906,7 @@
>    MemoryBufferRef MBRef(MB.getBuffer(),
>                          Saver.save(ArchiveName + MB.getBufferIdentifier() +
>                                     utostr(OffsetInArchive)));
> -  Obj = CHECK(lto::InputFile::create(MBRef), toString(this));
> +  Obj = CHECK(lto::InputFile::create(MBRef), this);
>  
>    Triple T(Obj->getTargetTriple());
>    EKind = getBitcodeELFKind(T);
> @@ -1081,28 +1071,28 @@
>    typedef typename ELFT::SymRange Elf_Sym_Range;
>  
>    ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
> -  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), toString(this));
> +  ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
>    for (const Elf_Shdr &Sec : Sections) {
>      if (Sec.sh_type != SHT_SYMTAB)
>        continue;
>  
> -    Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), toString(this));
> +    Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
>      uint32_t FirstNonLocal = Sec.sh_info;
>      StringRef StringTable =
> -        CHECK(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
> +        CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
>      std::vector<StringRef> V;
>  
>      for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
>        if (Sym.st_shndx != SHN_UNDEF)
> -        V.push_back(CHECK(Sym.getName(StringTable), toString(this)));
> +        V.push_back(CHECK(Sym.getName(StringTable), this));
>      return V;
>    }
>    return {};
>  }
>  
>  std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
>    std::unique_ptr<lto::InputFile> Obj =
> -      CHECK(lto::InputFile::create(this->MB), toString(this));
> +      CHECK(lto::InputFile::create(this->MB), this);
>    std::vector<StringRef> V;
>    for (const lto::InputFile::Symbol &Sym : Obj->symbols())
>      if (!Sym.isUndefined())
> Index: lld/COFF/InputFiles.cpp
> ===================================================================
> --- lld/COFF/InputFiles.cpp
> +++ lld/COFF/InputFiles.cpp
> @@ -63,7 +63,7 @@
>  
>  void ArchiveFile::parse() {
>    // Parse a MemoryBufferRef as an archive file.
> -  File = CHECK(Archive::create(MB), toString(this));
> +  File = CHECK(Archive::create(MB), this);
>  
>    // Read the symbol table to construct Lazy objects.
>    for (const Archive::Symbol &Sym : File->symbols())
> @@ -104,7 +104,7 @@
>  
>  void ObjFile::parse() {
>    // Parse a memory buffer as a COFF file.
> -  std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this));
> +  std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
>  
>    if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
>      Bin.release();


More information about the llvm-commits mailing list