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
else if (strncmp(argv[i], "--suppressions-list=", 20) == 0) {
std::string filename = argv[i];
filename = filename.substr(20);
std::string filename = argv[i]+20;
std::ifstream f(filename.c_str());
if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file: \"");
@ -191,8 +190,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Filter errors
// This is deprecated, see --supressions-list above
else if (strcmp(argv[i], "--suppressions") == 0 &&
strlen(argv[i]) == 14) {
else if (strcmp(argv[i], "--suppressions") == 0) {
++i;
if (i >= argc) {
@ -216,8 +214,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
else if (strncmp(argv[i], "--suppress=", 11) == 0) {
std::string suppression = argv[i];
suppression = suppression.substr(11);
std::string suppression = argv[i]+11;
const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression));
if (!errmsg.empty()) {
PrintMessage(errmsg);
@ -243,8 +240,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// Define the XML file version (and enable XML output)
else if (strncmp(argv[i], "--xml-version=", 14) == 0) {
std::string numberString(argv[i]);
numberString = numberString.substr(14);
std::string numberString(argv[i]+14);
std::istringstream iss(numberString);
if (!(iss >> _settings->_xml_version)) {
@ -282,7 +278,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false;
}
// 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("portability");
}
@ -290,8 +286,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// --error-exitcode=1
else if (strncmp(argv[i], "--error-exitcode=", 17) == 0) {
std::string temp = argv[i];
temp = temp.substr(17);
std::string temp = argv[i]+17;
std::istringstream iss(temp);
if (!(iss >> _settings->_exitCode)) {
_settings->_exitCode = 0;
@ -307,8 +302,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-D define"
if (strcmp(argv[i], "-D") == 0) {
++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0) {
if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-D' is missing.");
return false;
}
@ -331,8 +325,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-U undef"
if (strcmp(argv[i], "-U") == 0) {
++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0) {
if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-U' is missing.");
return false;
}
@ -354,7 +347,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-I path/"
if (strcmp(argv[i], "-I") == 0) {
++i;
if (i >= argc) {
if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-I' is missing.");
return false;
}
@ -391,7 +384,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "-i path/"
if (strcmp(argv[i], "-i") == 0) {
++i;
if (i >= argc) {
if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-i' is missing.");
return false;
}
@ -442,11 +435,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// "--template path/"
if (argv[i][10] == '=')
_settings->_outputFormat = argv[i] + 11;
else {
else if ((i+1) < argc && argv[i+1][0] != '-') {
++i;
_settings->_outputFormat = (argv[i] ? argv[i] : "");
}
if (_settings->_outputFormat.empty()) {
_settings->_outputFormat = argv[i];
} else {
PrintMessage("cppcheck: argument to '--template' is missing.");
return false;
}
@ -460,14 +452,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// Checking threads
else if (strcmp(argv[i], "-j") == 0 ||
strncmp(argv[i], "-j", 2) == 0) {
else if (strncmp(argv[i], "-j", 2) == 0) {
std::string numberString;
// "-j 3"
if (strcmp(argv[i], "-j") == 0) {
++i;
if (i >= argc) {
if (i >= argc || argv[i][0] == '-') {
PrintMessage("cppcheck: argument to '-j' is missing.");
return false;
}
@ -476,10 +467,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// "-j3"
else if (strncmp(argv[i], "-j", 2) == 0) {
numberString = argv[i];
numberString = numberString.substr(2);
}
else
numberString = argv[i]+2;
std::istringstream iss(numberString);
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.
// Also output a warning for the user.
// 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>::iterator iterIgnored = ignored.begin();
for (size_t i = 0 ; i < ignored.size();) {
const std::string extension = Path::getFilenameExtension(ignored[i]);
for (std::vector<std::string>::iterator i = ignored.begin(); i != ignored.end();) {
const std::string extension = Path::getFilenameExtension(*i);
if (extension == ".h" || extension == ".hpp") {
ignored.erase(iterIgnored + i);
if (!warned) {
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
}
i = ignored.erase(i);
warn = true;
} else
++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());
for (size_t i = 0 ; i < filenames.size();) {
for (std::vector<std::string>::iterator i = filenames.begin() ; i != filenames.end();) {
#if defined(_WIN32)
// For Windows we want case-insensitive path matching
const bool caseSensitive = false;
#else
const bool caseSensitive = true;
#endif
if (matcher.Match(filenames[i], caseSensitive))
filenames.erase(filenames.begin() + i);
if (matcher.Match(*i, caseSensitive))
i = filenames.erase(i);
else
++i;
}

View File

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

View File

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