-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Printf format checks are skipped for constexpr format strings #55805
Labels
clang:diagnostics
New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
confirmed
Verified by a second party
Comments
EugeneZelenko
added
clang:diagnostics
New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
and removed
new issue
labels
Jun 1, 2022
clang thinks these expressions are NOT literal, statically evaluated expressions are not considered as literal currently, and it fallbacks to warn you int main() {
printf(foo(), "abc", "def"); // CallExprClass
printf(Bar::value, "abc", "def"); // DeclRefExprClass
return 0;
} Related to #56583 |
May be helpful to see // Determine if an expression is a string literal or constant string.
// If this function returns false on the arguments to a function expecting a
// format string, we will usually need to emit a warning.
// True string literals are then checked by CheckFormatString.
static StringLiteralCheckType
checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
Sema::FormatArgumentPassingKind APK, unsigned format_idx,
unsigned firstDataArg, Sema::FormatStringType Type,
Sema::VariadicCallType CallType, bool InFunctionCall,
llvm::SmallBitVector &CheckedVarArgs,
UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset,
bool IgnoreStringsWithoutSpecifiers = false) {
/* statements */
} |
inclyc
added a commit
that referenced
this issue
Aug 4, 2022
This patch enhances clang's ability to check compile-time determinable string literals as format strings, and can give FixIt hints at literals (unlike gcc). Issue #55805 mentiond two compile-time string cases. And this patch partially fixes one. ``` constexpr const char* foo() { return "%s %d"; } int main() { printf(foo(), "abc", "def"); return 0; } ``` This patch enables clang check format string for this: ``` <source>:4:24: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat] printf(foo(), "abc", "def"); ~~~~~ ^~~~~ <source>:2:42: note: format string is defined here constexpr const char *foo() { return "%s %d"; } ^~ %s 1 warning generated. ``` Reviewed By: aaron.ballman Signed-off-by: YingChi Long <me@inclyc.cn> Differential Revision: https://reviews.llvm.org/D130906
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:diagnostics
New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
confirmed
Verified by a second party
demo.cpp:
Clang does not produce any warning. gcc 11.2.0 produces both warnings:
Clang version is:
The text was updated successfully, but these errors were encountered: