Skip to content

Commit ddde8be

Browse files
committed
[llvm-dwarfdump] --statistics: switch to json::OStream. NFC
Then it is trivial to make the output indented (the second parameter of json::OStream::OStream specifies the indentation). Reviewed By: jhenderson, echristo Differential Revision: https://reviews.llvm.org/D86045
1 parent 58c08c4 commit ddde8be

File tree

1 file changed

+69
-62
lines changed

1 file changed

+69
-62
lines changed

llvm/tools/llvm-dwarfdump/Statistics.cpp

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -469,46 +469,52 @@ static void collectStatsRecursive(DWARFDie Die, std::string FnPrefix,
469469
/// Print machine-readable output.
470470
/// The machine-readable format is single-line JSON output.
471471
/// \{
472-
static void printDatum(raw_ostream &OS, const char *Key, json::Value Value) {
473-
OS << ",\"" << Key << "\":" << Value;
472+
static void printDatum(json::OStream &J, const char *Key, json::Value Value) {
473+
J.attribute(Key, Value);
474474
LLVM_DEBUG(llvm::dbgs() << Key << ": " << Value << '\n');
475475
}
476476

477-
static void printLocationStats(raw_ostream &OS, const char *Key,
477+
static void printLocationStats(json::OStream &J, const char *Key,
478478
std::vector<unsigned> &LocationStats) {
479-
OS << ",\"" << Key << " with 0% of parent scope covered by DW_AT_location\":"
480-
<< LocationStats[0];
479+
J.attribute(
480+
(Twine(Key) + " with 0% of parent scope covered by DW_AT_location").str(),
481+
LocationStats[0]);
481482
LLVM_DEBUG(
482483
llvm::dbgs() << Key
483484
<< " with 0% of parent scope covered by DW_AT_location: \\"
484485
<< LocationStats[0] << '\n');
485-
OS << ",\"" << Key
486-
<< " with (0%,10%) of parent scope covered by DW_AT_location\":"
487-
<< LocationStats[1];
486+
J.attribute(
487+
(Twine(Key) + " with (0%,10%) of parent scope covered by DW_AT_location")
488+
.str(),
489+
LocationStats[1]);
488490
LLVM_DEBUG(llvm::dbgs()
489491
<< Key
490492
<< " with (0%,10%) of parent scope covered by DW_AT_location: "
491493
<< LocationStats[1] << '\n');
492494
for (unsigned i = 2; i < NumOfCoverageCategories - 1; ++i) {
493-
OS << ",\"" << Key << " with [" << (i - 1) * 10 << "%," << i * 10
494-
<< "%) of parent scope covered by DW_AT_location\":" << LocationStats[i];
495+
J.attribute((Twine(Key) + " with [" + Twine((i - 1) * 10) + "%," +
496+
Twine(i * 10) + "%) of parent scope covered by DW_AT_location")
497+
.str(),
498+
LocationStats[i]);
495499
LLVM_DEBUG(llvm::dbgs()
496500
<< Key << " with [" << (i - 1) * 10 << "%," << i * 10
497501
<< "%) of parent scope covered by DW_AT_location: "
498502
<< LocationStats[i]);
499503
}
500-
OS << ",\"" << Key
501-
<< " with 100% of parent scope covered by DW_AT_location\":"
502-
<< LocationStats[NumOfCoverageCategories - 1];
504+
J.attribute(
505+
(Twine(Key) + " with 100% of parent scope covered by DW_AT_location")
506+
.str(),
507+
LocationStats[NumOfCoverageCategories - 1]);
503508
LLVM_DEBUG(
504509
llvm::dbgs() << Key
505510
<< " with 100% of parent scope covered by DW_AT_location: "
506511
<< LocationStats[NumOfCoverageCategories - 1]);
507512
}
508513

509-
static void printSectionSizes(raw_ostream &OS, const SectionSizes &Sizes) {
514+
static void printSectionSizes(json::OStream &J, const SectionSizes &Sizes) {
510515
for (const auto &DebugSec : Sizes.DebugSectionSizes)
511-
OS << ",\"#bytes in " << DebugSec.getKey() << "\":" << DebugSec.getValue();
516+
J.attribute((Twine("#bytes in ") + DebugSec.getKey()).str(),
517+
int64_t(DebugSec.getValue()));
512518
}
513519

514520
/// \}
@@ -587,101 +593,102 @@ bool dwarfdump::collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
587593

588594
// Print summary.
589595
OS.SetBufferSize(1024);
590-
OS << "{\"version\":" << Version;
596+
json::OStream J(OS);
597+
J.objectBegin();
598+
J.attribute("version", Version);
591599
LLVM_DEBUG(llvm::dbgs() << "Variable location quality metrics\n";
592600
llvm::dbgs() << "---------------------------------\n");
593601

594-
printDatum(OS, "file", Filename.str());
595-
printDatum(OS, "format", FormatName);
602+
printDatum(J, "file", Filename.str());
603+
printDatum(J, "format", FormatName);
596604

597-
printDatum(OS, "#functions", NumFunctions);
598-
printDatum(OS, "#functions with location", NumFuncsWithSrcLoc);
599-
printDatum(OS, "#inlined functions", NumInlinedFunctions);
600-
printDatum(OS, "#inlined functions with abstract origins",
601-
NumAbstractOrigins);
605+
printDatum(J, "#functions", NumFunctions);
606+
printDatum(J, "#functions with location", NumFuncsWithSrcLoc);
607+
printDatum(J, "#inlined functions", NumInlinedFunctions);
608+
printDatum(J, "#inlined functions with abstract origins", NumAbstractOrigins);
602609

603610
// This includes local variables and formal parameters.
604-
printDatum(OS, "#unique source variables", VarParamUnique);
605-
printDatum(OS, "#source variables", VarParamTotal);
606-
printDatum(OS, "#source variables with location", VarParamWithLoc);
611+
printDatum(J, "#unique source variables", VarParamUnique);
612+
printDatum(J, "#source variables", VarParamTotal);
613+
printDatum(J, "#source variables with location", VarParamWithLoc);
607614

608-
printDatum(OS, "#call site entries", GlobalStats.CallSiteEntries);
609-
printDatum(OS, "#call site DIEs", GlobalStats.CallSiteDIEs);
610-
printDatum(OS, "#call site parameter DIEs", GlobalStats.CallSiteParamDIEs);
615+
printDatum(J, "#call site entries", GlobalStats.CallSiteEntries);
616+
printDatum(J, "#call site DIEs", GlobalStats.CallSiteDIEs);
617+
printDatum(J, "#call site parameter DIEs", GlobalStats.CallSiteParamDIEs);
611618

612-
printDatum(OS, "sum_all_variables(#bytes in parent scope)",
619+
printDatum(J, "sum_all_variables(#bytes in parent scope)",
613620
GlobalStats.ScopeBytes);
614-
printDatum(OS,
621+
printDatum(J,
615622
"sum_all_variables(#bytes in parent scope covered by "
616623
"DW_AT_location)",
617624
GlobalStats.ScopeBytesCovered);
618-
printDatum(OS,
625+
printDatum(J,
619626
"sum_all_variables(#bytes in parent scope covered by "
620627
"DW_OP_entry_value)",
621628
GlobalStats.ScopeEntryValueBytesCovered);
622629

623-
printDatum(OS, "sum_all_params(#bytes in parent scope)",
630+
printDatum(J, "sum_all_params(#bytes in parent scope)",
624631
GlobalStats.ParamScopeBytes);
625-
printDatum(
626-
OS,
627-
"sum_all_params(#bytes in parent scope covered by DW_AT_location)",
628-
GlobalStats.ParamScopeBytesCovered);
629-
printDatum(OS,
632+
printDatum(J,
633+
"sum_all_params(#bytes in parent scope covered by DW_AT_location)",
634+
GlobalStats.ParamScopeBytesCovered);
635+
printDatum(J,
630636
"sum_all_params(#bytes in parent scope covered by "
631637
"DW_OP_entry_value)",
632638
GlobalStats.ParamScopeEntryValueBytesCovered);
633639

634-
printDatum(OS, "sum_all_local_vars(#bytes in parent scope)",
640+
printDatum(J, "sum_all_local_vars(#bytes in parent scope)",
635641
GlobalStats.LocalVarScopeBytes);
636-
printDatum(OS,
642+
printDatum(J,
637643
"sum_all_local_vars(#bytes in parent scope covered by "
638644
"DW_AT_location)",
639645
GlobalStats.LocalVarScopeBytesCovered);
640-
printDatum(OS,
646+
printDatum(J,
641647
"sum_all_local_vars(#bytes in parent scope covered by "
642648
"DW_OP_entry_value)",
643649
GlobalStats.LocalVarScopeEntryValueBytesCovered);
644650

645-
printDatum(OS, "#bytes witin functions", GlobalStats.FunctionSize);
646-
printDatum(OS, "#bytes witin inlined functions",
651+
printDatum(J, "#bytes witin functions", GlobalStats.FunctionSize);
652+
printDatum(J, "#bytes witin inlined functions",
647653
GlobalStats.InlineFunctionSize);
648654

649655
// Print the summary for formal parameters.
650-
printDatum(OS, "#params", ParamTotal);
651-
printDatum(OS, "#params with source location", ParamWithSrcLoc);
652-
printDatum(OS, "#params with type", ParamWithType);
653-
printDatum(OS, "#params with binary location", ParamWithLoc);
656+
printDatum(J, "#params", ParamTotal);
657+
printDatum(J, "#params with source location", ParamWithSrcLoc);
658+
printDatum(J, "#params with type", ParamWithType);
659+
printDatum(J, "#params with binary location", ParamWithLoc);
654660

655661
// Print the summary for local variables.
656-
printDatum(OS, "#local vars", LocalVarTotal);
657-
printDatum(OS, "#local vars with source location", LocalVarWithSrcLoc);
658-
printDatum(OS, "#local vars with type", LocalVarWithType);
659-
printDatum(OS, "#local vars with binary location", LocalVarWithLoc);
662+
printDatum(J, "#local vars", LocalVarTotal);
663+
printDatum(J, "#local vars with source location", LocalVarWithSrcLoc);
664+
printDatum(J, "#local vars with type", LocalVarWithType);
665+
printDatum(J, "#local vars with binary location", LocalVarWithLoc);
660666

661667
// Print the debug section sizes.
662-
printSectionSizes(OS, Sizes);
668+
printSectionSizes(J, Sizes);
663669

664670
// Print the location statistics for variables (includes local variables
665671
// and formal parameters).
666-
printDatum(OS, "#variables processed by location statistics",
672+
printDatum(J, "#variables processed by location statistics",
667673
LocStats.NumVarParam);
668-
printLocationStats(OS, "#variables", LocStats.VarParamLocStats);
669-
printLocationStats(OS, "#variables - entry values",
674+
printLocationStats(J, "#variables", LocStats.VarParamLocStats);
675+
printLocationStats(J, "#variables - entry values",
670676
LocStats.VarParamNonEntryValLocStats);
671677

672678
// Print the location statistics for formal parameters.
673-
printDatum(OS, "#params processed by location statistics", LocStats.NumParam);
674-
printLocationStats(OS, "#params", LocStats.ParamLocStats);
675-
printLocationStats(OS, "#params - entry values",
679+
printDatum(J, "#params processed by location statistics", LocStats.NumParam);
680+
printLocationStats(J, "#params", LocStats.ParamLocStats);
681+
printLocationStats(J, "#params - entry values",
676682
LocStats.ParamNonEntryValLocStats);
677683

678684
// Print the location statistics for local variables.
679-
printDatum(OS, "#local vars processed by location statistics",
685+
printDatum(J, "#local vars processed by location statistics",
680686
LocStats.NumVar);
681-
printLocationStats(OS, "#local vars", LocStats.LocalVarLocStats);
682-
printLocationStats(OS, "#local vars - entry values",
687+
printLocationStats(J, "#local vars", LocStats.LocalVarLocStats);
688+
printLocationStats(J, "#local vars - entry values",
683689
LocStats.LocalVarNonEntryValLocStats);
684-
OS << "}\n";
690+
J.objectEnd();
691+
OS << '\n';
685692
LLVM_DEBUG(
686693
llvm::dbgs() << "Total Availability: "
687694
<< (int)std::round((VarParamWithLoc * 100.0) / VarParamTotal)

0 commit comments

Comments
 (0)