summaryrefslogtreecommitdiffstats
path: root/lld/lib/Core/YamlWriter.cpp
blob: 8efb94c8ca847c0660baaaf78809f2244860a085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//===- Core/YamlWriter.cpp - Writes YAML ----------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "YamlKeyValues.h"

#include "lld/Core/YamlWriter.h"
#include "lld/Core/Atom.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"

#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"

#include <vector>

namespace lld {
namespace yaml {

class Handler : public File::AtomHandler {
public:
  Handler(llvm::raw_ostream &out) : _out(out), _firstAtom(true) { }

  virtual void doFile(const class File &) { _firstAtom = true; }
  
  virtual void doAtom(const class Atom &atom) {
      // add blank line between atoms for readability
      if ( !_firstAtom )
        _out << "\n";
      _firstAtom = false;
        
      _out  << "    - "
            << KeyValues::nameKeyword
            << ":"
            << spacePadding(KeyValues::nameKeyword)
            << atom.name() 
            << "\n";
    
    if ( atom.internalName() != KeyValues::internalNameDefault ) {
      _out  << "      " 
            << KeyValues::internalNameKeyword 
            << ":"
            << spacePadding(KeyValues::internalNameKeyword)
            << KeyValues::internalName(atom.internalName()) 
            << "\n";
    }
    
    if ( atom.definition() != KeyValues::definitionDefault ) {
      _out  << "      " 
            << KeyValues::definitionKeyword 
            << ":"
            << spacePadding(KeyValues::definitionKeyword)
            << KeyValues::definition(atom.definition()) 
            << "\n";
    }
    
    if ( atom.scope() != KeyValues::scopeDefault ) {
      _out  << "      " 
            << KeyValues::scopeKeyword 
            << ":"
            << spacePadding(KeyValues::scopeKeyword)
            << KeyValues::scope(atom.scope()) 
            << "\n";
    }
    
    if ( atom.contentType() != KeyValues::contentTypeDefault ) {
      _out  << "      " 
            << KeyValues::contentTypeKeyword 
            << ":"
            << spacePadding(KeyValues::contentTypeKeyword)
            << KeyValues::contentType(atom.contentType()) 
            << "\n";
    }

    if ( atom.deadStrip() != KeyValues::deadStripKindDefault ) {
      _out  << "      " 
            << KeyValues::deadStripKindKeyword 
            << ":"
            << spacePadding(KeyValues::deadStripKindKeyword)
            << KeyValues::deadStripKind(atom.deadStrip()) 
            << "\n";
    }

    if ( atom.sectionChoice() != KeyValues::sectionChoiceDefault ) {
      _out  << "      " 
            << KeyValues::sectionChoiceKeyword 
            << ":"
            << spacePadding(KeyValues::sectionChoiceKeyword)
            << KeyValues::sectionChoice(atom.sectionChoice()) 
            << "\n";
      assert( ! atom.customSectionName().empty() );
      _out  << "      " 
            << KeyValues::sectionNameKeyword 
            << ":"
            << spacePadding(KeyValues::sectionNameKeyword)
            << atom.customSectionName()
            << "\n";
    }

    if ( atom.mergeDuplicates() != KeyValues::mergeDuplicatesDefault ) {
      _out  << "      " 
            << KeyValues::mergeDuplicatesKeyword 
            << ":"
            << spacePadding(KeyValues::mergeDuplicatesKeyword)
            << KeyValues::mergeDuplicates(atom.mergeDuplicates()) 
            << "\n";
    }

    if ( atom.autoHide() != KeyValues::autoHideDefault ) {
      _out  << "      " 
            << KeyValues::autoHideKeyword 
            << ":"
            << spacePadding(KeyValues::autoHideKeyword)
            << KeyValues::autoHide(atom.autoHide()) 
            << "\n";
    }

    if ( atom.isThumb() != KeyValues::isThumbDefault ) {
      _out  << "      " 
            << KeyValues::isThumbKeyword 
            << ":"
            << spacePadding(KeyValues::isThumbKeyword)
            << KeyValues::isThumb(atom.isThumb()) 
            << "\n";
    }

    if ( atom.isAlias() != KeyValues::isAliasDefault ) {
      _out  << "      " 
            << KeyValues::isAliasKeyword 
            << ":"
            << spacePadding(KeyValues::isAliasKeyword)
            << KeyValues::isAlias(atom.isAlias()) 
            << "\n";
    }

     
    if (atom.referencesBegin() != atom.referencesEnd()) {
      _out << "      fixups:\n";
      for (Reference::iterator it = atom.referencesBegin(),
           end = atom.referencesEnd(); it != end; ++it) {
        _out << "      - kind:      " << it->kind << "\n";
        _out << "        offset:    " << it->offsetInAtom << "\n";
      }
    }

  }

private:
  // return a string of the correct number of spaces to align value
  const char* spacePadding(const char* key) {
    const char* spaces = "                  ";
    assert(strlen(spaces) > strlen(key));
    return &spaces[strlen(key)];
  }



  llvm::raw_ostream&  _out;
  bool                _firstAtom;
};

void writeObjectText(File &file, llvm::raw_ostream &out) {
  Handler h(out);
  out << "---\n";
  out << "atoms:\n";
  file.forEachAtom(h);
  out << "...\n";
}

} // namespace yaml
} // namespace lld
OpenPOWER on IntegriCloud