[PATCH] D55220: [yaml2obj] Move redundant statements into a separate static function

Xing via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 3 07:28:27 PST 2018


Higuoxing created this revision.
Higuoxing added reviewers: jhenderson, grimar.
Herald added subscribers: llvm-commits, jakehehrlich.

Repository:
  rL LLVM

https://reviews.llvm.org/D55220

Files:
  tools/yaml2obj/yaml2elf.cpp


Index: tools/yaml2obj/yaml2elf.cpp
===================================================================
--- tools/yaml2obj/yaml2elf.cpp
+++ tools/yaml2obj/yaml2elf.cpp
@@ -226,6 +226,16 @@
   }
 }
 
+static bool checkSectionIndex(NameToIdxMap &SX2I, StringRef SecName,
+                              StringRef SecField, unsigned &Index) {
+  if (SX2I.lookup(SecField, Index) && !to_integer(SecField, Index)) {
+    WithColor::error() << "Unknown section referenced: '" << SecField
+                       << "' at YAML section '" << SecName << "'.\n";
+    return false;
+  }
+  return true;
+}
+
 template <class ELFT>
 bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
                                         ContiguousBlobAccumulator &CBA) {
@@ -245,11 +255,8 @@
 
     if (!Sec->Link.empty()) {
       unsigned Index;
-      if (SN2I.lookup(Sec->Link, Index) && !to_integer(Sec->Link, Index)) {
-        WithColor::error() << "Unknown section referenced: '" << Sec->Link
-                           << "' at YAML section '" << Sec->Name << "'.\n";
+      if (!checkSectionIndex(SN2I, Sec->Name, Sec->Link, Index))
         return false;
-      }
       SHeader.sh_link = Index;
     }
 
@@ -261,22 +268,15 @@
         SHeader.sh_link = getDotSymTabSecNo();
 
       unsigned Index;
-      if (SN2I.lookup(S->Info, Index) && !to_integer(S->Info, Index)) {
-        WithColor::error() << "Unknown section referenced: '" << S->Info
-                           << "' at YAML section '" << S->Name << "'.\n";
+      if (!checkSectionIndex(SN2I, S->Name, S->Info, Index))
         return false;
-      }
       SHeader.sh_info = Index;
-
       if (!writeSectionContent(SHeader, *S, CBA))
         return false;
     } else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
       unsigned SymIdx;
-      if (SymN2I.lookup(S->Info, SymIdx) && !to_integer(S->Info, SymIdx)) {
-        WithColor::error() << "Unknown symbol referenced: '" << S->Info
-                           << "' at YAML section '" << S->Name << "'.\n";
+      if (!checkSectionIndex(SymN2I, S->Name, S->Info, SymIdx))
         return false;
-      }
       SHeader.sh_info = SymIdx;
       if (!writeSectionContent(SHeader, *S, CBA))
         return false;
@@ -535,13 +535,9 @@
     unsigned int sectionIndex = 0;
     if (member.sectionNameOrType == "GRP_COMDAT")
       sectionIndex = llvm::ELF::GRP_COMDAT;
-    else if (SN2I.lookup(member.sectionNameOrType, sectionIndex) &&
-             !to_integer(member.sectionNameOrType, sectionIndex)) {
-      WithColor::error() << "Unknown section referenced: '"
-                         << member.sectionNameOrType << "' at YAML section' "
-                         << Section.Name << "\n";
+    else if (!checkSectionIndex(SN2I, Section.Name, member.sectionNameOrType,
+                                sectionIndex))
       return false;
-    }
     SIdx = sectionIndex;
     OS.write((const char *)&SIdx, sizeof(SIdx));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55220.176402.patch
Type: text/x-patch
Size: 2968 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181203/f57f3a55/attachment.bin>


More information about the llvm-commits mailing list