Skip to content

Commit

Permalink
Enforce style (#1000)
Browse files Browse the repository at this point in the history
This PR is a mechanical[^1] fix for #994; it selects some default naming
conventions for C++ code and applies them across the project. Open to
bikeshedding if there are particular places anyone doesn't like, but as
with all these changes the most important thing is having the tool
enabled at all.

The changes might not be perfect; there's a lot of surface area to cover
here but they should be pretty good. I've tested against the K
integration test suite to make sure nothing is broken there.

[^1]: `clang-tidy` + a pile of one-off `sed` scripts to cover edge
cases; fortunately style is easier to enforce than to apply so
`clang-tidy` will do on its own moving forwards.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **Refactor**
- Standardized naming conventions across the codebase for improved
consistency and readability. This includes converting camelCase to
snake_case and adjusting enum values to PascalCase where applicable.
- Renamed various entities (functions, variables, classes) to adhere to
the new naming convention, enhancing code clarity.
- **Style**
- Updated comments and documentation to reflect changes in naming
conventions and code structure.
- **Chores**
- Adjusted method signatures and variable names for consistency with the
newly adopted naming standards.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
Baltoli authored Mar 5, 2024
1 parent 87edd79 commit 0cf7dec
Show file tree
Hide file tree
Showing 119 changed files with 8,572 additions and 8,313 deletions.
36 changes: 36 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,39 @@ CheckOptions:
value: true
- key: 'cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor'
value: true
- key: 'readability-identifier-naming.ClassCase'
value: 'lower_case'
- key: 'readability-identifier-naming.ConstexprVariableCase'
value: 'lower_case'
- key: 'readability-identifier-naming.GlobalConstantCase'
value: 'UPPER_CASE'
- key: 'readability-identifier-naming.GlobalVariableCase'
value: 'lower_case'
- key: 'readability-identifier-naming.GlobalVariableIgnoredRegexp'
value: 'and_|not_|equals_|X'
- key: 'readability-identifier-naming.ParameterCase'
value: 'lower_case'
- key: 'readability-identifier-naming.MemberCase'
value: 'lower_case'
- key: 'readability-identifier-naming.PrivateMemberCase'
value: 'lower_case'
- key: 'readability-identifier-naming.PrivateMemberSuffix'
value: '_'
- key: 'readability-identifier-naming.TemplateParameterCase'
value: 'CamelCase'
- key: 'readability-identifier-naming.TypeTemplateParameterIgnoredRegexp'
value: 'expr-type' # https://github.com/llvm/llvm-project/issues/46097
- key: 'readability-identifier-naming.MethodCase'
value: 'lower_case'
- key: 'readability-identifier-naming.FunctionCase'
value: 'lower_case'
- key: 'readability-identifier-naming.FunctionIgnoredRegexp'
value: 'hook_.*'
- key: 'readability-identifier-naming.EnumCase'
value: 'lower_case'
- key: 'readability-identifier-naming.ScopedEnumConstantCase'
value: 'CamelCase'
- key: 'readability-identifier-naming.EnumConstantCase'
value: 'CamelCase'
- key: 'readability-identifier-naming.LocalVariableCase'
value: 'lower_case'
6 changes: 3 additions & 3 deletions bindings/c/include/kllvm-c/kllvm-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ typedef struct kore_sort kore_sort;
typedef struct kore_symbol kore_symbol;
typedef struct block block;

/* KOREPattern */
/* kore_pattern */

char *kore_pattern_dump(kore_pattern const *);

Expand Down Expand Up @@ -131,7 +131,7 @@ void kore_simplify_binary(

block *take_steps(int64_t depth, block *term);

/* KORESort */
/* kore_sort */

char *kore_sort_dump(kore_sort const *);

Expand All @@ -145,7 +145,7 @@ bool kore_sort_is_k(kore_sort const *);
kore_sort *kore_composite_sort_new(char const *);
void kore_composite_sort_add_argument(kore_sort const *, kore_sort const *);

/* KORESymbol */
/* kore_symbol */

kore_symbol *kore_symbol_new(char const *);

Expand Down
101 changes: 51 additions & 50 deletions bindings/c/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ auto managed(kore_symbol *ptr) {
*/

extern "C" {
void initStaticObjects();
void freeAllKoreMem();
void init_static_objects();
void free_all_kore_mem();
bool hook_BYTES_mutableBytesEnabled();
}

Expand All @@ -88,33 +88,33 @@ char kore_definition_macros __attribute__((weak)) = -1;
/* Completed types */

struct kore_error {
bool success_ = true;
std::optional<std::string> message_ = std::nullopt;
bool success = true;
std::optional<std::string> message = std::nullopt;

[[nodiscard]] char const *c_str() const {
if (!success_ && message_.has_value()) {
return message_->c_str();
if (!success && message.has_value()) {
return message->c_str();
}

return nullptr;
}

void set_error(std::string const &msg) {
success_ = false;
message_ = msg;
success = false;
message = msg;
}
};

struct kore_pattern {
std::shared_ptr<kllvm::KOREPattern> ptr_;
std::shared_ptr<kllvm::kore_pattern> ptr;
};

struct kore_sort {
std::shared_ptr<kllvm::KORESort> ptr_;
std::shared_ptr<kllvm::kore_sort> ptr;
};

struct kore_symbol {
std::shared_ptr<kllvm::KORESymbol> ptr_;
std::shared_ptr<kllvm::kore_symbol> ptr;
};

/* Error handling */
Expand All @@ -124,7 +124,7 @@ kore_error *kore_error_new(void) {
}

bool kore_error_is_success(kore_error const *err) {
return err->success_;
return err->success;
}

char const *kore_error_message(kore_error const *err) {
Expand All @@ -135,10 +135,10 @@ void kore_error_free(kore_error *err) {
delete err;
}

/* KOREPattern */
/* kore_pattern */

char *kore_pattern_dump(kore_pattern const *pat) {
return get_c_string(ast_to_string(*pat->ptr_));
return get_c_string(ast_to_string(*pat->ptr));
}

char *kore_pattern_pretty_print(kore_pattern const *pat) {
Expand Down Expand Up @@ -168,11 +168,11 @@ char *kore_pattern_pretty_print(kore_pattern const *pat) {
macros_out << macros;

auto pattern_out = std::ofstream(temp_path("pattern.kore"), std::ios::out);
pat->ptr_->print(pattern_out);
pat->ptr->print(pattern_out);
}

auto ss = std::stringstream{};
kllvm::printKORE(
kllvm::print_kore(
ss, temp_dir_name, temp_path("pattern.kore"), false, false, true);

fs::remove_all(temp_dir_name);
Expand All @@ -183,7 +183,7 @@ char *kore_pattern_pretty_print(kore_pattern const *pat) {
void kore_pattern_serialize(
kore_pattern const *pat, char **data_out, size_t *size_out) {
auto out = kllvm::serializer();
pat->ptr_->serialize_to(out);
pat->ptr->serialize_to(out);

auto const &binary_data = out.data();
auto binary_size = binary_data.size();
Expand All @@ -200,12 +200,12 @@ void kore_pattern_free(kore_pattern const *pat) {

kore_pattern *kore_pattern_parse(char const *kore_text) {
return new kore_pattern{
kllvm::parser::KOREParser::from_string(kore_text)->pattern()};
kllvm::parser::kore_parser::from_string(kore_text)->pattern()};
}

kore_pattern *kore_pattern_parse_file(char const *filename) {
return new kore_pattern{
kllvm::parser::KOREParser(std::string(filename)).pattern()};
kllvm::parser::kore_parser(std::string(filename)).pattern()};
}

kore_pattern *kore_pattern_new_token(char const *value, kore_sort const *sort) {
Expand All @@ -222,7 +222,7 @@ kore_pattern *kore_pattern_new_token_with_len(
kore_pattern *kore_pattern_new_injection(
kore_pattern const *term, kore_sort const *from, kore_sort const *to) {
return new kore_pattern{
kllvm::bindings::make_injection(term->ptr_, from->ptr_, to->ptr_)};
kllvm::bindings::make_injection(term->ptr, from->ptr, to->ptr)};
}

kore_pattern *kore_pattern_make_interpreter_input(
Expand Down Expand Up @@ -258,15 +258,15 @@ kore_pattern *kore_pattern_make_interpreter_input(
}

kore_pattern *kore_pattern_desugar_associative(kore_pattern const *pat) {
return new kore_pattern{pat->ptr_->desugarAssociative()};
return new kore_pattern{pat->ptr->desugar_associative()};
}

block *kore_pattern_construct(kore_pattern const *pat) {
return kllvm::bindings::construct_term(pat->ptr_);
return kllvm::bindings::construct_term(pat->ptr);
}

char *kore_block_dump(block *term) {
auto *hooked_str = printConfigurationToString(term)->data;
auto *hooked_str = print_configuration_to_string(term)->data;
auto len = std::strlen(hooked_str);

auto *new_str = static_cast<char *>(malloc(len + 1));
Expand All @@ -286,7 +286,7 @@ bool kore_block_get_bool(block *term) {

bool kore_simplify_bool(kore_error *err, kore_pattern const *pattern) {
try {
return kllvm::bindings::simplify_to_bool(pattern->ptr_);
return kllvm::bindings::simplify_to_bool(pattern->ptr);
} catch (std::exception &e) {
if (err == nullptr) {
throw;
Expand All @@ -301,8 +301,8 @@ void kore_simplify(
kore_error *err, kore_pattern const *pattern, kore_sort const *sort,
char **data_out, size_t *size_out) {
try {
auto *block = kllvm::bindings::simplify_to_term(pattern->ptr_, sort->ptr_);
serializeConfiguration(
auto *block = kllvm::bindings::simplify_to_term(pattern->ptr, sort->ptr);
serialize_configuration(
block, "SortKItem{}", data_out, size_out, true, true);
} catch (std::exception &e) {
if (err == nullptr) {
Expand All @@ -320,8 +320,8 @@ void kore_simplify_binary(
auto sort_str = std::unique_ptr<char, decltype(std::free) *>(
kore_sort_dump(sort), std::free);

auto *block = deserializeConfiguration(data_in, size_in);
serializeConfiguration(
auto *block = deserialize_configuration(data_in, size_in);
serialize_configuration(
block, sort_str.get(), data_out, size_out, true, true);
} catch (std::exception &e) {
if (err == nullptr) {
Expand All @@ -332,28 +332,29 @@ void kore_simplify_binary(
}
}

/* KORECompositePattern */
/* kore_composite_pattern */

kore_pattern *kore_composite_pattern_new(char const *name) {
return new kore_pattern{
kllvm::KORECompositePattern::Create(std::string(name))};
kllvm::kore_composite_pattern::create(std::string(name))};
}

kore_pattern *kore_composite_pattern_from_symbol(kore_symbol *sym) {
return new kore_pattern{kllvm::KORECompositePattern::Create(sym->ptr_.get())};
return new kore_pattern{
kllvm::kore_composite_pattern::create(sym->ptr.get())};
}

void kore_composite_pattern_add_argument(
kore_pattern *pat, kore_pattern const *arg) {
if (auto const &cast
= std::dynamic_pointer_cast<kllvm::KORECompositePattern>(pat->ptr_)) {
cast->addArgument(arg->ptr_);
= std::dynamic_pointer_cast<kllvm::kore_composite_pattern>(pat->ptr)) {
cast->add_argument(arg->ptr);
} else {
abort();
}
}

/* KOREStringPattern */
/* kore_string_pattern */

kore_pattern *kore_string_pattern_new(char const *contents) {
return kore_string_pattern_new_internal(std::string(contents));
Expand All @@ -364,70 +365,70 @@ kore_string_pattern_new_with_len(char const *contents, size_t len) {
return kore_string_pattern_new_internal(std::string(contents, len));
}

/* KORESort */
/* kore_sort */

char *kore_sort_dump(kore_sort const *sort) {
return get_c_string(ast_to_string(*sort->ptr_));
return get_c_string(ast_to_string(*sort->ptr));
}

void kore_sort_free(kore_sort const *sort) {
delete sort;
}

bool kore_sort_is_concrete(kore_sort const *sort) {
return sort->ptr_->isConcrete();
return sort->ptr->is_concrete();
}

bool kore_sort_is_kitem(kore_sort const *sort) {
return kllvm::bindings::is_sort_kitem(sort->ptr_);
return kllvm::bindings::is_sort_kitem(sort->ptr);
}

bool kore_sort_is_k(kore_sort const *sort) {
return kllvm::bindings::is_sort_k(sort->ptr_);
return kllvm::bindings::is_sort_k(sort->ptr);
}

/* KORECompositeSort */
/* kore_composite_sort */

kore_sort *kore_composite_sort_new(char const *name) {
return new kore_sort{kllvm::KORECompositeSort::Create(std::string(name))};
return new kore_sort{kllvm::kore_composite_sort::create(std::string(name))};
}

void kore_composite_sort_add_argument(
kore_sort const *sort, kore_sort const *arg) {
if (auto const &cast
= std::dynamic_pointer_cast<kllvm::KORECompositeSort>(sort->ptr_)) {
cast->addArgument(arg->ptr_);
= std::dynamic_pointer_cast<kllvm::kore_composite_sort>(sort->ptr)) {
cast->add_argument(arg->ptr);
} else {
abort();
}
}

/* KORESymbol */
/* kore_symbol */

kore_symbol *kore_symbol_new(char const *name) {
return new kore_symbol{kllvm::KORESymbol::Create(std::string(name))};
return new kore_symbol{kllvm::kore_symbol::create(std::string(name))};
}

void kore_symbol_free(kore_symbol const *sym) {
delete sym;
}

char *kore_symbol_dump(kore_symbol const *sym) {
return get_c_string(ast_to_string(*sym->ptr_));
return get_c_string(ast_to_string(*sym->ptr));
}

void kore_symbol_add_formal_argument(kore_symbol *sym, kore_sort const *sort) {
sym->ptr_->addFormalArgument(sort->ptr_);
sym->ptr->add_formal_argument(sort->ptr);
}

/* Memory management */

void kllvm_init(void) {
initStaticObjects();
init_static_objects();
}

void kllvm_free_all_memory(void) {
freeAllKoreMem();
free_all_kore_mem();
}

bool kllvm_mutable_bytes_enabled(void) {
Expand All @@ -448,7 +449,7 @@ char *get_c_string(std::string const &str) {
}

kore_pattern *kore_string_pattern_new_internal(std::string const &str) {
return new kore_pattern{kllvm::KOREStringPattern::Create(str)};
return new kore_pattern{kllvm::kore_string_pattern::create(str)};
}

kore_pattern *kore_pattern_new_token_internal(
Expand Down
Loading

0 comments on commit 0cf7dec

Please sign in to comment.