Skip to content

Fix format specifiers#188

Merged
1a1a11a merged 3 commits into1a1a11a:developfrom
laustam:fix-format-specifiers
Jun 18, 2025
Merged

Fix format specifiers#188
1a1a11a merged 3 commits into1a1a11a:developfrom
laustam:fix-format-specifiers

Conversation

@laustam
Copy link
Copy Markdown
Contributor

@laustam laustam commented May 28, 2025

This PR fixes an issue related to Issue #182.

Issue

A common error reported by Clang is the use of incorrect format specifiers in the printf function. More specifically, an example of the error shown here shows that an argument of type int64_t should use the %lld format specifier in printf calls:

libCacheSim/cache/eviction/LIRS.c:696:53: error: format specifies type 'unsigned long' but the argument has type 'int64_t' (aka 'long long') [-Werror,-Wformat]
    printf("%ld(%lu, %s, %s)->", (long)obj->obj_id, obj->obj_size,
                ~~~                                 ^~~~~~~~~~~~~
                %lld

Fix

It turns out that format specifiers (i.e. %lld) behave differently across platforms due to differences in how data types are defined and how format specifiers are interpreted (on Ubuntu/Linux, int64_t requires the %ld format specifier). The solution for this is to use the format macros defined in <inttypes.h>, specifically PRId64 in this case - this expands to the correct format specifier depending on the platform and ensures that this is portable and has correct formatting across all systems.

@laustam
Copy link
Copy Markdown
Contributor Author

laustam commented May 28, 2025

I believe PR #187 should solve the code quality complaints

@1a1a11a
Copy link
Copy Markdown
Owner

1a1a11a commented Jun 13, 2025

let me know if I need to take a look

@1a1a11a 1a1a11a requested a review from Copilot June 18, 2025 20:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes cross-platform printf format issues by replacing platform‐dependent specifiers (such as %ld and %lu) with standardized macros from <inttypes.h> (such as PRId64 and PRIu64). Key changes include:

  • Updating printf and ERROR statements across multiple files to use portable format macros.
  • Adjusting multi-line formatting in debug and error messages for improved readability.
  • Ensuring consistent usage of format specifiers in both C and C++ code parts.

Reviewed Changes

Copilot reviewed 12 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/test_dataStructure.c Replaced %lu with "%" PRIu64 in test output.
libCacheSim/traceReader/customizedReader/lcs.c Updated error and status messages to use portable format macros.
libCacheSim/mrcProfiler/mrcProfiler.h Fixed format specifiers in printed output (using PRId64 for integer values).
libCacheSim/include/libCacheSim/reader.h Reformatted multi-line comments and function signatures with no logic change.
libCacheSim/dataStructure/splay_tuple.c Replaced legacy specifiers with standard macros in tree printing function.
libCacheSim/dataStructure/histogram.c Updated CSV export format specifiers to use PRIu64.
libCacheSim/dataStructure/hashtable/chainedHashTableV2.c Reformatted error messages and adjusted function signatures.
libCacheSim/cache/eviction/cpp/GDSF.cpp Adjusted multi-line formatting and corrected format strings.
libCacheSim/cache/eviction/SLRUv0.c Updated printf specifiers for object details to use PRId64.
libCacheSim/cache/eviction/LIRS.c Corrected multiple printf format specifiers using PRId64 and PRIu64.
libCacheSim/cache/eviction/BeladySize.c Reformatted function signatures and log messages with portable macros.
libCacheSim/cache/admission/adaptsize/adaptsize_interface.cpp Updated admission interface debug prints to use PRIu64.

Comment thread libCacheSim/cache/eviction/LIRS.c Outdated
cursor[bot]

This comment was marked as outdated.

Repository owner deleted a comment from codemetrics-ai Bot Jun 18, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@1a1a11a 1a1a11a merged commit 783d575 into 1a1a11a:develop Jun 18, 2025
6 of 7 checks passed
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Inconsistent Format Specifiers for `obj_id`

The obj_id format specifier is inconsistent across printf statements. Line 714 uses PRId64 without casting for obj_id, while lines 723 and 733 incorrectly retain %ld with a (long) cast. This contrasts with obj_size, which was consistently updated to PRId64 on all three lines. All obj_id fields, being of the same type, should use a consistent format for portability.

libCacheSim/cache/eviction/LIRS.c#L713-L736

while (obj) {
printf("%" PRId64 "(%" PRId64 ", %s, %s)->", obj->obj_id, obj->obj_size,
obj->LIRS.in_cache ? "R" : "N", obj->LIRS.is_LIR ? "L" : "H");
obj = obj->queue.next;
}
printf("\n");
printf("Q Stack: \n");
cache_obj_t *obj_q = ((LRU_params_t *)params->LRU_q->eviction_params)->q_head;
while (obj_q) {
printf("%ld(%" PRId64 ", %s, %s)->", (long)obj_q->obj_id, obj_q->obj_size,
obj_q->LIRS.in_cache ? "R" : "N", obj_q->LIRS.is_LIR ? "L" : "H");
obj_q = obj_q->queue.next;
}
printf("\n");
printf("NH Stack: \n");
cache_obj_t *obj_nh =
((LRU_params_t *)params->LRU_nh->eviction_params)->q_head;
while (obj_nh) {
printf("%ld(%" PRId64 ", %s, %s)->", (long)obj_nh->obj_id, obj_nh->obj_size,
obj_nh->LIRS.in_cache ? "R" : "N", obj_nh->LIRS.is_LIR ? "L" : "H");
obj_nh = obj_nh->queue.next;
}

Fix in Cursor


Was this report helpful? Give feedback by reacting with 👍 or 👎

@1a1a11a
Copy link
Copy Markdown
Owner

1a1a11a commented Jun 18, 2025

It looks like clang-format give a lot of errors, so revert for now, can you fix them?

1a1a11a added a commit that referenced this pull request Jun 18, 2025
1a1a11a added a commit that referenced this pull request Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants