Skip to content

Commit 7133a67

Browse files
committed
cppcheck: Work across translation units
And while we're here, remove some suppressions that do nothing right now, and document why we have the remaining ones. There are also a couple of ones we can do inline, or do what it suggests, so just do that.
1 parent 08a8e78 commit 7133a67

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

Makefile

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ c_analyse_targets := $(c_files:%=%-analyse)
6464
h_analyse_targets := $(h_files:%=%-analyse)
6565

6666
analyse: CFLAGS+=$(debug_cflags)
67-
analyse: $(c_analyse_targets) $(h_analyse_targets)
67+
analyse: cppcheck $(c_analyse_targets) $(h_analyse_targets)
6868

6969
$(c_analyse_targets): %-analyse:
7070
# -W options here are not clang compatible, so out of generic CFLAGS
@@ -90,22 +90,39 @@ $(h_analyse_targets): %-analyse:
9090
$(MAKE) $*-shared-analyse
9191

9292
%-shared-analyse: %
93-
# cppcheck is a bit dim about unused functions/variables, leave that to
94-
# clang/GCC
95-
cppcheck $< --std=c99 --quiet --inline-suppr --force \
96-
--enable=all --suppress=missingIncludeSystem \
97-
--suppress=unusedFunction --suppress=unmatchedSuppression \
98-
--suppress=unreadVariable \
99-
--suppress=checkersReport \
100-
--suppress=constVariablePointer \
101-
--suppress=staticFunction \
102-
--suppress=normalCheckLevelMaxBranches \
103-
--suppress=unusedStructMember \
104-
--max-ctu-depth=32 --error-exitcode=1
10593
# clang-analyzer-unix.Malloc does not understand _drop_()
10694
clang-tidy $< --quiet -checks=-clang-analyzer-unix.Malloc -- -std=gnu99
10795
clang-format --dry-run --Werror $<
10896

97+
# --suppress=missingIncludeSystem:
98+
#
99+
# Without this there's a bunch of noise from cppcheck from the system headers
100+
# themselves.
101+
#
102+
# --suppress=unusedFunction:
103+
#
104+
# cppcheck does not understand _drop_ and marks those as unused. This is
105+
# already well checked by Clang/GCC, just leave it to them.
106+
#
107+
# --suppress=unmatchedSuppression:
108+
#
109+
# We run both locally and on CI, so there may be some suppressions that
110+
# depending on version do not match in one version but do on another.
111+
#
112+
# --suppress=unusedStructMember
113+
#
114+
# Structs are used across translation units and cppcheck gets this wrong.
115+
cppcheck: $(c_files) $(h_files)
116+
cppcheck $(c_files) $(h_files) --std=c99 --quiet --inline-suppr --force \
117+
--enable=all \
118+
--suppress=missingIncludeSystem \
119+
--suppress=unusedFunction \
120+
--suppress=unmatchedSuppression \
121+
--suppress=checkersReport \
122+
--suppress=unusedStructMember \
123+
--check-level=exhaustive \
124+
--max-ctu-depth=10 --error-exitcode=1
125+
109126
tests: tests/test_store
110127
tests/test_store
111128

@@ -115,4 +132,5 @@ integration_tests:
115132
tests/test_store: tests/test_store.c src/store.o src/util.o
116133
$(CC) $(CFLAGS) $(CPPFLAGS) -I./src -o $@ $^ $(LDLIBS)
117134

118-
.PHONY: all debug install uninstall clean analyse tests integration_tests
135+
.PHONY: all debug install uninstall clean analyse tests integration_tests \
136+
cppcheck

src/clipserve.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ static void _nonnull_ serve_clipboard(uint64_t hash,
147147
.target = req->target,
148148
.property = req->property};
149149

150+
// cppcheck-suppress constVariablePointer
151+
// _drop_(XFree) passes this to XFree, so keep it non-const.
150152
_drop_(XFree) char *window_title =
151153
get_window_title(dpy, req->requestor);
152154
dbg("Servicing request to window '%s' (0x%lX) for clip " PRI_HASH

src/config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static int resolve_path(const char *input, void *output) {
9292
* the config file to the type we expect for `struct Config`.
9393
*/
9494

95-
int convert_bool(const char *str, void *output) {
95+
static int convert_bool(const char *str, void *output) {
9696
const char *const truthy[] = {"1", "y", "yes", "true", "on"};
9797
const char *const falsy[] = {"0", "n", "no", "false", "off"};
9898

@@ -113,7 +113,7 @@ int convert_bool(const char *str, void *output) {
113113
return -EINVAL;
114114
}
115115

116-
int convert_positive_int(const char *str, void *output) {
116+
static int convert_positive_int(const char *str, void *output) {
117117
char *end;
118118
long val = strtol(str, &end, 10);
119119
if (*end != '\0' || end == str || val < 0 || val > INT_MAX) {
@@ -123,7 +123,7 @@ int convert_positive_int(const char *str, void *output) {
123123
return 0;
124124
}
125125

126-
int convert_ignore_window(const char *str, void *output) {
126+
static int convert_ignore_window(const char *str, void *output) {
127127
struct ignore_window *iw = output;
128128
iw->set = (bool)str;
129129
if (!iw->set) {
@@ -326,7 +326,7 @@ static int config_apply_default_values(struct config_entry entries[],
326326
* This is generally not expected to be called by applications -- call
327327
* config_setup() instead, which provides the right file for you.
328328
*/
329-
int config_setup_internal(FILE *file, struct config *cfg) {
329+
static int config_setup_internal(FILE *file, struct config *cfg) {
330330
struct config_entry entries[] = {
331331
{"max_clips", "CM_MAX_CLIPS", &cfg->max_clips, convert_positive_int,
332332
"1000", 0},

src/config.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ selection_atom_to_selection_type(Atom atom, const struct cm_selections *sels);
9696
enum selection_type _nonnull_
9797
storage_atom_to_selection_type(Atom atom, const struct cm_selections *sels);
9898

99-
int convert_bool(const char *str, void *output);
100-
int convert_positive_int(const char *str, void *output);
101-
int convert_ignore_window(const char *str, void *output);
102-
int config_setup_internal(FILE *file, struct config *cfg);
10399
void config_free(struct config *cfg);
104100
DEFINE_DROP_FUNC_PTR(struct config, config_free)
105101

src/store.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ cs_content_add(struct clip_store *cs, uint64_t hash, const char *content,
478478
*
479479
* @content: The content to unmap
480480
*/
481-
int cs_content_unmap(struct cs_content *content) {
481+
static int cs_content_unmap(struct cs_content *content) {
482482
if (content && content->data) {
483483
close(content->fd);
484484
if (munmap(content->data, (size_t)content->size)) {

src/store.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ void _nonnull_ drop_cs_unref(struct ref_guard *guard);
161161
int _must_use_ _nonnull_ cs_destroy(struct clip_store *cs);
162162
int _must_use_ _nonnull_ cs_init(struct clip_store *cs, int snip_fd,
163163
int content_dir_fd);
164-
int _must_use_ cs_content_unmap(struct cs_content *content);
165164
void drop_cs_content_unmap(struct cs_content *content);
166165
void drop_cs_destroy(struct clip_store *cs);
167166
int _must_use_ _nonnull_ cs_content_get(struct clip_store *cs, uint64_t hash,

0 commit comments

Comments
 (0)