[PATCH v1 15/48] perf pmu: Silence -Wshorten-64-to-32 warnings

From: Ian Rogers
Date: Tue Apr 01 2025 - 14:28:22 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/util/hwmon_pmu.c | 8 ++++----
tools/perf/util/pmu.c | 9 ++++-----
tools/perf/util/pmus.c | 16 ++++++++--------
tools/perf/util/tool_pmu.c | 2 +-
4 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c
index 3cce77fc8004..6245eeb593c9 100644
--- a/tools/perf/util/hwmon_pmu.c
+++ b/tools/perf/util/hwmon_pmu.c
@@ -170,7 +170,7 @@ bool parse_hwmon_filename(const char *filename,
for (size_t i = 0; fn_type[i] != '\0'; i++) {
if (fn_type[i] >= '0' && fn_type[i] <= '9') {
fn_type[i] = '\0';
- *number = strtoul(&filename[i], (char **)&fn_item, 10);
+ *number = (int)strtoul(&filename[i], (char **)&fn_item, 10);
if (*fn_item == '_')
fn_item++;
break;
@@ -194,7 +194,7 @@ bool parse_hwmon_filename(const char *filename,
return false;
}

- *type = elem - &hwmon_type_strs[0];
+ *type = (enum hwmon_type)(elem - &hwmon_type_strs[0]);
if (!item)
return true;

@@ -213,7 +213,7 @@ bool parse_hwmon_filename(const char *filename,
fn_item, filename);
return false;
}
- *item = elem - &hwmon_item_strs[0];
+ *item = (enum hwmon_item)(elem - &hwmon_item_strs[0]);
return true;
}

@@ -352,7 +352,7 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, const cha
return NULL;

hwm->hwmon_dir_fd = hwmon_dir;
- hwm->pmu.type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10);
+ hwm->pmu.type = PERF_PMU_TYPE_HWMON_START + (int)strtoul(sysfs_name + 5, NULL, 10);
if (hwm->pmu.type > PERF_PMU_TYPE_HWMON_END) {
pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name);
goto err_out;
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index b7ebac5ab1d1..5a940bf004fb 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -1269,7 +1269,7 @@ __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name)
{
struct perf_pmu_format *format = pmu_find_format(&pmu->format, name);
__u64 bits = 0;
- int fbit;
+ size_t fbit;

if (!format)
return 0;
@@ -1314,9 +1314,8 @@ static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,

static __u64 pmu_format_max_value(const unsigned long *format)
{
- int w;
+ size_t w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);

- w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
if (!w)
return 0;
if (w < 64)
@@ -1824,7 +1823,7 @@ int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_call
if (!pmu->is_core)
return 0;

- for (size_t i = 0; i < ARRAY_SIZE(terms); i++) {
+ for (int i = 0; i < (int)ARRAY_SIZE(terms); i++) {
int config = PERF_PMU_FORMAT_VALUE_CONFIG;

if (i < PERF_PMU_FORMAT_VALUE_CONFIG_END)
@@ -2281,7 +2280,7 @@ static void perf_pmu__compute_config_masks(struct perf_pmu *pmu)
return;

list_for_each_entry(format, &pmu->format, list) {
- unsigned int i;
+ size_t i;
__u64 *mask;

if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END)
diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
index b99292de7669..0b9071219376 100644
--- a/tools/perf/util/pmus.c
+++ b/tools/perf/util/pmus.c
@@ -59,7 +59,7 @@ static void pmu_read_sysfs(unsigned int to_read_pmus);

size_t pmu_name_len_no_suffix(const char *str)
{
- int orig_len, len;
+ size_t orig_len, len;
bool has_hex_digits = false;

orig_len = len = strlen(str);
@@ -353,7 +353,7 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
{
bool use_core_pmus = !pmu || pmu->is_core;
- int last_pmu_name_len = 0;
+ size_t last_pmu_name_len = 0;
const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";

if (!pmu) {
@@ -364,7 +364,7 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)

if (use_core_pmus) {
list_for_each_entry_continue(pmu, &core_pmus, list) {
- int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
+ size_t pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");

if (last_pmu_name_len == pmu_name_len &&
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
@@ -376,7 +376,7 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
pmu = list_prepare_entry(pmu, &other_pmus, list);
}
list_for_each_entry_continue(pmu, &other_pmus, list) {
- int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");
+ size_t pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "");

if (last_pmu_name_len == pmu_name_len &&
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
@@ -581,7 +581,7 @@ static int build_format_string(void *state, const char *name, int config,
const unsigned long *bits)
{
struct build_format_string_args *args = state;
- unsigned int num_bits;
+ size_t num_bits;
int ret1, ret2 = 0;

(void)config;
@@ -631,14 +631,14 @@ void perf_pmus__print_raw_pmu_events(const struct print_callbacks *print_cb, voi
.long_string = STRBUF_INIT,
.num_formats = 0,
};
- int len = pmu_name_len_no_suffix(pmu->name);
+ size_t len = pmu_name_len_no_suffix(pmu->name);
const char *desc = "(see 'man perf-list' or 'man perf-record' on how to encode it)";

if (!pmu->is_core)
desc = NULL;

- strbuf_addf(&format_args.short_string, "%.*s/", len, pmu->name);
- strbuf_addf(&format_args.long_string, "%.*s/", len, pmu->name);
+ strbuf_addf(&format_args.short_string, "%.*s/", (int)len, pmu->name);
+ strbuf_addf(&format_args.long_string, "%.*s/", (int)len, pmu->name);
perf_pmu__for_each_format(pmu, &format_args, build_format_string);

if (format_args.num_formats > 3)
diff --git a/tools/perf/util/tool_pmu.c b/tools/perf/util/tool_pmu.c
index 97b327d1ce4a..155eea8a0a49 100644
--- a/tools/perf/util/tool_pmu.c
+++ b/tools/perf/util/tool_pmu.c
@@ -103,7 +103,7 @@ enum tool_pmu_event evsel__tool_event(const struct evsel *evsel)

const char *evsel__tool_pmu_event_name(const struct evsel *evsel)
{
- return tool_pmu__event_to_str(evsel->core.attr.config);
+ return tool_pmu__event_to_str((enum tool_pmu_event)evsel->core.attr.config);
}

static bool read_until_char(struct io *io, char e)
--
2.49.0.504.g3bcea36a83-goog