Refactoring various issues in cmdlineparser, cppcheckexecutor, check64bit and tokenize.

This commit is contained in:
PKEuS 2012-01-01 21:17:16 +02:00 committed by Reijo Tomperi
parent 8cae17fda8
commit f4703e026a
4 changed files with 105 additions and 130 deletions

View File

@ -172,8 +172,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Filter errors // Filter errors
else if (strncmp(argv[i], "--suppressions-list=", 20) == 0) { else if (strncmp(argv[i], "--suppressions-list=", 20) == 0) {
std::string filename = argv[i]; std::string filename = argv[i]+20;
filename = filename.substr(20);
std::ifstream f(filename.c_str()); std::ifstream f(filename.c_str());
if (!f.is_open()) { if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file: \""); std::string message("cppcheck: Couldn't open the file: \"");
@ -191,8 +190,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Filter errors // Filter errors
// This is deprecated, see --supressions-list above // This is deprecated, see --supressions-list above
else if (strcmp(argv[i], "--suppressions") == 0 && else if (strcmp(argv[i], "--suppressions") == 0) {
strlen(argv[i]) == 14) {
++i; ++i;
if (i >= argc) { if (i >= argc) {
@ -216,8 +214,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
else if (strncmp(argv[i], "--suppress=", 11) == 0) { else if (strncmp(argv[i], "--suppress=", 11) == 0) {
std::string suppression = argv[i]; std::string suppression = argv[i]+11;
suppression = suppression.substr(11);
const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression)); const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression));
if (!errmsg.empty()) { if (!errmsg.empty()) {
PrintMessage(errmsg); PrintMessage(errmsg);
@ -243,8 +240,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Define the XML file version (and enable XML output) // Define the XML file version (and enable XML output)
else if (strncmp(argv[i], "--xml-version=", 14) == 0) { else if (strncmp(argv[i], "--xml-version=", 14) == 0) {
std::string numberString(argv[i]); std::string numberString(argv[i]+14);
numberString = numberString.substr(14);
std::istringstream iss(numberString); std::istringstream iss(numberString);
if (!(iss >> _settings->_xml_version)) { if (!(iss >> _settings->_xml_version)) {
@ -282,7 +278,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false; return false;
} }
// when "style" is enabled, also enable "performance" and "portability" // when "style" is enabled, also enable "performance" and "portability"
else if (strstr(argv[i]+9, "style")) { if (_settings->isEnabled("style")) {
_settings->addEnabled("performance"); _settings->addEnabled("performance");
_settings->addEnabled("portability"); _settings->addEnabled("portability");
} }
@ -290,8 +286,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// --error-exitcode=1 // --error-exitcode=1
else if (strncmp(argv[i], "--error-exitcode=", 17) == 0) { else if (strncmp(argv[i], "--error-exitcode=", 17) == 0) {
std::string temp = argv[i]; std::string temp = argv[i]+17;
temp = temp.substr(17);
std::istringstream iss(temp); std::istringstream iss(temp);
if (!(iss >> _settings->_exitCode)) { if (!(iss >> _settings->_exitCode)) {
_settings->_exitCode = 0; _settings->_exitCode = 0;
@ -307,8 +302,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-D define" // "-D define"
if (strcmp(argv[i], "-D") == 0) { if (strcmp(argv[i], "-D") == 0) {
++i; ++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 || if (i >= argc || argv[i][0] == '-') {
strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: argument to '-D' is missing."); PrintMessage("cppcheck: argument to '-D' is missing.");
return false; return false;
} }
@ -331,8 +325,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-U undef" // "-U undef"
if (strcmp(argv[i], "-U") == 0) { if (strcmp(argv[i], "-U") == 0) {
++i; ++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 || if (i >= argc || argv[i][0] == '-') {
strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: argument to '-U' is missing."); PrintMessage("cppcheck: argument to '-U' is missing.");
return false; return false;
} }
@ -354,7 +347,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-I path/" // "-I path/"
if (strcmp(argv[i], "-I") == 0) { if (strcmp(argv[i], "-I") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-I' is missing."); PrintMessage("cppcheck: argument to '-I' is missing.");
return false; return false;
} }
@ -391,7 +384,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-i path/" // "-i path/"
if (strcmp(argv[i], "-i") == 0) { if (strcmp(argv[i], "-i") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-i' is missing."); PrintMessage("cppcheck: argument to '-i' is missing.");
return false; return false;
} }
@ -442,11 +435,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "--template path/" // "--template path/"
if (argv[i][10] == '=') if (argv[i][10] == '=')
_settings->_outputFormat = argv[i] + 11; _settings->_outputFormat = argv[i] + 11;
else { else if ((i+1) < argc && argv[i+1][0] != '-') {
++i; ++i;
_settings->_outputFormat = (argv[i] ? argv[i] : ""); _settings->_outputFormat = argv[i];
} } else {
if (_settings->_outputFormat.empty()) {
PrintMessage("cppcheck: argument to '--template' is missing."); PrintMessage("cppcheck: argument to '--template' is missing.");
return false; return false;
} }
@ -460,14 +452,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
// Checking threads // Checking threads
else if (strcmp(argv[i], "-j") == 0 || else if (strncmp(argv[i], "-j", 2) == 0) {
strncmp(argv[i], "-j", 2) == 0) {
std::string numberString; std::string numberString;
// "-j 3" // "-j 3"
if (strcmp(argv[i], "-j") == 0) { if (strcmp(argv[i], "-j") == 0) {
++i; ++i;
if (i >= argc) { if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-j' is missing."); PrintMessage("cppcheck: argument to '-j' is missing.");
return false; return false;
} }
@ -476,10 +467,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
} }
// "-j3" // "-j3"
else if (strncmp(argv[i], "-j", 2) == 0) { else
numberString = argv[i]; numberString = argv[i]+2;
numberString = numberString.substr(2);
}
std::istringstream iss(numberString); std::istringstream iss(numberString);
if (!(iss >> _settings->_jobs)) { if (!(iss >> _settings->_jobs)) {

View File

@ -105,32 +105,31 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
// Remove header files from the list of ignored files. // Remove header files from the list of ignored files.
// Also output a warning for the user. // Also output a warning for the user.
// TODO: Remove all unknown files? (use FileLister::acceptFile()) // TODO: Remove all unknown files? (use FileLister::acceptFile())
bool warned = false; bool warn = false;
std::vector<std::string> ignored = parser.GetIgnoredPaths(); std::vector<std::string> ignored = parser.GetIgnoredPaths();
std::vector<std::string>::iterator iterIgnored = ignored.begin(); for (std::vector<std::string>::iterator i = ignored.begin(); i != ignored.end();) {
for (size_t i = 0 ; i < ignored.size();) { const std::string extension = Path::getFilenameExtension(*i);
const std::string extension = Path::getFilenameExtension(ignored[i]);
if (extension == ".h" || extension == ".hpp") { if (extension == ".h" || extension == ".hpp") {
ignored.erase(iterIgnored + i); i = ignored.erase(i);
if (!warned) { warn = true;
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
warned = true; // Warn only once
}
} else } else
++i; ++i;
} }
if (warn) {
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
}
PathMatch matcher(parser.GetIgnoredPaths()); PathMatch matcher(parser.GetIgnoredPaths());
for (size_t i = 0 ; i < filenames.size();) { for (std::vector<std::string>::iterator i = filenames.begin() ; i != filenames.end();) {
#if defined(_WIN32) #if defined(_WIN32)
// For Windows we want case-insensitive path matching // For Windows we want case-insensitive path matching
const bool caseSensitive = false; const bool caseSensitive = false;
#else #else
const bool caseSensitive = true; const bool caseSensitive = true;
#endif #endif
if (matcher.Match(filenames[i], caseSensitive)) if (matcher.Match(*i, caseSensitive))
filenames.erase(filenames.begin() + i); i = filenames.erase(i);
else else
++i; ++i;
} }

View File

@ -33,8 +33,7 @@ namespace {
/** Is given variable a pointer or array? */ /** Is given variable a pointer or array? */
static bool isaddr(const Variable *var) static bool isaddr(const Variable *var)
{ {
const Token *nametok = var ? var->nameToken() : 0; return (var && (var->isPointer() || var->isArray()));
return (var && (nametok->strAt(-2) == "*" || nametok->strAt(-1) == "*" || nametok->strAt(1) == "["));
} }
/** Is given variable an integer variable */ /** Is given variable an integer variable */

View File

@ -303,7 +303,7 @@ void Tokenizer::createTokens(std::istream &code)
// char/string.. // char/string..
// multiline strings are not handled. The preprocessor should handle that for us. // multiline strings are not handled. The preprocessor should handle that for us.
if (ch == '\'' || ch == '\"') { else if (ch == '\'' || ch == '\"') {
std::string line; std::string line;
// read char // read char
@ -366,88 +366,86 @@ void Tokenizer::createTokens(std::istream &code)
continue; continue;
} }
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch)) { if (ch == '.' &&
if (ch == '.' && CurrentToken.length() > 0 &&
CurrentToken.length() > 0 && std::isdigit(CurrentToken[0])) {
std::isdigit(CurrentToken[0])) { // Don't separate doubles "5.4"
// Don't separate doubles "5.4" } else if (strchr("+-", ch) &&
} else if (strchr("+-", ch) && CurrentToken.length() > 0 &&
CurrentToken.length() > 0 && std::isdigit(CurrentToken[0]) &&
std::isdigit(CurrentToken[0]) && CurrentToken.compare(0,2,"0x") != 0 &&
CurrentToken.compare(0,2,"0x") != 0 && (CurrentToken[CurrentToken.length()-1] == 'e' ||
(CurrentToken[CurrentToken.length()-1] == 'e' || CurrentToken[CurrentToken.length()-1] == 'E')) {
CurrentToken[CurrentToken.length()-1] == 'E')) { // Don't separate doubles "4.2e+10"
// Don't separate doubles "4.2e+10" } else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek())) {
} else if (CurrentToken.empty() && ch == '.' && std::isdigit(code.peek())) { // tokenize .125 into 0.125
// tokenize .125 into 0.125 CurrentToken = "0";
CurrentToken = "0"; } else if (ch=='&' && code.peek() == '&') {
} else if (ch=='&' && code.peek() == '&') { if (!CurrentToken.empty()) {
if (!CurrentToken.empty()) {
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
if (!CurrentToken.empty())
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
}
// &&
ch = (char)code.get();
addtoken("&&", lineno, FileIndex, true);
_tokensBack->setExpandedMacro(expandedMacro);
continue;
} else if (ch==':' && CurrentToken.empty() && code.peek() == ' ') {
// :
addtoken(":", lineno, FileIndex, true);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
continue;
} else if (ch==':' && CurrentToken.empty() && code.peek() == ':') {
// ::
ch = (char)code.get();
addtoken("::", lineno, FileIndex, true);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
continue;
} else {
if (CurrentToken == "#file") {
// Handle this where strings are handled
continue;
} else if (CurrentToken == "#endfile") {
if (lineNumbers.empty() || fileIndexes.empty()) {
cppcheckError(0);
deallocateTokens();
return;
}
lineno = lineNumbers.back();
lineNumbers.pop_back();
FileIndex = fileIndexes.back();
fileIndexes.pop_back();
CurrentToken.clear();
continue;
}
addtoken(CurrentToken.c_str(), lineno, FileIndex, true); addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
if (!CurrentToken.empty()) if (!CurrentToken.empty())
_tokensBack->setExpandedMacro(expandedMacro); _tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear(); CurrentToken.clear();
}
if (ch == '\n') { // &&
++lineno; ch = (char)code.get();
continue; addtoken("&&", lineno, FileIndex, true);
} else if (ch == ' ') { _tokensBack->setExpandedMacro(expandedMacro);
continue; continue;
} else if (ch==':' && CurrentToken.empty() && code.peek() == ' ') {
// :
addtoken(":", lineno, FileIndex, true);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
continue;
} else if (ch==':' && CurrentToken.empty() && code.peek() == ':') {
// ::
ch = (char)code.get();
addtoken("::", lineno, FileIndex, true);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
continue;
} else if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch)) {
if (CurrentToken == "#file") {
// Handle this where strings are handled
continue;
} else if (CurrentToken == "#endfile") {
if (lineNumbers.empty() || fileIndexes.empty()) {
cppcheckError(0);
deallocateTokens();
return;
} }
CurrentToken += ch; lineno = lineNumbers.back();
// Add "++", "--" or ">>" token lineNumbers.pop_back();
if ((ch == '+' || ch == '-' || ch == '>') && (code.peek() == ch)) FileIndex = fileIndexes.back();
CurrentToken += (char)code.get(); fileIndexes.pop_back();
addtoken(CurrentToken.c_str(), lineno, FileIndex);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear(); CurrentToken.clear();
continue; continue;
} }
addtoken(CurrentToken.c_str(), lineno, FileIndex, true);
if (!CurrentToken.empty())
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
if (ch == '\n') {
++lineno;
continue;
} else if (ch == ' ') {
continue;
}
CurrentToken += ch;
// Add "++", "--" or ">>" token
if ((ch == '+' || ch == '-' || ch == '>') && (code.peek() == ch))
CurrentToken += (char)code.get();
addtoken(CurrentToken.c_str(), lineno, FileIndex);
_tokensBack->setExpandedMacro(expandedMacro);
CurrentToken.clear();
continue;
} }
CurrentToken += ch; CurrentToken += ch;
@ -2832,7 +2830,7 @@ static void removeTemplates(Token *tok)
tok2 = tok2->link(); tok2 = tok2->link();
} }
if (tok2->str() == "{") { else if (tok2->str() == "{") {
tok2 = tok2->link()->next(); tok2 = tok2->link()->next();
Token::eraseTokens(tok, tok2); Token::eraseTokens(tok, tok2);
if (tok2 && tok2->str() == ";" && tok2->next()) if (tok2 && tok2->str() == ";" && tok2->next())
@ -3198,17 +3196,9 @@ void Tokenizer::simplifyTemplatesExpandTemplate(const Token *tok,
std::vector<const Token *> &typesUsedInTemplateInstantion, std::vector<const Token *> &typesUsedInTemplateInstantion,
std::list<Token *> &templateInstantiations) std::list<Token *> &templateInstantiations)
{ {
int _indentlevel = 0;
int _parlevel = 0;
for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next()) { for (const Token *tok3 = _tokens; tok3; tok3 = tok3->next()) {
if (tok3->str() == "{") if (tok3->str() == "{" || tok3->str() == "(")
++_indentlevel; tok3 = tok3->link();
else if (tok3->str() == "}")
--_indentlevel;
else if (tok3->str() == "(")
++_parlevel;
else if (tok3->str() == ")")
--_parlevel;
// Start of template.. // Start of template..
if (tok3 == tok) { if (tok3 == tok) {
@ -3216,9 +3206,7 @@ void Tokenizer::simplifyTemplatesExpandTemplate(const Token *tok,
} }
// member function implemented outside class definition // member function implemented outside class definition
else if (_indentlevel == 0 && else if (simplifyTemplatesInstantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %var% (")) {
_parlevel == 0 &&
simplifyTemplatesInstantiateMatch(tok3, name, typeParametersInDeclaration.size(), ":: ~| %var% (")) {
addtoken(newName.c_str(), tok3->linenr(), tok3->fileIndex()); addtoken(newName.c_str(), tok3->linenr(), tok3->fileIndex());
while (tok3->str() != "::") while (tok3->str() != "::")
tok3 = tok3->next(); tok3 = tok3->next();