2011-10-28 22:39:46 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2011-10-28 22:39:46 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-10-29 12:47:12 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
2011-10-28 22:39:46 +02:00
|
|
|
#include "checkinternal.h"
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
|
2011-10-29 12:47:12 +02:00
|
|
|
// Register this check class (by creating a static instance of it).
|
|
|
|
// Disabled in release builds
|
2011-10-28 22:39:46 +02:00
|
|
|
namespace {
|
|
|
|
CheckInternal instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckInternal::checkTokenMatchPatterns()
|
|
|
|
{
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
|
|
|
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
|
|
|
|
continue;
|
|
|
|
|
2012-05-25 12:09:41 +02:00
|
|
|
const std::string& funcname = tok->strAt(2);
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
// Get pattern string
|
|
|
|
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
|
2012-04-25 09:56:07 +02:00
|
|
|
if (!pattern_tok || pattern_tok->type() != Token::eString)
|
2011-10-28 22:39:46 +02:00
|
|
|
continue;
|
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
const std::string pattern = pattern_tok->strValue();
|
2011-10-28 22:39:46 +02:00
|
|
|
if (pattern.empty()) {
|
|
|
|
simplePatternError(tok, pattern, funcname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for signs of complex patterns
|
2011-11-07 00:50:45 +01:00
|
|
|
if (pattern.find_first_of("[|%") != std::string::npos)
|
2011-10-28 22:39:46 +02:00
|
|
|
continue;
|
2011-11-07 00:50:45 +01:00
|
|
|
else if (pattern.find("!!") != std::string::npos)
|
2011-10-28 22:39:46 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
simplePatternError(tok, pattern, funcname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckInternal::checkTokenSimpleMatchPatterns()
|
|
|
|
{
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
|
|
|
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
|
|
|
|
continue;
|
|
|
|
|
2012-05-25 12:09:41 +02:00
|
|
|
const std::string& funcname = tok->strAt(2);
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
// Get pattern string
|
|
|
|
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
|
2012-04-25 09:56:07 +02:00
|
|
|
if (!pattern_tok || pattern_tok->type() != Token::eString)
|
2011-10-28 22:39:46 +02:00
|
|
|
continue;
|
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
const std::string pattern = pattern_tok->strValue();
|
2011-10-28 22:39:46 +02:00
|
|
|
if (pattern.empty()) {
|
|
|
|
complexPatternError(tok, pattern, funcname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for [xyz] usage - but exclude standalone square brackets
|
|
|
|
unsigned int char_count = 0;
|
2011-11-07 00:50:45 +01:00
|
|
|
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
|
2011-10-30 18:34:49 +01:00
|
|
|
char c = pattern[pos];
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
if (c == ' ') {
|
|
|
|
char_count = 0;
|
|
|
|
} else if (c == ']') {
|
|
|
|
if (char_count > 0) {
|
|
|
|
complexPatternError(tok, pattern, funcname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++char_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check | usage: Count characters before the symbol
|
|
|
|
char_count = 0;
|
2011-11-07 00:50:45 +01:00
|
|
|
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
|
2011-10-30 18:34:49 +01:00
|
|
|
char c = pattern[pos];
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
if (c == ' ') {
|
|
|
|
char_count = 0;
|
|
|
|
} else if (c == '|') {
|
|
|
|
if (char_count > 0) {
|
|
|
|
complexPatternError(tok, pattern, funcname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++char_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for real errors
|
2011-11-07 00:50:45 +01:00
|
|
|
if (pattern.find_first_of("%") != std::string::npos || pattern.find("!!") != std::string::npos)
|
2011-10-28 22:39:46 +02:00
|
|
|
complexPatternError(tok, pattern, funcname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 12:13:46 +02:00
|
|
|
void CheckInternal::checkMissingPercentCharacter()
|
|
|
|
{
|
2012-01-21 19:55:32 +01:00
|
|
|
static std::set<std::string> magics;
|
|
|
|
if (magics.empty()) {
|
|
|
|
magics.insert("%any%");
|
|
|
|
magics.insert("%var%");
|
|
|
|
magics.insert("%type%");
|
|
|
|
magics.insert("%num%");
|
|
|
|
magics.insert("%bool%");
|
|
|
|
magics.insert("%str%");
|
|
|
|
magics.insert("%varid%");
|
|
|
|
magics.insert("%or%");
|
|
|
|
magics.insert("%oror%");
|
|
|
|
}
|
2011-10-29 12:13:46 +02:00
|
|
|
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
|
|
|
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
|
|
|
|
continue;
|
|
|
|
|
2012-05-25 12:09:41 +02:00
|
|
|
const std::string& funcname = tok->strAt(2);
|
2011-10-29 12:13:46 +02:00
|
|
|
|
|
|
|
// Get pattern string
|
|
|
|
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
|
2012-04-25 09:56:07 +02:00
|
|
|
if (!pattern_tok || pattern_tok->type() != Token::eString)
|
2011-10-29 12:13:46 +02:00
|
|
|
continue;
|
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
const std::string pattern = pattern_tok->strValue();
|
2011-10-29 12:13:46 +02:00
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
std::set<std::string>::const_iterator magic, magics_end = magics.end();
|
2011-10-29 12:13:46 +02:00
|
|
|
for (magic = magics.begin(); magic != magics_end; ++magic) {
|
2011-11-07 00:50:45 +01:00
|
|
|
const std::string broken_magic = (*magic).substr(0, (*magic).size()-1);
|
2011-10-29 12:13:46 +02:00
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = pattern.find(broken_magic, pos)) != std::string::npos) {
|
2011-10-29 12:13:46 +02:00
|
|
|
// Check if it's the full pattern
|
|
|
|
if (pattern.find(*magic, pos) != pos) {
|
|
|
|
// Known whitelist of substrings
|
|
|
|
if ((broken_magic == "%var" && pattern.find("%varid%", pos) == pos) ||
|
|
|
|
(broken_magic == "%or" && pattern.find("%oror%", pos) == pos)) {
|
|
|
|
++pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
missingPercentCharacterError(tok, pattern, funcname);
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:45:16 +02:00
|
|
|
void CheckInternal::checkUnknownPattern()
|
|
|
|
{
|
|
|
|
static std::set<std::string> knownPatterns;
|
|
|
|
if (knownPatterns.empty()) {
|
|
|
|
knownPatterns.insert("%any%");
|
|
|
|
knownPatterns.insert("%var%");
|
|
|
|
knownPatterns.insert("%type%");
|
|
|
|
knownPatterns.insert("%num%");
|
|
|
|
knownPatterns.insert("%bool%");
|
|
|
|
knownPatterns.insert("%str%");
|
|
|
|
knownPatterns.insert("%varid%");
|
|
|
|
knownPatterns.insert("%or%");
|
|
|
|
knownPatterns.insert("%oror%");
|
|
|
|
knownPatterns.insert("%op%");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
|
|
|
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get pattern string
|
|
|
|
const Token *pattern_tok = tok->tokAt(4)->nextArgument();
|
|
|
|
if (!pattern_tok || pattern_tok->type() != Token::eString)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string pattern = pattern_tok->strValue();
|
|
|
|
bool inBrackets = false;
|
|
|
|
|
|
|
|
for (std::string::size_type i = 0; i < pattern.length()-1; i++) {
|
|
|
|
if (pattern[i] == '[' && (i == 0 || pattern[i-1] == ' '))
|
|
|
|
inBrackets = true;
|
|
|
|
else if (pattern[i] == ']')
|
|
|
|
inBrackets = false;
|
|
|
|
else if (pattern[i] == '%' && pattern[i+1] != ' ' && pattern[i+1] != '|' && !inBrackets) {
|
|
|
|
std::string::size_type end = pattern.find('%', i+1);
|
|
|
|
if (end != std::string::npos) {
|
|
|
|
std::string s = pattern.substr(i, end-i+1);
|
|
|
|
if (knownPatterns.find(s) == knownPatterns.end())
|
|
|
|
unknownPatternError(tok, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
void CheckInternal::simplePatternError(const Token* tok, const std::string& pattern, const std::string &funcname)
|
2011-10-28 22:39:46 +02:00
|
|
|
{
|
|
|
|
reportError(tok, Severity::warning, "simplePatternError",
|
|
|
|
"Found simple pattern inside Token::" + funcname + "() call: \"" + pattern + "\""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:50:45 +01:00
|
|
|
void CheckInternal::complexPatternError(const Token* tok, const std::string& pattern, const std::string &funcname)
|
2011-10-28 22:39:46 +02:00
|
|
|
{
|
|
|
|
reportError(tok, Severity::error, "complexPatternError",
|
|
|
|
"Found complex pattern inside Token::" + funcname + "() call: \"" + pattern + "\""
|
|
|
|
);
|
|
|
|
}
|
2011-10-29 12:13:46 +02:00
|
|
|
|
|
|
|
void CheckInternal::missingPercentCharacterError(const Token* tok, const std::string& pattern, const std::string& funcname)
|
|
|
|
{
|
|
|
|
reportError(tok, Severity::error, "missingPercentCharacter",
|
|
|
|
"Missing percent end character in Token::" + funcname + "() pattern: \"" + pattern + "\""
|
|
|
|
);
|
|
|
|
}
|
2011-10-29 12:47:12 +02:00
|
|
|
|
2012-07-11 17:45:16 +02:00
|
|
|
void CheckInternal::unknownPatternError(const Token* tok, const std::string& pattern)
|
|
|
|
{
|
|
|
|
reportError(tok, Severity::error, "unkownPattern",
|
|
|
|
"Unkown pattern used: \"" + pattern + "\"");
|
|
|
|
}
|
|
|
|
|
2011-10-29 12:47:12 +02:00
|
|
|
#endif // #ifndef NDEBUG
|