reduce: add more settings from cppcheck cli

This commit is contained in:
Robert Reif 2013-08-19 06:06:53 +02:00 committed by Daniel Marjamäki
parent 982f63d58e
commit bf8a786265
4 changed files with 80 additions and 31 deletions

View File

@ -1790,7 +1790,8 @@ void SymbolDatabase::printOut(const char *title) const
} }
for (std::list<Type>::const_iterator type = typeList.begin(); type != typeList.end(); ++type) { for (std::list<Type>::const_iterator type = typeList.begin(); type != typeList.end(); ++type) {
std::cout << "Type: " << type->name() << std::endl; std::cout << "Type: " << &(*type) << std::endl;
std::cout << " name: " << type->name() << std::endl;
std::cout << " classDef: " << _tokenizer->list.fileLine(type->classDef) << std::endl; std::cout << " classDef: " << _tokenizer->list.fileLine(type->classDef) << std::endl;
std::cout << " classScope: " << type->classScope << std::endl; std::cout << " classScope: " << type->classScope << std::endl;
std::cout << " enclosingScope: " << type->enclosingScope << std::endl; std::cout << " enclosingScope: " << type->enclosingScope << std::endl;
@ -1805,13 +1806,13 @@ void SymbolDatabase::printOut(const char *title) const
if (type->derivedFrom[i].isVirtual) if (type->derivedFrom[i].isVirtual)
std::cout << "Virtual "; std::cout << "Virtual ";
std::cout << (type->derivedFrom[i].access == Public ? " Public " : std::cout << (type->derivedFrom[i].access == Public ? " Public" :
type->derivedFrom[i].access == Protected ? " Protected " : type->derivedFrom[i].access == Protected ? " Protected" :
type->derivedFrom[i].access == Private ? " Private " : type->derivedFrom[i].access == Private ? " Private" :
" Unknown"); " Unknown");
if (type->derivedFrom[i].type) if (type->derivedFrom[i].type)
std::cout << type->derivedFrom[i].type; std::cout << " " << type->derivedFrom[i].type;
else else
std::cout << " Unknown"; std::cout << " Unknown";

View File

@ -125,7 +125,7 @@ void Token::update_property_isStandardType()
if (_str.size() < 3) if (_str.size() < 3)
return; return;
static const char * const stdtype[] = {"int", "char", "bool", "long", "short", "float", "double", "wchar_t", "size_t", 0}; static const char * const stdtype[] = {"int", "char", "bool", "long", "short", "float", "double", "wchar_t", "size_t", "void", 0};
for (int i = 0; stdtype[i]; i++) { for (int i = 0; stdtype[i]; i++) {
if (_str == stdtype[i]) { if (_str == stdtype[i]) {
_isStandardType = true; _isStandardType = true;

View File

@ -8880,7 +8880,7 @@ void Tokenizer::simplifyAttribute()
void Tokenizer::simplifyKeyword() void Tokenizer::simplifyKeyword()
{ {
for (Token *tok = list.front(); tok; tok = tok->next()) { for (Token *tok = list.front(); tok; tok = tok->next()) {
while (Token::Match(tok, "volatile|inline|__inline|__forceinline|register|__restrict|__restrict__")) { while (Token::Match(tok, "volatile|inline|_inline|__inline|__forceinline|register|__restrict|__restrict__")) {
tok->deleteThis(); tok->deleteThis();
} }
} }
@ -9731,24 +9731,29 @@ void Tokenizer::simplifyReturnStrncat()
void Tokenizer::printUnknownTypes() void Tokenizer::printUnknownTypes()
{ {
std::set<std::string> unknowns; std::multimap<std::string, const Token *> unknowns;
for (unsigned int i = 1; i <= _varId; ++i) { for (unsigned int i = 1; i <= _varId; ++i) {
const Variable *var = _symbolDatabase->getVariableFromVarId(i); const Variable *var = _symbolDatabase->getVariableFromVarId(i);
// is unknown type? // is unknown type?
if (var && !var->type() && !var->typeStartToken()->isStandardType()) { if (var && !var->type() && !var->typeStartToken()->isStandardType()) {
std::string name; std::string name;
const Token * nameTok;
// single token type? // single token type?
if (var->typeStartToken() == var->typeEndToken()) if (var->typeStartToken() == var->typeEndToken()) {
name = var->typeStartToken()->str(); name = var->typeStartToken()->str();
nameTok = var->typeStartToken();
}
// complicated type // complicated type
else { else {
const Token *tok = var->typeStartToken(); const Token *tok = var->typeStartToken();
int level = 0; int level = 0;
nameTok = tok;
while (tok) { while (tok) {
// skip pointer and reference part of type // skip pointer and reference part of type
if (level == 0 && (tok->str() == "*" || tok->str() == "&")) if (level == 0 && (tok->str() == "*" || tok->str() == "&"))
@ -9772,23 +9777,29 @@ void Tokenizer::printUnknownTypes()
} }
} }
unknowns.insert(name); unknowns.insert(std::pair<std::string, const Token *>(name, nameTok));
} }
} }
if (!unknowns.empty()) { if (!unknowns.empty()) {
std::ostringstream ss; std::multimap<std::string, const Token *>::const_iterator it;
std::string last;
size_t count;
ss << unknowns.size() << " unknown types:" << std::endl; for (it = unknowns.begin(); it != unknowns.end(); ++it) {
// skip types is std namespace because they are not interesting
std::set<std::string>::const_iterator it; if (it->first.find("std::") != 0) {
std::size_t count = 1; if (it->first != last) {
last = it->first;
for (it = unknowns.begin(); it != unknowns.end(); ++it, ++count) count = 1;
ss << count << ": " << *it << std::endl; reportError(it->second, Severity::debug, "debug", "Unknown type \'" + it->first + "\'.");
} else {
if (_errorLogger) if (count < 3) // limit same type to 3
_errorLogger->reportOut(ss.str()); reportError(it->second, Severity::debug, "debug", "Unknown type \'" + it->first + "\'.");
count++;
}
}
}
} }
} }

View File

@ -28,11 +28,7 @@
class ReduceSettings : public Settings { class ReduceSettings : public Settings {
public: public:
ReduceSettings() : filename(0), linenr(0), hang(false), maxtime(0) { ReduceSettings() : filename(0), linenr(0), hang(false), maxtime(0) { }
addEnabled("all");
inconclusive = true;
_force = true;
}
const char *filename; const char *filename;
std::size_t linenr; std::size_t linenr;
@ -582,6 +578,8 @@ int main(int argc, char *argv[])
bool print = false; bool print = false;
ReduceSettings settings; ReduceSettings settings;
settings.maxtime = 300; // default timeout = 5 minutes settings.maxtime = 300; // default timeout = 5 minutes
bool def = false;
bool maxconfigs = false;
for (int i = 1, includePathIndex = 0; i < argc; i++) { for (int i = 1, includePathIndex = 0; i < argc; i++) {
if (strcmp(argv[i], "--stdout") == 0) if (strcmp(argv[i], "--stdout") == 0)
@ -590,11 +588,12 @@ int main(int argc, char *argv[])
settings.hang = true; settings.hang = true;
else if (strncmp(argv[i],"-D", 2) == 0) { else if (strncmp(argv[i],"-D", 2) == 0) {
if (!settings.userDefines.empty()) if (!settings.userDefines.empty())
settings.userDefines += " "; settings.userDefines += ";";
if ((strcmp(argv[i], "-D") == 0) && (i+1<argc)) if ((strcmp(argv[i], "-D") == 0) && (i+1<argc))
settings.userDefines += argv[++i]; settings.userDefines += argv[++i];
else else
settings.userDefines += argv[i] + 2; settings.userDefines += argv[i] + 2;
def = true;
} else if (std::strncmp(argv[i], "-I", 2) == 0) { } else if (std::strncmp(argv[i], "-I", 2) == 0) {
std::string path; std::string path;
@ -617,7 +616,7 @@ int main(int argc, char *argv[])
settings.maxtime = std::atoi(argv[i] + 10); settings.maxtime = std::atoi(argv[i] + 10);
else if (strncmp(argv[i],"--cfg=",6)==0) { else if (strncmp(argv[i],"--cfg=",6)==0) {
if (!settings.userDefines.empty()) if (!settings.userDefines.empty())
settings.userDefines += " "; settings.userDefines += ";";
settings.userDefines += argv[i] + 6; settings.userDefines += argv[i] + 6;
} else if (std::strncmp(argv[i], "--platform=", 11) == 0) { } else if (std::strncmp(argv[i], "--platform=", 11) == 0) {
std::string platform(11+argv[i]); std::string platform(11+argv[i]);
@ -638,7 +637,39 @@ int main(int argc, char *argv[])
} }
} else if (std::strcmp(argv[i], "--debug-warnings") == 0) } else if (std::strcmp(argv[i], "--debug-warnings") == 0)
settings.debugwarnings = true; settings.debugwarnings = true;
else if (settings.filename==NULL && strchr(argv[i],'.')) else if (std::strcmp(argv[i], "-f") == 0 || std::strcmp(argv[i], "--force") == 0)
settings._force = true;
else if (std::strncmp(argv[i], "--enable=", 9) == 0) {
std::string errmsg = settings.addEnabled(argv[i] + 9);
if (!errmsg.empty()) {
errmsg.erase(0, 11); // erase "cppcheck: " from message
std::cerr << errmsg << std::endl;
return EXIT_FAILURE;
}
// when "style" is enabled, also enable "warning", "performance" and "portability"
if (settings.isEnabled("style")) {
settings.addEnabled("warning");
settings.addEnabled("performance");
settings.addEnabled("portability");
}
} else if (std::strcmp(argv[i], "--inconclusive") == 0)
settings.inconclusive = true;
else if (std::strncmp(argv[i], "--max-configs=", 14) == 0) {
settings._force = false;
std::istringstream iss(14+argv[i]);
if (!(iss >> settings._maxConfigs)) {
std::cerr << "argument to '--max-configs=' is not a number." << std::endl;
return false;
}
if (settings._maxConfigs < 1) {
std::cerr << "argument to '--max-configs=' must be greater than 0." << std::endl;
return false;
}
maxconfigs = true;
} else if (settings.filename==NULL && strchr(argv[i],'.'))
settings.filename = argv[i]; settings.filename = argv[i];
else if (settings.linenr == 0U && MathLib::isInt(argv[i])) else if (settings.linenr == 0U && MathLib::isInt(argv[i]))
settings.linenr = std::atoi(argv[i]); settings.linenr = std::atoi(argv[i]);
@ -648,9 +679,15 @@ int main(int argc, char *argv[])
} }
} }
if (def && !settings._force && !maxconfigs)
settings._maxConfigs = 1U;
if (settings._force)
settings._maxConfigs = ~0U;
if ((!settings.hang && settings.linenr == 0U) || settings.filename == NULL) { if ((!settings.hang && settings.linenr == 0U) || settings.filename == NULL) {
std::cerr << "Syntax:" << std::endl std::cerr << "Syntax:" << std::endl
<< argv[0] << " [--stdout] [--cfg=X] [--hang] [--maxtime=60] [-D define] [-I includepath] filename [linenr]" << std::endl; << argv[0] << " [--stdout] [--cfg=X] [--hang] [--maxtime=60] [-D define] [-I includepath] [--force] [--enable=<id>] [--inconclusive] [--debug-warnings] [--max-configs=<limit>] filename [linenr]" << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }