From 4cd5791f4aa6deb572979bb38c1deedbc155efe0 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 29 Sep 2011 05:52:16 +0000 Subject: [PATCH] Do not warn about empty format strings when there are no data arguments. Fixes . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140777 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaChecking.cpp | 13 ++++++------- test/Sema/format-strings.c | 7 ++++++- 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(OrigFormatExpr), Str, - HasVAListArg, TheCall, format_idx); + numDataArgs, isa(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(OrigFormatExpr), Str, - HasVAListArg, TheCall, format_idx); + numDataArgs, isa(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, ...) -- 2.40.0