Skip to content

Commit beb99a4

Browse files
authored
Merge pull request #40452 from augusto2112/refl-dump-use-stream
Modify reflection dumping to use std::ostream instead of FILE *
2 parents f5f27d5 + 385d91a commit beb99a4

File tree

10 files changed

+218
-218
lines changed

10 files changed

+218
-218
lines changed

include/swift/Reflection/MetadataSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class MetadataSource {
178178
}
179179

180180
void dump() const;
181-
void dump(FILE *file, unsigned Indent = 0) const;
181+
void dump(std::ostream &stream, unsigned Indent = 0) const;
182182
template <typename Allocator>
183183
static const MetadataSource *decode(Allocator &A, const std::string &str) {
184184
auto begin = str.begin();

include/swift/Reflection/TypeLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class TypeInfo {
145145
bool isBitwiseTakable() const { return BitwiseTakable; }
146146

147147
void dump() const;
148-
void dump(FILE *file, unsigned Indent = 0) const;
148+
void dump(std::ostream &stream, unsigned Indent = 0) const;
149149

150150
// Using the provided reader, inspect our value.
151151
// Return false if we can't inspect value.

include/swift/Reflection/TypeRef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "swift/ABI/MetadataValues.h"
2626
#include "swift/Remote/MetadataReader.h"
2727
#include "swift/Basic/Unreachable.h"
28-
28+
#include <ostream>
2929
namespace swift {
3030
namespace reflection {
3131

@@ -177,7 +177,7 @@ class alignas(8) TypeRef {
177177
}
178178

179179
void dump() const;
180-
void dump(FILE *file, unsigned Indent = 0) const;
180+
void dump(std::ostream &stream, unsigned Indent = 0) const;
181181

182182
/// Build a demangle tree from this TypeRef.
183183
Demangle::NodePointer getDemangling(Demangle::Demangler &Dem) const;

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#include "swift/Reflection/TypeLowering.h"
2525
#include "swift/Reflection/TypeRef.h"
2626
#include "llvm/ADT/Optional.h"
27-
#include <vector>
27+
#include <ostream>
2828
#include <unordered_map>
29+
#include <vector>
2930

3031
namespace swift {
3132
namespace reflection {
@@ -209,7 +210,7 @@ struct ClosureContextInfo {
209210
unsigned NumBindings = 0;
210211

211212
void dump() const;
212-
void dump(FILE *file) const;
213+
void dump(std::ostream &stream) const;
213214
};
214215

215216
struct FieldTypeInfo {
@@ -732,12 +733,12 @@ class TypeRefBuilder {
732733
///
733734

734735
void dumpTypeRef(RemoteRef<char> MangledName,
735-
FILE *file, bool printTypeName = false);
736-
void dumpFieldSection(FILE *file);
737-
void dumpAssociatedTypeSection(FILE *file);
738-
void dumpBuiltinTypeSection(FILE *file);
739-
void dumpCaptureSection(FILE *file);
740-
void dumpAllSections(FILE *file);
736+
std::ostream &stream, bool printTypeName = false);
737+
void dumpFieldSection(std::ostream &stream);
738+
void dumpAssociatedTypeSection(std::ostream &stream);
739+
void dumpBuiltinTypeSection(std::ostream &stream);
740+
void dumpCaptureSection(std::ostream &stream);
741+
void dumpAllSections(std::ostream &stream);
741742
};
742743

743744

stdlib/public/Reflection/MetadataSource.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,50 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Reflection/MetadataSource.h"
14+
#include <iostream>
1415

1516
using namespace swift;
1617
using namespace reflection;
1718

1819
class PrintMetadataSource
1920
: public MetadataSourceVisitor<PrintMetadataSource, void> {
20-
FILE *file;
21+
std::ostream &stream;
2122
unsigned Indent;
2223

23-
FILE * indent(unsigned Amount) {
24+
std::ostream &indent(unsigned Amount) {
2425
for (unsigned i = 0; i < Amount; ++i)
25-
fprintf(file, " ");
26-
return file;
26+
stream << " ";
27+
return stream;
2728
}
2829

29-
FILE * printHeader(std::string Name) {
30-
fprintf(indent(Indent), "(%s", Name.c_str());
31-
return file;
30+
std::ostream &printHeader(std::string Name) {
31+
indent(Indent) << "(" << Name;
32+
return stream;
3233
}
3334

34-
FILE * printField(std::string name, std::string value) {
35+
std::ostream &printField(std::string name, std::string value) {
3536
if (!name.empty())
36-
fprintf(file, " %s=%s", name.c_str(), value.c_str());
37+
stream << " " << name << "=" << value;
3738
else
38-
fprintf(file, " %s", value.c_str());
39-
return file;
39+
stream << " " << value;
40+
return stream;
4041
}
4142

4243
void printRec(const MetadataSource *MS) {
43-
fprintf(file, "\n");
44+
stream << "\n";
4445

4546
Indent += 2;
4647
visit(MS);
4748
Indent -= 2;
4849
}
4950

5051
void closeForm() {
51-
fprintf(file, ")");
52+
stream << ")";
5253
}
5354

5455
public:
55-
PrintMetadataSource(FILE *file, unsigned Indent)
56-
: file(file), Indent(Indent) {}
56+
PrintMetadataSource(std::ostream &stream, unsigned Indent)
57+
: stream(stream), Indent(Indent) {}
5758

5859
void
5960
visitClosureBindingMetadataSource(const ClosureBindingMetadataSource *CB) {
@@ -96,11 +97,9 @@ class PrintMetadataSource
9697
}
9798
};
9899

99-
void MetadataSource::dump() const {
100-
dump(stderr, 0);
101-
}
100+
void MetadataSource::dump() const { dump(std::cerr, 0); }
102101

103-
void MetadataSource::dump(FILE *file, unsigned Indent) const {
104-
PrintMetadataSource(file, Indent).visit(this);
105-
fprintf(file, "\n");
102+
void MetadataSource::dump(std::ostream &stream, unsigned Indent) const {
103+
PrintMetadataSource(stream, Indent).visit(this);
104+
stream << "\n";
106105
}

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/Reflection/TypeRef.h"
2525
#include "swift/Reflection/TypeRefBuilder.h"
2626
#include "swift/Basic/Unreachable.h"
27+
#include <iostream>
2728

2829
#ifdef DEBUG_TYPE_LOWERING
2930
#define DEBUG_LOG(expr) expr;
@@ -35,36 +36,36 @@ namespace swift {
3536
namespace reflection {
3637

3738
void TypeInfo::dump() const {
38-
dump(stderr);
39+
dump(std::cerr);
3940
}
4041

4142
namespace {
4243

4344
class PrintTypeInfo {
44-
FILE *file;
45+
std::ostream &stream;
4546
unsigned Indent;
4647

47-
FILE * &indent(unsigned Amount) {
48+
std::ostream &indent(unsigned Amount) {
4849
for (unsigned i = 0; i < Amount; ++i)
49-
fprintf(file, " ");
50-
return file;
50+
stream << " ";
51+
return stream;
5152
}
5253

53-
FILE * &printHeader(const std::string &name) {
54-
fprintf(indent(Indent), "(%s", name.c_str());
55-
return file;
54+
std::ostream &printHeader(const std::string &name) {
55+
indent(Indent) << "(" << name;
56+
return stream;
5657
}
5758

58-
FILE * &printField(const std::string &name, const std::string &value) {
59+
std::ostream &printField(const std::string &name, const std::string &value) {
5960
if (!name.empty())
60-
fprintf(file, " %s=%s", name.c_str(), value.c_str());
61+
stream << " " << name << "=" << value;
6162
else
62-
fprintf(file, " %s", value.c_str());
63-
return file;
63+
stream << " " << name;
64+
return stream;
6465
}
6566

6667
void printRec(const TypeInfo &TI) {
67-
fprintf(file, "\n");
68+
stream << "\n";
6869

6970
Indent += 2;
7071
print(TI);
@@ -82,13 +83,13 @@ class PrintTypeInfo {
8283
void printFields(const RecordTypeInfo &TI) {
8384
Indent += 2;
8485
for (auto Field : TI.getFields()) {
85-
fprintf(file, "\n");
86+
stream << "\n";
8687
printHeader("field");
8788
if (!Field.Name.empty())
8889
printField("name", Field.Name);
8990
printField("offset", std::to_string(Field.Offset));
9091
printRec(Field.TI);
91-
fprintf(file, ")");
92+
stream << ")";
9293
}
9394
Indent -= 2;
9495
}
@@ -98,7 +99,7 @@ class PrintTypeInfo {
9899
int Index = -1;
99100
for (auto Case : TI.getCases()) {
100101
Index += 1;
101-
fprintf(file, "\n");
102+
stream << "\n";
102103
printHeader("case");
103104
if (!Case.Name.empty())
104105
printField("name", Case.Name);
@@ -107,26 +108,26 @@ class PrintTypeInfo {
107108
printField("offset", std::to_string(Case.Offset));
108109
printRec(Case.TI);
109110
}
110-
fprintf(file, ")");
111+
stream << ")";
111112
}
112113
Indent -= 2;
113114
}
114115

115116
public:
116-
PrintTypeInfo(FILE *file, unsigned Indent)
117-
: file(file), Indent(Indent) {}
117+
PrintTypeInfo(std::ostream &stream, unsigned Indent)
118+
: stream(stream), Indent(Indent) {}
118119

119120
void print(const TypeInfo &TI) {
120121
switch (TI.getKind()) {
121122
case TypeInfoKind::Invalid:
122123
printHeader("invalid");
123-
fprintf(file, ")");
124+
stream << ")";
124125
return;
125126

126127
case TypeInfoKind::Builtin:
127128
printHeader("builtin");
128129
printBasic(TI);
129-
fprintf(file, ")");
130+
stream << ")";
130131
return;
131132

132133
case TypeInfoKind::Record: {
@@ -165,7 +166,7 @@ class PrintTypeInfo {
165166
}
166167
printBasic(TI);
167168
printFields(RecordTI);
168-
fprintf(file, ")");
169+
stream << ")";
169170
return;
170171
}
171172

@@ -184,7 +185,7 @@ class PrintTypeInfo {
184185
}
185186
printBasic(TI);
186187
printCases(EnumTI);
187-
fprintf(file, ")");
188+
stream << ")";
188189
return;
189190
}
190191

@@ -207,7 +208,7 @@ class PrintTypeInfo {
207208
break;
208209
}
209210

210-
fprintf(file, ")");
211+
stream << ")";
211212
return;
212213
}
213214
}
@@ -218,9 +219,9 @@ class PrintTypeInfo {
218219

219220
} // end anonymous namespace
220221

221-
void TypeInfo::dump(FILE *file, unsigned Indent) const {
222-
PrintTypeInfo(file, Indent).print(*this);
223-
fprintf(file, "\n");
222+
void TypeInfo::dump(std::ostream &stream, unsigned Indent) const {
223+
PrintTypeInfo(stream, Indent).print(*this);
224+
stream << "\n";
224225
}
225226

226227
BuiltinTypeInfo::BuiltinTypeInfo(TypeRefBuilder &builder,

0 commit comments

Comments
 (0)