8000 gh-112301: Add -Wformat=2 compiler option to NODIST by nohlson · Pull Request #122474 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112301: Add -Wformat=2 compiler option to NODIST #122474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Limit pragmas to gcc/clang
  • Loading branch information
nohlson committed Aug 5, 2024
commit c4b8cf9d602429456a390b71a8cc2ae799a84953
4 changes: 4 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2855,8 +2855,10 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
// constant strings, and the variable used to index into the arrays
// is only assigned known constant values. Ignore warnings related
// to the format string not being a string literal.
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
Copy link
Member

Choose a reason for hiding this comment

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

Consider using _Py_COMP_DIAG_PUSH/_Py_COMP_DIAG_POP, and adding a macro like _Py_COMP_DIAG_IGNORE_DEPR_DECLS, to make this easier to port to other compilers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@encukou created macro for ignoring format nonliterals and applied it to this block

#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
switch (sizemod) {
case F_LONG:
len = issigned ?
Expand Down Expand Up @@ -2887,7 +2889,9 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer,
sprintf(buffer, fmt, va_arg(*vargs, unsigned int));
break;
}
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
assert(len >= 0);

int sign = (buffer[0] == '-');
Expand Down
4 changes: 4 additions & 0 deletions Python/getversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ void _Py_InitVersion(void)
#endif
// The format string is defined above and is observably safe.
Copy link
Member

Choose a reason for hiding this comment

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

Could it be switched to a #defined literal? That way it could look safe to the compiler, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just pushed a change. I figure instead of going through the trouble defining a format string in the preprocessor #if/#else blocks and adding all of the diagnostic pragmas we can just put the PyOS_snprintf() with the relevant format string literals in the respective #if/#else blocks. We eliminate the root cause of the warning instead of ignore it.

// Ignore warnings related to non-literal format strings.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
PyOS_snprintf(version, sizeof(version), buildinfo_format,
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}

const char *
Expand Down
Loading
0