[PATCH] D79985: [yaml2obj] - Move "repeated section/fill name" check earlier.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 15 00:52:28 PDT 2020


grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
grimar added a parent revision: D79984: [yaml2obj] - Add a technical prefix for each unnamed chunk..

This allows to simplify the code. Doing checks early is generally useful.

I am using it in another path, going to post it soon.

Depends on https://reviews.llvm.org/D79984


https://reviews.llvm.org/D79985

Files:
  llvm/lib/ObjectYAML/ELFEmitter.cpp


Index: llvm/lib/ObjectYAML/ELFEmitter.cpp
===================================================================
--- llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -260,7 +260,10 @@
     if (C->Name.empty())
       C->Name = StringRef(Twine(SuffixPrefix + Twine(I) + "]").str())
                     .copy(StringAlloc);
-    DocSections.insert(C->Name);
+
+    if (!DocSections.insert(C->Name).second)
+      reportError("repeated section/fill name: '" + C->Name +
+                  "' at YAML section/fill number " + Twine(I));
   }
 
   std::vector<StringRef> ImplicitSections;
@@ -1415,18 +1418,10 @@
 
 template <class ELFT> void ELFState<ELFT>::buildSectionIndex() {
   size_t SecNdx = -1;
-  StringSet<> Seen;
-  for (size_t I = 0; I < Doc.Chunks.size(); ++I) {
-    const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I];
-    bool IsSection = isa<ELFYAML::Section>(C.get());
-    if (IsSection)
-      ++SecNdx;
-
-    if (!Seen.insert(C->Name).second)
-      reportError("repeated section/fill name: '" + C->Name +
-                  "' at YAML section/fill number " + Twine(I));
-    if (!IsSection || HasError)
+  for (const std::unique_ptr<ELFYAML::Chunk> &C : Doc.Chunks) {
+    if (!isa<ELFYAML::Section>(C.get()))
       continue;
+    ++SecNdx;
 
     if (!SN2I.addName(C->Name, SecNdx))
       llvm_unreachable("buildSectionIndex() failed");
@@ -1489,6 +1484,8 @@
 bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc,
                               yaml::ErrorHandler EH) {
   ELFState<ELFT> State(Doc, EH);
+  if (State.HasError)
+    return false;
 
   // Finalize .strtab and .dynstr sections. We do that early because want to
   // finalize the string table builders before writing the content of the
@@ -1496,9 +1493,6 @@
   State.finalizeStrings();
 
   State.buildSectionIndex();
-  if (State.HasError)
-    return false;
-
   State.buildSymbolIndexes();
 
   std::vector<Elf_Phdr> PHeaders;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79985.264171.patch
Type: text/x-patch
Size: 1962 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200515/cd869c62/attachment.bin>


More information about the llvm-commits mailing list