[PATCH v1 24/48] perf report: Silence -Wshorten-64-to-32 warnings

From: Ian Rogers
Date: Tue Apr 01 2025 - 14:30:50 EST


The clang warning -Wshorten-64-to-32 can be useful to catch
inadvertent truncation. In some instances this truncation can lead to
changing the sign of a result, for example, truncation to return an
int to fit a sort routine. Silence the warning by making the implicit
truncation explicit.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-report.c | 4 ++--
tools/perf/util/callchain.c | 12 ++++++------
tools/perf/util/callchain.h | 2 +-
tools/perf/util/srccode.c | 6 +++---
tools/perf/util/srcline.c | 2 +-
5 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index b030ce72e13e..fff8f3db788e 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -800,7 +800,7 @@ static int count_lost_samples_event(const struct perf_tool *tool,
evsel = evlist__id2evsel(rep->session->evlist, sample->id);
if (evsel) {
struct hists *hists = evsel__hists(evsel);
- u32 count = event->lost_samples.lost;
+ u32 count = (u32)event->lost_samples.lost;

if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF)
hists__inc_nr_dropped_samples(hists, count);
@@ -1602,7 +1602,7 @@ int cmd_report(int argc, const char **argv)

session = perf_session__new(&data, &report.tool);
if (IS_ERR(session)) {
- ret = PTR_ERR(session);
+ ret = (int)PTR_ERR(session);
goto exit;
}

diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index d7b7eef740b9..c3206b9d7b52 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -214,7 +214,7 @@ __parse_callchain_report_opt(const char *arg, bool allow_record_opt)

if (get_stack_size(tok, &size) < 0)
return -1;
- callchain_param.dump_size = size;
+ callchain_param.dump_size = (u32)size;
try_stack_size = false;
} else if (!minpcnt_set) {
/* try to get the min percent */
@@ -224,7 +224,7 @@ __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
minpcnt_set = true;
} else {
/* try print limit at last */
- callchain_param.print_limit = strtoul(tok, &endptr, 0);
+ callchain_param.print_limit = (u32)strtoul(tok, &endptr, 0);
if (tok == endptr)
return -1;
}
@@ -295,7 +295,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
unsigned long size = 0;

ret = get_stack_size(tok, &size);
- param->dump_size = size;
+ param->dump_size = (u32)size;
}
} else if (!strncmp(name, "lbr", sizeof("lbr"))) {
if (!strtok_r(NULL, ",", &saveptr)) {
@@ -332,7 +332,7 @@ int perf_callchain_config(const char *var, const char *value)
int ret;

ret = get_stack_size(value, &size);
- callchain_param.dump_size = size;
+ callchain_param.dump_size = (u32)size;

return ret;
}
@@ -817,7 +817,7 @@ split_add_child(struct callchain_node *parent,
{
struct callchain_node *new;
struct list_head *old_tail;
- unsigned int idx_total = idx_parents + idx_local;
+ u64 idx_total = idx_parents + idx_local;

/* split */
new = create_child(parent, true);
@@ -1027,7 +1027,7 @@ merge_chain_branch(struct callchain_cursor *cursor,
struct callchain_node *child;
struct callchain_list *list, *next_list;
struct rb_node *n;
- int old_pos = cursor->nr;
+ u64 old_pos = cursor->nr;
int err = 0;

list_for_each_entry_safe(list, next_list, &src->val, list) {
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
index 86ed9e4d04f9..7a2abff6d0a5 100644
--- a/tools/perf/util/callchain.h
+++ b/tools/perf/util/callchain.h
@@ -65,7 +65,7 @@ struct callchain_node {
struct rb_node rb_node; /* to sort nodes in an output tree */
struct rb_root rb_root_in; /* input tree of children */
struct rb_root rb_root; /* sorted output tree of children */
- unsigned int val_nr;
+ u64 val_nr;
unsigned int count;
unsigned int children_count;
u64 hit;
diff --git a/tools/perf/util/srccode.c b/tools/perf/util/srccode.c
index 476e99896d5e..f5f8cdfd00c1 100644
--- a/tools/perf/util/srccode.c
+++ b/tools/perf/util/srccode.c
@@ -37,7 +37,7 @@ static LIST_HEAD(srcfile_list);
static long map_total_sz;
static int num_srcfiles;

-static int countlines(char *map, int maplen)
+static int countlines(char *map, size_t maplen)
{
int numl;
char *end = map + maplen;
@@ -136,7 +136,7 @@ static struct srcfile *find_srcfile(char *fn)
h->lines = calloc(h->numlines, sizeof(char *));
if (!h->lines)
goto out_map;
- fill_lines(h->lines, h->numlines, h->map, h->maplen);
+ fill_lines(h->lines, h->numlines, h->map, (int)h->maplen);
list_add(&h->nd, &srcfile_list);
hlist_add_head(&h->hash_nd, &srcfile_htab[hval]);
map_total_sz += h->maplen;
@@ -166,6 +166,6 @@ char *find_sourceline(char *fn, unsigned line, int *lenp)
if (!l)
return NULL;
p = memchr(l, '\n', sf->map + sf->maplen - l);
- *lenp = p - l;
+ *lenp = (int)(p - l);
return l;
}
diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index f32d0d4f4bc9..b2003c5490a2 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -443,7 +443,7 @@ static int filename_split(char *filename, unsigned int *line_nr)
sep = strchr(filename, ':');
if (sep) {
*sep++ = '\0';
- *line_nr = strtoul(sep, NULL, 0);
+ *line_nr = (unsigned int)strtoul(sep, NULL, 0);
return 1;
}
pr_debug("addr2line missing ':' in filename split\n");
--
2.49.0.504.g3bcea36a83-goog