Skip to content

Commit

Permalink
Enable more clang-tidy checks and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanoBilenchi committed Aug 25, 2024
1 parent e44df2d commit 6ed4b56
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 52 deletions.
17 changes: 14 additions & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/cowl_anon_ind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/cowl_entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cowl_iri.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/cowl_istream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cowl_istream_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions src/cowl_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 30 additions & 8 deletions src/cowl_object_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 7 additions & 8 deletions src/cowl_ontology.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
16 changes: 10 additions & 6 deletions src/cowl_sym_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cowl_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <stddef.h>
#include <string.h>

#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();
Expand Down
6 changes: 3 additions & 3 deletions src/writer/functional/cowl_func_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion test/tests/cowl_ontology_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
3 changes: 2 additions & 1 deletion test/tests/cowl_version_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 6ed4b56

Please sign in to comment.