[cfe-commits] r108750 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/Sema/format-strings-scanf.c
Ted Kremenek
kremenek at apple.com
Mon Jul 19 14:25:57 PDT 2010
Author: kremenek
Date: Mon Jul 19 16:25:57 2010
New Revision: 108750
URL: http://llvm.org/viewvc/llvm-project?rev=108750&view=rev
Log:
Hook up 'invalid conversion' warning for scanf format strings.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/test/Sema/format-strings-scanf.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=108750&r1=108749&r2=108750&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jul 19 16:25:57 2010
@@ -2936,7 +2936,7 @@
"more '%%' conversions than data arguments">, InGroup<Format>;
def warn_printf_data_arg_not_used : Warning<
"data argument not used by format string">, InGroup<FormatExtraArgs>;
-def warn_printf_invalid_conversion : Warning<
+def warn_format_invalid_conversion : Warning<
"invalid conversion specifier '%0'">, InGroup<Format>;
def warn_printf_incomplete_specifier : Warning<
"incomplete format specifier">, InGroup<Format>;
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=108750&r1=108749&r2=108750&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Jul 19 16:25:57 2010
@@ -1160,6 +1160,11 @@
void HandleNullChar(const char *nullCharacter);
protected:
+ bool HandleInvalidConversionSpecifier(unsigned argIndex, SourceLocation Loc,
+ const char *startSpec,
+ unsigned specifierLen,
+ const char *csStart, unsigned csLen);
+
SourceRange getFormatStringRange();
CharSourceRange getSpecifierRange(const char *startSpecifier,
unsigned specifierLen);
@@ -1237,6 +1242,36 @@
}
}
+bool
+CheckFormatHandler::HandleInvalidConversionSpecifier(unsigned argIndex,
+ SourceLocation Loc,
+ const char *startSpec,
+ unsigned specifierLen,
+ const char *csStart,
+ unsigned csLen) {
+
+ bool keepGoing = true;
+ if (argIndex < NumDataArgs) {
+ // Consider the argument coverered, even though the specifier doesn't
+ // make sense.
+ CoveredArgs.set(argIndex);
+ }
+ else {
+ // If argIndex exceeds the number of data arguments we
+ // don't issue a warning because that is just a cascade of warnings (and
+ // they may have intended '%%' anyway). We don't want to continue processing
+ // the format string after this point, however, as we will like just get
+ // gibberish when trying to match arguments.
+ keepGoing = false;
+ }
+
+ S.Diag(Loc, diag::warn_format_invalid_conversion)
+ << llvm::StringRef(csStart, csLen)
+ << getSpecifierRange(startSpec, specifierLen);
+
+ return keepGoing;
+}
+
//===--- CHECK: Printf format string checking ------------------------------===//
namespace {
@@ -1281,31 +1316,13 @@
const analyze_printf::PrintfSpecifier &FS,
const char *startSpecifier,
unsigned specifierLen) {
-
- unsigned argIndex = FS.getArgIndex();
- bool keepGoing = true;
- if (argIndex < NumDataArgs) {
- // Consider the argument coverered, even though the specifier doesn't
- // make sense.
- CoveredArgs.set(argIndex);
- }
- else {
- // If argIndex exceeds the number of data arguments we
- // don't issue a warning because that is just a cascade of warnings (and
- // they may have intended '%%' anyway). We don't want to continue processing
- // the format string after this point, however, as we will like just get
- // gibberish when trying to match arguments.
- keepGoing = false;
- }
-
const analyze_printf::ConversionSpecifier &CS =
- FS.getConversionSpecifier();
- SourceLocation Loc = getLocationOfByte(CS.getStart());
- S.Diag(Loc, diag::warn_printf_invalid_conversion)
- << llvm::StringRef(CS.getStart(), CS.getLength())
- << getSpecifierRange(startSpecifier, specifierLen);
+ FS.getConversionSpecifier();
- return keepGoing;
+ return HandleInvalidConversionSpecifier(FS.getArgIndex(),
+ getLocationOfByte(CS.getStart()),
+ startSpecifier, specifierLen,
+ CS.getStart(), CS.getLength());
}
bool CheckPrintfHandler::HandleAmount(
@@ -1596,6 +1613,11 @@
bool HandleScanfSpecifier(const analyze_scanf::ScanfSpecifier &FS,
const char *startSpecifier,
unsigned specifierLen);
+
+ bool HandleInvalidScanfConversionSpecifier(
+ const analyze_scanf::ScanfSpecifier &FS,
+ const char *startSpecifier,
+ unsigned specifierLen);
void HandleIncompleteScanList(const char *start, const char *end);
};
@@ -1607,6 +1629,20 @@
<< getSpecifierRange(start, end - start);
}
+bool CheckScanfHandler::HandleInvalidScanfConversionSpecifier(
+ const analyze_scanf::ScanfSpecifier &FS,
+ const char *startSpecifier,
+ unsigned specifierLen) {
+
+ const analyze_scanf::ConversionSpecifier &CS =
+ FS.getConversionSpecifier();
+
+ return HandleInvalidConversionSpecifier(FS.getArgIndex(),
+ getLocationOfByte(CS.getStart()),
+ startSpecifier, specifierLen,
+ CS.getStart(), CS.getLength());
+}
+
bool CheckScanfHandler::HandleScanfSpecifier(
const analyze_scanf::ScanfSpecifier &FS,
const char *startSpecifier,
Modified: cfe/trunk/test/Sema/format-strings-scanf.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-scanf.c?rev=108750&r1=108749&r2=108750&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-scanf.c (original)
+++ cfe/trunk/test/Sema/format-strings-scanf.c Mon Jul 19 16:25:57 2010
@@ -15,4 +15,5 @@
unsigned short s_x;
scanf ("%" "hu" "\n", &s_x); // no-warning
+ scanf("%y", i); // expected-warning{{invalid conversion specifier 'y'}}
}
More information about the cfe-commits
mailing list