Skip to content

Commit 00504e6

Browse files
committed
writeAttrs
1 parent 54aabe2 commit 00504e6

File tree

2 files changed

+66
-33
lines changed

2 files changed

+66
-33
lines changed

source/lib/XML.cpp

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,31 @@ class XMLGenerator
4242
std::string level_;
4343
llvm::raw_ostream* os_ = nullptr;
4444

45-
using Attrs =
46-
std::initializer_list<
47-
std::pair<
48-
llvm::StringRef,
49-
llvm::StringRef>>;
45+
struct Attr
46+
{
47+
llvm::StringRef name;
48+
std::string value;
49+
bool pred;
50+
51+
Attr(
52+
llvm::StringRef name_,
53+
llvm::StringRef value_,
54+
bool pred_ = true) noexcept
55+
: name(name_)
56+
, value(value_)
57+
, pred(pred_)
58+
{
59+
}
60+
61+
Attr(SymbolID USR)
62+
: name("usr")
63+
, value(toBase64(USR))
64+
, pred(USR != EmptySID)
65+
{
66+
}
67+
};
68+
69+
using Attrs = std::initializer_list<Attr>;
5070

5171
void writeAllSymbols();
5272

@@ -89,6 +109,7 @@ class XMLGenerator
89109
void writeTag(llvm::StringRef, Attrs);
90110
void writeTagLine(llvm::StringRef tag, llvm::StringRef value);
91111
void writeTagLine(llvm::StringRef tag, llvm::StringRef value, Attrs);
112+
void writeAttrs(Attrs attrs);
92113
void indent();
93114
void outdent();
94115

@@ -146,7 +167,7 @@ writeAllSymbols()
146167
auto const& I = corpus_->at(id);
147168
writeTag("symbol", {
148169
{ "name", I.getFullyQualifiedName(temp) },
149-
{ "usr", toBase64(I.USR) }
170+
{ I.USR }
150171
});
151172
}
152173
closeTag("all");
@@ -210,7 +231,7 @@ write(
210231

211232
openTag("namespace", {
212233
{ "name", I.Name },
213-
{ "usr", toBase64(I.USR) }
234+
{ I.USR }
214235
});
215236
writeInfo(I);
216237
writeNamespaces(I.Children.Namespaces);
@@ -239,7 +260,7 @@ write(
239260
}
240261
openTag(tag, {
241262
{ "name", I.Name },
242-
{ "usr", toBase64(I.USR) }
263+
{ I.USR }
243264
});
244265
writeSymbolInfo(I);
245266
writeRecords(I.Children.Records);
@@ -259,12 +280,12 @@ write(
259280
openTag("function", {
260281
{ "name", I.Name },
261282
{ "access", toString(I.Access) },
262-
{ "usr", toBase64(I.USR) }
283+
{ I.USR }
263284
});
264285
writeSymbolInfo(I);
265286
writeTag("return", {
266287
{ "name", I.ReturnType.Type.Name },
267-
{ "usr", toString(I.ReturnType.Type.USR) }
288+
{ I.ReturnType.Type.USR }
268289
});
269290

270291
write(I.Params);
@@ -292,7 +313,7 @@ write(
292313

293314
openTag("enum", {
294315
{ "name", I.Name },
295-
{ "usr", toBase64(I.USR) },
316+
{ I.USR }
296317
});
297318
writeInfo(I);
298319
for(auto const& v : I.Members)
@@ -314,7 +335,7 @@ write(
314335

315336
openTag("typedef", {
316337
{ "name", I.Name },
317-
{ "usr", toString(I.USR) }
338+
{ I.USR }
318339
});
319340
writeSymbolInfo(I);
320341
writeTagLine("qualname", I.Underlying.Type.QualName);
@@ -343,7 +364,7 @@ write(FieldTypeInfo const& I)
343364
{ "type", I.Type.Name },
344365
{ "qualname", I.Type.QualName },
345366
{ "reftype", toString(I.Type.RefType) },
346-
{ "usr", toString(I.Type.USR) }
367+
{ I.Type.USR }
347368
});
348369
}
349370

@@ -434,13 +455,10 @@ void
434455
XMLGenerator::
435456
openTag(
436457
llvm::StringRef tag,
437-
Attrs init)
458+
Attrs attrs)
438459
{
439460
*os_ << level_ << '<' << tag;
440-
for(auto const& attr : init)
441-
*os_ <<
442-
' ' << attr.first << '=' <<
443-
"\"" << escape(attr.second) << "\"";
461+
writeAttrs(attrs);
444462
*os_ << ">\n";
445463
indent();
446464
}
@@ -466,13 +484,10 @@ void
466484
XMLGenerator::
467485
writeTag(
468486
llvm::StringRef tag,
469-
Attrs init)
487+
Attrs attrs)
470488
{
471489
*os_ << level_ << "<" << tag;
472-
for(auto const& attr : init)
473-
*os_ <<
474-
' ' << attr.first << '=' <<
475-
"\"" << escape(attr.second) << "\"";
490+
writeAttrs(attrs);
476491
*os_ << "/>\n";
477492
}
478493

@@ -494,17 +509,25 @@ XMLGenerator::
494509
writeTagLine(
495510
llvm::StringRef tag,
496511
llvm::StringRef value,
497-
Attrs init)
512+
Attrs attrs)
498513
{
499-
*os_ << level_ <<
500-
"<" << tag;
501-
for(auto const& attr : init)
502-
*os_ <<
503-
' ' << attr.first << '=' <<
504-
"\"" << escape(attr.second) << "\"";
514+
*os_ << level_ << "<" << tag;
515+
writeAttrs(attrs);
505516
*os_ << ">" << escape(value) << "</" << tag << ">" << "\n";
506517
}
507518

519+
void
520+
XMLGenerator::
521+
writeAttrs(
522+
Attrs attrs)
523+
{
524+
for(auto const& attr : attrs)
525+
if(attr.pred)
526+
*os_ <<
527+
' ' << attr.name << '=' <<
528+
"\"" << escape(attr.value) << "\"";
529+
}
530+
508531
void
509532
XMLGenerator::
510533
indent()

testfiles/1.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
struct T
2-
{
1+
namespace N1 {
32
void f1();
4-
};
3+
int f2();
4+
5+
namespace N2 {
6+
char f3();
7+
void const* f4();
8+
}
9+
10+
struct T {
11+
void f1();
12+
void f2(int);
13+
};
14+
}

0 commit comments

Comments
 (0)