[PATCH v1 23/48] perf annotate: Silence -Wshorten-64-to-32 warnings

Ian Rogers
2025-04-01T18:25:23Z

tools/perf/builtin-annotate.c | 4 ++--
tools/perf/util/annotate.c | 22 +++++++++++-----------
tools/perf/util/disasm.c | 11 ++++++-----
3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 9833c2c82a2f..97943a12b6c9 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -157,7 +157,7 @@ static void process_branch_stack(struct branch_stack *bs, struct addr_location *
if (!bi)
return;

- for (i = bs->nr - 1; i >= 0; i--) {
+ for (i = (int)bs->nr - 1; i >= 0; i--) {
/*
* XXX filter against symbol
*/
@@ -880,7 +880,7 @@ int cmd_annotate(int argc, const char **argv)

annotate.session = perf_session__new(&data, &annotate.tool);
if (IS_ERR(annotate.session))
- return PTR_ERR(annotate.session);
+ return (int)PTR_ERR(annotate.session);

annotate.session->itrace_synth_opts = &itrace_synth_opts;

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 1e59b9e5339d..c582e0ecb86f 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -337,7 +337,8 @@ static int symbol__account_br_cntr(struct annotated_branch *branch,
unsigned int base = evsel__leader(evsel)->br_cntr_idx;
unsigned int off = offset * evsel->evlist->nr_br_cntr;
u64 *branch_br_cntr = branch->br_cntr;
- unsigned int i, mask, width;
+ unsigned int i, width;
+ u64 mask;

if (!br_cntr || !branch_br_cntr)
return 0;
@@ -360,7 +361,7 @@ static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym,
u64 br_cntr)
{
struct annotated_branch *branch;
- unsigned offset;
+ unsigned int offset;
int ret;

if (sym == NULL)
@@ -377,7 +378,7 @@ static int symbol__account_cycles(u64 addr, u64 start, struct symbol *sym,
if (start >= addr)
start = 0;
}
- offset = addr - sym->start;
+ offset = (unsigned int)(addr - sym->start);
ret = __symbol__account_cycles(branch->cycles_hist,
start ? start - sym->start : 0,
offset, cycles,
@@ -1563,7 +1564,7 @@ static int annotation__max_ins_name(struct annotation *notes)
if (al->offset == -1)
continue;

- len = strlen(disasm_line(al)->ins.name);
+ len = (int)strlen(disasm_line(al)->ins.name);
if (max_name < len)
max_name = len;
}
@@ -2183,20 +2184,20 @@ static int annotation_options__add_disassemblers_str(struct annotation_options *
{
while (str && *str != '\0') {
const char *comma = strchr(str, ',');
- int len = comma ? comma - str : (int)strlen(str);
+ size_t len = comma ? comma - str : strlen(str);
bool match = false;

for (u8 i = 0; i < ARRAY_SIZE(perf_disassembler__strs); i++) {
const char *dis_str = perf_disassembler__strs[i];

- if (len == (int)strlen(dis_str) && !strncmp(str, dis_str, len)) {
+ if (len == strlen(dis_str) && !strncmp(str, dis_str, len)) {
annotation_options__add_disassembler(options, i);
match = true;
break;
}
}
if (!match) {
- pr_err("Invalid disassembler '%.*s'\n", len, str);
+ pr_err("Invalid disassembler '%.*s'\n", (int)len, str);
return -1;
}
str = comma ? comma + 1 : NULL;
@@ -2405,7 +2406,7 @@ static int extract_reg_offset(struct arch *arch, const char *str,
str++;
}

- op_loc->offset = strtol(str, &p, 0);
+ op_loc->offset = (int)strtol(str, &p, 0);

p = strchr(p, arch->objdump.register_char);
if (p == NULL)
@@ -2515,8 +2516,7 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
/* FIXME: Handle other segment registers */
if (!strncmp(insn_str, "%gs:", 4)) {
op_loc->segment = INSN_SEG_X86_GS;
- op_loc->offset = strtol(insn_str + 4,
- &p, 0);
+ op_loc->offset = (int)strtol(insn_str + 4, &p, 0);
if (p && p != insn_str + 4)
op_loc->imm = true;
continue;
@@ -2530,7 +2530,7 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
if (*s == arch->objdump.register_char)
op_loc->reg1 = get_dwarf_regnum(s, arch->e_machine, arch->e_flags);
else if (*s == arch->objdump.imm_char) {
- op_loc->offset = strtol(s + 1, &p, 0);
+ op_loc->offset = (int)strtol(s + 1, &p, 0);
if (p && p != s + 1)
op_loc->imm = true;
}
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 8f0eb56c6fc6..7e24851a3934 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -856,7 +856,7 @@ static int ins__cmp(const void *a, const void *b)

static void ins__sort(struct arch *arch)
{
- const int nmemb = arch->nr_instructions;
+ const size_t nmemb = arch->nr_instructions;

qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
}
@@ -864,7 +864,7 @@ static void ins__sort(struct arch *arch)
static struct ins_ops *__ins__find(struct arch *arch, const char *name, struct disasm_line *dl)
{
struct ins *ins;
- const int nmemb = arch->nr_instructions;
+ const size_t nmemb = arch->nr_instructions;

if (arch__is(arch, "powerpc")) {
/*
@@ -1263,7 +1263,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
char *build_id_filename;
char *build_id_path = NULL;
char *pos;
- int len;
+ size_t len;

if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
!dso__is_kcore(dso))
@@ -1754,7 +1754,8 @@ static int symbol__disassemble_raw(char *filename, struct symbol *sym,
u64 end = map__rip_2objdump(map, sym->end);
u64 len = end - start;
u64 offset;
- int i, count;
+ int i;
+ ssize_t count;
u8 *buf = NULL;
char disasm_buf[512];
struct disasm_line *dl;
@@ -1823,7 +1824,7 @@ static int symbol__disassemble_raw(char *filename, struct symbol *sym,

out:
free(buf);
- return count < 0 ? count : 0;
+ return count < 0 ? (int)count : 0;

err:
count = -1;
--
2.49.0.504.g3bcea36a83-goog