aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2011-09-29 05:52:16 +0000
committerTed Kremenek <kremenek@apple.com>2011-09-29 05:52:16 +0000
commit4cd5791f4aa6deb572979bb38c1deedbc155efe0 (patch)
tree1cfded7c8c6851db997d70a0350cbb05dec84436
parente37cdc42bf7298a974dfdfa9c03ef11398e7c889 (diff)
Do not warn about empty format strings when there are no data arguments. Fixes <rdar://problem/9473155>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140777 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Sema/SemaChecking.cpp13
-rw-r--r--test/Sema/format-strings.c7
2 files changed, 12 insertions, 8 deletions
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index c29ffb5368..e0b24c4be6 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -1880,9 +1880,10 @@ void Sema::CheckFormatString(const StringLiteral *FExpr,
StringRef StrRef = FExpr->getString();
const char *Str = StrRef.data();
unsigned StrLen = StrRef.size();
+ const unsigned numDataArgs = TheCall->getNumArgs() - firstDataArg;
// CHECK: empty format string?
- if (StrLen == 0) {
+ if (StrLen == 0 && numDataArgs > 0) {
Diag(FExpr->getLocStart(), diag::warn_empty_format_string)
<< OrigFormatExpr->getSourceRange();
return;
@@ -1890,18 +1891,16 @@ void Sema::CheckFormatString(const StringLiteral *FExpr,
if (isPrintf) {
CheckPrintfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
- TheCall->getNumArgs() - firstDataArg,
- isa<ObjCStringLiteral>(OrigFormatExpr), Str,
- HasVAListArg, TheCall, format_idx);
+ numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
+ Str, HasVAListArg, TheCall, format_idx);
if (!analyze_format_string::ParsePrintfString(H, Str, Str + StrLen))
H.DoneProcessing();
}
else {
CheckScanfHandler H(*this, FExpr, OrigFormatExpr, firstDataArg,
- TheCall->getNumArgs() - firstDataArg,
- isa<ObjCStringLiteral>(OrigFormatExpr), Str,
- HasVAListArg, TheCall, format_idx);
+ numDataArgs, isa<ObjCStringLiteral>(OrigFormatExpr),
+ Str, HasVAListArg, TheCall, format_idx);
if (!analyze_format_string::ParseScanfString(H, Str, Str + StrLen))
H.DoneProcessing();
diff --git a/test/Sema/format-strings.c b/test/Sema/format-strings.c
index 20c665b978..6b5f7e2c26 100644
--- a/test/Sema/format-strings.c
+++ b/test/Sema/format-strings.c
@@ -87,7 +87,12 @@ void check_empty_format_string(char* buf, ...)
va_list ap;
va_start(ap,buf);
vprintf("",ap); // expected-warning {{format string is empty}}
- sprintf(buf,""); // expected-warning {{format string is empty}}
+ sprintf(buf, "", 1); // expected-warning {{format string is empty}}
+
+ // Don't warn about empty format strings when there are no data arguments.
+ // This can arise from macro expansions and non-standard format string
+ // functions.
+ sprintf(buf, ""); // no-warning
}
void check_wide_string(char* b, ...)