Skip to content

Commit 31920a4

Browse files
authored
Merge pull request #347 from PJK/warnings
Fix mutiple "pedantic" compiler warnings, mostly around unused parameters
2 parents 30b972c + 7a7f60b commit 31920a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1300
-1259
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true
5252
ConstructorInitializerIndentWidth: 4
5353
ContinuationIndentWidth: 4
5454
Cpp11BracedListStyle: true
55-
DerivePointerAlignment: true
55+
DerivePointerAlignment: false
5656
DisableFormat: false
5757
ExperimentalAutoDetectBinPacking: false
5858
FixNamespaceComments: true

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ else()
9898
set(CBOR_RESTRICT_SPECIFIER "restrict")
9999

100100
set(CMAKE_C_FLAGS_DEBUG
101-
"${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true")
102-
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG")
101+
"${CMAKE_C_FLAGS_DEBUG} -O0 -pedantic -Wall -Wextra -g -ggdb -DDEBUG=true")
102+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -pedantic -Wall -Wextra -DNDEBUG")
103103

104104
if(SANITIZE)
105105
set(CMAKE_C_FLAGS_DEBUG
@@ -124,6 +124,19 @@ else()
124124
add_definitions(-DEIGHT_BYTE_SIZE_T)
125125
endif()
126126

127+
include(CheckCSourceCompiles)
128+
129+
check_c_source_compiles("
130+
int main() {
131+
__builtin_unreachable();
132+
return 0;
133+
}
134+
" HAS_BUILTIN_UNREACHABLE)
135+
136+
if (HAS_BUILTIN_UNREACHABLE)
137+
add_definitions(-D_CBOR_HAS_BUILTIN_UNREACHABLE)
138+
endif()
139+
127140
enable_testing()
128141

129142
set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")

examples/cjson2cbor.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
#include "cbor/internal/builder_callbacks.h"
2323
#include "cbor/internal/loaders.h"
2424

25-
typedef void (*cbor_load_callback_t)(cJSON *, const struct cbor_callbacks *,
26-
void *);
25+
typedef void (*cbor_load_callback_t)(cJSON*, const struct cbor_callbacks*,
26+
void*);
2727

28-
cbor_item_t *cjson_cbor_load(void *source,
28+
cbor_item_t* cjson_cbor_load(void* source,
2929
cbor_load_callback_t cbor_load_callback) {
3030
static struct cbor_callbacks callbacks = {
3131
.uint64 = &cbor_builder_uint64_callback,
@@ -51,9 +51,9 @@ cbor_item_t *cjson_cbor_load(void *source,
5151
return context.root;
5252
}
5353

54-
void cjson_cbor_stream_decode(cJSON *source,
55-
const struct cbor_callbacks *callbacks,
56-
void *context) {
54+
void cjson_cbor_stream_decode(cJSON* source,
55+
const struct cbor_callbacks* callbacks,
56+
void* context) {
5757
switch (source->type) {
5858
case cJSON_False: {
5959
callbacks->boolean(context, false);
@@ -83,13 +83,13 @@ void cjson_cbor_stream_decode(cJSON *source,
8383
}
8484
case cJSON_String: {
8585
// XXX: Assume cJSON handled unicode correctly
86-
callbacks->string(context, (unsigned char *)source->valuestring,
86+
callbacks->string(context, (unsigned char*)source->valuestring,
8787
strlen(source->valuestring));
8888
return;
8989
}
9090
case cJSON_Array: {
9191
callbacks->array_start(context, cJSON_GetArraySize(source));
92-
cJSON *item = source->child;
92+
cJSON* item = source->child;
9393
while (item != NULL) {
9494
cjson_cbor_stream_decode(item, callbacks, context);
9595
item = item->next;
@@ -98,9 +98,9 @@ void cjson_cbor_stream_decode(cJSON *source,
9898
}
9999
case cJSON_Object: {
100100
callbacks->map_start(context, cJSON_GetArraySize(source));
101-
cJSON *item = source->child;
101+
cJSON* item = source->child;
102102
while (item != NULL) {
103-
callbacks->string(context, (unsigned char *)item->string,
103+
callbacks->string(context, (unsigned char*)item->string,
104104
strlen(item->string));
105105
cjson_cbor_stream_decode(item, callbacks, context);
106106
item = item->next;
@@ -115,24 +115,24 @@ void usage(void) {
115115
exit(1);
116116
}
117117

118-
int main(int argc, char *argv[]) {
118+
int main(int argc, char* argv[]) {
119119
if (argc != 2) usage();
120-
FILE *f = fopen(argv[1], "rb");
120+
FILE* f = fopen(argv[1], "rb");
121121
if (f == NULL) usage();
122122
/* Read input file into a buffer (cJSON doesn't work with streams) */
123123
fseek(f, 0, SEEK_END);
124124
size_t length = (size_t)ftell(f);
125125
fseek(f, 0, SEEK_SET);
126-
char *json_buffer = malloc(length + 1);
126+
char* json_buffer = malloc(length + 1);
127127
fread(json_buffer, length, 1, f);
128128
json_buffer[length] = '\0';
129129

130130
/* Convert between JSON and CBOR */
131-
cJSON *json = cJSON_Parse(json_buffer);
132-
cbor_item_t *cbor = cjson_cbor_load(json, cjson_cbor_stream_decode);
131+
cJSON* json = cJSON_Parse(json_buffer);
132+
cbor_item_t* cbor = cjson_cbor_load(json, cjson_cbor_stream_decode);
133133

134134
/* Print out CBOR bytes */
135-
unsigned char *buffer;
135+
unsigned char* buffer;
136136
size_t buffer_size;
137137
cbor_serialize_alloc(cbor, &buffer, &buffer_size);
138138

examples/sort.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* standard library functions.
1515
*/
1616

17-
int compareUint(const void *a, const void *b) {
18-
uint8_t av = cbor_get_uint8(*(cbor_item_t **)a),
19-
bv = cbor_get_uint8(*(cbor_item_t **)b);
17+
int compare_uint(const void* a, const void* b) {
18+
uint8_t av = cbor_get_uint8(*(cbor_item_t**)a),
19+
bv = cbor_get_uint8(*(cbor_item_t**)b);
2020

2121
if (av < bv)
2222
return -1;
@@ -27,15 +27,15 @@ int compareUint(const void *a, const void *b) {
2727
}
2828

2929
int main(void) {
30-
cbor_item_t *array = cbor_new_definite_array(4);
30+
cbor_item_t* array = cbor_new_definite_array(4);
3131
bool success = cbor_array_push(array, cbor_move(cbor_build_uint8(4)));
3232
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(3)));
3333
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(1)));
3434
success &= cbor_array_push(array, cbor_move(cbor_build_uint8(2)));
3535
if (!success) return 1;
3636

37-
qsort(cbor_array_handle(array), cbor_array_size(array), sizeof(cbor_item_t *),
38-
compareUint);
37+
qsort(cbor_array_handle(array), cbor_array_size(array), sizeof(cbor_item_t*),
38+
compare_uint);
3939

4040
cbor_describe(array, stdout);
4141
fflush(stdout);

examples/streaming_array.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void flush(size_t bytes) {
3030
*/
3131
int main(int argc, char* argv[]) {
3232
if (argc != 2) usage();
33-
long n = strtol(argv[1], NULL, 10);
33+
size_t n;
34+
scanf(argv[1], "%zu", &n);
3435
out = freopen(NULL, "wb", stdout);
3536
if (!out) exit(1);
3637

examples/streaming_parser.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
#include <string.h>
1010
#include "cbor.h"
1111

12-
#ifdef __GNUC__
13-
#define UNUSED(x) __attribute__((__unused__)) x
14-
#else
15-
#define UNUSED(x) x
16-
#endif
17-
1812
void usage(void) {
1913
printf("Usage: streaming_parser [input file]\n");
2014
exit(1);
@@ -30,7 +24,7 @@ void usage(void) {
3024
const char* key = "a secret key";
3125
bool key_found = false;
3226

33-
void find_string(void* UNUSED(_ctx), cbor_data buffer, uint64_t len) {
27+
void find_string(void* _ctx _CBOR_UNUSED, cbor_data buffer, uint64_t len) {
3428
if (key_found) {
3529
printf("Found the value: %.*s\n", (int)len, buffer);
3630
key_found = false;

src/cbor.c

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "cbor/internal/builder_callbacks.h"
1010
#include "cbor/internal/loaders.h"
1111

12-
cbor_item_t *cbor_load(cbor_data source, size_t source_size,
13-
struct cbor_load_result *result) {
12+
cbor_item_t* cbor_load(cbor_data source, size_t source_size,
13+
struct cbor_load_result* result) {
1414
/* Context stack */
1515
static struct cbor_callbacks callbacks = {
1616
.uint8 = &cbor_builder_uint8_callback,
@@ -114,8 +114,8 @@ cbor_item_t *cbor_load(cbor_data source, size_t source_size,
114114
return NULL;
115115
}
116116

117-
static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
118-
cbor_item_t *res = NULL;
117+
static cbor_item_t* _cbor_copy_int(cbor_item_t* item, bool negative) {
118+
cbor_item_t* res = NULL;
119119
switch (cbor_int_get_width(item)) {
120120
case CBOR_INT_8:
121121
res = cbor_build_uint8(cbor_get_uint8(item));
@@ -136,8 +136,7 @@ static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
136136
return res;
137137
}
138138

139-
static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
140-
// cppcheck-suppress missingReturn
139+
static cbor_item_t* _cbor_copy_float_ctrl(cbor_item_t* item) {
141140
switch (cbor_float_get_width(item)) {
142141
case CBOR_FLOAT_0:
143142
return cbor_build_ctrl(cbor_ctrl_value(item));
@@ -147,11 +146,13 @@ static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
147146
return cbor_build_float4(cbor_float_get_float4(item));
148147
case CBOR_FLOAT_64:
149148
return cbor_build_float8(cbor_float_get_float8(item));
149+
default:
150+
_CBOR_UNREACHABLE;
151+
return NULL;
150152
}
151153
}
152154

153-
cbor_item_t *cbor_copy(cbor_item_t *item) {
154-
// cppcheck-suppress missingReturn
155+
cbor_item_t* cbor_copy(cbor_item_t* item) {
155156
switch (cbor_typeof(item)) {
156157
case CBOR_TYPE_UINT:
157158
return _cbor_copy_int(item, false);
@@ -162,13 +163,13 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
162163
return cbor_build_bytestring(cbor_bytestring_handle(item),
163164
cbor_bytestring_length(item));
164165
} else {
165-
cbor_item_t *res = cbor_new_indefinite_bytestring();
166+
cbor_item_t* res = cbor_new_indefinite_bytestring();
166167
if (res == NULL) {
167168
return NULL;
168169
}
169170

170171
for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) {
171-
cbor_item_t *chunk_copy =
172+
cbor_item_t* chunk_copy =
172173
cbor_copy(cbor_bytestring_chunks_handle(item)[i]);
173174
if (chunk_copy == NULL) {
174175
cbor_decref(&res);
@@ -185,16 +186,16 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
185186
}
186187
case CBOR_TYPE_STRING:
187188
if (cbor_string_is_definite(item)) {
188-
return cbor_build_stringn((const char *)cbor_string_handle(item),
189+
return cbor_build_stringn((const char*)cbor_string_handle(item),
189190
cbor_string_length(item));
190191
} else {
191-
cbor_item_t *res = cbor_new_indefinite_string();
192+
cbor_item_t* res = cbor_new_indefinite_string();
192193
if (res == NULL) {
193194
return NULL;
194195
}
195196

196197
for (size_t i = 0; i < cbor_string_chunk_count(item); i++) {
197-
cbor_item_t *chunk_copy =
198+
cbor_item_t* chunk_copy =
198199
cbor_copy(cbor_string_chunks_handle(item)[i]);
199200
if (chunk_copy == NULL) {
200201
cbor_decref(&res);
@@ -210,7 +211,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
210211
return res;
211212
}
212213
case CBOR_TYPE_ARRAY: {
213-
cbor_item_t *res;
214+
cbor_item_t* res;
214215
if (cbor_array_is_definite(item)) {
215216
res = cbor_new_definite_array(cbor_array_size(item));
216217
} else {
@@ -221,7 +222,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
221222
}
222223

223224
for (size_t i = 0; i < cbor_array_size(item); i++) {
224-
cbor_item_t *entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
225+
cbor_item_t* entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
225226
if (entry_copy == NULL) {
226227
cbor_decref(&res);
227228
return NULL;
@@ -236,7 +237,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
236237
return res;
237238
}
238239
case CBOR_TYPE_MAP: {
239-
cbor_item_t *res;
240+
cbor_item_t* res;
240241
if (cbor_map_is_definite(item)) {
241242
res = cbor_new_definite_map(cbor_map_size(item));
242243
} else {
@@ -246,14 +247,14 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
246247
return NULL;
247248
}
248249

249-
struct cbor_pair *it = cbor_map_handle(item);
250+
struct cbor_pair* it = cbor_map_handle(item);
250251
for (size_t i = 0; i < cbor_map_size(item); i++) {
251-
cbor_item_t *key_copy = cbor_copy(it[i].key);
252+
cbor_item_t* key_copy = cbor_copy(it[i].key);
252253
if (key_copy == NULL) {
253254
cbor_decref(&res);
254255
return NULL;
255256
}
256-
cbor_item_t *value_copy = cbor_copy(it[i].value);
257+
cbor_item_t* value_copy = cbor_copy(it[i].value);
257258
if (value_copy == NULL) {
258259
cbor_decref(&res);
259260
cbor_decref(&key_copy);
@@ -272,16 +273,19 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
272273
return res;
273274
}
274275
case CBOR_TYPE_TAG: {
275-
cbor_item_t *item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
276+
cbor_item_t* item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
276277
if (item_copy == NULL) {
277278
return NULL;
278279
}
279-
cbor_item_t *tag = cbor_build_tag(cbor_tag_value(item), item_copy);
280+
cbor_item_t* tag = cbor_build_tag(cbor_tag_value(item), item_copy);
280281
cbor_decref(&item_copy);
281282
return tag;
282283
}
283284
case CBOR_TYPE_FLOAT_CTRL:
284285
return _cbor_copy_float_ctrl(item);
286+
default:
287+
_CBOR_UNREACHABLE;
288+
return NULL;
285289
}
286290
}
287291

@@ -300,11 +304,11 @@ static int _pow(int b, int ex) {
300304
return res;
301305
}
302306

303-
static void _cbor_type_marquee(FILE *out, char *label, int indent) {
307+
static void _cbor_type_marquee(FILE* out, char* label, int indent) {
304308
fprintf(out, "%*.*s[%s] ", indent, indent, " ", label);
305309
}
306310

307-
static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
311+
static void _cbor_nested_describe(cbor_item_t* item, FILE* out, int indent) {
308312
const int indent_offset = 4;
309313
switch (cbor_typeof(item)) {
310314
case CBOR_TYPE_UINT: {
@@ -328,7 +332,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
328332
_cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out,
329333
indent + indent_offset);
330334
} else {
331-
const unsigned char *data = cbor_bytestring_handle(item);
335+
const unsigned char* data = cbor_bytestring_handle(item);
332336
fprintf(out, "Definite, Length: %zuB, Data:\n",
333337
cbor_bytestring_length(item));
334338
fprintf(out, "%*s", indent + indent_offset, " ");
@@ -417,7 +421,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
417421
}
418422
}
419423

420-
void cbor_describe(cbor_item_t *item, FILE *out) {
424+
void cbor_describe(cbor_item_t* item, FILE* out) {
421425
_cbor_nested_describe(item, out, 0);
422426
}
423427

0 commit comments

Comments
 (0)