Message refactorization: checkio.h/cpp

This commit is contained in:
PKEuS 2012-07-13 10:36:58 -07:00
parent 41fecb2e6d
commit 1e704edbd5
3 changed files with 83 additions and 82 deletions

View File

@ -252,7 +252,7 @@ void CheckIO::checkFileUsage()
void CheckIO::fflushOnInputStreamError(const Token *tok, const std::string &varname) void CheckIO::fflushOnInputStreamError(const Token *tok, const std::string &varname)
{ {
reportError(tok, Severity::error, reportError(tok, Severity::error,
"fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour."); "fflushOnInputStream", "fflush() called on input stream '" + varname + "' results in undefined behaviour.");
} }
void CheckIO::ioWithoutPositioningError(const Token *tok) void CheckIO::ioWithoutPositioningError(const Token *tok)
@ -265,13 +265,13 @@ void CheckIO::readWriteOnlyFileError(const Token *tok)
{ {
reportError(tok, Severity::error, reportError(tok, Severity::error,
"readWriteOnlyFile", "Read operation on a file that was only opened for writing."); "readWriteOnlyFile", "Read operation on a file that was opened only for writing.");
} }
void CheckIO::writeReadOnlyFileError(const Token *tok) void CheckIO::writeReadOnlyFileError(const Token *tok)
{ {
reportError(tok, Severity::error, reportError(tok, Severity::error,
"writeReadOnlyFile", "Write operation on a file that was only opened for reading."); "writeReadOnlyFile", "Write operation on a file that was opened only for reading.");
} }
void CheckIO::useClosedFileError(const Token *tok) void CheckIO::useClosedFileError(const Token *tok)
@ -505,7 +505,7 @@ void CheckIO::checkWrongPrintfScanfArguments()
if (!width.empty()) { if (!width.empty()) {
int numWidth = std::atoi(width.c_str()); int numWidth = std::atoi(width.c_str());
if (numWidth != (variableInfo->dimension(0) - 1)) if (numWidth != (variableInfo->dimension(0) - 1))
invalidScanfFormatWidthError(tok, tok->str(), numFormat, numWidth, variableInfo); invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo);
} }
} }
} else if (!scan) { } else if (!scan) {
@ -599,7 +599,7 @@ void CheckIO::wrongPrintfScanfArgumentsError(const Token* tok,
<< " parameters but " << " parameters but "
<< (numFormat > numFunction ? "only " : "") << (numFormat > numFunction ? "only " : "")
<< numFunction << numFunction
<< " are given"; << " are given.";
reportError(tok, severity, "wrongPrintfScanfArgNum", errmsg.str()); reportError(tok, severity, "wrongPrintfScanfArgNum", errmsg.str());
} }
@ -607,52 +607,52 @@ void CheckIO::wrongPrintfScanfArgumentsError(const Token* tok,
void CheckIO::invalidScanfArgTypeError(const Token* tok, const std::string &functionName, unsigned int numFormat) void CheckIO::invalidScanfArgTypeError(const Token* tok, const std::string &functionName, unsigned int numFormat)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << functionName << " argument no. " << numFormat << ": requires non-const pointers or arrays as arguments"; errmsg << functionName << " argument no. " << numFormat << ": requires a non-const pointer or array as argument.";
reportError(tok, Severity::warning, "invalidScanfArgType", errmsg.str()); reportError(tok, Severity::warning, "invalidScanfArgType", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_s(const Token* tok, unsigned int numFormat) void CheckIO::invalidPrintfArgTypeError_s(const Token* tok, unsigned int numFormat)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%s in format string (no. " << numFormat << ") requires a char* given in the argument list"; errmsg << "%s in format string (no. " << numFormat << ") requires a char* given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_s", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_s", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_n(const Token* tok, unsigned int numFormat) void CheckIO::invalidPrintfArgTypeError_n(const Token* tok, unsigned int numFormat)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%n in format string (no. " << numFormat << ") requires a pointer to an non-const integer given in the argument list"; errmsg << "%n in format string (no. " << numFormat << ") requires a pointer to an non-const integer given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_n", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_n", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_p(const Token* tok, unsigned int numFormat) void CheckIO::invalidPrintfArgTypeError_p(const Token* tok, unsigned int numFormat)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%p in format string (no. " << numFormat << ") requires an integer or pointer given in the argument list"; errmsg << "%p in format string (no. " << numFormat << ") requires an address given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_p", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_p", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, char c) void CheckIO::invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, char c)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an integer given in the argument list"; errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an integer given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_int", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_int", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, char c) void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, char c)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an unsigned integer given in the argument list"; errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an unsigned integer given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_uint", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_uint", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, char c) void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, char c)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a signed integer given in the argument list"; errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a signed integer given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_sint", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_sint", errmsg.str());
} }
void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c) void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a floating point number given in the argument list"; errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a floating point number given in the argument list.";
reportError(tok, Severity::warning, "invalidPrintfArgType_float", errmsg.str()); reportError(tok, Severity::warning, "invalidPrintfArgType_float", errmsg.str());
} }
void CheckIO::invalidScanfFormatWidthError(const Token* tok, const std::string &functionName, unsigned int numFormat, int width, const Variable *var) void CheckIO::invalidScanfFormatWidthError(const Token* tok, unsigned int numFormat, int width, const Variable *var)
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
Severity::SeverityType severity = Severity::warning; Severity::SeverityType severity = Severity::warning;
@ -663,16 +663,16 @@ void CheckIO::invalidScanfFormatWidthError(const Token* tok, const std::string &
if (!_settings->inconclusive) if (!_settings->inconclusive)
return; return;
inconclusive = true; inconclusive = true;
errmsg << functionName << " width " << width << " in format string (no. " << numFormat << ") is smaller than destination" errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is smaller than destination buffer"
<< ": " << var->name() << "[" << var->dimension(0) << "]"; << " '" << var->name() << "[" << var->dimension(0) << "]'.";
} else { } else {
errmsg << functionName << " width " << width << " in format string (no. " << numFormat << ") is larger than destination" errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is larger than destination buffer '"
<< ", use %" << (var->dimension(0) - 1) << "s to prevent overflowing destination: " << var->name() << "[" << var->dimension(0) << "]"; << var->name() << "[" << var->dimension(0) << "]', use %" << (var->dimension(0) - 1) << "s to prevent overflowing it.";
severity = Severity::error; severity = Severity::error;
} }
} else } else
errmsg << functionName << " width " << width << " in format string (no. " << numFormat << ") doesn't match destination"; errmsg << "Width " << width << " given in format string (no. " << numFormat << ") doesn't match destination buffer.";
reportError(tok, severity, "invalidScanfFormatWidth", errmsg.str(), inconclusive); reportError(tok, severity, "invalidScanfFormatWidth", errmsg.str(), inconclusive);
} }

View File

@ -90,7 +90,7 @@ private:
void invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, char c); void invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, char c);
void invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, char c); void invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, char c);
void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c); void invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c);
void invalidScanfFormatWidthError(const Token* tok, const std::string &functionName, unsigned int numFormat, int width, const Variable *var); void invalidScanfFormatWidthError(const Token* tok, unsigned int numFormat, int width, const Variable *var);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
CheckIO c(0, settings, errorLogger); CheckIO c(0, settings, errorLogger);
@ -111,7 +111,7 @@ private:
c.invalidPrintfArgTypeError_uint(0, 1, 'u'); c.invalidPrintfArgTypeError_uint(0, 1, 'u');
c.invalidPrintfArgTypeError_sint(0, 1, 'i'); c.invalidPrintfArgTypeError_sint(0, 1, 'i');
c.invalidPrintfArgTypeError_float(0, 1, 'f'); c.invalidPrintfArgTypeError_float(0, 1, 'f');
c.invalidScanfFormatWidthError(0, "scanf", 10, 5, NULL); c.invalidScanfFormatWidthError(0, 10, 5, NULL);
} }
std::string myName() const { std::string myName() const {
@ -121,11 +121,12 @@ private:
std::string classInfo() const { std::string classInfo() const {
return "Check input/output operations.\n" return "Check input/output operations.\n"
"* Bad usage of the function 'sprintf' (overlapping data)\n" "* Bad usage of the function 'sprintf' (overlapping data)\n"
"* Missing or wrong width specifiers in 'scanf' format string\n"
"* Use a file that has been closed\n" "* Use a file that has been closed\n"
"* File input/output without positioning results in undefined behaviour\n" "* File input/output without positioning results in undefined behaviour\n"
"* Read to a file that has only been opened for writing (or vice versa)\n" "* Read to a file that has only been opened for writing (or vice versa)\n"
"* Using fflush() on an input stream\n" "* Using fflush() on an input stream\n"
"* Invalid usage of output stream. For example: std::cout << std::cout;'\n" "* Invalid usage of output stream. For example: 'std::cout << std::cout;'\n"
"* Wrong number of arguments given to 'printf' or 'scanf;'\n"; "* Wrong number of arguments given to 'printf' or 'scanf;'\n";
} }
}; };

View File

@ -126,7 +126,7 @@ private:
" rewind(f);\n" " rewind(f);\n"
" fwrite(buffer, 5, 6, f);\n" " fwrite(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Write operation on a file that was only opened for reading.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Write operation on a file that was opened only for reading.\n", errout.str());
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
" f = fopen(name, \"r+\");\n" " f = fopen(name, \"r+\");\n"
@ -141,7 +141,7 @@ private:
" rewind(f);\n" " rewind(f);\n"
" fread(buffer, 5, 6, f);\n" " fread(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was only opened for writing.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
" f = fopen(name, \"w+\");\n" " f = fopen(name, \"w+\");\n"
@ -156,7 +156,7 @@ private:
" rewind(f);\n" " rewind(f);\n"
" fread(buffer, 5, 6, f);\n" " fread(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was only opened for writing.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
" f = fopen(name, \"a+\");\n" " f = fopen(name, \"a+\");\n"
@ -170,7 +170,7 @@ private:
" fwrite(buffer, 5, 6, f);\n" " fwrite(buffer, 5, 6, f);\n"
" fclose(f);\n" " fclose(f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was only opened for reading.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was opened only for reading.\n", errout.str());
// Call unknown function // Call unknown function
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
@ -187,14 +187,14 @@ private:
" clearerr(f);\n" " clearerr(f);\n"
" fread(buffer, 5, 6, f);\n" " fread(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was only opened for writing.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Read operation on a file that was opened only for writing.\n", errout.str());
// freopen and tmpfile // freopen and tmpfile
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
" f = freopen(name, \"r\", f);\n" " f = freopen(name, \"r\", f);\n"
" fwrite(buffer, 5, 6, f);\n" " fwrite(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was only opened for reading.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Write operation on a file that was opened only for reading.\n", errout.str());
// Crash tests // Crash tests
check("void foo(FILE*& f) {\n" check("void foo(FILE*& f) {\n"
@ -229,7 +229,7 @@ private:
" if(a) fwrite(buffer, 5, 6, f);\n" " if(a) fwrite(buffer, 5, 6, f);\n"
" else fread(buffer, 5, 6, f);\n" " else fread(buffer, 5, 6, f);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Read operation on a file that was only opened for writing.\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Read operation on a file that was opened only for writing.\n", errout.str());
} }
void useClosedFile() { void useClosedFile() {
@ -343,7 +343,7 @@ private:
"{\n" "{\n"
" fflush(stdin);\n" " fflush(stdin);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) fflush() called on input stream \"stdin\" may result in undefined behaviour.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) fflush() called on input stream 'stdin' results in undefined behaviour.\n", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"
@ -382,7 +382,7 @@ private:
" scanf(\"aa%ld\", &a);\n" // No %s " scanf(\"aa%ld\", &a);\n" // No %s
" scanf(\"%*[^~]\");\n" // Ignore input " scanf(\"%*[^~]\");\n" // Ignore input
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (warning) scanf format string has 0 parameters but 1 are given\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (warning) scanf format string has 0 parameters but 1 are given.\n", errout.str());
} }
void testScanf3() { void testScanf3() {
@ -419,10 +419,10 @@ private:
" fscanf(bar, \"%1d\", &foo, &bar);\n" " fscanf(bar, \"%1d\", &foo, &bar);\n"
" scanf(\"%*1x %1x %29s\", &count, KeyName, foo);\n" " scanf(\"%*1x %1x %29s\", &count, KeyName, foo);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (warning) scanf format string has 0 parameters but 1 are given\n" ASSERT_EQUALS("[test.cpp:2]: (warning) scanf format string has 0 parameters but 1 are given.\n"
"[test.cpp:3]: (warning) scanf format string has 1 parameters but 2 are given\n" "[test.cpp:3]: (warning) scanf format string has 1 parameters but 2 are given.\n"
"[test.cpp:4]: (warning) fscanf format string has 1 parameters but 2 are given\n" "[test.cpp:4]: (warning) fscanf format string has 1 parameters but 2 are given.\n"
"[test.cpp:5]: (warning) scanf format string has 2 parameters but 3 are given\n", errout.str()); "[test.cpp:5]: (warning) scanf format string has 2 parameters but 3 are given.\n", errout.str());
check("void foo() {\n" check("void foo() {\n"
" scanf(\"%1d\");\n" " scanf(\"%1d\");\n"
@ -430,10 +430,10 @@ private:
" sscanf(bar, \"%1d%1d\", &foo);\n" " sscanf(bar, \"%1d%1d\", &foo);\n"
" scanf(\"%*1x %1x %29s\", &count);\n" " scanf(\"%*1x %1x %29s\", &count);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) scanf format string has 1 parameters but only 0 are given\n" ASSERT_EQUALS("[test.cpp:2]: (error) scanf format string has 1 parameters but only 0 are given.\n"
"[test.cpp:3]: (error) scanf format string has 2 parameters but only 1 are given\n" "[test.cpp:3]: (error) scanf format string has 2 parameters but only 1 are given.\n"
"[test.cpp:4]: (error) sscanf format string has 2 parameters but only 1 are given\n" "[test.cpp:4]: (error) sscanf format string has 2 parameters but only 1 are given.\n"
"[test.cpp:5]: (error) scanf format string has 2 parameters but only 1 are given\n", errout.str()); "[test.cpp:5]: (error) scanf format string has 2 parameters but only 1 are given.\n", errout.str());
check("void foo() {\n" check("void foo() {\n"
" char input[10];\n" " char input[10];\n"
@ -442,7 +442,7 @@ private:
" sscanf(input, \"%4s\", output);\n" " sscanf(input, \"%4s\", output);\n"
" sscanf(input, \"%5s\", output);\n" " sscanf(input, \"%5s\", output);\n"
"}", false); "}", false);
ASSERT_EQUALS("[test.cpp:6]: (error) sscanf width 5 in format string (no. 1) is larger than destination, use %4s to prevent overflowing destination: output[5]\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Width 5 given in format string (no. 1) is larger than destination buffer 'output[5]', use %4s to prevent overflowing it.\n", errout.str());
check("void foo() {\n" check("void foo() {\n"
" char input[10];\n" " char input[10];\n"
@ -452,8 +452,8 @@ private:
" sscanf(input, \"%4s\", output);\n" " sscanf(input, \"%4s\", output);\n"
" sscanf(input, \"%5s\", output);\n" " sscanf(input, \"%5s\", output);\n"
"}", true); "}", true);
ASSERT_EQUALS("[test.cpp:5]: (warning, inconclusive) sscanf width 3 in format string (no. 1) is smaller than destination: output[5]\n" ASSERT_EQUALS("[test.cpp:5]: (warning, inconclusive) Width 3 given in format string (no. 1) is smaller than destination buffer 'output[5]'.\n"
"[test.cpp:7]: (error) sscanf width 5 in format string (no. 1) is larger than destination, use %4s to prevent overflowing destination: output[5]\n" "[test.cpp:7]: (error) Width 5 given in format string (no. 1) is larger than destination buffer 'output[5]', use %4s to prevent overflowing it.\n"
"[test.cpp:4]: (warning) scanf without field width limits can crash with huge input data.\n", errout.str()); "[test.cpp:4]: (warning) scanf without field width limits can crash with huge input data.\n", errout.str());
} }
@ -469,24 +469,24 @@ private:
" sprintf(string1, \"%-*.*s\", 32, string2);\n" // #3364 " sprintf(string1, \"%-*.*s\", 32, string2);\n" // #3364
" snprintf(a, 9, \"%s%d\", \"11223344\");\n" // #3655 " snprintf(a, 9, \"%s%d\", \"11223344\");\n" // #3655
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (error) printf format string has 1 parameters but only 0 are given\n" ASSERT_EQUALS("[test.cpp:2]: (error) printf format string has 1 parameters but only 0 are given.\n"
"[test.cpp:3]: (error) printf format string has 2 parameters but only 1 are given\n" "[test.cpp:3]: (error) printf format string has 2 parameters but only 1 are given.\n"
"[test.cpp:4]: (error) printf format string has 3 parameters but only 2 are given\n" "[test.cpp:4]: (error) printf format string has 3 parameters but only 2 are given.\n"
"[test.cpp:5]: (error) printf format string has 3 parameters but only 2 are given\n" "[test.cpp:5]: (error) printf format string has 3 parameters but only 2 are given.\n"
"[test.cpp:6]: (error) printf format string has 3 parameters but only 2 are given\n" "[test.cpp:6]: (error) printf format string has 3 parameters but only 2 are given.\n"
"[test.cpp:7]: (error) fprintf format string has 2 parameters but only 0 are given\n" "[test.cpp:7]: (error) fprintf format string has 2 parameters but only 0 are given.\n"
"[test.cpp:8]: (error) snprintf format string has 2 parameters but only 0 are given\n" "[test.cpp:8]: (error) snprintf format string has 2 parameters but only 0 are given.\n"
"[test.cpp:9]: (error) sprintf format string has 3 parameters but only 2 are given\n" "[test.cpp:9]: (error) sprintf format string has 3 parameters but only 2 are given.\n"
"[test.cpp:10]: (error) snprintf format string has 2 parameters but only 1 are given\n", errout.str()); "[test.cpp:10]: (error) snprintf format string has 2 parameters but only 1 are given.\n", errout.str());
check("void foo(char *str) {\n" check("void foo(char *str) {\n"
" printf(\"\", 0);\n" " printf(\"\", 0);\n"
" printf(\"%u\", 123, bar());\n" " printf(\"%u\", 123, bar());\n"
" printf(\"%u%s\", 0, bar(), 43123);\n" " printf(\"%u%s\", 0, bar(), 43123);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (warning) printf format string has 0 parameters but 1 are given\n" ASSERT_EQUALS("[test.cpp:2]: (warning) printf format string has 0 parameters but 1 are given.\n"
"[test.cpp:3]: (warning) printf format string has 1 parameters but 2 are given\n" "[test.cpp:3]: (warning) printf format string has 1 parameters but 2 are given.\n"
"[test.cpp:4]: (warning) printf format string has 2 parameters but 3 are given\n", errout.str()); "[test.cpp:4]: (warning) printf format string has 2 parameters but 3 are given.\n", errout.str());
check("void foo(char *str) {\n" check("void foo(char *str) {\n"
" printf(\"%u\", 0);\n" " printf(\"%u\", 0);\n"
@ -512,9 +512,9 @@ private:
" printf(\"%s\", \"s4\");\n" " printf(\"%s\", \"s4\");\n"
" printf(\"%u\", s);\n" " printf(\"%u\", s);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %s in format string (no. 1) requires a char* given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %s in format string (no. 1) requires a char* given in the argument list.\n"
"[test.cpp:4]: (warning) %s in format string (no. 2) requires a char* given in the argument list\n" "[test.cpp:4]: (warning) %s in format string (no. 2) requires a char* given in the argument list.\n"
"[test.cpp:5]: (warning) %s in format string (no. 1) requires a char* given in the argument list\n", errout.str()); "[test.cpp:5]: (warning) %s in format string (no. 1) requires a char* given in the argument list.\n", errout.str());
check("void foo(const int* cpi, const int ci, int i, int* pi, std::string s) {\n" check("void foo(const int* cpi, const int ci, int i, int* pi, std::string s) {\n"
" printf(\"%n\", cpi);\n" " printf(\"%n\", cpi);\n"
@ -524,11 +524,11 @@ private:
" printf(\"%n\", s);\n" " printf(\"%n\", s);\n"
" printf(\"%n\", \"s4\");\n" " printf(\"%n\", \"s4\");\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list\n" ASSERT_EQUALS("[test.cpp:2]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
"[test.cpp:3]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list\n" "[test.cpp:3]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
"[test.cpp:4]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list\n" "[test.cpp:4]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
"[test.cpp:6]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list\n" "[test.cpp:6]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n"
"[test.cpp:7]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list\n", errout.str()); "[test.cpp:7]: (warning) %n in format string (no. 1) requires a pointer to an non-const integer given in the argument list.\n", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, unsigned int u) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, unsigned int u) {\n"
@ -541,9 +541,9 @@ private:
" printf(\"%X\", u);\n" " printf(\"%X\", u);\n"
" printf(\"%X\", i);\n" " printf(\"%X\", i);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %X in format string (no. 1) requires an integer given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %X in format string (no. 1) requires an integer given in the argument list.\n"
"[test.cpp:4]: (warning) %c in format string (no. 1) requires an integer given in the argument list\n" "[test.cpp:4]: (warning) %c in format string (no. 1) requires an integer given in the argument list.\n"
"[test.cpp:5]: (warning) %o in format string (no. 1) requires an integer given in the argument list\n", errout.str()); "[test.cpp:5]: (warning) %o in format string (no. 1) requires an integer given in the argument list.\n", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, double d, unsigned int u, unsigned char uc) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, double d, unsigned int u, unsigned char uc) {\n"
@ -556,10 +556,10 @@ private:
" printf(\"%i\", bp);\n" " printf(\"%i\", bp);\n"
" printf(\"%i\", uc);\n" // char is smaller than int, so there shouldn't be a problem " printf(\"%i\", uc);\n" // char is smaller than int, so there shouldn't be a problem
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %i in format string (no. 1) requires a signed integer given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %i in format string (no. 1) requires a signed integer given in the argument list.\n"
"[test.cpp:4]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list\n" "[test.cpp:4]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n"
"[test.cpp:5]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list\n" "[test.cpp:5]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n"
"[test.cpp:6]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list\n", errout.str()); "[test.cpp:6]: (warning) %d in format string (no. 1) requires a signed integer given in the argument list.\n", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, bool bo) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, double d, int i, bool bo) {\n"
@ -572,10 +572,10 @@ private:
" printf(\"%u\", bp);\n" " printf(\"%u\", bp);\n"
" printf(\"%u\", bo);\n" // bool shouldn't have a negative sign " printf(\"%u\", bo);\n" // bool shouldn't have a negative sign
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
"[test.cpp:4]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list\n" "[test.cpp:4]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
"[test.cpp:5]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list\n" "[test.cpp:5]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n"
"[test.cpp:6]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list\n", errout.str()); "[test.cpp:6]: (warning) %u in format string (no. 1) requires an unsigned integer given in the argument list.\n", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, char c) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, char c) {\n"
@ -585,8 +585,8 @@ private:
" printf(\"%p\", cpi);\n" " printf(\"%p\", cpi);\n"
" printf(\"%p\", b);\n" " printf(\"%p\", b);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %p in format string (no. 1) requires an integer or pointer given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %p in format string (no. 1) requires an address given in the argument list.\n"
"[test.cpp:4]: (warning) %p in format string (no. 1) requires an integer or pointer given in the argument list\n", errout.str()); "[test.cpp:4]: (warning) %p in format string (no. 1) requires an address given in the argument list.\n", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, double d) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, double d) {\n"
@ -598,10 +598,10 @@ private:
" printf(\"%f\", b);\n" " printf(\"%f\", b);\n"
" printf(\"%f\", (float)cpi);\n" " printf(\"%f\", (float)cpi);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (warning) %e in format string (no. 1) requires a floating point number given in the argument list\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %e in format string (no. 1) requires a floating point number given in the argument list.\n"
"[test.cpp:4]: (warning) %E in format string (no. 1) requires a floating point number given in the argument list\n" "[test.cpp:4]: (warning) %E in format string (no. 1) requires a floating point number given in the argument list.\n"
"[test.cpp:5]: (warning) %f in format string (no. 1) requires a floating point number given in the argument list\n" "[test.cpp:5]: (warning) %f in format string (no. 1) requires a floating point number given in the argument list.\n"
"[test.cpp:6]: (warning) %G in format string (no. 1) requires a floating point number given in the argument list\n", errout.str()); "[test.cpp:6]: (warning) %G in format string (no. 1) requires a floating point number given in the argument list.\n", errout.str());
check("class foo;\n" check("class foo;\n"
"void foo(foo f) {\n" "void foo(foo f) {\n"
@ -609,9 +609,9 @@ private:
" printf(\"%f\", f);\n" " printf(\"%f\", f);\n"
" printf(\"%p\", f);\n" " printf(\"%p\", f);\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an integer given in the argument list\n" TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 1) requires an integer given in the argument list.\n"
"[test.cpp:4]: (warning) %f in format string (no. 1) requires an integer given in the argument list\n" "[test.cpp:4]: (warning) %f in format string (no. 1) requires an integer given in the argument list.\n"
"[test.cpp:5]: (warning) %p in format string (no. 1) requires an integer given in the argument list\n", "", errout.str()); "[test.cpp:5]: (warning) %p in format string (no. 1) requires an integer given in the argument list.\n", "", errout.str());
} }
}; };