Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce clang-format on the entire code base. #3679

Merged
merged 4 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
125 changes: 48 additions & 77 deletions backends/bmv2/common/JsonObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "lib/json.h"
#include "JsonObjects.h"

#include "helpers.h"
#include "lib/json.h"

namespace BMV2 {

Expand Down Expand Up @@ -51,8 +52,7 @@ JsonObjects::JsonObjects() {
field_aliases = insert_array_field(toplevel, "field_aliases");
}

Util::JsonArray*
JsonObjects::get_field_list_contents(unsigned id) const {
Util::JsonArray* JsonObjects::get_field_list_contents(unsigned id) const {
for (auto e : *field_lists) {
auto obj = e->to<Util::JsonObject>();
auto val = obj->get("id")->to<Util::JsonValue>();
Expand All @@ -63,8 +63,7 @@ JsonObjects::get_field_list_contents(unsigned id) const {
return nullptr;
}

Util::JsonObject*
JsonObjects::find_object_by_name(Util::JsonArray* array, const cstring& name) {
Util::JsonObject* JsonObjects::find_object_by_name(Util::JsonArray* array, const cstring& name) {
for (auto e : *array) {
auto obj = e->to<Util::JsonObject>();
auto val = obj->get("name")->to<Util::JsonValue>();
Expand All @@ -76,34 +75,27 @@ JsonObjects::find_object_by_name(Util::JsonArray* array, const cstring& name) {
}

/// Insert a json array to a parent object under key 'name'.
Util::JsonArray*
JsonObjects::insert_array_field(Util::JsonObject* parent, cstring name) {
Util::JsonArray* JsonObjects::insert_array_field(Util::JsonObject* parent, cstring name) {
auto result = new Util::JsonArray();
parent->emplace(name, result);
return result;
}

/// Append a json array to a parent json array.
Util::JsonArray*
JsonObjects::append_array(Util::JsonArray* parent) {
Util::JsonArray* JsonObjects::append_array(Util::JsonArray* parent) {
auto result = new Util::JsonArray();
parent->append(result);
return result;
}

/// Insert a json array named 'parameters' in a parent json object.
Util::JsonArray*
JsonObjects::create_parameters(Util::JsonObject* object) {
Util::JsonArray* JsonObjects::create_parameters(Util::JsonObject* object) {
return insert_array_field(object, "parameters");
}

void
JsonObjects::add_program_info(const cstring& name) {
toplevel->emplace("program", name);
}
void JsonObjects::add_program_info(const cstring& name) { toplevel->emplace("program", name); }

void
JsonObjects::add_meta_info() {
void JsonObjects::add_meta_info() {
static constexpr int version_major = JSON_MAJOR_VERSION;
static constexpr int version_minor = JSON_MINOR_VERSION;
auto version = insert_array_field(meta, "version");
Expand All @@ -121,8 +113,8 @@ JsonObjects::add_meta_info() {
* if 0 header does not contain varbit fields
* @param fields a JsonArray for the fields in the header
*/
unsigned
JsonObjects::add_header_type(const cstring& name, Util::JsonArray*& fields, unsigned max_length) {
unsigned JsonObjects::add_header_type(const cstring& name, Util::JsonArray*& fields,
unsigned max_length) {
std::string sname(name, name.size());
auto header_type_id_it = header_type_id.find(sname);
if (header_type_id_it != header_type_id.end()) {
Expand All @@ -139,18 +131,15 @@ JsonObjects::add_header_type(const cstring& name, Util::JsonArray*& fields, unsi
auto temp = new Util::JsonArray();
header_type->emplace("fields", temp);
}
if (max_length > 0)
header_type->emplace("max_length", max_length);
if (max_length > 0) header_type->emplace("max_length", max_length);
header_types->append(header_type);
return id;
}

unsigned
JsonObjects::add_union_type(const cstring& name, Util::JsonArray*& fields) {
unsigned JsonObjects::add_union_type(const cstring& name, Util::JsonArray*& fields) {
std::string sname(name, name.size());
auto it = union_type_id.find(sname);
if (it != union_type_id.end())
return it->second;
if (it != union_type_id.end()) return it->second;
auto union_type = new Util::JsonObject();
unsigned id = BMV2::nextId("header_union_types");
union_type_id[sname] = id;
Expand All @@ -166,10 +155,8 @@ JsonObjects::add_union_type(const cstring& name, Util::JsonArray*& fields) {
return id;
}


/// Create a header type with empty field list.
unsigned
JsonObjects::add_header_type(const cstring& name) {
unsigned JsonObjects::add_header_type(const cstring& name) {
std::string sname(name, name.size());
auto header_type_id_it = header_type_id.find(sname);
if (header_type_id_it != header_type_id.end()) {
Expand All @@ -188,8 +175,7 @@ JsonObjects::add_header_type(const cstring& name) {

/// Create a set of fields to an existing header type.
/// The header type is decribed by the name.
void
JsonObjects::add_header_field(const cstring& name, Util::JsonArray*& field) {
void JsonObjects::add_header_field(const cstring& name, Util::JsonArray*& field) {
CHECK_NULL(field);
Util::JsonObject* headerType = find_object_by_name(header_types, name);
Util::JsonArray* fields = headerType->get("fields")->to<Util::JsonArray>();
Expand All @@ -198,8 +184,7 @@ JsonObjects::add_header_field(const cstring& name, Util::JsonArray*& field) {
}

/// Create a header instance in json.
unsigned
JsonObjects::add_header(const cstring& type, const cstring& name) {
unsigned JsonObjects::add_header(const cstring& type, const cstring& name) {
auto header = new Util::JsonObject();
unsigned id = BMV2::nextId("headers");
LOG1("add header id " << id);
Expand All @@ -213,8 +198,8 @@ JsonObjects::add_header(const cstring& type, const cstring& name) {
}

/// Create a header_union instance in json.
unsigned
JsonObjects::add_union(const cstring& type, Util::JsonArray*& headers, const cstring& name) {
unsigned JsonObjects::add_union(const cstring& type, Util::JsonArray*& headers,
const cstring& name) {
auto u = new Util::JsonObject();
unsigned id = BMV2::nextId("header_unions");
LOG3("add header_union id " << id);
Expand All @@ -227,8 +212,7 @@ JsonObjects::add_union(const cstring& type, Util::JsonArray*& headers, const cst
return id;
}

unsigned
JsonObjects::add_metadata(const cstring& type, const cstring& name) {
unsigned JsonObjects::add_metadata(const cstring& type, const cstring& name) {
auto header = new Util::JsonObject();
unsigned id = BMV2::nextId("headers");
LOG3("add metadata header id " << id);
Expand All @@ -241,9 +225,8 @@ JsonObjects::add_metadata(const cstring& type, const cstring& name) {
return id;
}

void
JsonObjects::add_header_stack(const cstring& type, const cstring& name,
const unsigned size, const std::vector<unsigned>& ids) {
void JsonObjects::add_header_stack(const cstring& type, const cstring& name, const unsigned size,
const std::vector<unsigned>& ids) {
auto stack = new Util::JsonObject();
unsigned id = BMV2::nextId("stack");
stack->emplace("name", name);
Expand All @@ -258,9 +241,8 @@ JsonObjects::add_header_stack(const cstring& type, const cstring& name,
header_stacks->append(stack);
}

void
JsonObjects::add_header_union_stack(const cstring& type, const cstring& name,
const unsigned size, const std::vector<unsigned>& ids) {
void JsonObjects::add_header_union_stack(const cstring& type, const cstring& name,
const unsigned size, const std::vector<unsigned>& ids) {
auto stack = new Util::JsonObject();
unsigned id = BMV2::nextId("union_stack");
stack->emplace("name", name);
Expand All @@ -276,18 +258,16 @@ JsonObjects::add_header_union_stack(const cstring& type, const cstring& name,
}

/// Add an error to json.
void
JsonObjects::add_error(const cstring& name, const unsigned type) {
void JsonObjects::add_error(const cstring& name, const unsigned type) {
auto arr = append_array(errors);
arr->append(name);
arr->append(type);
}

/// Add a single enum entry to json.
/// A enum entry is identified with { enum_name, entry_name, entry_value }
void
JsonObjects::add_enum(const cstring& enum_name, const cstring& entry_name,
const unsigned entry_value) {
void JsonObjects::add_enum(const cstring& enum_name, const cstring& entry_name,
const unsigned entry_value) {
// look up enum in json by name
Util::JsonObject* enum_json = find_object_by_name(enums, enum_name);
if (enum_json == nullptr) { // first entry in a new enum
Expand All @@ -310,8 +290,7 @@ JsonObjects::add_enum(const cstring& enum_name, const cstring& entry_name,
}
}

unsigned
JsonObjects::add_parser(const cstring& name) {
unsigned JsonObjects::add_parser(const cstring& name) {
auto parser = new Util::JsonObject();
unsigned id = BMV2::nextId("parser");
parser->emplace("name", name);
Expand All @@ -327,10 +306,8 @@ JsonObjects::add_parser(const cstring& name) {

/// insert parser state into a parser identified by parser_id
/// return the id of the parser state
unsigned
JsonObjects::add_parser_state(const unsigned parser_id, const cstring& state_name) {
if (map_parser.find(parser_id) == map_parser.end())
BUG("parser %1% not found.", parser_id);
unsigned JsonObjects::add_parser_state(const unsigned parser_id, const cstring& state_name) {
if (map_parser.find(parser_id) == map_parser.end()) BUG("parser %1% not found.", parser_id);
auto parser = map_parser[parser_id];
auto states = parser->get("parse_states")->to<Util::JsonArray>();
auto state = new Util::JsonObject();
Expand All @@ -349,8 +326,7 @@ JsonObjects::add_parser_state(const unsigned parser_id, const cstring& state_nam
return state_id;
}

void
JsonObjects::add_parser_transition(const unsigned state_id, Util::IJson* transition) {
void JsonObjects::add_parser_transition(const unsigned state_id, Util::IJson* transition) {
if (map_parser_state.find(state_id) == map_parser_state.end())
BUG("parser state %1% not found.", state_id);
auto state = map_parser_state[state_id];
Expand All @@ -361,8 +337,7 @@ JsonObjects::add_parser_transition(const unsigned state_id, Util::IJson* transit
transitions->append(trans);
}

void
JsonObjects::add_parser_op(const unsigned state_id, Util::IJson* op) {
void JsonObjects::add_parser_op(const unsigned state_id, Util::IJson* op) {
if (map_parser_state.find(state_id) == map_parser_state.end())
BUG("parser state %1% not found.", state_id);
auto state = map_parser_state[state_id];
Expand All @@ -371,22 +346,20 @@ JsonObjects::add_parser_op(const unsigned state_id, Util::IJson* op) {
statements->append(op);
}

void
JsonObjects::add_parser_transition_key(const unsigned state_id, Util::IJson* newKey) {
void JsonObjects::add_parser_transition_key(const unsigned state_id, Util::IJson* newKey) {
if (map_parser_state.find(state_id) != map_parser_state.end()) {
auto state = map_parser_state[state_id];
auto keys = state->get("transition_key")->to<Util::JsonArray>();
CHECK_NULL(keys);
auto new_keys = newKey->to<Util::JsonArray>();
for (auto k : *new_keys) {
keys->append(k);
}
auto state = map_parser_state[state_id];
auto keys = state->get("transition_key")->to<Util::JsonArray>();
CHECK_NULL(keys);
auto new_keys = newKey->to<Util::JsonArray>();
for (auto k : *new_keys) {
keys->append(k);
}
}
}

void
JsonObjects::add_parse_vset(const cstring& name, const unsigned bitwidth,
const big_int& size) {
void JsonObjects::add_parse_vset(const cstring& name, const unsigned bitwidth,
const big_int& size) {
auto parse_vset = new Util::JsonObject();
unsigned id = BMV2::nextId("parse_vsets");
parse_vset->emplace("name", name);
Expand All @@ -396,8 +369,8 @@ JsonObjects::add_parse_vset(const cstring& name, const unsigned bitwidth,
parse_vsets->append(parse_vset);
}

unsigned
JsonObjects::add_action(const cstring& name, Util::JsonArray*& params, Util::JsonArray*& body) {
unsigned JsonObjects::add_action(const cstring& name, Util::JsonArray*& params,
Util::JsonArray*& body) {
CHECK_NULL(params);
CHECK_NULL(body);
auto action = new Util::JsonObject();
Expand All @@ -410,19 +383,17 @@ JsonObjects::add_action(const cstring& name, Util::JsonArray*& params, Util::Jso
return id;
}

void
JsonObjects::add_extern_attribute(const cstring& name, const cstring& type,
const cstring& value, Util::JsonArray* attributes) {
void JsonObjects::add_extern_attribute(const cstring& name, const cstring& type,
const cstring& value, Util::JsonArray* attributes) {
auto attr = new Util::JsonObject();
attr->emplace("name", name);
attr->emplace("type", type);
attr->emplace("value", value);
attributes->append(attr);
}

void
JsonObjects::add_extern(const cstring& name, const cstring& type,
Util::JsonArray* attributes) {
void JsonObjects::add_extern(const cstring& name, const cstring& type,
Util::JsonArray* attributes) {
auto extn = new Util::JsonObject();
unsigned id = BMV2::nextId("extern_instances");
extn->emplace("name", name);
Expand Down
22 changes: 10 additions & 12 deletions backends/bmv2/common/JsonObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ limitations under the License.
#define BACKENDS_BMV2_COMMON_JSONOBJECTS_H_

#include <map>

#include "lib/json.h"
#include "lib/ordered_map.h"

namespace BMV2 {

class JsonObjects {
public:
static Util::JsonObject* find_object_by_name(Util::JsonArray* array,
const cstring& name);
static Util::JsonObject* find_object_by_name(Util::JsonArray* array, const cstring& name);

void add_program_info(const cstring& name);
void add_meta_info();
Expand All @@ -37,23 +37,21 @@ class JsonObjects {
void add_header_field(const cstring& name, Util::JsonArray*& field);
unsigned add_header(const cstring& type, const cstring& name);
unsigned add_metadata(const cstring& type, const cstring& name);
void add_header_stack(const cstring& type, const cstring& name,
const unsigned size, const std::vector<unsigned>& header_ids);
void add_header_union_stack(const cstring& type, const cstring& name,
const unsigned size, const std::vector<unsigned>& header_ids);
void add_header_stack(const cstring& type, const cstring& name, const unsigned size,
const std::vector<unsigned>& header_ids);
void add_header_union_stack(const cstring& type, const cstring& name, const unsigned size,
const std::vector<unsigned>& header_ids);
void add_error(const cstring& name, const unsigned type);
void add_enum(const cstring& enum_name, const cstring& entry_name,
const unsigned entry_value);
void add_enum(const cstring& enum_name, const cstring& entry_name, const unsigned entry_value);
unsigned add_parser(const cstring& name);
unsigned add_parser_state(const unsigned id, const cstring& state_name);
void add_parser_transition(const unsigned id, Util::IJson* transition);
void add_parser_op(const unsigned id, Util::IJson* op);
void add_parser_transition_key(const unsigned id, Util::IJson* key);
void add_parse_vset(const cstring& name, const unsigned bitwidth,
const big_int& size);
void add_parse_vset(const cstring& name, const unsigned bitwidth, const big_int& size);
unsigned add_action(const cstring& name, Util::JsonArray*& params, Util::JsonArray*& body);
void add_extern_attribute(const cstring& name, const cstring& type,
const cstring& value, Util::JsonArray* attributes);
void add_extern_attribute(const cstring& name, const cstring& type, const cstring& value,
Util::JsonArray* attributes);
void add_extern(const cstring& name, const cstring& type, Util::JsonArray* attributes);
JsonObjects();
Util::JsonArray* insert_array_field(Util::JsonObject* parent, cstring name);
Expand Down
Loading