speed up checks by caching commonly looked up stuff in the symbol database (checkio)

This commit is contained in:
Robert Reif 2012-10-30 15:52:45 +01:00 committed by Daniel Marjamäki
parent 8e14d7682c
commit d7b658a5aa
1 changed files with 369 additions and 351 deletions

View File

@ -40,19 +40,24 @@ namespace {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckIO::checkCoutCerrMisusage() void CheckIO::checkCoutCerrMisusage()
{ {
bool firstCout = false; const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { std::size_t functions = symbolDatabase->functionScopes.size();
if (tok->str() == "(") for (std::size_t i = 0; i < functions; ++i) {
tok = tok->link(); const Scope * scope = symbolDatabase->functionScopes[i];
bool firstCout = false;
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (tok->str() == "(")
tok = tok->link();
if (Token::Match(tok, "std :: cout|cerr")) { if (Token::Match(tok, "std :: cout|cerr")) {
if (firstCout && tok->strAt(-1) == "<<" && tok->strAt(3) != ".") { if (firstCout && tok->strAt(-1) == "<<" && tok->strAt(3) != ".") {
coutCerrMisusageError(tok, tok->strAt(2)); coutCerrMisusageError(tok, tok->strAt(2));
firstCout = false;
} else if (tok->strAt(3) == "<<")
firstCout = true;
} else if (firstCout && tok->str() == ";")
firstCout = false; firstCout = false;
} else if (tok->strAt(3) == "<<") }
firstCout = true;
} else if (firstCout && tok->str() == ";")
firstCout = false;
} }
} }
@ -116,134 +121,138 @@ void CheckIO::checkFileUsage()
} }
} }
unsigned int indent = 0; std::size_t functions = symbolDatabase->functionScopes.size();
for (const Token* tok = _tokenizer->list.front(); tok; tok = tok->next()) { for (std::size_t j = 0; j < functions; ++j) {
if (tok->str() == "{") const Scope * scope = symbolDatabase->functionScopes[j];
indent++; unsigned int indent = 0;
else if (tok->str() == "}") { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
indent--; if (tok->str() == "{")
for (std::map<unsigned int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) { indent++;
if (indent < i->second.mode_indent) { else if (tok->str() == "}") {
i->second.mode_indent = 0; indent--;
i->second.mode = UNKNOWN; for (std::map<unsigned int, Filepointer>::iterator i = filepointers.begin(); i != filepointers.end(); ++i) {
} if (indent < i->second.mode_indent) {
if (indent < i->second.op_indent) { i->second.mode_indent = 0;
i->second.op_indent = 0; i->second.mode = UNKNOWN;
i->second.lastOperation = Filepointer::UNKNOWN_OP; }
} if (indent < i->second.op_indent) {
} i->second.op_indent = 0;
} else if (tok->varId() && Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile")) { i->second.lastOperation = Filepointer::UNKNOWN_OP;
std::map<unsigned int, Filepointer>::iterator i = filepointers.find(tok->varId());
if (i != filepointers.end()) {
i->second.mode = UNKNOWN;
i->second.lastOperation = Filepointer::UNKNOWN_OP;
}
} else if (Token::Match(tok, "%var% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) {
std::string mode;
const Token* fileTok = 0;
Filepointer::Operation operation = Filepointer::NONE;
if ((tok->str() == "fopen" || tok->str() == "freopen" || tok->str() == "tmpfile") && tok->strAt(-1) == "=") {
if (tok->str() != "tmpfile") {
const Token* modeTok = tok->tokAt(2)->nextArgument();
if (modeTok && modeTok->type() == Token::eString)
mode = modeTok->strValue();
} else
mode = "wb+";
fileTok = tok->tokAt(-2);
operation = Filepointer::OPEN;
} else if (tok->str() == "rewind" || tok->str() == "fseek" || tok->str() == "fsetpos" || tok->str() == "fflush") {
if (Token::simpleMatch(tok, "fflush ( stdin )"))
fflushOnInputStreamError(tok, tok->strAt(2));
else {
fileTok = tok->tokAt(2);
operation = Filepointer::POSITIONING;
}
} else if (tok->str() == "fgetc" || tok->str() == "fgets" || tok->str() == "fread" || tok->str() == "fscanf" || tok->str() == "getc") {
if (tok->str() == "fscanf")
fileTok = tok->tokAt(2);
else
fileTok = tok->linkAt(1)->previous();
operation = Filepointer::READ;
} else if (tok->str() == "fputc" || tok->str() == "fputs" || tok->str() == "fwrite" || tok->str() == "fprintf" || tok->str() == "putcc") {
if (tok->str() == "fprintf")
fileTok = tok->tokAt(2);
else
fileTok = tok->linkAt(1)->previous();
operation = Filepointer::WRITE;
} else if (tok->str() == "fclose") {
fileTok = tok->tokAt(2);
operation = Filepointer::CLOSE;
} else if (whitelist.find(tok->str()) != whitelist.end()) {
fileTok = tok->tokAt(2);
if (tok->str() == "ungetc" && fileTok)
fileTok = fileTok->nextArgument();
operation = Filepointer::UNIMPORTANT;
} else if (!Token::Match(tok, "if|for|while|catch|return")) {
const Token* const end2 = tok->linkAt(1);
for (const Token* tok2 = tok->tokAt(2); tok2 != end2; tok2 = tok2->next()) {
if (tok2->varId() && filepointers.find(tok2->varId()) != filepointers.end()) {
fileTok = tok2;
operation = Filepointer::UNKNOWN_OP; // Assume that repositioning was last operation and that the file is opened now
break;
} }
} }
} } else if (tok->varId() && Token::Match(tok, "%var% =") && (tok->strAt(2) != "fopen" && tok->strAt(2) != "freopen" && tok->strAt(2) != "tmpfile")) {
std::map<unsigned int, Filepointer>::iterator i = filepointers.find(tok->varId());
if (i != filepointers.end()) {
i->second.mode = UNKNOWN;
i->second.lastOperation = Filepointer::UNKNOWN_OP;
}
} else if (Token::Match(tok, "%var% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) {
std::string mode;
const Token* fileTok = 0;
Filepointer::Operation operation = Filepointer::NONE;
if (!fileTok || !fileTok->varId()) if ((tok->str() == "fopen" || tok->str() == "freopen" || tok->str() == "tmpfile") && tok->strAt(-1) == "=") {
continue; if (tok->str() != "tmpfile") {
const Token* modeTok = tok->tokAt(2)->nextArgument();
if (modeTok && modeTok->type() == Token::eString)
mode = modeTok->strValue();
} else
mode = "wb+";
fileTok = tok->tokAt(-2);
operation = Filepointer::OPEN;
} else if (tok->str() == "rewind" || tok->str() == "fseek" || tok->str() == "fsetpos" || tok->str() == "fflush") {
if (Token::simpleMatch(tok, "fflush ( stdin )"))
fflushOnInputStreamError(tok, tok->strAt(2));
else {
fileTok = tok->tokAt(2);
operation = Filepointer::POSITIONING;
}
} else if (tok->str() == "fgetc" || tok->str() == "fgets" || tok->str() == "fread" || tok->str() == "fscanf" || tok->str() == "getc") {
if (tok->str() == "fscanf")
fileTok = tok->tokAt(2);
else
fileTok = tok->linkAt(1)->previous();
operation = Filepointer::READ;
} else if (tok->str() == "fputc" || tok->str() == "fputs" || tok->str() == "fwrite" || tok->str() == "fprintf" || tok->str() == "putcc") {
if (tok->str() == "fprintf")
fileTok = tok->tokAt(2);
else
fileTok = tok->linkAt(1)->previous();
operation = Filepointer::WRITE;
} else if (tok->str() == "fclose") {
fileTok = tok->tokAt(2);
operation = Filepointer::CLOSE;
} else if (whitelist.find(tok->str()) != whitelist.end()) {
fileTok = tok->tokAt(2);
if (tok->str() == "ungetc" && fileTok)
fileTok = fileTok->nextArgument();
operation = Filepointer::UNIMPORTANT;
} else if (!Token::Match(tok, "if|for|while|catch|return")) {
const Token* const end2 = tok->linkAt(1);
for (const Token* tok2 = tok->tokAt(2); tok2 != end2; tok2 = tok2->next()) {
if (tok2->varId() && filepointers.find(tok2->varId()) != filepointers.end()) {
fileTok = tok2;
operation = Filepointer::UNKNOWN_OP; // Assume that repositioning was last operation and that the file is opened now
break;
}
}
}
if (filepointers.find(fileTok->varId()) == filepointers.end()) { // function call indicates: Its a File if (!fileTok || !fileTok->varId())
filepointers.insert(std::make_pair(fileTok->varId(), Filepointer(UNKNOWN))); continue;
}
Filepointer& f = filepointers[fileTok->varId()];
switch (operation) { if (filepointers.find(fileTok->varId()) == filepointers.end()) { // function call indicates: Its a File
case Filepointer::OPEN: filepointers.insert(std::make_pair(fileTok->varId(), Filepointer(UNKNOWN)));
f.mode = getMode(mode); }
f.mode_indent = indent; Filepointer& f = filepointers[fileTok->varId()];
break;
case Filepointer::POSITIONING: switch (operation) {
if (f.mode == CLOSED) case Filepointer::OPEN:
useClosedFileError(tok); f.mode = getMode(mode);
break; f.mode_indent = indent;
case Filepointer::READ: break;
if (f.mode == CLOSED) case Filepointer::POSITIONING:
useClosedFileError(tok); if (f.mode == CLOSED)
else if (f.mode == WRITE_MODE) useClosedFileError(tok);
readWriteOnlyFileError(tok); break;
else if (f.lastOperation == Filepointer::WRITE) case Filepointer::READ:
ioWithoutPositioningError(tok); if (f.mode == CLOSED)
break; useClosedFileError(tok);
case Filepointer::WRITE: else if (f.mode == WRITE_MODE)
if (f.mode == CLOSED) readWriteOnlyFileError(tok);
useClosedFileError(tok); else if (f.lastOperation == Filepointer::WRITE)
else if (f.mode == READ_MODE) ioWithoutPositioningError(tok);
writeReadOnlyFileError(tok); break;
else if (f.lastOperation == Filepointer::READ) case Filepointer::WRITE:
ioWithoutPositioningError(tok); if (f.mode == CLOSED)
break; useClosedFileError(tok);
case Filepointer::CLOSE: else if (f.mode == READ_MODE)
if (f.mode == CLOSED) writeReadOnlyFileError(tok);
useClosedFileError(tok); else if (f.lastOperation == Filepointer::READ)
else ioWithoutPositioningError(tok);
f.mode = CLOSED; break;
f.mode_indent = indent; case Filepointer::CLOSE:
break; if (f.mode == CLOSED)
case Filepointer::UNIMPORTANT: useClosedFileError(tok);
if (f.mode == CLOSED) else
useClosedFileError(tok); f.mode = CLOSED;
break; f.mode_indent = indent;
case Filepointer::UNKNOWN_OP: break;
f.mode = UNKNOWN; case Filepointer::UNIMPORTANT:
f.mode_indent = 0; if (f.mode == CLOSED)
break; useClosedFileError(tok);
default: break;
break; case Filepointer::UNKNOWN_OP:
} f.mode = UNKNOWN;
if (operation != Filepointer::NONE && operation != Filepointer::UNIMPORTANT) { f.mode_indent = 0;
f.op_indent = indent; break;
f.lastOperation = operation; default:
break;
}
if (operation != Filepointer::NONE && operation != Filepointer::UNIMPORTANT) {
f.op_indent = indent;
f.lastOperation = operation;
}
} }
} }
} }
@ -289,40 +298,45 @@ void CheckIO::invalidScanf()
if (!_settings->isEnabled("style")) if (!_settings->isEnabled("style"))
return; return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase();
const Token *formatToken = 0; std::size_t functions = symbolDatabase->functionScopes.size();
if (Token::Match(tok, "scanf|vscanf ( %str% ,")) for (std::size_t j = 0; j < functions; ++j) {
formatToken = tok->tokAt(2); const Scope * scope = symbolDatabase->functionScopes[j];
else if (Token::Match(tok, "sscanf|vsscanf|fscanf|vfscanf (")) { for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
const Token* nextArg = tok->tokAt(2)->nextArgument(); const Token *formatToken = 0;
if (nextArg && nextArg->type() == Token::eString) if (Token::Match(tok, "scanf|vscanf ( %str% ,"))
formatToken = nextArg; formatToken = tok->tokAt(2);
else else if (Token::Match(tok, "sscanf|vsscanf|fscanf|vfscanf (")) {
continue; const Token* nextArg = tok->tokAt(2)->nextArgument();
} else if (nextArg && nextArg->type() == Token::eString)
continue; formatToken = nextArg;
else
bool format = false; continue;
} else
// scan the string backwards, so we dont need to keep states
const std::string &formatstr(formatToken->str());
for (unsigned int i = 1; i < formatstr.length(); i++) {
if (formatstr[i] == '%')
format = !format;
else if (!format)
continue; continue;
else if (std::isdigit(formatstr[i]) || formatstr[i] == '*') { bool format = false;
format = false;
}
else if (std::isalpha(formatstr[i]) || formatstr[i] == '[') { // scan the string backwards, so we dont need to keep states
if (formatstr[i] == 's' || formatstr[i] == '[' || formatstr[i] == 'S' || (formatstr[i] == 'l' && formatstr[i+1] == 's')) // #3490 - field width limits are only necessary for string input const std::string &formatstr(formatToken->str());
invalidScanfError(tok, false); for (unsigned int i = 1; i < formatstr.length(); i++) {
else if (formatstr[i] != 'n' && formatstr[i] != 'c' && _settings->platformType != Settings::Win32A && _settings->platformType != Settings::Win32W && _settings->platformType != Settings::Win64 && _settings->isEnabled("portability")) if (formatstr[i] == '%')
invalidScanfError(tok, true); // Warn about libc bug in versions prior to 2.13-25 format = !format;
format = false;
else if (!format)
continue;
else if (std::isdigit(formatstr[i]) || formatstr[i] == '*') {
format = false;
}
else if (std::isalpha(formatstr[i]) || formatstr[i] == '[') {
if (formatstr[i] == 's' || formatstr[i] == '[' || formatstr[i] == 'S' || (formatstr[i] == 'l' && formatstr[i+1] == 's')) // #3490 - field width limits are only necessary for string input
invalidScanfError(tok, false);
else if (formatstr[i] != 'n' && formatstr[i] != 'c' && _settings->platformType != Settings::Win32A && _settings->platformType != Settings::Win32W && _settings->platformType != Settings::Win64 && _settings->isEnabled("portability"))
invalidScanfError(tok, true); // Warn about libc bug in versions prior to 2.13-25
format = false;
}
} }
} }
} }
@ -401,216 +415,220 @@ void CheckIO::checkWrongPrintfScanfArguments()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
bool warning = _settings->isEnabled("style"); bool warning = _settings->isEnabled("style");
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { std::size_t functions = symbolDatabase->functionScopes.size();
if (!tok->isName()) continue; for (std::size_t j = 0; j < functions; ++j) {
const Scope * scope = symbolDatabase->functionScopes[j];
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!tok->isName()) continue;
const Token* argListTok = 0; // Points to first va_list argument const Token* argListTok = 0; // Points to first va_list argument
std::string formatString; std::string formatString;
if (Token::Match(tok, "printf|scanf ( %str%")) { if (Token::Match(tok, "printf|scanf ( %str%")) {
formatString = tok->strAt(2); formatString = tok->strAt(2);
if (tok->strAt(3) == ",") { if (tok->strAt(3) == ",") {
argListTok = tok->tokAt(4); argListTok = tok->tokAt(4);
} else if (tok->strAt(3) == ")") { } else if (tok->strAt(3) == ")") {
argListTok = 0; argListTok = 0;
} else { } else {
continue; continue;
}
} else if (Token::Match(tok, "sprintf|fprintf|sscanf|fscanf ( %any%")) {
const Token* formatStringTok = tok->tokAt(2)->nextArgument(); // Find second parameter (format string)
if (Token::Match(formatStringTok, "%str% ,")) {
argListTok = formatStringTok->nextArgument(); // Find third parameter (first argument of va_args)
formatString = formatStringTok->str();
} else if (Token::Match(formatStringTok, "%str% )")) {
argListTok = 0; // Find third parameter (first argument of va_args)
formatString = formatStringTok->str();
} else {
continue;
}
} else if (Token::Match(tok, "snprintf|fnprintf (")) {
const Token* formatStringTok = tok->tokAt(2);
for (int i = 0; i < 2 && formatStringTok; i++) {
formatStringTok = formatStringTok->nextArgument(); // Find third parameter (format string)
}
if (Token::Match(formatStringTok, "%str% ,")) {
argListTok = formatStringTok->nextArgument(); // Find fourth parameter (first argument of va_args)
formatString = formatStringTok->str();
} else if (Token::Match(formatStringTok, "%str% )")) {
argListTok = 0; // Find fourth parameter (first argument of va_args)
formatString = formatStringTok->str();
} else {
continue;
}
} else {
continue;
}
// Count format string parameters..
bool scan = Token::Match(tok, "sscanf|fscanf|scanf");
unsigned int numFormat = 0;
bool percent = false;
const Token* argListTok2 = argListTok;
for (std::string::iterator i = formatString.begin(); i != formatString.end(); ++i) {
if (*i == '%') {
percent = !percent;
} else if (percent && *i == '[') {
while (i != formatString.end()) {
if (*i == ']') {
numFormat++;
if (argListTok)
argListTok = argListTok->nextArgument();
percent = false;
break;
}
++i;
} }
if (i == formatString.end()) } else if (Token::Match(tok, "sprintf|fprintf|sscanf|fscanf ( %any%")) {
break; const Token* formatStringTok = tok->tokAt(2)->nextArgument(); // Find second parameter (format string)
} else if (percent) { if (Token::Match(formatStringTok, "%str% ,")) {
percent = false; argListTok = formatStringTok->nextArgument(); // Find third parameter (first argument of va_args)
formatString = formatStringTok->str();
} else if (Token::Match(formatStringTok, "%str% )")) {
argListTok = 0; // Find third parameter (first argument of va_args)
formatString = formatStringTok->str();
} else {
continue;
}
} else if (Token::Match(tok, "snprintf|fnprintf (")) {
const Token* formatStringTok = tok->tokAt(2);
for (int i = 0; i < 2 && formatStringTok; i++) {
formatStringTok = formatStringTok->nextArgument(); // Find third parameter (format string)
}
if (Token::Match(formatStringTok, "%str% ,")) {
argListTok = formatStringTok->nextArgument(); // Find fourth parameter (first argument of va_args)
formatString = formatStringTok->str();
} else if (Token::Match(formatStringTok, "%str% )")) {
argListTok = 0; // Find fourth parameter (first argument of va_args)
formatString = formatStringTok->str();
} else {
continue;
}
} else {
continue;
}
bool _continue = false; // Count format string parameters..
std::string width; bool scan = Token::Match(tok, "sscanf|fscanf|scanf");
while (i != formatString.end() && *i != ']' && !std::isalpha(*i)) { unsigned int numFormat = 0;
if (*i == '*') { bool percent = false;
if (scan) const Token* argListTok2 = argListTok;
_continue = true; for (std::string::iterator i = formatString.begin(); i != formatString.end(); ++i) {
else { if (*i == '%') {
percent = !percent;
} else if (percent && *i == '[') {
while (i != formatString.end()) {
if (*i == ']') {
numFormat++; numFormat++;
if (argListTok) if (argListTok)
argListTok = argListTok->nextArgument(); argListTok = argListTok->nextArgument();
percent = false;
break;
} }
} else if (std::isdigit(*i)) ++i;
width += *i; }
++i; if (i == formatString.end())
} break;
if (i == formatString.end()) } else if (percent) {
break; percent = false;
if (_continue)
continue;
if (scan || *i != 'm') { // %m is a non-standard extension that requires no parameter on print functions. bool _continue = false;
numFormat++; std::string width;
while (i != formatString.end() && *i != ']' && !std::isalpha(*i)) {
// Perform type checks if (*i == '*') {
if (argListTok && Token::Match(argListTok->next(), "[,)]")) { // We can currently only check the type of arguments matching this simple pattern. if (scan)
const Variable* variableInfo = symbolDatabase->getVariableFromVarId(argListTok->varId()); _continue = true;
const Token* varTypeTok = variableInfo ? variableInfo->typeStartToken() : NULL; else {
if (varTypeTok && varTypeTok->str() == "static") numFormat++;
varTypeTok = varTypeTok->next(); if (argListTok)
argListTok = argListTok->nextArgument();
if (scan && varTypeTok) {
if (warning && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const"))
invalidScanfArgTypeError(tok, tok->str(), numFormat);
if (*i == 's' && variableInfo && isKnownType(variableInfo, varTypeTok) && variableInfo->isArray() && (variableInfo->dimensions().size() == 1) && variableInfo->dimensions()[0].known) {
if (!width.empty()) {
int numWidth = std::atoi(width.c_str());
if (numWidth != (variableInfo->dimension(0) - 1))
invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo);
}
} }
} else if (!scan && warning) { } else if (std::isdigit(*i))
switch (*i) { width += *i;
case 's': ++i;
if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray())) }
invalidPrintfArgTypeError_s(tok, numFormat); if (i == formatString.end())
break; break;
case 'n': if (_continue)
if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")) || argListTok->type() == Token::eString) continue;
invalidPrintfArgTypeError_n(tok, numFormat);
break; if (scan || *i != 'm') { // %m is a non-standard extension that requires no parameter on print functions.
case 'c': numFormat++;
case 'x':
case 'X': // Perform type checks
case 'o': if (argListTok && Token::Match(argListTok->next(), "[,)]")) { // We can currently only check the type of arguments matching this simple pattern.
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "bool|short|long|int|char|size_t") && !variableInfo->isPointer() && !variableInfo->isArray()) const Variable* variableInfo = symbolDatabase->getVariableFromVarId(argListTok->varId());
invalidPrintfArgTypeError_int(tok, numFormat, *i); const Token* varTypeTok = variableInfo ? variableInfo->typeStartToken() : NULL;
else if (argListTok->type() == Token::eString) if (varTypeTok && varTypeTok->str() == "static")
invalidPrintfArgTypeError_int(tok, numFormat, *i); varTypeTok = varTypeTok->next();
break;
case 'd': if (scan && varTypeTok) {
case 'i': if (warning && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const"))
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) { invalidScanfArgTypeError(tok, tok->str(), numFormat);
if ((varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "bool|short|long|int")) && varTypeTok->str() != "char")
invalidPrintfArgTypeError_sint(tok, numFormat, *i); if (*i == 's' && variableInfo && isKnownType(variableInfo, varTypeTok) && variableInfo->isArray() && (variableInfo->dimensions().size() == 1) && variableInfo->dimensions()[0].known) {
} else if (argListTok->type() == Token::eString) if (!width.empty()) {
invalidPrintfArgTypeError_sint(tok, numFormat, *i); int numWidth = std::atoi(width.c_str());
break; if (numWidth != (variableInfo->dimension(0) - 1))
case 'u': invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo);
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
if ((!varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "char|short|long|int|size_t")) && varTypeTok->str() != "bool")
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
} else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
break;
case 'p':
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "short|long|int|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
invalidPrintfArgTypeError_p(tok, numFormat);
else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_p(tok, numFormat);
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray()))
invalidPrintfArgTypeError_float(tok, numFormat, *i);
else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_float(tok, numFormat, *i);
break;
case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int)
case 'l': // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int)
// If the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character
if (i != formatString.end() && *(i+1) == *i) {
if (i+1 != formatString.end() && !isalpha(*(i+2))) {
std::string modifier;
modifier += *i;
modifier += *(i+1);
invalidLengthModifierError(tok, numFormat, modifier);
} }
} else { }
} else if (!scan && warning) {
switch (*i) {
case 's':
if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray()))
invalidPrintfArgTypeError_s(tok, numFormat);
break;
case 'n':
if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")) || argListTok->type() == Token::eString)
invalidPrintfArgTypeError_n(tok, numFormat);
break;
case 'c':
case 'x':
case 'X':
case 'o':
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "bool|short|long|int|char|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
invalidPrintfArgTypeError_int(tok, numFormat, *i);
else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_int(tok, numFormat, *i);
break;
case 'd':
case 'i':
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
if ((varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "bool|short|long|int")) && varTypeTok->str() != "char")
invalidPrintfArgTypeError_sint(tok, numFormat, *i);
} else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_sint(tok, numFormat, *i);
break;
case 'u':
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
if ((!varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "char|short|long|int|size_t")) && varTypeTok->str() != "bool")
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
} else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
break;
case 'p':
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "short|long|int|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
invalidPrintfArgTypeError_p(tok, numFormat);
else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_p(tok, numFormat);
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray()))
invalidPrintfArgTypeError_float(tok, numFormat, *i);
else if (argListTok->type() == Token::eString)
invalidPrintfArgTypeError_float(tok, numFormat, *i);
break;
case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int)
case 'l': // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int)
// If the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character
if (i != formatString.end() && *(i+1) == *i) {
if (i+1 != formatString.end() && !isalpha(*(i+2))) {
std::string modifier;
modifier += *i;
modifier += *(i+1);
invalidLengthModifierError(tok, numFormat, modifier);
}
} else {
if (i != formatString.end() && !isalpha(*(i+1))) {
std::string modifier;
modifier += *i;
invalidLengthModifierError(tok, numFormat, modifier);
}
}
break;
case 'j': // intmax_t or uintmax_t
case 'z': // size_t
case 't': // ptrdiff_t
case 'L': // long double
// Expect an alphabetical character after these specifiers
if (i != formatString.end() && !isalpha(*(i+1))) { if (i != formatString.end() && !isalpha(*(i+1))) {
std::string modifier; std::string modifier;
modifier += *i; modifier += *i;
invalidLengthModifierError(tok, numFormat, modifier); invalidLengthModifierError(tok, numFormat, modifier);
} }
break;
default:
break;
} }
break;
case 'j': // intmax_t or uintmax_t
case 'z': // size_t
case 't': // ptrdiff_t
case 'L': // long double
// Expect an alphabetical character after these specifiers
if (i != formatString.end() && !isalpha(*(i+1))) {
std::string modifier;
modifier += *i;
invalidLengthModifierError(tok, numFormat, modifier);
}
break;
default:
break;
} }
} }
}
if (argListTok) if (argListTok)
argListTok = argListTok->nextArgument(); // Find next argument argListTok = argListTok->nextArgument(); // Find next argument
}
} }
} }
}
// Count printf/scanf parameters.. // Count printf/scanf parameters..
unsigned int numFunction = 0; unsigned int numFunction = 0;
while (argListTok2) { while (argListTok2) {
numFunction++; numFunction++;
argListTok2 = argListTok2->nextArgument(); // Find next argument argListTok2 = argListTok2->nextArgument(); // Find next argument
} }
// Mismatching number of parameters => warning // Mismatching number of parameters => warning
if (numFormat != numFunction) if (numFormat != numFunction)
wrongPrintfScanfArgumentsError(tok, tok->str(), numFormat, numFunction); wrongPrintfScanfArgumentsError(tok, tok->str(), numFormat, numFunction);
}
} }
} }