diff options
author | Hans Wennborg <hans@hanshq.net> | 2011-12-10 13:20:11 +0000 |
---|---|---|
committer | Hans Wennborg <hans@hanshq.net> | 2011-12-10 13:20:11 +0000 |
commit | b1a5e09f6fda84cd1b4af43fa4a85547bf2c2c56 (patch) | |
tree | bfb864b39e601d7ac5c827fd81e381d411711f81 /clang/lib/Sema/SemaChecking.cpp | |
parent | 29dad49cfd3fe31596b5e574578e2c9383100d4b (diff) | |
download | bcm5719-llvm-b1a5e09f6fda84cd1b4af43fa4a85547bf2c2c56.tar.gz bcm5719-llvm-b1a5e09f6fda84cd1b4af43fa4a85547bf2c2c56.zip |
Check that arguments to a scanf call match the format specifier,
and offer fixits when there is a mismatch.
llvm-svn: 146326
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index db60f2388aa..1c93931b5c1 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -2371,8 +2371,38 @@ bool CheckScanfHandler::HandleScanfSpecifier( if (!CheckNumArgs(FS, CS, startSpecifier, specifierLen, argIndex)) return false; - // FIXME: Check that the argument type matches the format specifier. - + // Check that the argument type matches the format specifier. + const Expr *Ex = getDataArg(argIndex); + const analyze_scanf::ScanfArgTypeResult &ATR = FS.getArgType(S.Context); + if (ATR.isValid() && !ATR.matchesType(S.Context, Ex->getType())) { + ScanfSpecifier fixedFS = FS; + bool success = fixedFS.fixType(Ex->getType(), S.getLangOptions()); + + if (success) { + // Get the fix string from the fixed format specifier. + llvm::SmallString<128> buf; + llvm::raw_svector_ostream os(buf); + fixedFS.toString(os); + + EmitFormatDiagnostic( + S.PDiag(diag::warn_printf_conversion_argument_type_mismatch) + << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() + << Ex->getSourceRange(), + getLocationOfByte(CS.getStart()), + /*IsStringLocation*/true, + getSpecifierRange(startSpecifier, specifierLen), + FixItHint::CreateReplacement( + getSpecifierRange(startSpecifier, specifierLen), + os.str())); + } else { + S.Diag(getLocationOfByte(CS.getStart()), + diag::warn_printf_conversion_argument_type_mismatch) + << ATR.getRepresentativeTypeName(S.Context) << Ex->getType() + << getSpecifierRange(startSpecifier, specifierLen) + << Ex->getSourceRange(); + } + } + return true; } |