diff options
author | Bob Wilson <bob.wilson@apple.com> | 2016-03-11 21:55:37 +0000 |
---|---|---|
committer | Bob Wilson <bob.wilson@apple.com> | 2016-03-11 21:55:37 +0000 |
commit | cf2cf0dba47269efb9286b77088ac66cb3d22dcf (patch) | |
tree | d9c02aae8d0c70d79a4eb8e7cea86548b1e12f76 /clang/lib | |
parent | f2bc850ee8ab4bdbf0909756397fc54dfc100a84 (diff) | |
download | bcm5719-llvm-cf2cf0dba47269efb9286b77088ac66cb3d22dcf.tar.gz bcm5719-llvm-cf2cf0dba47269efb9286b77088ac66cb3d22dcf.zip |
Add fix-it for format-security warnings.
llvm-svn: 263299
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index eb5b5890495..966d3493926 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -3621,20 +3621,32 @@ bool Sema::CheckFormatArguments(ArrayRef<const Expr *> Args, // format is either NSString or CFString. This is a hack to prevent // diag when using the NSLocalizedString and CFCopyLocalizedString macros // which are usually used in place of NS and CF string literals. - if (Type == FST_NSString && - SourceMgr.isInSystemMacro(Args[format_idx]->getLocStart())) + SourceLocation FormatLoc = Args[format_idx]->getLocStart(); + if (Type == FST_NSString && SourceMgr.isInSystemMacro(FormatLoc)) return false; // If there are no arguments specified, warn with -Wformat-security, otherwise // warn only with -Wformat-nonliteral. - if (Args.size() == firstDataArg) - Diag(Args[format_idx]->getLocStart(), - diag::warn_format_nonliteral_noargs) + if (Args.size() == firstDataArg) { + const SemaDiagnosticBuilder &D = + Diag(FormatLoc, diag::warn_format_nonliteral_noargs); + switch (Type) { + default: + D << OrigFormatExpr->getSourceRange(); + break; + case FST_Kprintf: + case FST_FreeBSDKPrintf: + case FST_Printf: + D << FixItHint::CreateInsertion(FormatLoc, "\"%s\", "); + break; + case FST_NSString: + D << FixItHint::CreateInsertion(FormatLoc, "@\"%@\", "); + break; + } + } else { + Diag(FormatLoc, diag::warn_format_nonliteral) << OrigFormatExpr->getSourceRange(); - else - Diag(Args[format_idx]->getLocStart(), - diag::warn_format_nonliteral) - << OrigFormatExpr->getSourceRange(); + } return false; } |