xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/ArchiveEmitter.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===- ArchiveEmitter.cpp ---------------------------- --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ObjectYAML/ArchiveYAML.h"
10 #include "llvm/ObjectYAML/yaml2obj.h"
11 #include "llvm/Support/Error.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 using namespace llvm;
15 using namespace ArchYAML;
16 
17 namespace llvm {
18 namespace yaml {
19 
20 bool yaml2archive(ArchYAML::Archive &Doc, raw_ostream &Out, ErrorHandler EH) {
21   Out.write(Doc.Magic.data(), Doc.Magic.size());
22 
23   if (Doc.Content) {
24     Doc.Content->writeAsBinary(Out);
25     return true;
26   }
27 
28   if (!Doc.Members)
29     return true;
30 
31   auto WriteField = [&](StringRef Field, uint8_t Size) {
32     Out.write(Field.data(), Field.size());
33     for (size_t I = Field.size(); I != Size; ++I)
34       Out.write(' ');
35   };
36 
37   for (const Archive::Child &C : *Doc.Members) {
38     for (auto &P : C.Fields)
39       WriteField(P.second.Value, P.second.MaxLength);
40 
41     if (C.Content)
42       C.Content->writeAsBinary(Out);
43     if (C.PaddingByte)
44       Out.write(*C.PaddingByte);
45   }
46 
47   return true;
48 }
49 
50 } // namespace yaml
51 } // namespace llvm
52