Recommit r368812 "[llvm/Object] - Convert SectionRef::getName() to return Expected<>"

Changes: no changes. A fix for the clang code will be landed right on top.

Original commit message:

SectionRef::getName() returns std::error_code now.
Returning Expected<> instead has multiple benefits.

For example, it forces user to check the error returned.
Also Expected<> may keep a valuable string error message,
what is more useful than having a error code.
(Object\invalid.test was updated to show the new messages printed.)

This patch makes a change for all users to switch to Expected<> version.

Note: in a few places the error returned was ignored before my changes.
In such places I left them ignored. My intention was to convert the interface
used, and not to improve and/or the existent users in this patch.
(Though I think this is good idea for a follow-ups to revisit such places
and either remove consumeError calls or comment each of them to clarify why
it is OK to have them).

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

llvm-svn: 368826
diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp
index 89b89f0..87df126 100644
--- a/llvm/tools/llvm-size/llvm-size.cpp
+++ b/llvm/tools/llvm-size/llvm-size.cpp
@@ -106,17 +106,6 @@
 
 static std::string ToolName;
 
-/// If ec is not success, print the error and return true.
-static bool error(std::error_code ec) {
-  if (!ec)
-    return false;
-
-  HadError = true;
-  errs() << ToolName << ": error reading file: " << ec.message() << ".\n";
-  errs().flush();
-  return true;
-}
-
 static bool error(Twine Message) {
   HadError = true;
   errs() << ToolName << ": " << Message << ".\n";
@@ -397,11 +386,14 @@
       uint64_t size = Section.getSize();
       total += size;
 
-      StringRef name;
-      if (error(Section.getName(name)))
+      Expected<StringRef> name_or_err = Section.getName();
+      if (!name_or_err) {
+        error(name_or_err.takeError(), Obj->getFileName());
         return;
+      }
+
       uint64_t addr = Section.getAddress();
-      max_name_len = std::max(max_name_len, name.size());
+      max_name_len = std::max(max_name_len, name_or_err->size());
       max_size_len = std::max(max_size_len, getNumLengthAsString(size));
       max_addr_len = std::max(max_addr_len, getNumLengthAsString(addr));
     }
@@ -431,14 +423,16 @@
     for (const SectionRef &Section : Obj->sections()) {
       if (!considerForSize(Obj, Section))
         continue;
-      StringRef name;
-      if (error(Section.getName(name)))
+
+      Expected<StringRef> name_or_err = Section.getName();
+      if (!name_or_err) {
+        error(name_or_err.takeError(), Obj->getFileName());
         return;
+      }
+
       uint64_t size = Section.getSize();
       uint64_t addr = Section.getAddress();
-      std::string namestr = name;
-
-      outs() << format(fmt.str().c_str(), namestr.c_str(), size, addr);
+      outs() << format(fmt.str().c_str(), name_or_err->str().c_str(), size, addr);
     }
 
     if (ELFCommons) {