[llvm] r359818 - [yaml2obj] - Make interface of `NameToIdxMap` class be human friendly and fix users.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu May 2 12:28:05 PDT 2019


Author: grimar
Date: Thu May  2 12:28:04 2019
New Revision: 359818

URL: http://llvm.org/viewvc/llvm-project?rev=359818&view=rev
Log:
[yaml2obj] - Make interface of `NameToIdxMap` class be human friendly and fix users.

This patch inverses the values returned by `addName` and
`lookup` methods of the class mentioned so that they
now return true on success and false on failure.
Also, it does minor code cleanup.

Differential revision: https://reviews.llvm.org/D61190

Modified:
    llvm/trunk/tools/yaml2obj/yaml2elf.cpp

Modified: llvm/trunk/tools/yaml2obj/yaml2elf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/yaml2obj/yaml2elf.cpp?rev=359818&r1=359817&r2=359818&view=diff
==============================================================================
--- llvm/trunk/tools/yaml2obj/yaml2elf.cpp (original)
+++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp Thu May  2 12:28:04 2019
@@ -57,33 +57,34 @@ public:
 } // end anonymous namespace
 
 // Used to keep track of section and symbol names, so that in the YAML file
-// sections and symbols can be referenced by name instead of by index.
-namespace {
-class NameToIdxMap {
-  StringMap<int> Map;
-public:
-  /// \returns true if name is already present in the map.
-  bool addName(StringRef Name, unsigned i) {
-    return !Map.insert(std::make_pair(Name, (int)i)).second;
-  }
-  /// \returns true if name is not present in the map
-  bool lookup(StringRef Name, unsigned &Idx) const {
-    StringMap<int>::const_iterator I = Map.find(Name);
-    if (I == Map.end())
-      return true;
-    Idx = I->getValue();
-    return false;
-  }
-  /// asserts if name is not present in the map
-  unsigned get(StringRef Name) const {
-    unsigned Idx = 0;
-    auto missing = lookup(Name, Idx);
-    (void)missing;
-    assert(!missing && "Expected section not found in index");
-    return Idx;
-  }
-  unsigned size() const { return Map.size(); }
-};
+// sections and symbols can be referenced by name instead of by index.
+namespace {
+class NameToIdxMap {
+  StringMap<unsigned> Map;
+
+public:
+  /// \Returns false if name is already present in the map.
+  bool addName(StringRef Name, unsigned Ndx) {
+    return Map.insert({Name, Ndx}).second;
+  }
+  /// \Returns false if name is not present in the map.
+  bool lookup(StringRef Name, unsigned &Idx) const {
+    auto I = Map.find(Name);
+    if (I == Map.end())
+      return false;
+    Idx = I->getValue();
+    return true;
+  }
+  /// Asserts if name is not present in the map.
+  unsigned get(StringRef Name) const {
+    unsigned Idx;
+    if (lookup(Name, Idx))
+      return Idx;
+    assert(false && "Expected section not found in index");
+    return 0;
+  }
+  unsigned size() const { return Map.size(); }
+};
 } // end anonymous namespace
 
 template <class T>
@@ -235,13 +236,13 @@ void ELFState<ELFT>::initProgramHeaders(
     PHeaders.push_back(Phdr);
   }
 }
-
-static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
-                                StringRef IndexSrc, unsigned &IndexDest) {
-  if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
-    WithColor::error() << "Unknown section referenced: '" << IndexSrc
-                       << "' at YAML section '" << SecName << "'.\n";
-    return false;
+
+static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
+                                StringRef IndexSrc, unsigned &IndexDest) {
+  if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
+    WithColor::error() << "Unknown section referenced: '" << IndexSrc
+                       << "' at YAML section '" << SecName << "'.\n";
+    return false;
   }
   return true;
 }
@@ -392,13 +393,13 @@ void ELFState<ELFT>::setProgramHeaderLay
   for (auto &YamlPhdr : Doc.ProgramHeaders) {
     Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
 
-    std::vector<Elf_Shdr *> Sections;
-    for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
-      unsigned Index;
-      if (SN2I.lookup(SecName.Section, Index)) {
-        WithColor::error() << "Unknown section referenced: '" << SecName.Section
-                           << "' by program header.\n";
-        exit(1);
+    std::vector<Elf_Shdr *> Sections;
+    for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
+      unsigned Index;
+      if (!SN2I.lookup(SecName.Section, Index)) {
+        WithColor::error() << "Unknown section referenced: '" << SecName.Section
+                           << "' by program header.\n";
+        exit(1);
       }
       Sections.push_back(&SHeaders[Index]);
     }
@@ -469,13 +470,13 @@ void ELFState<ELFT>::addSymbols(ArrayRef
     zero(Symbol);
     if (!Sym.Name.empty())
       Symbol.st_name = Strtab.getOffset(Sym.Name);
-    Symbol.setBindingAndType(Sym.Binding, Sym.Type);
-    if (!Sym.Section.empty()) {
-      unsigned Index;
-      if (SN2I.lookup(Sym.Section, Index)) {
-        WithColor::error() << "Unknown section referenced: '" << Sym.Section
-                           << "' by YAML symbol " << Sym.Name << ".\n";
-        exit(1);
+    Symbol.setBindingAndType(Sym.Binding, Sym.Type);
+    if (!Sym.Section.empty()) {
+      unsigned Index;
+      if (!SN2I.lookup(Sym.Section, Index)) {
+        WithColor::error() << "Unknown section referenced: '" << Sym.Section
+                           << "' by YAML symbol " << Sym.Name << ".\n";
+        exit(1);
       }
       Symbol.st_shndx = Index;
     } else if (Sym.Index) {
@@ -543,13 +544,13 @@ ELFState<ELFT>::writeSectionContent(Elf_
   auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
 
   for (const auto &Rel : Section.Relocations) {
-    unsigned SymIdx = 0;
-    // If a relocation references a symbol, try to look one up in the symbol
-    // table. If it is not there, treat the value as a symbol index.
-    if (Rel.Symbol && SymN2I.lookup(*Rel.Symbol, SymIdx) &&
-        !to_integer(*Rel.Symbol, SymIdx)) {
-      WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
-                         << "' at YAML section '" << Section.Name << "'.\n";
+    unsigned SymIdx = 0;
+    // If a relocation references a symbol, try to look one up in the symbol
+    // table. If it is not there, treat the value as a symbol index.
+    if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
+        !to_integer(*Rel.Symbol, SymIdx)) {
+      WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
+                         << "' at YAML section '" << Section.Name << "'.\n";
       return false;
     }
 
@@ -579,13 +580,13 @@ bool ELFState<ELFT>::writeSectionContent
          "Section type is not SHT_GROUP");
 
   SHeader.sh_entsize = 4;
-  SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
-
-  unsigned SymIdx;
-  if (SymN2I.lookup(Section.Signature, SymIdx) &&
-      !to_integer(Section.Signature, SymIdx)) {
-    WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
-                       << "' at YAML section '" << Section.Name << "'.\n";
+  SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
+
+  unsigned SymIdx;
+  if (!SymN2I.lookup(Section.Signature, SymIdx) &&
+      !to_integer(Section.Signature, SymIdx)) {
+    WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
+                       << "' at YAML section '" << Section.Name << "'.\n";
     return false;
   }
   SHeader.sh_info = SymIdx;
@@ -780,20 +781,20 @@ bool ELFState<ELFT>::writeSectionContent
 
 template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
   for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
-    StringRef Name = Doc.Sections[i]->Name;
-    DotShStrtab.add(Name);
-    // "+ 1" to take into account the SHT_NULL entry.
-    if (SN2I.addName(Name, i + 1)) {
-      WithColor::error() << "Repeated section name: '" << Name
-                         << "' at YAML section number " << i << ".\n";
-      return false;
+    StringRef Name = Doc.Sections[i]->Name;
+    DotShStrtab.add(Name);
+    // "+ 1" to take into account the SHT_NULL entry.
+    if (!SN2I.addName(Name, i + 1)) {
+      WithColor::error() << "Repeated section name: '" << Name
+                         << "' at YAML section number " << i << ".\n";
+      return false;
     }
   }
 
   auto SecNo = 1 + Doc.Sections.size();
   // Add special sections after input sections, if necessary.
   for (StringRef Name : implicitSectionNames())
-    if (!SN2I.addName(Name, SecNo)) {
+    if (SN2I.addName(Name, SecNo)) {
       // Account for this section, since it wasn't in the Doc
       ++SecNo;
       DotShStrtab.add(Name);
@@ -816,13 +817,13 @@ bool ELFState<ELFT>::buildSymbolIndex(Ar
                                 "' after global in Symbols list.\n";
       return false;
     }
-    if (Sym.Binding.value != ELF::STB_LOCAL)
-      GlobalSymbolSeen = true;
-
-    if (!Name.empty() && SymN2I.addName(Name, I)) {
-      WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
-      return false;
-    }
+    if (Sym.Binding.value != ELF::STB_LOCAL)
+      GlobalSymbolSeen = true;
+
+    if (!Name.empty() && !SymN2I.addName(Name, I)) {
+      WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
+      return false;
+    }
   }
   return true;
 }




More information about the llvm-commits mailing list