blob: 8efb94c8ca847c0660baaaf78809f2244860a085 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/YamlWriter.cpp - Writes YAML ----------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Nick Kledzik7735a7d2012-01-04 23:58:17 +000010#include "YamlKeyValues.h"
11
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000012#include "lld/Core/YamlWriter.h"
13#include "lld/Core/Atom.h"
14#include "lld/Core/File.h"
15#include "lld/Core/Reference.h"
16
17#include "llvm/ADT/OwningPtr.h"
18#include "llvm/Support/DataTypes.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/system_error.h"
21
22#include <vector>
23
24namespace lld {
25namespace yaml {
26
27class Handler : public File::AtomHandler {
28public:
Nick Kledzik7735a7d2012-01-04 23:58:17 +000029 Handler(llvm::raw_ostream &out) : _out(out), _firstAtom(true) { }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000030
Nick Kledzik7735a7d2012-01-04 23:58:17 +000031 virtual void doFile(const class File &) { _firstAtom = true; }
32
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000033 virtual void doAtom(const class Atom &atom) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +000034 // add blank line between atoms for readability
35 if ( !_firstAtom )
36 _out << "\n";
37 _firstAtom = false;
38
39 _out << " - "
40 << KeyValues::nameKeyword
41 << ":"
42 << spacePadding(KeyValues::nameKeyword)
43 << atom.name()
44 << "\n";
Nick Kledzik38eec3d2011-12-22 02:38:01 +000045
Nick Kledzik7735a7d2012-01-04 23:58:17 +000046 if ( atom.internalName() != KeyValues::internalNameDefault ) {
47 _out << " "
48 << KeyValues::internalNameKeyword
49 << ":"
50 << spacePadding(KeyValues::internalNameKeyword)
51 << KeyValues::internalName(atom.internalName())
52 << "\n";
53 }
Nick Kledzik38eec3d2011-12-22 02:38:01 +000054
Nick Kledzik7735a7d2012-01-04 23:58:17 +000055 if ( atom.definition() != KeyValues::definitionDefault ) {
56 _out << " "
57 << KeyValues::definitionKeyword
58 << ":"
59 << spacePadding(KeyValues::definitionKeyword)
60 << KeyValues::definition(atom.definition())
61 << "\n";
62 }
63
64 if ( atom.scope() != KeyValues::scopeDefault ) {
65 _out << " "
66 << KeyValues::scopeKeyword
67 << ":"
68 << spacePadding(KeyValues::scopeKeyword)
69 << KeyValues::scope(atom.scope())
70 << "\n";
71 }
72
73 if ( atom.contentType() != KeyValues::contentTypeDefault ) {
74 _out << " "
75 << KeyValues::contentTypeKeyword
76 << ":"
77 << spacePadding(KeyValues::contentTypeKeyword)
78 << KeyValues::contentType(atom.contentType())
79 << "\n";
80 }
81
82 if ( atom.deadStrip() != KeyValues::deadStripKindDefault ) {
83 _out << " "
84 << KeyValues::deadStripKindKeyword
85 << ":"
86 << spacePadding(KeyValues::deadStripKindKeyword)
87 << KeyValues::deadStripKind(atom.deadStrip())
88 << "\n";
89 }
90
91 if ( atom.sectionChoice() != KeyValues::sectionChoiceDefault ) {
92 _out << " "
93 << KeyValues::sectionChoiceKeyword
94 << ":"
95 << spacePadding(KeyValues::sectionChoiceKeyword)
96 << KeyValues::sectionChoice(atom.sectionChoice())
97 << "\n";
98 assert( ! atom.customSectionName().empty() );
99 _out << " "
100 << KeyValues::sectionNameKeyword
101 << ":"
102 << spacePadding(KeyValues::sectionNameKeyword)
103 << atom.customSectionName()
104 << "\n";
105 }
106
107 if ( atom.mergeDuplicates() != KeyValues::mergeDuplicatesDefault ) {
108 _out << " "
109 << KeyValues::mergeDuplicatesKeyword
110 << ":"
111 << spacePadding(KeyValues::mergeDuplicatesKeyword)
112 << KeyValues::mergeDuplicates(atom.mergeDuplicates())
113 << "\n";
114 }
115
116 if ( atom.autoHide() != KeyValues::autoHideDefault ) {
117 _out << " "
118 << KeyValues::autoHideKeyword
119 << ":"
120 << spacePadding(KeyValues::autoHideKeyword)
121 << KeyValues::autoHide(atom.autoHide())
122 << "\n";
123 }
124
125 if ( atom.isThumb() != KeyValues::isThumbDefault ) {
126 _out << " "
127 << KeyValues::isThumbKeyword
128 << ":"
129 << spacePadding(KeyValues::isThumbKeyword)
130 << KeyValues::isThumb(atom.isThumb())
131 << "\n";
132 }
133
134 if ( atom.isAlias() != KeyValues::isAliasDefault ) {
135 _out << " "
136 << KeyValues::isAliasKeyword
137 << ":"
138 << spacePadding(KeyValues::isAliasKeyword)
139 << KeyValues::isAlias(atom.isAlias())
140 << "\n";
141 }
142
143
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000144 if (atom.referencesBegin() != atom.referencesEnd()) {
145 _out << " fixups:\n";
146 for (Reference::iterator it = atom.referencesBegin(),
147 end = atom.referencesEnd(); it != end; ++it) {
148 _out << " - kind: " << it->kind << "\n";
149 _out << " offset: " << it->offsetInAtom << "\n";
150 }
151 }
152
153 }
154
155private:
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000156 // return a string of the correct number of spaces to align value
157 const char* spacePadding(const char* key) {
158 const char* spaces = " ";
159 assert(strlen(spaces) > strlen(key));
160 return &spaces[strlen(key)];
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000161 }
162
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000163
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000164
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000165 llvm::raw_ostream& _out;
166 bool _firstAtom;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000167};
168
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000169void writeObjectText(File &file, llvm::raw_ostream &out) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000170 Handler h(out);
171 out << "---\n";
172 out << "atoms:\n";
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000173 file.forEachAtom(h);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000174 out << "...\n";
175}
176
177} // namespace yaml
178} // namespace lld