[dsymutil] Out-line the YAML serialization code. NFC
authorFrederic Riss <friss@apple.com>
Fri, 5 Jun 2015 20:27:04 +0000 (20:27 +0000)
committerFrederic Riss <friss@apple.com>
Fri, 5 Jun 2015 20:27:04 +0000 (20:27 +0000)
It will get a bit bigger in an upcoming commit. No need to have all
of that in the header.

Also move parseYAMLDebugMap() to the same place as the serialization
code. This way it will be able to share a private Context object with
it.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239185 91177308-0d34-0410-b5e6-96231b3b80d8

tools/dsymutil/DebugMap.cpp
tools/dsymutil/DebugMap.h
tools/dsymutil/MachODebugMapParser.cpp

index dcbba19b1079fe22fa6bfa9ada5219303dc9903d..5e6cb8c74065523512973bb297c4c2147a3e4a94 100644 (file)
@@ -86,5 +86,114 @@ void DebugMap::print(raw_ostream &OS) const {
 #ifndef NDEBUG
 void DebugMap::dump() const { print(errs()); }
 #endif
+
+ErrorOr<std::unique_ptr<DebugMap>>
+DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
+                            bool Verbose) {
+  auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
+  if (auto Err = ErrOrFile.getError())
+    return Err;
+
+  std::unique_ptr<DebugMap> Res;
+  yaml::Input yin((*ErrOrFile)->getBuffer(), &PrependPath);
+  yin >> Res;
+
+  if (auto EC = yin.error())
+    return EC;
+
+  return std::move(Res);
+}
+}
+
+namespace yaml {
+
+// Normalize/Denormalize between YAML and a DebugMapObject.
+struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
+  YamlDMO(IO &io) {}
+  YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
+  dsymutil::DebugMapObject denormalize(IO &IO);
+
+  std::string Filename;
+  std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
+};
+
+void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
+    mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
+  io.mapRequired("sym", s.first);
+  io.mapRequired("objAddr", s.second.ObjectAddress);
+  io.mapRequired("binAddr", s.second.BinaryAddress);
+  io.mapOptional("size", s.second.Size);
+}
+
+void MappingTraits<dsymutil::DebugMapObject>::mapping(
+    IO &io, dsymutil::DebugMapObject &DMO) {
+  MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
+  io.mapRequired("filename", Norm->Filename);
+  io.mapRequired("symbols", Norm->Entries);
+}
+
+void ScalarTraits<Triple>::output(const Triple &val, void *,
+                                  llvm::raw_ostream &out) {
+  out << val.str();
+}
+
+StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
+  value = Triple(scalar);
+  return StringRef();
+}
+
+size_t
+SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
+    IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
+  return seq.size();
+}
+
+dsymutil::DebugMapObject &
+SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
+    IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
+    size_t index) {
+  if (index >= seq.size()) {
+    seq.resize(index + 1);
+    seq[index].reset(new dsymutil::DebugMapObject);
+  }
+  return *seq[index];
+}
+
+void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
+                                                dsymutil::DebugMap &DM) {
+  io.mapRequired("triple", DM.BinaryTriple);
+  io.mapOptional("objects", DM.Objects);
+}
+
+void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
+    IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
+  if (!DM)
+    DM.reset(new DebugMap());
+  io.mapRequired("triple", DM->BinaryTriple);
+  io.mapOptional("objects", DM->Objects);
+}
+
+MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
+    IO &io, dsymutil::DebugMapObject &Obj) {
+  Filename = Obj.Filename;
+  Entries.reserve(Obj.Symbols.size());
+  for (auto &Entry : Obj.Symbols)
+    Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
+}
+
+dsymutil::DebugMapObject
+MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
+  void *Ctxt = IO.getContext();
+  StringRef PrependPath = *reinterpret_cast<StringRef *>(Ctxt);
+  SmallString<80> Path(PrependPath);
+  sys::path::append(Path, Filename);
+  dsymutil::DebugMapObject Res(Path);
+  for (auto &Entry : Entries) {
+    auto &Mapping = Entry.second;
+    Res.addSymbol(Entry.first, Mapping.ObjectAddress, Mapping.BinaryAddress,
+                  Mapping.Size);
+  }
+  return Res;
+}
 }
 }
index a5dc56e65495f005726c85a5bce882f6355efac9..6f63d7dcfbe08fa32cdb07a9f3f2e37578c16cec 100644 (file)
@@ -98,6 +98,10 @@ public:
 #ifndef NDEBUG
   void dump() const;
 #endif
+
+  /// Read a debug map for \a InputFile.
+  static ErrorOr<std::unique_ptr<DebugMap>>
+  parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
 };
 
 /// \brief The DebugMapObject represents one object file described by
@@ -185,102 +189,37 @@ using namespace llvm::dsymutil;
 
 template <>
 struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
-
-  static void
-  mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
-    io.mapRequired("sym", s.first);
-    io.mapRequired("objAddr", s.second.ObjectAddress);
-    io.mapRequired("binAddr", s.second.BinaryAddress);
-    io.mapOptional("size", s.second.Size);
-  }
-
+  static void mapping(IO &io,
+                      std::pair<std::string, DebugMapObject::SymbolMapping> &s);
   static const bool flow = true;
 };
 
 template <> struct MappingTraits<dsymutil::DebugMapObject> {
-  // Normalize/Denormalize between YAML and a DebugMapObject.
-  struct YamlDMO {
-    YamlDMO(IO &io) {}
-
-    YamlDMO(IO &io, dsymutil::DebugMapObject &Obj) {
-      Filename = Obj.Filename;
-      Entries.reserve(Obj.Symbols.size());
-      for (auto &Entry : Obj.Symbols)
-        Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
-    }
-
-    dsymutil::DebugMapObject denormalize(IO &IO) {
-      void *Ctxt = IO.getContext();
-      StringRef PrependPath = *reinterpret_cast<StringRef*>(Ctxt);
-      SmallString<80> Path(PrependPath);
-      sys::path::append(Path, Filename);
-      dsymutil::DebugMapObject Res(Path);
-      for (auto &Entry : Entries) {
-        auto &Mapping = Entry.second;
-        Res.addSymbol(Entry.first, Mapping.ObjectAddress, Mapping.BinaryAddress,
-                      Mapping.Size);
-      }
-      return Res;
-    }
-
-    std::string Filename;
-    std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
-  };
-
-  static void mapping(IO &io, dsymutil::DebugMapObject &DMO) {
-    MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
-    io.mapRequired("filename", Norm->Filename);
-    io.mapRequired("symbols", Norm->Entries);
-  }
+  struct YamlDMO;
+  static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
 };
 
 template <> struct ScalarTraits<Triple> {
-
-  static void output(const Triple &val, void *, llvm::raw_ostream &out) {
-    out << val.str();
-  }
-
-  static StringRef input(StringRef scalar, void *, Triple &value) {
-    value = Triple(scalar);
-    return StringRef();
-  }
-
+  static void output(const Triple &val, void *, llvm::raw_ostream &out);
+  static StringRef input(StringRef scalar, void *, Triple &value);
   static bool mustQuote(StringRef) { return true; }
 };
 
 template <>
 struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
-
   static size_t
-  size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
-    return seq.size();
-  }
-
+  size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
   static dsymutil::DebugMapObject &
   element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
-          size_t index) {
-    if (index >= seq.size()) {
-      seq.resize(index + 1);
-      seq[index].reset(new dsymutil::DebugMapObject);
-    }
-    return *seq[index];
-  }
+          size_t index);
 };
 
 template <> struct MappingTraits<dsymutil::DebugMap> {
-  static void mapping(IO &io, dsymutil::DebugMap &DM) {
-    io.mapRequired("triple", DM.BinaryTriple);
-    io.mapOptional("objects", DM.Objects);
-  }
+  static void mapping(IO &io, dsymutil::DebugMap &DM);
 };
 
- template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
-  static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
-    if (!DM)
-      DM.reset(new DebugMap());
-    io.mapRequired("triple", DM->BinaryTriple);
-    io.mapOptional("objects", DM->Objects);
-  }
+template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
+  static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
 };
 }
 }
index 14dafb4065b43885f02d939031de97fef65436fe..ad2bf5ffa6f53735f7529be76537db8dc7db9e2b 100644 (file)
@@ -242,31 +242,18 @@ void MachODebugMapParser::loadMainBinarySymbols() {
   }
 }
 
-ErrorOr<std::unique_ptr<DebugMap>>
-parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose) {
-  auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
-  if (auto Err =ErrOrFile.getError())
-    return Err;
-
-  std::unique_ptr<DebugMap> Res;
-  yaml::Input yin((*ErrOrFile)->getBuffer(), &PrependPath);
-  yin >> Res;
-
-  if (auto EC = yin.error())
-    return EC;
-
-  return std::move(Res);
-}
-
 namespace llvm {
 namespace dsymutil {
-llvm::ErrorOr<std::unique_ptr<DebugMap>>
-parseDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose, bool InputIsYAML) {
+
+llvm::ErrorOr<std::unique_ptr<DebugMap>> parseDebugMap(StringRef InputFile,
+                                                       StringRef PrependPath,
+                                                       bool Verbose,
+                                                       bool InputIsYAML) {
   if (!InputIsYAML) {
     MachODebugMapParser Parser(InputFile, PrependPath, Verbose);
     return Parser.parse();
   } else {
-    return parseYAMLDebugMap(InputFile, PrependPath, Verbose);
+    return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
   }
 }
 }