[llvm] 4192c41 - [llvm-objdump] Add WarningHandler as a member variable. NFC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 14 08:04:37 PDT 2023


Author: Fangrui Song
Date: 2023-08-14T08:04:33-07:00
New Revision: 4192c419b86cfafbbdb3de80fe0bc185a70afa82

URL: https://github.com/llvm/llvm-project/commit/4192c419b86cfafbbdb3de80fe0bc185a70afa82
DIFF: https://github.com/llvm/llvm-project/commit/4192c419b86cfafbbdb3de80fe0bc185a70afa82.diff

LOG: [llvm-objdump] Add WarningHandler as a member variable. NFC

This can be used to avoid `auto WarningHandler = ...`.
Similar to llvm-readobj.

Added: 
    

Modified: 
    llvm/tools/llvm-objdump/ELFDump.cpp
    llvm/tools/llvm-objdump/llvm-objdump.cpp
    llvm/tools/llvm-objdump/llvm-objdump.h

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-objdump/ELFDump.cpp b/llvm/tools/llvm-objdump/ELFDump.cpp
index 34fe2ddb0e08ae..0920bb3cde50a4 100644
--- a/llvm/tools/llvm-objdump/ELFDump.cpp
+++ b/llvm/tools/llvm-objdump/ELFDump.cpp
@@ -38,6 +38,7 @@ template <typename ELFT> class ELFDumper : public Dumper {
   void printDynamicSection();
   void printProgramHeaders();
   void printSymbolVersion();
+  void printSymbolVersionDependency(const typename ELFT::Shdr &Sec);
 };
 } // namespace
 
@@ -346,19 +347,13 @@ template <typename ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
 }
 
 template <class ELFT>
-static void printSymbolVersionDependency(StringRef FileName,
-                                         const ELFFile<ELFT> &Obj,
-                                         const typename ELFT::Shdr &Sec) {
+void ELFDumper<ELFT>::printSymbolVersionDependency(
+    const typename ELFT::Shdr &Sec) {
   outs() << "\nVersion References:\n";
-
-  auto WarningHandler = [&](const Twine &Msg) {
-    reportWarning(Msg, FileName);
-    return Error::success();
-  };
   Expected<std::vector<VerNeed>> V =
-      Obj.getVersionDependencies(Sec, WarningHandler);
+      getELFFile().getVersionDependencies(Sec, this->WarningHandler);
   if (!V) {
-    reportWarning(toString(V.takeError()), FileName);
+    reportWarning(toString(V.takeError()), Obj.getFileName());
     return;
   }
 
@@ -420,7 +415,7 @@ template <class ELFT> void ELFDumper<ELFT>::printSymbolVersion() {
     StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
 
     if (Shdr.sh_type == ELF::SHT_GNU_verneed)
-      printSymbolVersionDependency<ELFT>(FileName, Elf, Shdr);
+      printSymbolVersionDependency(Shdr);
     else
       printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
   }

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 5b6e7133bbf053..d5e5b40dc502b5 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -235,13 +235,20 @@ static StringRef ToolName;
 
 std::unique_ptr<BuildIDFetcher> BIDFetcher;
 
+Dumper::Dumper(const object::ObjectFile &O) : O(O) {
+  WarningHandler = [this](const Twine &Msg) {
+    if (Warnings.insert(Msg.str()).second)
+      reportWarning(Msg, this->O.getFileName());
+    return Error::success();
+  };
+}
+
 void Dumper::reportUniqueWarning(Error Err) {
   reportUniqueWarning(toString(std::move(Err)));
 }
 
 void Dumper::reportUniqueWarning(const Twine &Msg) {
-  if (Warnings.insert(StringRef(Msg.str())).second)
-    reportWarning(Msg, O.getFileName());
+  cantFail(WarningHandler(Msg));
 }
 
 static Expected<std::unique_ptr<Dumper>> createDumper(const ObjectFile &Obj) {

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h
index 9f6a03133d7ffa..7778cf6c2784eb 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.h
+++ b/llvm/tools/llvm-objdump/llvm-objdump.h
@@ -18,6 +18,7 @@
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/FormattedStream.h"
+#include <functional>
 #include <memory>
 
 namespace llvm {
@@ -76,8 +77,11 @@ class Dumper {
   const object::ObjectFile &O;
   StringSet<> Warnings;
 
+protected:
+  std::function<Error(const Twine &Msg)> WarningHandler;
+
 public:
-  Dumper(const object::ObjectFile &O) : O(O) {}
+  Dumper(const object::ObjectFile &O);
   virtual ~Dumper() {}
 
   void reportUniqueWarning(Error Err);


        


More information about the llvm-commits mailing list