diff --git a/.clang-tidy b/.clang-tidy index eec6dda..6d56616 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,13 +1,24 @@ --- Checks: > -*, + bugprone-*, + -bugprone-assignment-in-if-condition, + -bugprone-easily-swappable-parameters, + -bugprone-implicit-widening-of-multiplication-result, + -bugprone-macro-parentheses, clang-analyzer-*, -clang-analyzer-cplusplus-*, -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, - misc-header-include-cycle, - misc-include-cleaner, + misc-*, + -misc-no-recursion, + modernize-*, performance-*, -performance-enum-size, - portability-* + portability-*, + readability-*, + -readability-identifier-length, + -readability-magic-numbers CheckOptions: misc-include-cleaner.IgnoreHeaders: sys/_types/_size_t\.h + readability-braces-around-statements.ShortStatementLines: 2 + readability-function-cognitive-complexity.IgnoreMacros: true diff --git a/src/cowl_anon_ind.c b/src/cowl_anon_ind.c index 80b9f27..a4a4145 100644 --- a/src/cowl_anon_ind.c +++ b/src/cowl_anon_ind.c @@ -84,7 +84,7 @@ static CowlAnonInd *anon_ind(CowlString *id, bool copy_id) { } } else if (ret == UHASH_PRESENT) { val = uhash_key(CowlObjectTable, &inst_tbl, idx); - (void)cowl_object_incr_ref(val); + cowl_object_incr_ref(val); } return val; diff --git a/src/cowl_entity.c b/src/cowl_entity.c index 5102945..ceb6314 100644 --- a/src/cowl_entity.c +++ b/src/cowl_entity.c @@ -70,7 +70,7 @@ static CowlEntity *cowl_entity_alloc(CowlObjectType type, CowlIRI *iri) { ulib_free(entity); return NULL; } - (void)cowl_object_incr_ref(entity); + cowl_object_incr_ref(entity); #endif entity->iri = cowl_retain(iri); @@ -101,7 +101,7 @@ CowlAnyEntity *cowl_entity_get_impl(CowlObjectType type, CowlIRI *iri) { } } else if (ret == UHASH_PRESENT) { entity = uhash_key(CowlObjectTable, &inst_tbl, idx); - (void)cowl_object_incr_ref(entity); + cowl_object_incr_ref(entity); } return entity; diff --git a/src/cowl_iri.c b/src/cowl_iri.c index d9ac55c..a80a2c6 100644 --- a/src/cowl_iri.c +++ b/src/cowl_iri.c @@ -83,7 +83,7 @@ CowlIRI *cowl_iri_unvalidated(CowlString *ns, CowlString *rem) { } } else if (ret == UHASH_PRESENT) { val = uhash_key(CowlObjectTable, &inst_tbl, idx); - (void)cowl_object_incr_ref(val); + cowl_object_incr_ref(val); } return val; diff --git a/src/cowl_istream.c b/src/cowl_istream.c index 37c9dd8..7df8a2a 100644 --- a/src/cowl_istream.c +++ b/src/cowl_istream.c @@ -113,13 +113,13 @@ cowl_ret cowl_istream_process_string(CowlIStream *stream, UString const *string) } static bool onto_stream_handle_import(void *ctx, CowlAny *import_iri) { - void **c = ctx; + void **c = (void **)ctx; CowlIStreamHandlers *handle = c[1]; return (*((cowl_ret *)c[0]) = handle->import(handle->ctx, import_iri)) == COWL_OK; } static bool onto_stream_handle_axiom(void *ctx, CowlAny *axiom) { - void **c = ctx; + void **c = (void **)ctx; CowlIStreamHandlers *handle = c[1]; return (*((cowl_ret *)c[0]) = handle->axiom(handle->ctx, axiom)) == COWL_OK; } @@ -147,13 +147,13 @@ cowl_ret cowl_istream_process_ontology(CowlIStream *stream, CowlOntology *onto) if (handle->import) { void *ctx[] = { &ret, handle }; - CowlIterator iter = { ctx, onto_stream_handle_import }; + CowlIterator iter = { (void *)ctx, onto_stream_handle_import }; if (!cowl_ontology_iterate_import_iris(onto, &iter, false)) return ret; } if (handle->axiom) { void *ctx[] = { &ret, handle }; - CowlIterator iter = { ctx, onto_stream_handle_axiom }; + CowlIterator iter = { (void *)ctx, onto_stream_handle_axiom }; if (!cowl_ontology_iterate_axioms(onto, &iter, false)) return ret; } diff --git a/src/cowl_istream_private.h b/src/cowl_istream_private.h index 7e75086..e10a118 100644 --- a/src/cowl_istream_private.h +++ b/src/cowl_istream_private.h @@ -29,7 +29,7 @@ struct CowlIStream { CowlSymTable *st; }; -CowlIStream *cowl_istream(CowlManager *manager, CowlSymTable *st, CowlIStreamHandlers config); +CowlIStream *cowl_istream(CowlManager *manager, CowlSymTable *st, CowlIStreamHandlers handlers); void cowl_istream_free(CowlIStream *stream); COWL_END_DECLS diff --git a/src/cowl_object.c b/src/cowl_object.c index 780ec38..406b407 100644 --- a/src/cowl_object.c +++ b/src/cowl_object.c @@ -54,10 +54,14 @@ */ #define TF_FC(field_count) (field_count) -#define TF_NONE 0x0U -#define TF_PRIMITIVE 0x4U -#define TF_ENTITY 0x8U -#define TF_CARD_RESTR 0x10U + +enum { + TF_NONE = 0x0U, + TF_FIELD_COUNT = 0x3U, + TF_PRIMITIVE = 0x4U, + TF_ENTITY = 0x8U, + TF_CARD_RESTR = 0x10U, +}; static ulib_byte type_flags[COWL_OT_COUNT] = { [COWL_OT_STRING] = TF_NONE, @@ -142,17 +146,18 @@ static ulib_byte type_flags[COWL_OT_COUNT] = { }; #define type_flag_is_special(f) (!(f)) -#define type_flag_is_primitive(f) ((f) & (0x4U)) -#define type_flag_is_entity(f) ((f) & (0x8U)) -#define type_flag_is_card_restr(f) ((f) & (0x10U)) -#define type_flag_field_count(f) ((f) & (0x3U)) +#define type_flag_is_primitive(f) ((f) & TF_PRIMITIVE) +#define type_flag_is_entity(f) ((f) & TF_ENTITY) +#define type_flag_is_card_restr(f) ((f) & TF_CARD_RESTR) +#define type_flag_field_count(f) ((f) & TF_FIELD_COUNT) #define type_is_primitive(t) type_flag_is_primitive(type_flags[t]) #define type_is_entity(t) type_flag_is_entity(type_flags[t]) #define type_field_count(t) type_flag_field_count(type_flags[t]) CowlAny *cowl_retain(CowlAny *object) { - return cowl_object_incr_ref(object); + cowl_object_incr_ref(object); + return object; } static inline void release_impl(CowlObjectType type, CowlComposite *object) { @@ -246,8 +251,10 @@ bool cowl_has_iri_string(CowlAny *object, UString iri_str) { CowlIRI *iri = cowl_get_iri(object); if (!iri) return false; - CowlString *ns = cowl_iri_get_ns(iri), *rem = cowl_iri_get_rem(iri); - ulib_uint ns_len = cowl_string_get_length(ns), rem_len = cowl_string_get_length(rem); + CowlString *ns = cowl_iri_get_ns(iri); + CowlString *rem = cowl_iri_get_rem(iri); + ulib_uint ns_len = cowl_string_get_length(ns); + ulib_uint rem_len = cowl_string_get_length(rem); if (ustring_length(iri_str) != ns_len + rem_len) return false; if (!ustring_ends_with(iri_str, *cowl_string_get_raw(rem))) return false; @@ -330,7 +337,9 @@ bool cowl_equals(CowlAny *lhs, CowlAny *rhs) { if (has_opt != cowl_has_opt_field(rhs)) return false; if (has_opt) ++n; - CowlComposite *l = lhs, *r = rhs; + CowlComposite *l = lhs; + CowlComposite *r = rhs; + for (ulib_byte i = 0; i < n; ++i) { if (!cowl_equals(l->fields[i].obj, r->fields[i].obj)) return false; } diff --git a/src/cowl_object_private.h b/src/cowl_object_private.h index b601fa6..0280c46 100644 --- a/src/cowl_object_private.h +++ b/src/cowl_object_private.h @@ -35,14 +35,36 @@ typedef struct CowlComposite { ((CowlObject){ .flags = cowl_object_flags(TYPE, HAS_BIT) }) #define COWL_OBJECT_INIT(TYPE) COWL_OBJECT_BIT_INIT(TYPE, 0) -#define cowl_object_get_ref(o) cowl_object_flags_get_ref(((CowlObject *)(o))->flags) -#define cowl_object_incr_ref(o) (cowl_object_flags_incr_ref(((CowlObject *)(o))->flags), (o)) -#define cowl_object_decr_ref(o) \ - (cowl_object_flags_decr_ref(((CowlObject *)(o))->flags), cowl_object_get_ref(o)) - -#define cowl_object_bit_get(o) cowl_object_flags_has_bit(((CowlObject *)(o))->flags) -#define cowl_object_bit_set(o) cowl_object_flags_set_bit(((CowlObject *)(o))->flags) -#define cowl_object_bit_unset(o) cowl_object_flags_unset_bit(((CowlObject *)(o))->flags) +ULIB_INLINE +ulib_uint cowl_object_get_ref(CowlAny *o) { + return cowl_object_flags_get_ref(((CowlObject *)o)->flags); +} + +ULIB_INLINE +void cowl_object_incr_ref(CowlAny *o) { + cowl_object_flags_incr_ref(((CowlObject *)o)->flags); +} + +ULIB_INLINE +ulib_uint cowl_object_decr_ref(CowlAny *o) { + cowl_object_flags_decr_ref(((CowlObject *)o)->flags); + return cowl_object_flags_get_ref(((CowlObject *)o)->flags); +} + +ULIB_INLINE +bool cowl_object_bit_get(CowlAny *o) { + return cowl_object_flags_has_bit(((CowlObject *)o)->flags); +} + +ULIB_INLINE +void cowl_object_bit_set(CowlAny *o) { + cowl_object_flags_set_bit(((CowlObject *)o)->flags); +} + +ULIB_INLINE +void cowl_object_bit_unset(CowlAny *o) { + cowl_object_flags_unset_bit(((CowlObject *)o)->flags); +} COWL_END_DECLS diff --git a/src/cowl_ontology.c b/src/cowl_ontology.c index 93cf6a0..b9fbdff 100644 --- a/src/cowl_ontology.c +++ b/src/cowl_ontology.c @@ -499,9 +499,8 @@ bool cowl_ontology_iterate_related(CowlOntology *onto, CowlAnyPrimitive *primiti if (cowl_vector_count(at) < cowl_vector_count(ar)) { return cowl_ontology_iterate_axioms_of_type(onto, type, iter, imports); - } else { - return cowl_ontology_iterate_axioms_for_primitive(onto, primitive, &l_iter, imports); } + return cowl_ontology_iterate_axioms_for_primitive(onto, primitive, &l_iter, imports); } void cowl_ontology_set_iri(CowlOntology *onto, CowlIRI *iri) { @@ -572,27 +571,27 @@ CowlIRI *cowl_ontology_get_import_iri(CowlOntology *onto, CowlOntology *import) return NULL; } -cowl_ret cowl_ontology_add_import(CowlOntology *onto, CowlIRI *iri) { +cowl_ret cowl_ontology_add_import(CowlOntology *onto, CowlIRI *import) { ulib_uint idx = UHASH_INDEX_MISSING; UHash(CowlObjectTable) map = uhmap_pi(CowlObjectTable, cowl_primitive_hash, cowl_primitive_equals); if (!onto->imports && !(onto->imports = cowl_table(&map))) goto err; UHash(CowlObjectTable) *tbl = &onto->imports->data; - uhash_ret hret = uhash_put(CowlObjectTable, tbl, iri, &idx); + uhash_ret hret = uhash_put(CowlObjectTable, tbl, import, &idx); if (hret == UHASH_INSERTED) { - cowl_retain(iri); + cowl_retain(import); } else if (hret == UHASH_PRESENT) { return COWL_OK; } else { goto err; } - CowlOntology *import = NULL; + CowlOntology *import_onto = NULL; CowlImportLoader loader = cowl_manager_get_import_loader(onto->manager); - if (loader.load_ontology && !(import = loader.load_ontology(loader.ctx, iri))) goto err; - uhash_value(CowlObjectTable, tbl, idx) = import; + if (loader.load_ontology && !(import_onto = loader.load_ontology(loader.ctx, import))) goto err; + uhash_value(CowlObjectTable, tbl, idx) = import_onto; return COWL_OK; diff --git a/src/cowl_sym_table.c b/src/cowl_sym_table.c index 003a38f..bd2be50 100644 --- a/src/cowl_sym_table.c +++ b/src/cowl_sym_table.c @@ -39,7 +39,9 @@ static inline void cowl_sym_table_set_clean(CowlSymTable *st) { static cowl_ret cowl_update_reverse_map(CowlSymTable *st) { if (!cowl_sym_table_is_dirty(st)) return COWL_OK; - CowlTable *h1 = st->prefix_ns_map, *h2 = st->ns_prefix_map; + CowlTable *h1 = st->prefix_ns_map; + CowlTable *h2 = st->ns_prefix_map; + cowl_table_foreach (h1, e) { if (uhmap_set(CowlObjectTable, &h2->data, *e.val, *e.key, NULL) == UHASH_ERR) { return COWL_ERR_MEM; @@ -176,7 +178,8 @@ cowl_ret cowl_sym_table_unregister_prefix(CowlSymTable *st, CowlString *prefix) if (cowl_vocab_is_reserved_prefix(prefix)) return COWL_ERR; CowlTable *table = cowl_sym_table_get_prefix_ns_map(st, false); - CowlAny *ex_key, *ex_value; + CowlAny *ex_key; + CowlAny *ex_value; if (uhmap_pop(CowlObjectTable, &table->data, prefix, &ex_key, &ex_value)) { table = cowl_sym_table_get_prefix_ns_map(st, true); @@ -192,7 +195,8 @@ cowl_ret cowl_sym_table_unregister_ns(CowlSymTable *st, CowlString *ns) { if (cowl_vocab_is_reserved_ns(ns)) return COWL_ERR; CowlTable *table = cowl_sym_table_get_prefix_ns_map(st, true); - CowlAny *ex_key, *ex_value; + CowlAny *ex_key; + CowlAny *ex_value; if (uhmap_pop(CowlObjectTable, &table->data, ns, &ex_key, &ex_value)) { table = cowl_sym_table_get_prefix_ns_map(st, false); @@ -204,11 +208,11 @@ cowl_ret cowl_sym_table_unregister_ns(CowlSymTable *st, CowlString *ns) { return COWL_OK; } -cowl_ret cowl_sym_table_merge(CowlSymTable *st, CowlSymTable *other, bool overwrite) { - CowlTable *table = cowl_sym_table_get_prefix_ns_map(other, false); +cowl_ret cowl_sym_table_merge(CowlSymTable *dst, CowlSymTable *src, bool overwrite) { + CowlTable *table = cowl_sym_table_get_prefix_ns_map(src, false); cowl_table_foreach (table, e) { if (cowl_vocab_is_reserved_prefix(*e.key)) continue; - cowl_ret ret = cowl_sym_table_register_prefix(st, *e.key, *e.val, overwrite); + cowl_ret ret = cowl_sym_table_register_prefix(dst, *e.key, *e.val, overwrite); if (ret) return ret; } return COWL_OK; diff --git a/src/cowl_writer.c b/src/cowl_writer.c index 738490d..abc9f54 100644 --- a/src/cowl_writer.c +++ b/src/cowl_writer.c @@ -27,7 +27,7 @@ #include #include -#define UINT_MAX_DIGITS 20 +#define UINT_MAX_DIGITS 20 // NOLINT(modernize-macro-to-enum) cowl_ret cowl_write(UOStream *stream, CowlAny *object) { CowlWriter writer = cowl_get_writer(); diff --git a/src/writer/functional/cowl_func_writer.c b/src/writer/functional/cowl_func_writer.c index 4b57454..ce28b96 100644 --- a/src/writer/functional/cowl_func_writer.c +++ b/src/writer/functional/cowl_func_writer.c @@ -151,7 +151,8 @@ static ustream_ret cowl_func_write_vector(UOStream *s, CowlVector *vec, CowlSymT static ustream_ret cowl_func_write_table(UOStream *s, CowlTable *table, CowlSymTable *st) { if (!table) return s->state; - ulib_uint current = 0, last = cowl_table_count(table) - 1; + ulib_uint current = 0; + ulib_uint last = cowl_table_count(table) - 1; cowl_table_foreach (table, obj) { cowl_func_write_obj(s, *obj.key, st); @@ -423,9 +424,8 @@ static ustream_ret cowl_func_write_obj(UOStream *s, CowlAny *obj, CowlSymTable * default: { if (type >= COWL_OT_FIRST_A && type <= COWL_OT_LAST_A) { return cowl_func_write_annot_construct(s, obj, st); - } else { - return cowl_func_write_construct(s, obj, st); } + return cowl_func_write_construct(s, obj, st); } } } diff --git a/test/tests/cowl_ontology_tests.c b/test/tests/cowl_ontology_tests.c index cf9a753..b65d648 100644 --- a/test/tests/cowl_ontology_tests.c +++ b/test/tests/cowl_ontology_tests.c @@ -184,7 +184,7 @@ bool cowl_test_ontology_axiom_count_for_primitive(void) { cowl_release(primitive); utest_assert_uint(count, ==, test_primitive_axiom_count[COWL_PT_IRI]); - CowlIterator iter = { &primitive, cowl_test_get_first_anon_ind }; + CowlIterator iter = { (void *)&primitive, cowl_test_get_first_anon_ind }; cowl_ontology_iterate_primitives(onto, COWL_PF_ANON_IND, &iter, true); count = cowl_ontology_axiom_count_for_primitive(onto, primitive, true); utest_assert_uint(count, ==, test_primitive_axiom_count[COWL_PT_ANON_IND]); diff --git a/test/tests/cowl_version_tests.c b/test/tests/cowl_version_tests.c index f0ffb21..8a18997 100644 --- a/test/tests/cowl_version_tests.c +++ b/test/tests/cowl_version_tests.c @@ -13,7 +13,8 @@ #include "ulib.h" bool cowl_test_version(void) { - UVersion v = cowl_get_version(), zero = { 0 }; + UVersion v = cowl_get_version(); + UVersion zero = { 0 }; utest_assert_int(uversion_compare(v, zero), ==, 1); return true; }