Skip to content

Commit

Permalink
Merge pull request #256 from chaoticgd/compact_json
Browse files Browse the repository at this point in the history
Add --compact option to omit whitespace in stdump JSON output
  • Loading branch information
chaoticgd authored Oct 23, 2024
2 parents 6d8fb38 + 37d80da commit b428bf0
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Mac builds compiled for x64 processors are now provided in addition to the arm64 builds.
- demangle: A newline is now printed at the end of the output.
- stdump: The `functions` and `globals` subcommands now output a header comment like the `types` command.
- stdump: Added `--compact` option to omit whitespace and newlines from JSON output.
- stdump: The output format of the `files` subcommand has been updated.
- stdump: A crash in the command line parser has been fixed.

Expand Down
9 changes: 8 additions & 1 deletion src/ccc/ast_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace ccc::ast {

void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& database)
template <typename Writer>
void write_json(Writer& json, const Node* ptr, const SymbolDatabase& database)
{
CCC_ASSERT(ptr);
const Node& node = *ptr;
Expand Down Expand Up @@ -197,4 +198,10 @@ void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& databas
json.EndObject();
}

template void write_json<rapidjson::Writer<rapidjson::StringBuffer>>(
rapidjson::Writer<rapidjson::StringBuffer>& json, const Node* ptr, const SymbolDatabase& database);

template void write_json<rapidjson::PrettyWriter<rapidjson::StringBuffer>>(
rapidjson::PrettyWriter<rapidjson::StringBuffer>& json, const Node* ptr, const SymbolDatabase& database);

}
13 changes: 4 additions & 9 deletions src/ccc/ast_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@
#include <rapidjson/prettywriter.h>

#include "ast.h"
#include "symbol_database.h"

namespace ccc {
namespace ccc::ast {

class SymbolDatabase;
template <typename Writer>
void write_json(Writer& json, const Node* ptr, const SymbolDatabase& database);

using JsonWriter = rapidjson::PrettyWriter<rapidjson::StringBuffer>;

namespace ast {

void write_json(JsonWriter& json, const Node* ptr, const SymbolDatabase& database);

}
}
91 changes: 59 additions & 32 deletions src/ccc/symbol_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@

#include "symbol_json.h"

#include "ast_json.h"
#include "registers.h"

namespace ccc {

const u32 JSON_FORMAT_VERSION = 15;

template <typename SymbolType>
template <typename Writer, typename SymbolType>
static void write_symbol_list(
JsonWriter& json,
Writer& json,
const SymbolList<SymbolType>& list,
const SymbolDatabase& database,
const std::set<SymbolSourceHandle>* sources);

static void write_json(JsonWriter& json, const GlobalStorage& storage, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const RegisterStorage& storage, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const StackStorage& storage, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const DataType& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const Function& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const GlobalVariable& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const Label& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const LocalVariable& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const Module& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const ParameterVariable& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const Section& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const SourceFile& symbol, const SymbolDatabase& database);
static void write_json(JsonWriter& json, const SymbolSource& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const GlobalStorage& storage, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const RegisterStorage& storage, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const StackStorage& storage, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const DataType& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const Function& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const GlobalVariable& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const Label& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const LocalVariable& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const Module& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const ParameterVariable& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const Section& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const SourceFile& symbol, const SymbolDatabase& database);
template <typename Writer> static void write_json(Writer& json, const SymbolSource& symbol, const SymbolDatabase& database);

template <typename Writer>
void write_json(
JsonWriter& json,
Writer& json,
const SymbolDatabase& database,
const char* application_name,
const std::set<SymbolSourceHandle>* sources)
Expand All @@ -50,17 +52,29 @@ void write_json(
#define CCC_X(SymbolType, symbol_list) \
if (!std::is_same_v<SymbolType, SymbolSource>) { \
json.Key(#symbol_list); \
write_symbol_list<SymbolType>(json, database.symbol_list, database, sources); \
write_symbol_list<Writer, SymbolType>(json, database.symbol_list, database, sources); \
}
CCC_FOR_EACH_SYMBOL_TYPE_DO_X
#undef CCC_X

json.EndObject();
}

template <typename SymbolType>
template void write_json<rapidjson::Writer<rapidjson::StringBuffer>>(
rapidjson::Writer<rapidjson::StringBuffer>& json,
const SymbolDatabase& database,
const char* application_name,
const std::set<SymbolSourceHandle>* sources);

template void write_json<rapidjson::PrettyWriter<rapidjson::StringBuffer>>(
rapidjson::PrettyWriter<rapidjson::StringBuffer>& json,
const SymbolDatabase& database,
const char* application_name,
const std::set<SymbolSourceHandle>* sources);

template <typename Writer, typename SymbolType>
static void write_symbol_list(
JsonWriter& json,
Writer& json,
const SymbolList<SymbolType>& list,
const SymbolDatabase& database,
const std::set<SymbolSourceHandle>* sources)
Expand Down Expand Up @@ -105,7 +119,8 @@ static void write_symbol_list(
json.EndArray();
}

static void write_json(JsonWriter& json, const GlobalStorage& storage, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const GlobalStorage& storage, const SymbolDatabase& database)
{
json.Key("storage");
json.StartObject();
Expand All @@ -116,7 +131,8 @@ static void write_json(JsonWriter& json, const GlobalStorage& storage, const Sym
json.EndObject();
}

static void write_json(JsonWriter& json, const RegisterStorage& storage, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const RegisterStorage& storage, const SymbolDatabase& database)
{
json.Key("storage");
json.StartObject();
Expand All @@ -137,7 +153,8 @@ static void write_json(JsonWriter& json, const RegisterStorage& storage, const S
json.EndObject();
}

static void write_json(JsonWriter& json, const StackStorage& storage, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const StackStorage& storage, const SymbolDatabase& database)
{
json.Key("storage");
json.StartObject();
Expand All @@ -148,7 +165,8 @@ static void write_json(JsonWriter& json, const StackStorage& storage, const Symb
json.EndObject();
}

static void write_json(JsonWriter& json, const DataType& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const DataType& symbol, const SymbolDatabase& database)
{
if (symbol.files.empty()) {
json.Key("files");
Expand All @@ -160,7 +178,8 @@ static void write_json(JsonWriter& json, const DataType& symbol, const SymbolDat
}
}

static void write_json(JsonWriter& json, const Function& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const Function& symbol, const SymbolDatabase& database)
{
if (!symbol.relative_path.empty()) {
json.Key("relative_path");
Expand Down Expand Up @@ -236,7 +255,8 @@ static void write_json(JsonWriter& json, const Function& symbol, const SymbolDat
}
}

static void write_json(JsonWriter& json, const GlobalVariable& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const GlobalVariable& symbol, const SymbolDatabase& database)
{
write_json(json, symbol.storage, database);

Expand All @@ -246,9 +266,11 @@ static void write_json(JsonWriter& json, const GlobalVariable& symbol, const Sym
}
}

static void write_json(JsonWriter& json, const Label& symbol, const SymbolDatabase& database) {}
template <typename Writer>
static void write_json(Writer& json, const Label& symbol, const SymbolDatabase& database) {}

static void write_json(JsonWriter& json, const LocalVariable& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const LocalVariable& symbol, const SymbolDatabase& database)
{
if (const GlobalStorage* storage = std::get_if<GlobalStorage>(&symbol.storage)) {
write_json(json, *storage, database);
Expand All @@ -271,9 +293,11 @@ static void write_json(JsonWriter& json, const LocalVariable& symbol, const Symb
}
}

static void write_json(JsonWriter& json, const Module& symbol, const SymbolDatabase& database) {}
template <typename Writer>
static void write_json(Writer& json, const Module& symbol, const SymbolDatabase& database) {}

static void write_json(JsonWriter& json, const ParameterVariable& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const ParameterVariable& symbol, const SymbolDatabase& database)
{
if (const RegisterStorage* storage = std::get_if<RegisterStorage>(&symbol.storage)) {
write_json(json, *storage, database);
Expand All @@ -284,9 +308,11 @@ static void write_json(JsonWriter& json, const ParameterVariable& symbol, const
}
}

static void write_json(JsonWriter& json, const Section& symbol, const SymbolDatabase& database) {}
template <typename Writer>
static void write_json(Writer& json, const Section& symbol, const SymbolDatabase& database) {}

static void write_json(JsonWriter& json, const SourceFile& symbol, const SymbolDatabase& database)
template <typename Writer>
static void write_json(Writer& json, const SourceFile& symbol, const SymbolDatabase& database)
{
if (!symbol.working_dir.empty()) {
json.Key("working_dir");
Expand Down Expand Up @@ -332,6 +358,7 @@ static void write_json(JsonWriter& json, const SourceFile& symbol, const SymbolD
}
}

static void write_json(JsonWriter& json, const SymbolSource& symbol, const SymbolDatabase& database) {}
template <typename Writer>
static void write_json(Writer& json, const SymbolSource& symbol, const SymbolDatabase& database) {}

}
8 changes: 6 additions & 2 deletions src/ccc/symbol_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

#pragma once

#include "ast_json.h"
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>

#include "symbol_database.h"

namespace ccc {

extern const u32 JSON_FORMAT_VERSION;

template <typename Writer>
void write_json(
JsonWriter& json,
Writer& json,
const SymbolDatabase& database,
const char* application_name,
const std::set<SymbolSourceHandle>* sources = nullptr);
Expand Down
18 changes: 14 additions & 4 deletions src/stdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum Flags {
FLAG_CALLER_STACK_OFFSETS = 1 << 1,
FLAG_LOCAL_SYMBOLS = 1 << 2,
FLAG_PROCEDURE_DESCRIPTORS = 1 << 3,
FLAG_EXTERNAL_SYMBOLS = 1 << 4
FLAG_EXTERNAL_SYMBOLS = 1 << 4,
FLAG_COMPACT_JSON = 1 << 5
};

struct Options {
Expand Down Expand Up @@ -78,7 +79,9 @@ static const StdumpCommand commands[] = {
"may include other symbols where their type is not recoverable."
}},
{print_json, "json", {
"Print all of the above as JSON."
"Print all of the above as JSON.",
"",
"--compact Omit whitespace and newlines from the output."
}},
{print_symbols, "symbols", {
"Print the raw symbols in the input symbol table(s). If no additional options",
Expand Down Expand Up @@ -374,8 +377,13 @@ static void print_json(FILE* out, const Options& options)
std::unique_ptr<SymbolFile> symbol_file;
SymbolDatabase database = read_symbol_table(symbol_file, options);
rapidjson::StringBuffer buffer;
JsonWriter writer(buffer);
write_json(writer, database, "stdump");
if (options.flags & FLAG_COMPACT_JSON) {
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
write_json(writer, database, "stdump");
} else {
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
write_json(writer, database, "stdump");
}
fprintf(out, "%s", buffer.GetString());
}

Expand Down Expand Up @@ -555,6 +563,8 @@ static Options parse_command_line_arguments(int argc, char** argv)
options.flags |= FLAG_PROCEDURE_DESCRIPTORS;
} else if (strcmp(arg, "--externals") == 0) {
options.flags |= FLAG_EXTERNAL_SYMBOLS;
} else if (strcmp(arg, "--compact") == 0) {
options.flags |= FLAG_COMPACT_JSON;
} else if (strcmp(arg, "--output") == 0 || strcmp(arg, "-o") == 0) {
if (i + 1 < argc) {
options.output_file = argv[++i];
Expand Down
2 changes: 1 addition & 1 deletion src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static int main_test(const fs::path& input_directory)

// Test the JSON writing code.
rapidjson::StringBuffer buffer;
JsonWriter writer(buffer);
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
write_json(writer, database, "test");
} else {
printf("%s", symbol_file.error().message.c_str());
Expand Down

0 comments on commit b428bf0

Please sign in to comment.