changed the astyle formatting flags

This commit is contained in:
Daniel Marjamäki 2011-10-13 20:53:06 +02:00
parent b792efb761
commit 6f8e42a5af
180 changed files with 7393 additions and 15313 deletions

View File

@ -47,22 +47,16 @@ static void AddFilesToList(const std::string& FileList, std::vector<std::string>
// drawback : creates a dependency
std::istream *Files;
std::ifstream Infile;
if (FileList.compare("-") == 0) // read from stdin
{
if (FileList.compare("-") == 0) { // read from stdin
Files = &std::cin;
}
else
{
} else {
Infile.open(FileList.c_str());
Files = &Infile;
}
if (Files)
{
if (Files) {
std::string FileName;
while (std::getline(*Files, FileName)) // next line
{
if (!FileName.empty())
{
while (std::getline(*Files, FileName)) { // next line
if (!FileName.empty()) {
PathNames.push_back(FileName);
}
}
@ -85,10 +79,8 @@ void CmdLineParser::PrintMessage(const std::string &message)
bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--version") == 0)
{
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--version") == 0) {
_showVersion = true;
_exitAfterPrint = true;
return true;
@ -107,8 +99,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->debugFalsePositive = true;
// Enable all checks - will be removed in future
else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0)
{
else if (strcmp(argv[i], "-a") == 0 || strcmp(argv[i], "--all") == 0) {
PrintMessage("cppcheck: -a/--all option is deprecated and will be removed in 1.55 release.");
PrintMessage("cppcheck: please use --enable=all instead.");
}
@ -118,66 +109,56 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->inconclusive = true;
// Checking coding style - will be removed in the future
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0)
{
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--style") == 0) {
PrintMessage("cppcheck: -s/--style option is deprecated and will be removed in 1.55 release.");
PrintMessage("cppcheck: please use --enable=style instead.");
const std::string errmsg = _settings->addEnabled("style");
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
}
// Filter errors
else if (strncmp(argv[i], "--exitcode-suppressions", 23) == 0)
{
else if (strncmp(argv[i], "--exitcode-suppressions", 23) == 0) {
std::string filename;
// exitcode-suppressions filename.txt
// Deprecated
if (strcmp(argv[i], "--exitcode-suppressions") == 0)
{
if (strcmp(argv[i], "--exitcode-suppressions") == 0) {
++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0)
{
strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: No filename specified for the --exitcode-suppressions option");
return false;
}
filename = argv[i];
}
// exitcode-suppressions=filename.txt
else
{
else {
filename = 24 + argv[i];
}
std::ifstream f(filename.c_str());
if (!f.is_open())
{
if (!f.is_open()) {
PrintMessage("cppcheck: Couldn't open the file \"" + std::string(filename) + "\"");
return false;
}
const std::string errmsg(_settings->nofail.parseFile(f));
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
}
// 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];
filename = filename.substr(20);
std::ifstream f(filename.c_str());
if (!f.is_open())
{
if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file \"");
message += std::string(filename);
message += "\"";
@ -185,8 +166,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false;
}
const std::string errmsg(_settings->nomsg.parseFile(f));
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
@ -195,19 +175,16 @@ 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)
{
strlen(argv[i]) == 14) {
++i;
if (i >= argc)
{
if (i >= argc) {
PrintMessage("cppcheck: No file specified for the --suppressions option");
return false;
}
std::ifstream f(argv[i]);
if (!f.is_open())
{
if (!f.is_open()) {
std::string message("cppcheck: Couldn't open the file \"");
message += std::string(argv[i]);
message += "\"";
@ -215,20 +192,17 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false;
}
const std::string errmsg(_settings->nomsg.parseFile(f));
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
}
else if (strncmp(argv[i], "--suppress=", 11) == 0)
{
else if (strncmp(argv[i], "--suppress=", 11) == 0) {
std::string suppression = argv[i];
suppression = suppression.substr(11);
const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression));
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
@ -251,20 +225,17 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->_xml = true;
// 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]);
numberString = numberString.substr(14);
std::istringstream iss(numberString);
if (!(iss >> _settings->_xml_version))
{
if (!(iss >> _settings->_xml_version)) {
PrintMessage("cppcheck: argument to '--xml-version' is not a number");
return false;
}
if (_settings->_xml_version < 0 || _settings->_xml_version > 2)
{
if (_settings->_xml_version < 0 || _settings->_xml_version > 2) {
// We only have xml versions 1 and 2
PrintMessage("cppcheck: --xml-version can only be 1 or 2.");
return false;
@ -282,30 +253,25 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else if (strncmp(argv[i], "--append=", 9) == 0)
_settings->append(9 + argv[i]);
else if (strncmp(argv[i], "--enable=", 9) == 0)
{
else if (strncmp(argv[i], "--enable=", 9) == 0) {
const std::string errmsg = _settings->addEnabled(argv[i] + 9);
if (!errmsg.empty())
{
if (!errmsg.empty()) {
PrintMessage(errmsg);
return false;
}
// when "style" is enabled, also enable "performance" and "portability"
else if (strstr(argv[i]+9, "style"))
{
else if (strstr(argv[i]+9, "style")) {
_settings->addEnabled("performance");
_settings->addEnabled("portability");
}
}
// --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];
temp = temp.substr(17);
std::istringstream iss(temp);
if (!(iss >> _settings->_exitCode))
{
if (!(iss >> _settings->_exitCode)) {
_settings->_exitCode = 0;
PrintMessage("cppcheck: Argument must be an integer. Try something like '--error-exitcode=1'");
return false;
@ -313,17 +279,14 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// User define
else if (strncmp(argv[i], "-D", 2) == 0)
{
else if (strncmp(argv[i], "-D", 2) == 0) {
std::string define;
// "-D define"
if (strcmp(argv[i], "-D") == 0)
{
if (strcmp(argv[i], "-D") == 0) {
++i;
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
strncmp(argv[i], "--", 2) == 0)
{
strncmp(argv[i], "--", 2) == 0) {
PrintMessage("cppcheck: argument to '-D' is missing");
return false;
}
@ -331,8 +294,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
define = argv[i];
}
// "-Ddefine"
else
{
else {
define = 2 + argv[i];
}
@ -342,16 +304,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// Include paths
else if (strncmp(argv[i], "-I", 2) == 0)
{
else if (strncmp(argv[i], "-I", 2) == 0) {
std::string path;
// "-I path/"
if (strcmp(argv[i], "-I") == 0)
{
if (strcmp(argv[i], "-I") == 0) {
++i;
if (i >= argc)
{
if (i >= argc) {
PrintMessage("cppcheck: argument to '-I' is missing");
return false;
}
@ -359,8 +318,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// "-Ipath/"
else
{
else {
path = 2 + argv[i];
}
path = Path::fromNativeSeparators(path);
@ -374,23 +332,19 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// file list specified
else if (strncmp(argv[i], "--file-list=", 12) == 0)
{
else if (strncmp(argv[i], "--file-list=", 12) == 0) {
// open this file and read every input file (1 file name per line)
AddFilesToList(12 + argv[i], _pathnames);
}
// Ignored paths
else if (strncmp(argv[i], "-i", 2) == 0)
{
else if (strncmp(argv[i], "-i", 2) == 0) {
std::string path;
// "-i path/"
if (strcmp(argv[i], "-i") == 0)
{
if (strcmp(argv[i], "-i") == 0) {
++i;
if (i >= argc)
{
if (i >= argc) {
PrintMessage("cppcheck: argument to '-i' is missing");
return false;
}
@ -398,19 +352,16 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// "-ipath/"
else
{
else {
path = 2 + argv[i];
}
if (!path.empty())
{
if (!path.empty()) {
path = Path::fromNativeSeparators(path);
path = Path::simplifyPath(path.c_str());
path = Path::removeQuotationMarks(path);
if (!FileLister::fileExists(path) && FileLister::isDirectory(path))
{
if (!FileLister::fileExists(path) && FileLister::isDirectory(path)) {
// If directory name doesn't end with / or \, add it
if (path[path.length()-1] != '/')
path += '/';
@ -420,30 +371,25 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// Report progress
else if (strcmp(argv[i], "--report-progress") == 0)
{
else if (strcmp(argv[i], "--report-progress") == 0) {
_settings->reportProgress = true;
}
// --std
else if (strcmp(argv[i], "--std=posix") == 0)
{
else if (strcmp(argv[i], "--std=posix") == 0) {
_settings->posix = true;
}
// --C99
else if (strcmp(argv[i], "--std=c99") == 0)
{
else if (strcmp(argv[i], "--std=c99") == 0) {
_settings->c99 = true;
}
// Output formatter
else if (strcmp(argv[i], "--template") == 0)
{
else if (strcmp(argv[i], "--template") == 0) {
// "--template path/"
++i;
if (i >= argc)
{
if (i >= argc) {
PrintMessage("cppcheck: argument to '--template' is missing");
return false;
}
@ -457,16 +403,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)
{
strncmp(argv[i], "-j", 2) == 0) {
std::string numberString;
// "-j 3"
if (strcmp(argv[i], "-j") == 0)
{
if (strcmp(argv[i], "-j") == 0) {
++i;
if (i >= argc)
{
if (i >= argc) {
PrintMessage("cppcheck: argument to '-j' is missing");
return false;
}
@ -475,21 +418,18 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// "-j3"
else if (strncmp(argv[i], "-j", 2) == 0)
{
else if (strncmp(argv[i], "-j", 2) == 0) {
numberString = argv[i];
numberString = numberString.substr(2);
}
std::istringstream iss(numberString);
if (!(iss >> _settings->_jobs))
{
if (!(iss >> _settings->_jobs)) {
PrintMessage("cppcheck: argument to '-j' is not a number");
return false;
}
if (_settings->_jobs > 10000)
{
if (_settings->_jobs > 10000) {
// This limit is here just to catch typos. If someone has
// need for more jobs, this value should be increased.
PrintMessage("cppcheck: argument for '-j' is allowed to be 10000 at max");
@ -498,27 +438,23 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// deprecated: auto deallocated classes..
else if (strcmp(argv[i], "--auto-dealloc") == 0)
{
else if (strcmp(argv[i], "--auto-dealloc") == 0) {
++i;
PrintMessage("cppcheck: --auto-dealloc option is deprecated and will be removed in 1.55 release.");
}
// print all possible error messages..
else if (strcmp(argv[i], "--errorlist") == 0)
{
else if (strcmp(argv[i], "--errorlist") == 0) {
_showErrorMessages = true;
_settings->_xml = true;
_exitAfterPrint = true;
}
// documentation..
else if (strcmp(argv[i], "--doc") == 0)
{
else if (strcmp(argv[i], "--doc") == 0) {
std::ostringstream doc;
// Get documentation..
for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it)
{
for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
doc << "===" << (*it)->name() << "===\n"
<< (*it)->classInfo() << "\n\n";
}
@ -533,14 +469,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// --test-2-pass Experimental 2-pass checking of files
// This command line flag will be removed
else if (strcmp(argv[i], "--test-2-pass") == 0)
{
else if (strcmp(argv[i], "--test-2-pass") == 0) {
_settings->test_2_pass = true;
}
// show timing information..
else if (strncmp(argv[i], "--showtime=", 11) == 0)
{
else if (strncmp(argv[i], "--showtime=", 11) == 0) {
const std::string showtimeMode = argv[i] + 11;
if (showtimeMode == "file")
_settings->_showtime = SHOWTIME_FILE;
@ -554,33 +488,27 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
#ifdef HAVE_RULES
// Rule given at command line
else if (strncmp(argv[i], "--rule=", 7) == 0)
{
else if (strncmp(argv[i], "--rule=", 7) == 0) {
Settings::Rule rule;
rule.pattern = 7 + argv[i];
_settings->rules.push_back(rule);
}
// Rule file
else if (strncmp(argv[i], "--rule-file=", 12) == 0)
{
else if (strncmp(argv[i], "--rule-file=", 12) == 0) {
TiXmlDocument doc;
if (doc.LoadFile(12+argv[i]))
{
if (doc.LoadFile(12+argv[i])) {
TiXmlElement *node = doc.FirstChildElement();
for (; node && node->ValueStr() == "rule"; node = node->NextSiblingElement())
{
for (; node && node->ValueStr() == "rule"; node = node->NextSiblingElement()) {
Settings::Rule rule;
TiXmlElement *pattern = node->FirstChildElement("pattern");
if (pattern)
{
if (pattern) {
rule.pattern = pattern->GetText();
}
TiXmlElement *message = node->FirstChildElement("message");
if (message)
{
if (message) {
TiXmlElement *severity = message->FirstChildElement("severity");
if (severity)
rule.severity = severity->GetText();
@ -602,14 +530,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
#endif
// Check configuration
else if (strcmp(argv[i], "--check-config") == 0)
{
else if (strcmp(argv[i], "--check-config") == 0) {
_settings->checkConfiguration = true;
}
// Specify platform
else if (strncmp(argv[i], "--platform=", 11) == 0)
{
else if (strncmp(argv[i], "--platform=", 11) == 0) {
std::string platform(11+argv[i]);
if (platform == "win32A")
@ -622,8 +548,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->platform(Settings::Unix32);
else if (platform == "unix64")
_settings->platform(Settings::Unix64);
else
{
else {
std::string message("cppcheck: error: unrecognized platform\"");
message += argv[i];
message += "\"";
@ -633,16 +558,14 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// Print help
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
_pathnames.clear();
_showHelp = true;
_exitAfterPrint = true;
break;
}
else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0)
{
else if (strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) {
std::string message("cppcheck: error: unrecognized command line option \"");
message += argv[i];
message += "\"";
@ -650,37 +573,32 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false;
}
else
{
else {
std::string path = Path::fromNativeSeparators(argv[i]);
path = Path::removeQuotationMarks(path);
_pathnames.push_back(path);
}
}
if (_settings->isEnabled("unusedFunction") && _settings->_jobs > 1)
{
if (_settings->isEnabled("unusedFunction") && _settings->_jobs > 1) {
PrintMessage("cppcheck: unusedFunction check can't be used with -j option, so it was disabled.");
}
// FIXME: Make the _settings.test_2_pass thread safe
if (_settings->test_2_pass && _settings->_jobs > 1)
{
if (_settings->test_2_pass && _settings->_jobs > 1) {
PrintMessage("cppcheck: --test-2-pass doesn't work with -j option yet.");
}
if (argc <= 1)
_showHelp = true;
if (_showHelp)
{
if (_showHelp) {
PrintHelp();
return true;
}
// Print error only if we have "real" command and expect files
if (!_exitAfterPrint && _pathnames.empty())
{
if (!_exitAfterPrint && _pathnames.empty()) {
PrintMessage("cppcheck: No C or C++ source files found.");
return false;
}

View File

@ -36,8 +36,7 @@ class Settings;
* based on options user has given. Couple of options are handled as
* class internal options.
*/
class CmdLineParser
{
class CmdLineParser {
public:
/**
* The constructor.
@ -55,48 +54,42 @@ public:
/**
* Return if user wanted to see program version.
*/
bool GetShowVersion() const
{
bool GetShowVersion() const {
return _showVersion;
}
/**
* Return if user wanted to see list of error messages.
*/
bool GetShowErrorMessages() const
{
bool GetShowErrorMessages() const {
return _showErrorMessages;
}
/**
* Return the path names user gave to command line.
*/
std::vector<std::string> GetPathNames() const
{
std::vector<std::string> GetPathNames() const {
return _pathnames;
}
/**
* Return if help is shown to user.
*/
bool GetShowHelp() const
{
bool GetShowHelp() const {
return _showHelp;
}
/**
* Return if we should exit after printing version, help etc.
*/
bool ExitAfterPrinting() const
{
bool ExitAfterPrinting() const {
return _exitAfterPrint;
}
/**
* Return a list of paths user wants to ignore.
*/
std::vector<std::string> GetIgnoredPaths() const
{
std::vector<std::string> GetIgnoredPaths() const {
return _ignoredPaths;
}

View File

@ -49,10 +49,8 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
bool success = parser.ParseFromArgs(argc, argv);
cppcheck->settings(_settings); // copy the settings
if (success)
{
if (parser.GetShowVersion() && !parser.GetShowErrorMessages())
{
if (success) {
if (parser.GetShowVersion() && !parser.GetShowErrorMessages()) {
const char * extraVersion = cppcheck->extraVersion();
if (strlen(extraVersion) > 0)
std::cout << "Cppcheck " << cppcheck->version() << " ("
@ -61,8 +59,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
std::cout << "Cppcheck " << cppcheck->version() << std::endl;
}
if (parser.GetShowErrorMessages())
{
if (parser.GetShowErrorMessages()) {
errorlist = true;
std::cout << ErrorLogger::ErrorMessage::getXMLHeader(_settings._xml_version);
cppcheck->getErrorMessages();
@ -78,13 +75,11 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
std::list<std::string>::iterator iter;
for (iter = _settings._includePaths.begin();
iter != _settings._includePaths.end();
)
{
) {
const std::string path(Path::toNativeSeparators(*iter));
if (FileLister::isDirectory(path))
++iter;
else
{
else {
// If the include path is not found, warn user and remove the
// non-existing path from the list.
std::cout << "cppcheck: warning: Couldn't find path given by -I '" + path + "'" << std::endl;
@ -97,30 +92,25 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
std::vector<std::string> filenames;
std::map<std::string, long> filesizes;
if (!pathnames.empty())
{
if (!pathnames.empty()) {
// Execute recursiveAddFiles() to each given file parameter
std::vector<std::string>::const_iterator iter;
for (iter = pathnames.begin(); iter != pathnames.end(); ++iter)
FileLister::recursiveAddFiles(filenames, filesizes, Path::toNativeSeparators(*iter));
}
if (!filenames.empty())
{
if (!filenames.empty()) {
// 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;
std::vector<std::string> ignored = parser.GetIgnoredPaths();
std::vector<std::string>::iterator iterIgnored = ignored.begin();
for (int i = (int)ignored.size() - 1; i >= 0; i--)
{
for (int i = (int)ignored.size() - 1; i >= 0; i--) {
const std::string extension = Path::getFilenameExtension(ignored[i]);
if (extension == ".h" || extension == ".hpp")
{
if (extension == ".h" || extension == ".hpp") {
ignored.erase(iterIgnored + i);
if (!warned)
{
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
@ -130,8 +120,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
PathMatch matcher(parser.GetIgnoredPaths());
std::vector<std::string>::iterator iterBegin = filenames.begin();
for (int i = (int)filenames.size() - 1; i >= 0; i--)
{
for (int i = (int)filenames.size() - 1; i >= 0; i--) {
#if defined(_WIN32)
// For Windows we want case-insensitive path matching
const bool caseSensitive = false;
@ -141,26 +130,20 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
if (matcher.Match(filenames[(unsigned int)i], caseSensitive))
filenames.erase(iterBegin + i);
}
}
else
{
} else {
std::cout << "cppcheck: error: could not find or open any of the paths given." << std::endl;
return false;
}
if (!filenames.empty())
{
if (!filenames.empty()) {
std::vector<std::string>::iterator iter;
for (iter = filenames.begin(); iter != filenames.end(); ++iter)
{
for (iter = filenames.begin(); iter != filenames.end(); ++iter) {
_filenames.push_back(*iter);
_filesizes[*iter] = filesizes[*iter];
}
return true;
}
else
{
} else {
std::cout << "cppcheck: error: no files to check - all paths ignored." << std::endl;
return false;
}
@ -171,8 +154,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
Preprocessor::missingIncludeFlag = false;
CppCheck cppCheck(*this, true);
if (!parseFromArgs(&cppCheck, argc, argv))
{
if (!parseFromArgs(&cppCheck, argc, argv)) {
return EXIT_FAILURE;
}
@ -180,28 +162,23 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
time1 = std::time(0);
_settings = cppCheck.settings();
if (_settings._xml)
{
if (_settings._xml) {
reportErr(ErrorLogger::ErrorMessage::getXMLHeader(_settings._xml_version));
}
unsigned int returnValue = 0;
if (_settings._jobs == 1)
{
if (_settings._jobs == 1) {
// Single process
long totalfilesize = 0;
for (std::map<std::string, long>::const_iterator i = _filesizes.begin(); i != _filesizes.end(); ++i)
{
for (std::map<std::string, long>::const_iterator i = _filesizes.begin(); i != _filesizes.end(); ++i) {
totalfilesize += i->second;
}
long processedsize = 0;
for (unsigned int c = 0; c < _filenames.size(); c++)
{
for (unsigned int c = 0; c < _filenames.size(); c++) {
returnValue += cppCheck.check(_filenames[c]);
if (_filesizes.find(_filenames[c]) != _filesizes.end())
{
if (_filesizes.find(_filenames[c]) != _filesizes.end()) {
processedsize += _filesizes[_filenames[c]];
}
if (!_settings._errorsOnly)
@ -209,26 +186,20 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
}
cppCheck.checkFunctionUsage();
}
else if (!ThreadExecutor::isEnabled())
{
} else if (!ThreadExecutor::isEnabled()) {
std::cout << "No thread support yet implemented for this platform." << std::endl;
}
else
{
} else {
// Multiple processes
Settings &settings = cppCheck.settings();
ThreadExecutor executor(_filenames, _filesizes, settings, *this);
returnValue = executor.check();
}
if (!cppCheck.settings().checkConfiguration)
{
if (!cppCheck.settings().checkConfiguration) {
if (!_settings._errorsOnly)
reportUnmatchedSuppressions(cppCheck.settings().nomsg.getUnmatchedGlobalSuppressions());
if (Preprocessor::missingIncludeFlag)
{
if (Preprocessor::missingIncludeFlag) {
const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack;
ErrorLogger::ErrorMessage msg(callStack,
Severity::information,
@ -244,8 +215,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
}
}
if (_settings._xml)
{
if (_settings._xml) {
reportErr(ErrorLogger::ErrorMessage::getXMLFooter(_settings._xml_version));
}
@ -279,8 +249,7 @@ void CppCheckExecutor::reportProgress(const std::string &filename, const char st
// Report progress messages every 10 seconds
const std::time_t time2 = std::time(NULL);
if (time2 >= (time1 + 10))
{
if (time2 >= (time1 + 10)) {
time1 = time2;
// current time in the format "Www Mmm dd hh:mm:ss yyyy"
@ -301,8 +270,7 @@ void CppCheckExecutor::reportProgress(const std::string &filename, const char st
void CppCheckExecutor::reportStatus(unsigned int fileindex, unsigned int filecount, long sizedone, long sizetotal)
{
if (filecount > 1)
{
if (filecount > 1) {
std::ostringstream oss;
oss << fileindex << "/" << filecount
<< " files checked " <<
@ -314,16 +282,11 @@ void CppCheckExecutor::reportStatus(unsigned int fileindex, unsigned int filecou
void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
if (errorlist)
{
if (errorlist) {
reportOut(msg.toXML(false, _settings._xml_version));
}
else if (_settings._xml)
{
} else if (_settings._xml) {
reportErr(msg.toXML(_settings._verbose, _settings._xml_version));
}
else
{
} else {
reportErr(msg.toString(_settings._verbose, _settings._outputFormat));
}
}

View File

@ -35,8 +35,7 @@ class CppCheck;
* just rewrite this class for your needs and possibly use other methods
* from CppCheck class instead the ones used here.
*/
class CppCheckExecutor : public ErrorLogger
{
class CppCheckExecutor : public ErrorLogger {
public:
/**
* Constructor

View File

@ -44,8 +44,7 @@ bool FileLister::acceptFile(const std::string &filename)
extension == ".c" ||
extension == ".c++" ||
extension == ".tpp" ||
extension == ".txx")
{
extension == ".txx") {
return true;
}
@ -153,11 +152,9 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
oss << cleanedPath;
if (MyIsDirectory(cleanedPath.c_str()))
{
if (MyIsDirectory(cleanedPath.c_str())) {
char c = cleanedPath[ cleanedPath.size()-1 ];
switch (c)
{
switch (c) {
case '\\':
oss << '*';
bdir << cleanedPath;
@ -170,13 +167,10 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
if (cleanedPath != ".")
bdir << cleanedPath << '\\';
}
}
else
{
} else {
std::string::size_type pos;
pos = cleanedPath.find_last_of('\\');
if (std::string::npos != pos)
{
if (std::string::npos != pos) {
bdir << cleanedPath.substr(0, pos + 1);
}
}
@ -186,8 +180,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
if (INVALID_HANDLE_VALUE == hFind)
return;
do
{
do {
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
continue;
@ -196,8 +189,7 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
TransformUcs2ToAnsi(ffd.cFileName, ansiFfd, wcslen(ffd.cFileName) + 1);
#else // defined(UNICODE)
const char * ansiFfd = &ffd.cFileName[0];
if (strchr(ansiFfd,'?'))
{
if (strchr(ansiFfd,'?')) {
ansiFfd = &ffd.cAlternateFileName[0];
}
#endif // defined(UNICODE)
@ -205,32 +197,26 @@ void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, std::map
std::ostringstream fname;
fname << bdir.str().c_str() << ansiFfd;
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
// File
// If recursive is not used, accept all files given by user
if (Path::sameFileName(path,ansiFfd) || FileLister::acceptFile(ansiFfd))
{
if (Path::sameFileName(path,ansiFfd) || FileLister::acceptFile(ansiFfd)) {
const std::string nativename = Path::fromNativeSeparators(fname.str());
filenames.push_back(nativename);
// Limitation: file sizes are assumed to fit in a 'long'
filesizes[nativename] = ffd.nFileSizeLow;
}
}
else
{
} else {
// Directory
FileLister::recursiveAddFiles(filenames, filesizes, fname.str());
}
#if defined(UNICODE)
delete [] ansiFfd;
#endif // defined(UNICODE)
}
while (FindNextFile(hFind, &ffd) != FALSE);
} while (FindNextFile(hFind, &ffd) != FALSE);
if (INVALID_HANDLE_VALUE != hFind)
{
if (INVALID_HANDLE_VALUE != hFind) {
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
@ -274,14 +260,12 @@ void FileLister::recursiveAddFiles2(std::vector<std::string> &relative,
glob_t glob_results;
glob(oss.str().c_str(), GLOB_MARK, 0, &glob_results);
for (unsigned int i = 0; i < glob_results.gl_pathc; i++)
{
for (unsigned int i = 0; i < glob_results.gl_pathc; i++) {
const std::string filename = glob_results.gl_pathv[i];
if (filename == "." || filename == ".." || filename.length() == 0)
continue;
if (filename[filename.length()-1] != '/')
{
if (filename[filename.length()-1] != '/') {
// File
#ifdef PATH_MAX
char fname[PATH_MAX];
@ -295,21 +279,18 @@ void FileLister::recursiveAddFiles2(std::vector<std::string> &relative,
}
// Does absolute path exist? then bail out
if (std::find(absolute.begin(), absolute.end(), std::string(fname)) != absolute.end())
{
if (std::find(absolute.begin(), absolute.end(), std::string(fname)) != absolute.end()) {
#ifndef PATH_MAX
free(fname);
#endif
continue;
}
if (Path::sameFileName(path,filename) || FileLister::acceptFile(filename))
{
if (Path::sameFileName(path,filename) || FileLister::acceptFile(filename)) {
relative.push_back(filename);
absolute.push_back(fname);
struct stat sb;
if (stat(fname, &sb) == 0)
{
if (stat(fname, &sb) == 0) {
// Limitation: file sizes are assumed to fit in a 'long'
filesizes[filename] = static_cast<long>(sb.st_size);
}
@ -318,9 +299,7 @@ void FileLister::recursiveAddFiles2(std::vector<std::string> &relative,
#ifndef PATH_MAX
free(fname);
#endif
}
else
{
} else {
// Directory
recursiveAddFiles2(relative, absolute, filesizes, filename);
}
@ -341,11 +320,9 @@ bool FileLister::isDirectory(const std::string &path)
glob_t glob_results;
glob(path.c_str(), GLOB_MARK, 0, &glob_results);
if (glob_results.gl_pathc == 1)
{
if (glob_results.gl_pathc == 1) {
const std::string glob_path = glob_results.gl_pathv[0];
if (!glob_path.empty() && glob_path[glob_path.size() - 1] == '/')
{
if (!glob_path.empty() && glob_path[glob_path.size() - 1] == '/') {
ret = true;
}
}
@ -359,8 +336,7 @@ bool FileLister::fileExists(const std::string &path)
struct stat statinfo;
int result = stat(path.c_str(), &statinfo);
if (result < 0) // Todo: should check errno == ENOENT?
{
if (result < 0) { // Todo: should check errno == ENOENT?
// File not found
return false;
}

View File

@ -27,8 +27,7 @@
/// @{
/** @brief Cross-platform FileLister */
class FileLister
{
class FileLister {
public:
/**
* @brief Recursively add source files to a vector.

View File

@ -30,8 +30,7 @@ bool PathMatch::Match(const std::string &path, bool caseSensitive)
return false;
std::vector<std::string>::const_iterator iterMask;
for (iterMask = _masks.begin(); iterMask != _masks.end(); ++iterMask)
{
for (iterMask = _masks.begin(); iterMask != _masks.end(); ++iterMask) {
std::string mask(*iterMask);
if (!caseSensitive)
std::transform(mask.begin(), mask.end(), mask.begin(), ::tolower);
@ -41,8 +40,7 @@ bool PathMatch::Match(const std::string &path, bool caseSensitive)
std::transform(findpath.begin(), findpath.end(), findpath.begin(), ::tolower);
// Filtering directory name
if (mask[mask.length() - 1] == '/')
{
if (mask[mask.length() - 1] == '/') {
if (findpath[findpath.length() - 1] != '/')
findpath = RemoveFilename(findpath);
@ -59,8 +57,7 @@ bool PathMatch::Match(const std::string &path, bool caseSensitive)
return true;
}
// Filtering filename
else
{
else {
if (mask.length() > findpath.length())
continue;
// Check if path ends with mask

View File

@ -28,8 +28,7 @@
/**
* @brief Simple path matching for ignoring paths in CLI.
*/
class PathMatch
{
class PathMatch {
public:
/**

View File

@ -59,64 +59,52 @@ void ThreadExecutor::addFileContent(const std::string &path, const std::string &
int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
{
char type = 0;
if (read(rpipe, &type, 1) <= 0)
{
if (read(rpipe, &type, 1) <= 0) {
if (errno == EAGAIN)
return 0;
return -1;
}
if (type != '1' && type != '2' && type != '3')
{
if (type != '1' && type != '2' && type != '3') {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
unsigned int len = 0;
if (read(rpipe, &len, sizeof(len)) <= 0)
{
if (read(rpipe, &len, sizeof(len)) <= 0) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
char *buf = new char[len];
if (read(rpipe, buf, len) <= 0)
{
if (read(rpipe, buf, len) <= 0) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
exit(0);
}
if (type == '1')
{
if (type == '1') {
_errorLogger.reportOut(buf);
}
else if (type == '2')
{
} else if (type == '2') {
ErrorLogger::ErrorMessage msg;
msg.deserialize(buf);
std::string file;
unsigned int line(0);
if (!msg._callStack.empty())
{
if (!msg._callStack.empty()) {
file = msg._callStack.back().getfile(false);
line = msg._callStack.back().line;
}
if (!_settings.nomsg.isSuppressed(msg._id, file, line))
{
if (!_settings.nomsg.isSuppressed(msg._id, file, line)) {
// Alert only about unique errors
std::string errmsg = msg.toString(_settings._verbose);
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
{
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) {
_errorList.push_back(errmsg);
_errorLogger.reportErr(msg);
}
}
}
else if (type == '3')
{
} else if (type == '3') {
std::istringstream iss(buf);
unsigned int fileResult = 0;
iss >> fileResult;
@ -135,8 +123,7 @@ unsigned int ThreadExecutor::check()
unsigned int result = 0;
long totalfilesize = 0;
for (std::map<std::string, long>::const_iterator i = _filesizes.begin(); i != _filesizes.end(); ++i)
{
for (std::map<std::string, long>::const_iterator i = _filesizes.begin(); i != _filesizes.end(); ++i) {
totalfilesize += i->second;
}
@ -145,40 +132,32 @@ unsigned int ThreadExecutor::check()
std::map<int, std::string> pipeFile;
long processedsize = 0;
unsigned int i = 0;
while (true)
{
while (true) {
// Start a new child
if (i < _filenames.size() && rpipes.size() < _settings._jobs)
{
if (i < _filenames.size() && rpipes.size() < _settings._jobs) {
int pipes[2];
if (pipe(pipes) == -1)
{
if (pipe(pipes) == -1) {
perror("pipe");
exit(1);
}
int flags = 0;
if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0)
{
if ((flags = fcntl(pipes[0], F_GETFL, 0)) < 0) {
perror("fcntl");
exit(1);
}
if (fcntl(pipes[0], F_SETFL, flags | O_NONBLOCK) < 0)
{
if (fcntl(pipes[0], F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl");
exit(1);
}
pid_t pid = fork();
if (pid < 0)
{
if (pid < 0) {
// Error
std::cerr << "Failed to create child process" << std::endl;
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
} else if (pid == 0) {
close(pipes[0]);
_wpipe = pipes[1];
@ -186,13 +165,10 @@ unsigned int ThreadExecutor::check()
fileChecker.settings(_settings);
unsigned int resultOfCheck = 0;
if (_fileContents.size() > 0 && _fileContents.find(_filenames[i]) != _fileContents.end())
{
if (_fileContents.size() > 0 && _fileContents.find(_filenames[i]) != _fileContents.end()) {
// File content was given as a string
resultOfCheck = fileChecker.check(_filenames[i], _fileContents[ _filenames[i] ]);
}
else
{
} else {
// Read file from a file
resultOfCheck = fileChecker.check(_filenames[i]);
}
@ -209,9 +185,7 @@ unsigned int ThreadExecutor::check()
pipeFile[pipes[0]] = _filenames[i];
++i;
}
else if (!rpipes.empty())
{
} else if (!rpipes.empty()) {
fd_set rfds;
FD_ZERO(&rfds);
for (std::list<int>::const_iterator rp = rpipes.begin(); rp != rpipes.end(); ++rp)
@ -219,25 +193,19 @@ unsigned int ThreadExecutor::check()
int r = select(*std::max_element(rpipes.begin(), rpipes.end()) + 1, &rfds, NULL, NULL, NULL);
if (r > 0)
{
if (r > 0) {
std::list<int>::iterator rp = rpipes.begin();
while (rp != rpipes.end())
{
if (FD_ISSET(*rp, &rfds))
{
while (rp != rpipes.end()) {
if (FD_ISSET(*rp, &rfds)) {
int readRes = handleRead(*rp, result);
if (readRes == -1)
{
if (readRes == -1) {
long size = 0;
std::map<int, std::string>::iterator p = pipeFile.find(*rp);
if (p != pipeFile.end())
{
if (p != pipeFile.end()) {
std::string name = p->second;
pipeFile.erase(p);
std::map<std::string, long>::const_iterator fs = _filesizes.find(name);
if (fs != _filesizes.end())
{
if (fs != _filesizes.end()) {
size = fs->second;
}
}
@ -249,29 +217,24 @@ unsigned int ThreadExecutor::check()
close(*rp);
rp = rpipes.erase(rp);
}
else
} else
++rp;
}
else
} else
++rp;
}
}
int stat = 0;
pid_t child = waitpid(0, &stat, WNOHANG);
if (child > 0)
{
if (child > 0) {
std::string childname;
std::map<pid_t, std::string>::iterator c = childFile.find(child);
if (c != childFile.end())
{
if (c != childFile.end()) {
childname = c->second;
childFile.erase(c);
}
if (WIFSIGNALED(stat))
{
if (WIFSIGNALED(stat)) {
std::ostringstream oss;
oss << "Internal error: Child process crashed with signal " << WTERMSIG(stat);
@ -287,9 +250,7 @@ unsigned int ThreadExecutor::check()
_errorLogger.reportErr(errmsg);
}
}
}
else
{
} else {
// All done
break;
}
@ -306,8 +267,7 @@ void ThreadExecutor::writeToPipe(char type, const std::string &data)
out[0] = type;
std::memcpy(&(out[1]), &len, sizeof(len));
std::memcpy(&(out[1+sizeof(len)]), data.c_str(), len);
if (write(_wpipe, out, len + 1 + sizeof(len)) <= 0)
{
if (write(_wpipe, out, len + 1 + sizeof(len)) <= 0) {
delete [] out;
out = 0;
std::cerr << "#### ThreadExecutor::writeToPipe, Failed to write to pipe" << std::endl;

View File

@ -36,8 +36,7 @@
* This class will take a list of filenames and settings and check then
* all files using threads.
*/
class ThreadExecutor : public ErrorLogger
{
class ThreadExecutor : public ErrorLogger {
public:
ThreadExecutor(const std::vector<std::string> &filenames, const std::map<std::string, long> &filesizes, Settings &settings, ErrorLogger &_errorLogger);
virtual ~ThreadExecutor();
@ -84,8 +83,7 @@ public:
/**
* @return true if support for threads exist.
*/
static bool isEnabled()
{
static bool isEnabled() {
return true;
}
#else
@ -93,8 +91,7 @@ public:
/**
* @return true if support for threads exist.
*/
static bool isEnabled()
{
static bool isEnabled() {
return false;
}
#endif

View File

@ -27,8 +27,7 @@ AboutDialog::AboutDialog(const QString &version, const QString &extraVersion, QW
mUI.setupUi(this);
QString fmtVersion(version);
if (!extraVersion.isEmpty())
{
if (!extraVersion.isEmpty()) {
fmtVersion += " (" + extraVersion + ")";
}
mUI.mVersion->setText(mUI.mVersion->text().arg(fmtVersion));

View File

@ -32,8 +32,7 @@ class QWidget;
* @brief About dialog
*
*/
class AboutDialog : public QDialog
{
class AboutDialog : public QDialog {
Q_OBJECT
public:
AboutDialog(const QString &version, const QString &extraVersion,

View File

@ -40,8 +40,7 @@
* Executable: kate
* Parameters: -l(line) (file)
*/
class Application
{
class Application {
public:
Application() { }
Application(const QString &name, const QString &path, const QString &params);
@ -50,8 +49,7 @@ public:
* @brief Get application name.
* @return Application name.
*/
QString getName() const
{
QString getName() const {
return mName;
}
@ -59,8 +57,7 @@ public:
* @brief Get application path.
* @return Application path.
*/
QString getPath() const
{
QString getPath() const {
return mPath;
}
@ -68,8 +65,7 @@ public:
* @brief Get application command line parameters.
* @return Application command line parameters.
*/
QString getParameters() const
{
QString getParameters() const {
return mParameters;
}
@ -77,8 +73,7 @@ public:
* @brief Set application name.
* @param name Application name.
*/
void setName(const QString &name)
{
void setName(const QString &name) {
mName = name;
}
@ -86,8 +81,7 @@ public:
* @brief Set application path.
* @param path Application path.
*/
void setPath(const QString &path)
{
void setPath(const QString &path) {
mPath = path;
}
@ -95,8 +89,7 @@ public:
* @brief Set application command line parameters.
* @param parameters Application command line parameters.
*/
void setParameters(const QString &parameters)
{
void setParameters(const QString &parameters) {
mParameters = parameters;
}

View File

@ -62,8 +62,7 @@ void ApplicationDialog::Browse()
QString(),
filter);
if (!selectedFile.isEmpty())
{
if (!selectedFile.isEmpty()) {
QString path(QDir::toNativeSeparators(selectedFile));
mUI.mPath->setText(path);
}
@ -81,8 +80,7 @@ Application ApplicationDialog::GetApplication() const
void ApplicationDialog::Ok()
{
if (mUI.mName->text().isEmpty() || mUI.mPath->text().isEmpty() ||
mUI.mParameters->text().isEmpty())
{
mUI.mParameters->text().isEmpty()) {
QMessageBox msg(QMessageBox::Warning,
tr("Cppcheck"),
tr("You must specify a name, a path and parameters for the application!"),
@ -91,9 +89,7 @@ void ApplicationDialog::Ok()
msg.exec();
}
else
{
} else {
// Convert possible native (Windows) path to internal presentation format
mUI.mPath->setText(QDir::fromNativeSeparators(mUI.mPath->text()));
accept();

View File

@ -36,8 +36,7 @@ class QWidget;
* to modify/add an application to open errors with.
*
*/
class ApplicationDialog : public QDialog
{
class ApplicationDialog : public QDialog {
Q_OBJECT
public:

View File

@ -50,20 +50,16 @@ bool ApplicationList::LoadSettings()
// Params will be empty first time starting with the new setting.
// Return false and inform user about problem with application settings.
bool succeeded = true;
if (!names.empty() && !paths.empty() && params.empty())
{
if (!names.empty() && !paths.empty() && params.empty()) {
for (int i = 0; i < paths.length(); i++)
params << "";
succeeded = false;
}
if (names.empty() && paths.empty() && params.empty())
{
do
{
if (names.empty() && paths.empty() && params.empty()) {
do {
// use as default for gnome environments
if (QFileInfo("/usr/bin/gedit").isExecutable())
{
if (QFileInfo("/usr/bin/gedit").isExecutable()) {
Application app;
app.setName("gedit");
app.setPath("/usr/bin/gedit");
@ -73,8 +69,7 @@ bool ApplicationList::LoadSettings()
break;
}
// use as default for kde environments
if (QFileInfo("/usr/bin/kate").isExecutable())
{
if (QFileInfo("/usr/bin/kate").isExecutable()) {
Application app;
app.setName("kate");
app.setPath("/usr/bin/kate");
@ -83,19 +78,15 @@ bool ApplicationList::LoadSettings()
defapp = 0;
break;
}
if (FindDefaultWindowsEditor())
{
if (FindDefaultWindowsEditor()) {
defapp = 0;
break;
}
}
while (0);
} while (0);
}
if (names.size() > 0 && (names.size() == paths.size()))
{
for (int i = 0; i < names.size(); i++)
{
if (names.size() > 0 && (names.size() == paths.size())) {
for (int i = 0; i < names.size(); i++) {
const Application app(names[i], paths[i], params[i]);
AddApplication(app);
}
@ -117,8 +108,7 @@ void ApplicationList::SaveSettings()
QStringList paths;
QStringList params;
for (int i = 0; i < GetApplicationCount(); i++)
{
for (int i = 0; i < GetApplicationCount(); i++) {
Application app = GetApplication(i);
names << app.getName();
paths << app.getPath();
@ -139,8 +129,7 @@ int ApplicationList::GetApplicationCount() const
Application ApplicationList::GetApplication(const int index) const
{
if (index >= 0 && index < mApplications.size())
{
if (index >= 0 && index < mApplications.size()) {
return mApplications[index];
}
@ -149,16 +138,14 @@ Application ApplicationList::GetApplication(const int index) const
void ApplicationList::SetApplication(int index, const Application &app)
{
if (index >= 0 && index < mApplications.size())
{
if (index >= 0 && index < mApplications.size()) {
mApplications.replace(index, app);
}
}
void ApplicationList::AddApplication(const Application &app)
{
if (app.getName().isEmpty() || app.getPath().isEmpty())
{
if (app.getName().isEmpty() || app.getPath().isEmpty()) {
return;
}
mApplications << app;
@ -171,22 +158,19 @@ void ApplicationList::RemoveApplication(const int index)
void ApplicationList::SetDefault(const int index)
{
if (index < mApplications.size() && index >= 0)
{
if (index < mApplications.size() && index >= 0) {
mDefaultApplicationIndex = index;
}
}
void ApplicationList::Copy(const ApplicationList *list)
{
if (!list)
{
if (!list) {
return;
}
Clear();
for (int i = 0; i < list->GetApplicationCount(); i++)
{
for (int i = 0; i < list->GetApplicationCount(); i++) {
const Application app = list->GetApplication(i);
AddApplication(app);
}
@ -203,8 +187,7 @@ bool ApplicationList::FindDefaultWindowsEditor()
{
const QString appPath(getenv("ProgramFiles"));
const QString notepadppPath = appPath + "\\Notepad++\\notepad++.exe";
if (QFileInfo(notepadppPath).isExecutable())
{
if (QFileInfo(notepadppPath).isExecutable()) {
Application app;
app.setName("Notepad++");
app.setPath("\"" + notepadppPath + "\"");
@ -215,8 +198,7 @@ bool ApplicationList::FindDefaultWindowsEditor()
const QString windowsPath(getenv("windir"));
const QString notepadPath = windowsPath + "\\system32\\notepad.exe";
if (QFileInfo(notepadPath).isExecutable())
{
if (QFileInfo(notepadPath).isExecutable()) {
Application app;
app.setName("Notepad");
app.setPath(notepadPath);

View File

@ -30,8 +30,7 @@
/**
* @brief List of applications user has specified to open errors with.
*/
class ApplicationList : public QObject
{
class ApplicationList : public QObject {
Q_OBJECT
public:
@ -70,8 +69,7 @@ public:
* @brief Return the default application.
* @return Index of the default application.
*/
int GetDefaultApplication() const
{
int GetDefaultApplication() const {
return mDefaultApplicationIndex;
}

View File

@ -28,8 +28,7 @@ CheckStatistics::CheckStatistics(QObject *parent)
void CheckStatistics::AddItem(ShowTypes::ShowType type)
{
switch (type)
{
switch (type) {
case ShowTypes::ShowStyle:
mStyle++;
break;
@ -68,8 +67,7 @@ void CheckStatistics::Clear()
unsigned CheckStatistics::GetCount(ShowTypes::ShowType type) const
{
unsigned count = 0;
switch (type)
{
switch (type) {
case ShowTypes::ShowStyle:
count = mStyle;
break;

View File

@ -29,8 +29,7 @@
/**
* A class for check statistics.
*/
class CheckStatistics : public QObject
{
class CheckStatistics : public QObject {
public:
CheckStatistics(QObject *parent = NULL);

View File

@ -47,8 +47,7 @@ void CheckThread::run()
QString file;
file = mResult.GetNextFile();
while (!file.isEmpty() && mState == Running)
{
while (!file.isEmpty() && mState == Running) {
qDebug() << "Checking file" << file;
mCppcheck.check(file.toStdString());
emit FileChecked(file);

View File

@ -32,8 +32,7 @@
* @brief Thread to run cppcheck
*
*/
class CheckThread : public QThread
{
class CheckThread : public QThread {
Q_OBJECT
public:
CheckThread(ThreadResult &result);
@ -73,8 +72,7 @@ protected:
* has been completed. Thread must be stopped cleanly, just terminating thread
* likely causes unpredictable side-effects.
*/
enum State
{
enum State {
Running, /**< The thread is checking. */
Stopping, /**< The thread will stop after current work. */
Stopped, /**< The thread has been stopped. */

View File

@ -37,8 +37,7 @@ CsvReport::~CsvReport()
bool CsvReport::Create()
{
bool success = false;
if (Report::Create())
{
if (Report::Create()) {
mTxtWriter.setDevice(Report::GetFile());
success = true;
}

View File

@ -36,8 +36,7 @@
* easy to import to many other programs.
* @todo This class should be inherited from TxtReport?
*/
class CsvReport : public Report
{
class CsvReport : public Report {
public:
CsvReport(const QString &filename, QObject * parent = 0);
virtual ~CsvReport();

View File

@ -56,8 +56,7 @@ QString ErrorItem::ToString() const
str += GuiSeverity::toString(severity) +"\n";
str += summary + "\n";
str += message + "\n";
for (int i = 0; i < files.size(); i++)
{
for (int i = 0; i < files.size(); i++) {
str += " " + files[i] + ": " + QString::number(lines[i]) + "\n";
}
return str;

View File

@ -35,13 +35,10 @@ class ErrorLine;
* GUI needs its own versions of conversions since GUI uses Qt's QString
* instead of the std::string used by lib/cli.
*/
class GuiSeverity : Severity
{
class GuiSeverity : Severity {
public:
static QString toString(SeverityType severity)
{
switch (severity)
{
static QString toString(SeverityType severity) {
switch (severity) {
case none:
return "";
case error:
@ -62,8 +59,7 @@ public:
return "???";
}
static SeverityType fromString(const QString &severity)
{
static SeverityType fromString(const QString &severity) {
if (severity.isEmpty())
return none;
if (severity == "none")
@ -94,8 +90,7 @@ public:
* Full path is stored instead of relative path for flexibility. It is easy
* to get the relative path from full path when needed.
*/
class ErrorItem
{
class ErrorItem {
public:
ErrorItem();
ErrorItem(const ErrorItem &item);
@ -123,8 +118,7 @@ Q_DECLARE_METATYPE(ErrorItem);
/**
* @brief A class containing error data for one shown error line.
*/
class ErrorLine
{
class ErrorLine {
public:
QString file;
unsigned int line;

View File

@ -32,8 +32,7 @@ QStringList FileList::GetDefaultFilters()
bool FileList::FilterMatches(const QFileInfo &inf)
{
if (inf.isFile())
{
if (inf.isFile()) {
const QStringList filters = FileList::GetDefaultFilters();
QString ext("*.");
ext += inf.suffix();
@ -57,14 +56,11 @@ void FileList::AddDirectory(const QString &directory, bool recursive)
const QStringList filters = FileList::GetDefaultFilters();
const QStringList origNameFilters = dir.nameFilters();
dir.setNameFilters(filters);
if (!recursive)
{
if (!recursive) {
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList items = dir.entryInfoList();
mFileList += items;
}
else
{
} else {
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QFileInfoList items = dir.entryInfoList();
mFileList += items;
@ -73,8 +69,7 @@ void FileList::AddDirectory(const QString &directory, bool recursive)
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
QFileInfo item;
foreach(item, list)
{
foreach(item, list) {
const QString path = item.canonicalFilePath();
AddDirectory(path, recursive);
}
@ -84,8 +79,7 @@ void FileList::AddDirectory(const QString &directory, bool recursive)
void FileList::AddPathList(const QStringList &paths)
{
QString path;
foreach(path, paths)
{
foreach(path, paths) {
QFileInfo inf(path);
if (inf.isFile())
AddFile(path);
@ -96,18 +90,14 @@ void FileList::AddPathList(const QStringList &paths)
QStringList FileList::GetFileList() const
{
if (mExcludedPaths.empty())
{
if (mExcludedPaths.empty()) {
QStringList names;
foreach(QFileInfo item, mFileList)
{
foreach(QFileInfo item, mFileList) {
QString name = QDir::fromNativeSeparators(item.canonicalFilePath());
names << name;
}
return names;
}
else
{
} else {
return ApplyExcludeList();
}
}
@ -120,8 +110,7 @@ void FileList::AddExcludeList(const QStringList &paths)
QStringList FileList::ApplyExcludeList() const
{
QStringList paths;
foreach(QFileInfo item, mFileList)
{
foreach(QFileInfo item, mFileList) {
QString name = QDir::fromNativeSeparators(item.canonicalFilePath());
if (!Match(name))
paths << name;
@ -131,16 +120,12 @@ QStringList FileList::ApplyExcludeList() const
bool FileList::Match(const QString &path) const
{
for (int i = 0; i < mExcludedPaths.size(); i++)
{
if (mExcludedPaths[i].endsWith('/'))
{
for (int i = 0; i < mExcludedPaths.size(); i++) {
if (mExcludedPaths[i].endsWith('/')) {
const QString pathexclude("/" + mExcludedPaths[i]);
if (path.indexOf(pathexclude) != -1)
return true;
}
else
{
} else {
if (path.endsWith(mExcludedPaths[i]))
return true;
}

View File

@ -35,8 +35,7 @@
* there is ignore filters then only paths not matching those filters are
* returned.
*/
class FileList
{
class FileList {
public:
/**

View File

@ -38,8 +38,7 @@ FileViewDialog::FileViewDialog(const QString &file,
void FileViewDialog::LoadTextFile(const QString &filename, QTextEdit *edit)
{
QFile file(filename);
if (!file.exists())
{
if (!file.exists()) {
QString msg(tr("Could not find the file: %1"));
msg = msg.arg(filename);
@ -53,8 +52,7 @@ void FileViewDialog::LoadTextFile(const QString &filename, QTextEdit *edit)
}
file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!file.isReadable())
{
if (!file.isReadable()) {
QString msg(tr("Could not read the file: %1"));
msg = msg.arg(filename);

View File

@ -36,8 +36,7 @@ class QTextEdit;
* the authors list.
*
*/
class FileViewDialog : public QDialog
{
class FileViewDialog : public QDialog {
Q_OBJECT
public:
FileViewDialog(const QString &file,

View File

@ -65,11 +65,9 @@ void LogView::SaveButtonClicked()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Log"),
"", tr("Text files (*.txt *.log);;All files (*.*)"));
if (!fileName.isEmpty())
{
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Cppcheck"),
tr("Could not open file for writing: \"%1\"").arg(fileName));
return;

View File

@ -29,8 +29,7 @@
* @brief A tool window that shows checking log.
*
*/
class LogView : public QWidget
{
class LogView : public QWidget {
Q_OBJECT
public:
LogView(QWidget *parent = 0);

View File

@ -58,8 +58,7 @@ int main(int argc, char *argv[])
// Rest of the arguments are handled in MainWindow::HandleCLIParams()
bool CheckArgs(const QStringList &args)
{
if (args.contains("-h") || args.contains("--help"))
{
if (args.contains("-h") || args.contains("--help")) {
ShowUsage();
return false;
}

View File

@ -134,13 +134,11 @@ MainWindow::MainWindow() :
QStringList args = QCoreApplication::arguments();
//Remove the application itself
args.removeFirst();
if (!args.isEmpty())
{
if (!args.isEmpty()) {
HandleCLIParams(args);
}
for (int i = 0; i < MaxRecentProjects; ++i)
{
for (int i = 0; i < MaxRecentProjects; ++i) {
mRecentProjectActs[i] = new QAction(this);
mRecentProjectActs[i]->setVisible(false);
connect(mRecentProjectActs[i], SIGNAL(triggered()),
@ -151,8 +149,7 @@ MainWindow::MainWindow() :
UpdateMRUMenuItems();
QActionGroup* platformGroup = new QActionGroup(this);
for (int i = 0; i < mPlatforms.getCount(); i++)
{
for (int i = 0; i < mPlatforms.getCount(); i++) {
Platform plat = mPlatforms.mPlatforms[i];
QAction *act = new QAction(this);
plat.mActMainWindow = act;
@ -185,28 +182,23 @@ MainWindow::~MainWindow()
void MainWindow::HandleCLIParams(const QStringList &params)
{
if (params.contains("-p"))
{
if (params.contains("-p")) {
QString projFile;
const int ind = params.indexOf("-p");
if ((ind + 1) < params.length())
projFile = params[ind + 1];
LoadProjectFile(projFile);
}
else
} else
DoCheckFiles(params);
}
void MainWindow::LoadSettings()
{
// Window/dialog sizes
if (mSettings->value(SETTINGS_WINDOW_MAXIMIZED, false).toBool())
{
if (mSettings->value(SETTINGS_WINDOW_MAXIMIZED, false).toBool()) {
showMaximized();
}
else
{
} else {
resize(mSettings->value(SETTINGS_WINDOW_WIDTH, 800).toInt(),
mSettings->value(SETTINGS_WINDOW_HEIGHT, 600).toInt());
}
@ -235,8 +227,7 @@ void MainWindow::LoadSettings()
SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toString());
bool succeeded = mApplications->LoadSettings();
if (!succeeded)
{
if (!succeeded) {
const QString msg = tr("There was a problem with loading the editor application settings.\n\n"
"This is probably because the settings were changed between the Cppcheck versions. "
"Please check (and fix) the editor application settings, otherwise the editor "
@ -283,8 +274,7 @@ void MainWindow::SaveSettings()
void MainWindow::DoCheckFiles(const QStringList &files)
{
if (files.isEmpty())
{
if (files.isEmpty()) {
return;
}
ClearResults();
@ -298,8 +288,7 @@ void MainWindow::DoCheckFiles(const QStringList &files)
mUI.mResults->Clear();
mThread->ClearFiles();
if (fileNames.isEmpty())
{
if (fileNames.isEmpty()) {
QMessageBox msg(QMessageBox::Warning,
tr("Cppcheck"),
tr("No suitable files found to check!"),
@ -329,8 +318,7 @@ void MainWindow::DoCheckFiles(const QStringList &files)
QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
{
if (mProject)
{
if (mProject) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("You must close the project file before selecting new files or directories!"));
@ -345,27 +333,22 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
// NOTE: we use QFileDialog::getOpenFileNames() and
// QFileDialog::getExistingDirectory() because they show native Windows
// selection dialog which is a lot more usable than QT:s own dialog.
if (mode == QFileDialog::ExistingFiles)
{
if (mode == QFileDialog::ExistingFiles) {
selected = QFileDialog::getOpenFileNames(this,
tr("Select files to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
if (selected.isEmpty())
mCurrentDirectory.clear();
else
{
else {
QFileInfo inf(selected[0]);
mCurrentDirectory = inf.absolutePath();
}
FormatAndSetTitle();
}
else if (mode == QFileDialog::DirectoryOnly)
{
} else if (mode == QFileDialog::DirectoryOnly) {
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory to check"),
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
if (!dir.isEmpty())
{
if (!dir.isEmpty()) {
qDebug() << "Setting current directory to: " << dir;
mCurrentDirectory = dir;
selected.append(dir);
@ -394,10 +377,8 @@ void MainWindow::CheckDirectory()
checkDir.setFilter(QDir::Files | QDir::Readable);
checkDir.setNameFilters(filters);
QStringList projFiles = checkDir.entryList();
if (!projFiles.empty())
{
if (projFiles.size() == 1)
{
if (!projFiles.empty()) {
if (projFiles.size() == 1) {
// If one project file found, suggest loading it
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
@ -409,21 +390,16 @@ void MainWindow::CheckDirectory()
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int dlgResult = msgBox.exec();
if (dlgResult == QMessageBox::Yes)
{
if (dlgResult == QMessageBox::Yes) {
QString path = checkDir.canonicalPath();
if (!path.endsWith("/"))
path += "/";
path += projFiles[0];
LoadProjectFile(path);
}
else
{
} else {
DoCheckFiles(dir);
}
}
else
{
} else {
// If multiple project files found inform that there are project
// files also available.
QMessageBox msgBox(this);
@ -437,14 +413,11 @@ void MainWindow::CheckDirectory()
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int dlgResult = msgBox.exec();
if (dlgResult == QMessageBox::Yes)
{
if (dlgResult == QMessageBox::Yes) {
DoCheckFiles(dir);
}
}
}
else
{
} else {
DoCheckFiles(dir);
}
}
@ -452,8 +425,7 @@ void MainWindow::CheckDirectory()
void MainWindow::AddIncludeDirs(const QStringList &includeDirs, Settings &result)
{
QString dir;
foreach(dir, includeDirs)
{
foreach(dir, includeDirs) {
QString incdir;
if (!QDir::isAbsolutePath(dir))
incdir = mCurrentDirectory + "/";
@ -472,16 +444,14 @@ Settings MainWindow::GetCppcheckSettings()
Settings result;
// If project file loaded, read settings from it
if (mProject)
{
if (mProject) {
ProjectFile *pfile = mProject->GetProjectFile();
QStringList dirs = pfile->GetIncludeDirs();
AddIncludeDirs(dirs, result);
QStringList defines = pfile->GetDefines();
QString define;
foreach(define, defines)
{
foreach(define, defines) {
if (!result.userDefines.empty())
result.userDefines += ";";
result.userDefines += define.toStdString();
@ -492,8 +462,7 @@ Settings MainWindow::GetCppcheckSettings()
// Global include directories must be added AFTER the per project include
// directories so per project include directories can override global ones.
const QString globalIncludes = mSettings->value(SETTINGS_GLOBAL_INCLUDE_PATHS).toString();
if (!globalIncludes.isEmpty())
{
if (!globalIncludes.isEmpty()) {
QStringList includes = globalIncludes.split(";");
AddIncludeDirs(includes, result);
}
@ -514,8 +483,7 @@ Settings MainWindow::GetCppcheckSettings()
result.inconclusive = mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool();
result.platformType = (Settings::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt();
if (result._jobs <= 0)
{
if (result._jobs <= 0) {
result._jobs = 1;
}
@ -524,8 +492,7 @@ Settings MainWindow::GetCppcheckSettings()
void MainWindow::CheckDone()
{
if (mExiting)
{
if (mExiting) {
close();
return;
}
@ -537,8 +504,7 @@ void MainWindow::CheckDone()
EnableProjectActions(true);
EnableProjectOpenActions(true);
if (mUI.mResults->HasResults())
{
if (mUI.mResults->HasResults()) {
mUI.mActionClearResults->setEnabled(true);
mUI.mActionSave->setEnabled(true);
}
@ -559,8 +525,7 @@ void MainWindow::CheckLockDownUI()
void MainWindow::ProgramSettings()
{
SettingsDialog dialog(mApplications, mTranslation, this);
if (dialog.exec() == QDialog::Accepted)
{
if (dialog.exec() == QDialog::Accepted) {
dialog.SaveSettingValues();
mUI.mResults->UpdateSettings(dialog.ShowFullPath(),
dialog.SaveFullPath(),
@ -603,8 +568,7 @@ void MainWindow::OpenXML()
filter,
&selectedFilter);
if (!selectedFile.isEmpty())
{
if (!selectedFile.isEmpty()) {
mUI.mResults->ReadErrorsXml(selectedFile);
}
}
@ -663,13 +627,10 @@ void MainWindow::UncheckAll()
void MainWindow::closeEvent(QCloseEvent *event)
{
// Check that we aren't checking files
if (!mThread->IsChecking())
{
if (!mThread->IsChecking()) {
SaveSettings();
event->accept();
}
else
{
} else {
const QString text(tr("Checking is running.\n\n" \
"Do you want to stop the checking and exit Cppcheck?."));
@ -681,8 +642,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
msg.setDefaultButton(QMessageBox::No);
int rv = msg.exec();
if (rv == QMessageBox::Yes)
{
if (rv == QMessageBox::Yes) {
// This isn't really very clean way to close threads but since the app is
// exiting it doesn't matter.
mThread->Stop();
@ -739,35 +699,25 @@ void MainWindow::Save()
filter,
&selectedFilter);
if (!selectedFile.isEmpty())
{
if (!selectedFile.isEmpty()) {
Report::Type type = Report::TXT;
if (selectedFilter == tr("XML files version 1 (*.xml)"))
{
if (selectedFilter == tr("XML files version 1 (*.xml)")) {
type = Report::XML;
if (!selectedFile.endsWith(".xml", Qt::CaseInsensitive))
selectedFile += ".xml";
}
else if (selectedFilter == tr("XML files version 2 (*.xml)"))
{
} else if (selectedFilter == tr("XML files version 2 (*.xml)")) {
type = Report::XMLV2;
if (!selectedFile.endsWith(".xml", Qt::CaseInsensitive))
selectedFile += ".xml";
}
else if (selectedFilter == tr("Text files (*.txt)"))
{
} else if (selectedFilter == tr("Text files (*.txt)")) {
type = Report::TXT;
if (!selectedFile.endsWith(".txt", Qt::CaseInsensitive))
selectedFile += ".txt";
}
else if (selectedFilter == tr("CSV files (*.csv)"))
{
} else if (selectedFilter == tr("CSV files (*.csv)")) {
type = Report::CSV;
if (!selectedFile.endsWith(".csv", Qt::CaseInsensitive))
selectedFile += ".csv";
}
else
{
} else {
if (selectedFile.endsWith(".xml", Qt::CaseInsensitive))
type = Report::XML;
else if (selectedFile.endsWith(".txt", Qt::CaseInsensitive))
@ -817,8 +767,7 @@ void MainWindow::SetLanguage(const QString &code)
return;
QString error;
if (!mTranslation->SetLanguage(code, error))
{
if (!mTranslation->SetLanguage(code, error)) {
const QString msg(tr("Failed to change the user interface language:"
"\n\n%1\n\n"
"The user interface language has been reset to English. Open "
@ -830,9 +779,7 @@ void MainWindow::SetLanguage(const QString &code)
QMessageBox::Ok,
this);
msgBox.exec();
}
else
{
} else {
//Translate everything that is visible here
mUI.retranslateUi(this);
mUI.mResults->Translate();
@ -870,8 +817,7 @@ void MainWindow::OpenProjectFile()
QString(),
filter);
if (!filepath.isEmpty())
{
if (!filepath.isEmpty()) {
LoadProjectFile(filepath);
}
}
@ -911,16 +857,13 @@ void MainWindow::CheckProject(Project *project)
// file's location, see above). This is to keep the compatibility with
// old "silent" project file loading when we checked the director where the
// project file was located.
if (paths.isEmpty())
{
if (paths.isEmpty()) {
paths << mCurrentDirectory;
}
// Convert relative paths to absolute paths
for (int i = 0; i < paths.size(); i++)
{
if (!QDir::isAbsolutePath(paths[i]))
{
for (int i = 0; i < paths.size(); i++) {
if (!QDir::isAbsolutePath(paths[i])) {
QString path = mCurrentDirectory + "/";
path += paths[i];
paths[i] = QDir::cleanPath(path);
@ -937,8 +880,7 @@ void MainWindow::NewProjectFile()
QString(),
filter);
if (!filepath.isEmpty())
{
if (!filepath.isEmpty()) {
EnableProjectActions(true);
QFileInfo inf(filepath);
const QString filename = inf.fileName();
@ -964,8 +906,7 @@ void MainWindow::CloseProjectFile()
void MainWindow::EditProjectFile()
{
if (!mProject)
{
if (!mProject) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
QString(tr("No project file loaded")),
@ -992,8 +933,7 @@ void MainWindow::ShowStatistics()
StatsDialog statsDialog(this);
// Show a dialog with the previous scan statistics and project information
if (mProject)
{
if (mProject) {
statsDialog.setProject(*mProject);
}
statsDialog.setPathSelected(mCurrentDirectory);
@ -1006,16 +946,14 @@ void MainWindow::ShowStatistics()
void MainWindow::Log(const QString &logline)
{
if (mLogView)
{
if (mLogView) {
mLogView->AppendLine(logline);
}
}
void MainWindow::DebugError(const ErrorItem &item)
{
if (mLogView)
{
if (mLogView) {
mLogView->AppendLine(item.ToString());
}
}
@ -1040,16 +978,12 @@ void MainWindow::EnableProjectOpenActions(bool enable)
void MainWindow::OpenRecentProject()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
{
if (action) {
const QString project = action->data().toString();
QFileInfo inf(project);
if (inf.exists())
{
if (inf.exists()) {
LoadProjectFile(project);
}
else
{
} else {
const QString text(tr("The project file\n\n%1\n\n could not be found!\n\n"
"Do you want to remove the file from the recently "
"used projects -list?").arg(project));
@ -1062,8 +996,7 @@ void MainWindow::OpenRecentProject()
msg.setDefaultButton(QMessageBox::No);
int rv = msg.exec();
if (rv == QMessageBox::Yes)
{
if (rv == QMessageBox::Yes) {
RemoveProjectMRU(project);
}
@ -1073,16 +1006,14 @@ void MainWindow::OpenRecentProject()
void MainWindow::UpdateMRUMenuItems()
{
for (int i = 0; i < MaxRecentProjects + 1; i++)
{
for (int i = 0; i < MaxRecentProjects + 1; i++) {
if (mRecentProjectActs[i] != NULL)
mUI.mMenuFile->removeAction(mRecentProjectActs[i]);
}
QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();
const int numRecentProjects = qMin(projects.size(), (int)MaxRecentProjects);
for (int i = 0; i < numRecentProjects; i++)
{
for (int i = 0; i < numRecentProjects; i++) {
const QString filename = QFileInfo(projects[i]).fileName();
const QString text = QString("&%1 %2").arg(i + 1).arg(filename);
mRecentProjectActs[i]->setText(text);
@ -1119,8 +1050,7 @@ void MainWindow::RemoveProjectMRU(const QString &project)
void MainWindow::SelectPlatform()
{
QAction *action = qobject_cast<QAction *>(sender());
if (action)
{
if (action) {
const Settings::PlatformType platform = (Settings::PlatformType) action->data().toInt();
mSettings->setValue(SETTINGS_CHECKED_PLATFORM, platform);
}

View File

@ -49,8 +49,7 @@ class QAction;
* @brief Main window for cppcheck-gui
*
*/
class MainWindow : public QMainWindow
{
class MainWindow : public QMainWindow {
Q_OBJECT
public:

View File

@ -50,10 +50,8 @@ int Platforms::getCount() const
Platform& Platforms::get(Settings::PlatformType platform)
{
QList<Platform>::iterator iter = mPlatforms.begin();
while (iter != mPlatforms.end())
{
if ((*iter).mType == platform)
{
while (iter != mPlatforms.end()) {
if ((*iter).mType == platform) {
return *iter;
}
++iter;

View File

@ -31,8 +31,7 @@
/**
* @brief Checked platform GUI-data.
*/
struct Platform
{
struct Platform {
QString mTitle; /**< Text visible in the GUI. */
Settings::PlatformType mType; /**< Type in the core. */
QAction *mActMainWindow; /**< Pointer to main window action item. */
@ -41,8 +40,7 @@ struct Platform
/**
* @brief List of checked platforms.
*/
class Platforms : public QObject
{
class Platforms : public QObject {
Q_OBJECT
public:

View File

@ -65,10 +65,8 @@ bool Project::IsOpen() const
bool Project::Open()
{
mPFile = new ProjectFile(mFilename, this);
if (QFile::exists(mFilename))
{
if (!mPFile->Read())
{
if (QFile::exists(mFilename)) {
if (!mPFile->Read()) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not read the project file."),
@ -99,8 +97,7 @@ void Project::Edit()
dlg.SetExcludedPaths(ignorepaths);
int rv = dlg.exec();
if (rv == QDialog::Accepted)
{
if (rv == QDialog::Accepted) {
QString root = dlg.GetRootPath();
mPFile->SetRootPath(root);
QStringList includes = dlg.GetIncludePaths();
@ -113,8 +110,7 @@ void Project::Edit()
mPFile->SetExcludedPaths(excludedpaths);
bool writeSuccess = mPFile->Write();
if (!writeSuccess)
{
if (!writeSuccess) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Could not write the project file."),

View File

@ -32,8 +32,7 @@ class ProjectFile;
* @brief A class that contains project data and manages projects.
* Currently only project file creation and editing is implemented.
*/
class Project : public QObject
{
class Project : public QObject {
Q_OBJECT
public:
@ -79,8 +78,7 @@ public:
* @brief Return current project file.
* @return project file.
*/
ProjectFile * GetProjectFile() const
{
ProjectFile * GetProjectFile() const {
return mPFile;
}

View File

@ -68,13 +68,10 @@ bool ProjectFile::Read(const QString &filename)
QXmlStreamReader xmlReader(&file);
bool insideProject = false;
bool projectTagFound = false;
while (!xmlReader.atEnd())
{
switch (xmlReader.readNext())
{
while (!xmlReader.atEnd()) {
switch (xmlReader.readNext()) {
case QXmlStreamReader::StartElement:
if (xmlReader.name() == ProjectElementName)
{
if (xmlReader.name() == ProjectElementName) {
insideProject = true;
projectTagFound = true;
}
@ -134,8 +131,7 @@ bool ProjectFile::Read(const QString &filename)
QStringList ProjectFile::GetIncludeDirs() const
{
QStringList dirs;
foreach(QString path, mIncludeDirs)
{
foreach(QString path, mIncludeDirs) {
dirs << QDir::fromNativeSeparators(path);
}
return dirs;
@ -149,8 +145,7 @@ QStringList ProjectFile::GetDefines() const
QStringList ProjectFile::GetCheckPaths() const
{
QStringList paths;
foreach(QString path, mPaths)
{
foreach(QString path, mPaths) {
paths << QDir::fromNativeSeparators(path);
}
return paths;
@ -159,8 +154,7 @@ QStringList ProjectFile::GetCheckPaths() const
QStringList ProjectFile::GetExcludedPaths() const
{
QStringList paths;
foreach(QString path, mExcludedPaths)
{
foreach(QString path, mExcludedPaths) {
paths << QDir::fromNativeSeparators(path);
}
return paths;
@ -178,16 +172,13 @@ void ProjectFile::ReadIncludeDirs(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do
{
do {
type = reader.readNext();
switch (type)
{
switch (type) {
case QXmlStreamReader::StartElement:
// Read dir-elements
if (reader.name().toString() == DirElementName)
{
if (reader.name().toString() == DirElementName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", DirNameAttrib).toString();
if (!name.isEmpty())
@ -212,23 +203,19 @@ void ProjectFile::ReadIncludeDirs(QXmlStreamReader &reader)
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
while (!allRead);
} while (!allRead);
}
void ProjectFile::ReadDefines(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do
{
do {
type = reader.readNext();
switch (type)
{
switch (type) {
case QXmlStreamReader::StartElement:
// Read define-elements
if (reader.name().toString() == DefineName)
{
if (reader.name().toString() == DefineName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", DefineNameAttrib).toString();
if (!name.isEmpty())
@ -253,24 +240,20 @@ void ProjectFile::ReadDefines(QXmlStreamReader &reader)
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
while (!allRead);
} while (!allRead);
}
void ProjectFile::ReadCheckPaths(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do
{
do {
type = reader.readNext();
switch (type)
{
switch (type) {
case QXmlStreamReader::StartElement:
// Read dir-elements
if (reader.name().toString() == PathName)
{
if (reader.name().toString() == PathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", PathNameAttrib).toString();
if (!name.isEmpty())
@ -295,31 +278,26 @@ void ProjectFile::ReadCheckPaths(QXmlStreamReader &reader)
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
while (!allRead);
} while (!allRead);
}
void ProjectFile::ReadExcludes(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do
{
do {
type = reader.readNext();
switch (type)
{
switch (type) {
case QXmlStreamReader::StartElement:
// Read exclude-elements
if (reader.name().toString() == ExcludePathName)
{
if (reader.name().toString() == ExcludePathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", ExcludePathNameAttrib).toString();
if (!name.isEmpty())
mExcludedPaths << name;
}
// Read ignore-elements - deprecated but support reading them
else if (reader.name().toString() == IgnorePathName)
{
else if (reader.name().toString() == IgnorePathName) {
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", IgnorePathNameAttrib).toString();
if (!name.isEmpty())
@ -346,8 +324,7 @@ void ProjectFile::ReadExcludes(QXmlStreamReader &reader)
case QXmlStreamReader::ProcessingInstruction:
break;
}
}
while (!allRead);
} while (!allRead);
}
void ProjectFile::SetIncludes(const QStringList &includes)
@ -385,18 +362,15 @@ bool ProjectFile::Write(const QString &filename)
xmlWriter.writeStartElement(ProjectElementName);
xmlWriter.writeAttribute(ProjectVersionAttrib, ProjectFileVersion);
if (!mRootPath.isEmpty())
{
if (!mRootPath.isEmpty()) {
xmlWriter.writeStartElement(RootPathName);
xmlWriter.writeAttribute(RootPathNameAttrib, mRootPath);
xmlWriter.writeEndElement();
}
if (!mIncludeDirs.isEmpty())
{
if (!mIncludeDirs.isEmpty()) {
xmlWriter.writeStartElement(IncludDirElementName);
foreach(QString incdir, mIncludeDirs)
{
foreach(QString incdir, mIncludeDirs) {
xmlWriter.writeStartElement(DirElementName);
xmlWriter.writeAttribute(DirNameAttrib, incdir);
xmlWriter.writeEndElement();
@ -404,11 +378,9 @@ bool ProjectFile::Write(const QString &filename)
xmlWriter.writeEndElement();
}
if (!mDefines.isEmpty())
{
if (!mDefines.isEmpty()) {
xmlWriter.writeStartElement(DefinesElementName);
foreach(QString define, mDefines)
{
foreach(QString define, mDefines) {
xmlWriter.writeStartElement(DefineName);
xmlWriter.writeAttribute(DefineNameAttrib, define);
xmlWriter.writeEndElement();
@ -416,11 +388,9 @@ bool ProjectFile::Write(const QString &filename)
xmlWriter.writeEndElement();
}
if (!mPaths.isEmpty())
{
if (!mPaths.isEmpty()) {
xmlWriter.writeStartElement(PathsElementName);
foreach(QString path, mPaths)
{
foreach(QString path, mPaths) {
xmlWriter.writeStartElement(PathName);
xmlWriter.writeAttribute(PathNameAttrib, path);
xmlWriter.writeEndElement();
@ -428,11 +398,9 @@ bool ProjectFile::Write(const QString &filename)
xmlWriter.writeEndElement();
}
if (!mExcludedPaths.isEmpty())
{
if (!mExcludedPaths.isEmpty()) {
xmlWriter.writeStartElement(ExcludeElementName);
foreach(QString path, mExcludedPaths)
{
foreach(QString path, mExcludedPaths) {
xmlWriter.writeStartElement(ExcludePathName);
xmlWriter.writeAttribute(ExcludePathNameAttrib, path);
xmlWriter.writeEndElement();

View File

@ -33,8 +33,7 @@
* The project files contain project-specific settings for checking. For
* example a list of include paths.
*/
class ProjectFile : public QObject
{
class ProjectFile : public QObject {
Q_OBJECT
public:
@ -51,8 +50,7 @@ public:
* @brief Get project root path.
* @return project root path.
*/
QString GetRootPath() const
{
QString GetRootPath() const {
return mRootPath;
}
@ -84,8 +82,7 @@ public:
* @brief Get filename for the project file.
* @return file name.
*/
QString GetFilename()
{
QString GetFilename() {
return mFilename;
}
@ -93,8 +90,7 @@ public:
* @brief Set project root path.
* @param rootpath new project root path.
*/
void SetRootPath(const QString &rootpath)
{
void SetRootPath(const QString &rootpath) {
mRootPath = rootpath;
}
@ -132,8 +128,7 @@ public:
* @brief Set filename for the project file.
* @param filename Filename to use.
*/
void SetFilename(const QString &filename)
{
void SetFilename(const QString &filename) {
mFilename = filename;
}

View File

@ -118,8 +118,7 @@ QStringList ProjectFileDialog::GetIncludePaths() const
{
const int count = mUI.mListIncludeDirs->count();
QStringList includePaths;
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++) {
QListWidgetItem *item = mUI.mListIncludeDirs->item(i);
includePaths << QDir::fromNativeSeparators(item->text());
}
@ -130,8 +129,7 @@ QStringList ProjectFileDialog::GetDefines() const
{
QString define = mUI.mEditDefines->text();
QStringList defines;
if (!define.isEmpty())
{
if (!define.isEmpty()) {
define = define.trimmed();
if (define.indexOf(';') != -1)
defines = define.split(";");
@ -145,8 +143,7 @@ QStringList ProjectFileDialog::GetPaths() const
{
const int count = mUI.mListPaths->count();
QStringList paths;
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++) {
QListWidgetItem *item = mUI.mListPaths->item(i);
paths << QDir::fromNativeSeparators(item->text());
}
@ -157,8 +154,7 @@ QStringList ProjectFileDialog::GetExcludedPaths() const
{
const int count = mUI.mListExcludedPaths->count();
QStringList paths;
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++) {
QListWidgetItem *item = mUI.mListExcludedPaths->item(i);
paths << QDir::fromNativeSeparators(item->text());
}
@ -173,8 +169,7 @@ void ProjectFileDialog::SetRootPath(const QString &root)
void ProjectFileDialog::SetIncludepaths(const QStringList &includes)
{
foreach(QString dir, includes)
{
foreach(QString dir, includes) {
AddIncludeDir(dir);
}
}
@ -183,8 +178,7 @@ void ProjectFileDialog::SetDefines(const QStringList &defines)
{
QString definestr;
QString define;
foreach(define, defines)
{
foreach(define, defines) {
definestr += define;
definestr += ";";
}
@ -196,16 +190,14 @@ void ProjectFileDialog::SetDefines(const QStringList &defines)
void ProjectFileDialog::SetPaths(const QStringList &paths)
{
foreach(QString path, paths)
{
foreach(QString path, paths) {
AddPath(path);
}
}
void ProjectFileDialog::SetExcludedPaths(const QStringList &paths)
{
foreach(QString path, paths)
{
foreach(QString path, paths) {
AddExcludePath(path);
}
}
@ -218,8 +210,7 @@ void ProjectFileDialog::AddIncludeDir()
tr("Select include directory"),
rootpath);
if (!selectedDir.isEmpty())
{
if (!selectedDir.isEmpty()) {
AddIncludeDir(selectedDir);
}
}
@ -232,8 +223,7 @@ void ProjectFileDialog::AddPath()
tr("Select a directory to check"),
rootpath);
if (!selectedDir.isEmpty())
{
if (!selectedDir.isEmpty()) {
AddPath(selectedDir);
}
}
@ -273,8 +263,7 @@ void ProjectFileDialog::AddExcludePath()
tr("Select directory to ignore"),
rootpath);
if (!selectedDir.isEmpty())
{
if (!selectedDir.isEmpty()) {
if (!selectedDir.endsWith('/'))
selectedDir += '/';
AddExcludePath(selectedDir);

View File

@ -36,8 +36,7 @@ class QLineEdit;
/**
* @brief A dialog for editing project file data.
*/
class ProjectFileDialog : public QDialog
{
class ProjectFileDialog : public QDialog {
Q_OBJECT
public:
ProjectFileDialog(const QString &path, QWidget *parent = 0);

View File

@ -36,8 +36,7 @@ Report::~Report()
bool Report::Create()
{
bool succeed = false;
if (!mFile.isOpen())
{
if (!mFile.isOpen()) {
mFile.setFileName(mFilename);
succeed = mFile.open(QIODevice::WriteOnly | QIODevice::Text);
}
@ -47,8 +46,7 @@ bool Report::Create()
bool Report::Open()
{
bool succeed = false;
if (!mFile.isOpen())
{
if (!mFile.isOpen()) {
mFile.setFileName(mFilename);
succeed = mFile.open(QIODevice::ReadOnly | QIODevice::Text);
}

View File

@ -31,12 +31,10 @@
/**
* @brief A base class for reports.
*/
class Report : public QObject
{
class Report : public QObject {
public:
enum Type
{
enum Type {
TXT,
XML,
XMLV2,

View File

@ -96,15 +96,13 @@ QStandardItem *ResultsTree::CreateLineNumberItem(const QString &linenumber)
void ResultsTree::AddErrorItem(const ErrorItem &item)
{
if (item.files.isEmpty())
{
if (item.files.isEmpty()) {
return;
}
QString realfile = StripPath(item.files[0], false);
if (realfile.isEmpty())
{
if (realfile.isEmpty()) {
realfile = tr("Undefined file");
}
@ -112,20 +110,17 @@ void ResultsTree::AddErrorItem(const ErrorItem &item)
//bool hide = !mShowTypes[SeverityToShowType(item.severity)];
//If specified, filter on summary, message, filename, and id
if (!hide && !mFilter.isEmpty())
{
if (!hide && !mFilter.isEmpty()) {
if (!item.summary.contains(mFilter, Qt::CaseInsensitive) &&
!item.message.contains(mFilter, Qt::CaseInsensitive) &&
!item.file.contains(mFilter, Qt::CaseInsensitive) &&
!item.errorId.contains(mFilter, Qt::CaseInsensitive))
{
!item.errorId.contains(mFilter, Qt::CaseInsensitive)) {
hide = true;
}
}
//if there is at least one error that is not hidden, we have a visible error
if (!hide)
{
if (!hide) {
mVisibleErrors = true;
}
@ -160,8 +155,7 @@ void ResultsTree::AddErrorItem(const ErrorItem &item)
stditem->setData(QVariant(data));
//Add backtrace files as children
for (int i = 1; i < item.files.size(); i++)
{
for (int i = 1; i < item.files.size(); i++) {
line.file = StripPath(item.files[i], false);
line.line = item.lines[i];
QStandardItem *child_item;
@ -184,8 +178,7 @@ void ResultsTree::AddErrorItem(const ErrorItem &item)
//TODO just hide/show current error and it's file
//since this does a lot of unnecessary work
if (!hide)
{
if (!hide) {
ShowFileItem(realfile);
}
}
@ -196,8 +189,7 @@ QStandardItem *ResultsTree::AddBacktraceFiles(QStandardItem *parent,
const QString &icon)
{
if (!parent)
{
if (!parent) {
return 0;
}
@ -211,8 +203,7 @@ QStandardItem *ResultsTree::AddBacktraceFiles(QStandardItem *parent,
//TODO message has parameter names so we'll need changes to the core
//cppcheck so we can get proper translations
QString summary;
if (item.inconclusive)
{
if (item.inconclusive) {
summary = tr("[Inconclusive]");
summary += " ";
}
@ -220,22 +211,18 @@ QStandardItem *ResultsTree::AddBacktraceFiles(QStandardItem *parent,
list << CreateNormalItem(summary);
// Check for duplicate rows and don't add them if found
for (int i = 0; i < parent->rowCount(); i++)
{
for (int i = 0; i < parent->rowCount(); i++) {
// The first column is the file name and is always the same so
// we skip it in other platforms than Windows, where filename case
// is ignored. So in Windows we can get filenames "header.h" and
// "Header.h" and must compare them as identical filenames.
// the third column is the line number so check it first
if (parent->child(i, 2)->text() == list[2]->text())
{
if (parent->child(i, 2)->text() == list[2]->text()) {
// the second column is the severity so check it next
if (parent->child(i, 1)->text() == list[1]->text())
{
if (parent->child(i, 1)->text() == list[1]->text()) {
// the fourth column is the summary so check it last
if (parent->child(i, 3)->text() == list[3]->text())
{
if (parent->child(i, 3)->text() == list[3]->text()) {
#if defined(_WIN32)
const QString first = parent->child(i, 0)->text().toLower();
const QString second = list[0]->text().toLower();
@ -254,8 +241,7 @@ QStandardItem *ResultsTree::AddBacktraceFiles(QStandardItem *parent,
setRowHidden(parent->rowCount() - 1, parent->index(), hide);
if (!icon.isEmpty())
{
if (!icon.isEmpty()) {
list[0]->setIcon(QIcon(icon));
}
@ -266,8 +252,7 @@ QStandardItem *ResultsTree::AddBacktraceFiles(QStandardItem *parent,
QString ResultsTree::SeverityToTranslatedString(Severity::SeverityType severity)
{
switch (severity)
{
switch (severity) {
case Severity::style:
return tr("style");
break;
@ -307,8 +292,7 @@ QString ResultsTree::SeverityToTranslatedString(Severity::SeverityType severity)
QStandardItem *ResultsTree::FindFileItem(const QString &name)
{
QList<QStandardItem *> list = mModel.findItems(name);
if (list.size() > 0)
{
if (list.size() > 0) {
return list[0];
}
return 0;
@ -321,8 +305,7 @@ void ResultsTree::Clear()
void ResultsTree::LoadSettings()
{
for (int i = 0; i < mModel.columnCount(); i++)
{
for (int i = 0; i < mModel.columnCount(); i++) {
//mFileTree.columnWidth(i);
QString temp = QString(SETTINGS_RESULT_COLUMN_WIDTH).arg(i);
setColumnWidth(i, mSettings->value(temp, 800 / mModel.columnCount()).toInt());
@ -335,8 +318,7 @@ void ResultsTree::LoadSettings()
void ResultsTree::SaveSettings()
{
for (int i = 0; i < mModel.columnCount(); i++)
{
for (int i = 0; i < mModel.columnCount(); i++) {
QString temp = QString(SETTINGS_RESULT_COLUMN_WIDTH).arg(i);
mSettings->setValue(temp, columnWidth(i));
}
@ -344,8 +326,7 @@ void ResultsTree::SaveSettings()
void ResultsTree::ShowResults(ShowTypes::ShowType type, bool show)
{
if (type != ShowTypes::ShowNone && mShowSeverities.isShown(type) != show)
{
if (type != ShowTypes::ShowNone && mShowSeverities.isShown(type) != show) {
mShowSeverities.show(type, show);
RefreshTree();
}
@ -361,8 +342,7 @@ void ResultsTree::ShowHiddenResults()
{
//Clear the "hide" flag for each item
int filecount = mModel.rowCount();
for (int i = 0; i < filecount; i++)
{
for (int i = 0; i < filecount; i++) {
QStandardItem *file = mModel.item(i, 0);
if (!file)
continue;
@ -372,11 +352,9 @@ void ResultsTree::ShowHiddenResults()
file->setData(QVariant(data));
int errorcount = file->rowCount();
for (int j = 0; j < errorcount; j++)
{
for (int j = 0; j < errorcount; j++) {
QStandardItem *child = file->child(j, 0);
if (child)
{
if (child) {
data = child->data().toMap();
data["hide"] = false;
child->setData(QVariant(data));
@ -394,12 +372,10 @@ void ResultsTree::RefreshTree()
//Get the amount of files in the tree
int filecount = mModel.rowCount();
for (int i = 0; i < filecount; i++)
{
for (int i = 0; i < filecount; i++) {
//Get file i
QStandardItem *file = mModel.item(i, 0);
if (!file)
{
if (!file) {
continue;
}
@ -409,12 +385,10 @@ void ResultsTree::RefreshTree()
//By default it shouldn't be visible
bool show = false;
for (int j = 0; j < errorcount; j++)
{
for (int j = 0; j < errorcount; j++) {
//Get the error itself
QStandardItem *child = file->child(j, 0);
if (!child)
{
if (!child) {
continue;
}
@ -427,19 +401,16 @@ void ResultsTree::RefreshTree()
bool hide = (data["hide"].toBool() || !mShowSeverities.isShown(ShowTypes::VariantToShowType(data["severity"])));
//If specified, filter on summary, message, filename, and id
if (!hide && !mFilter.isEmpty())
{
if (!hide && !mFilter.isEmpty()) {
if (!data["summary"].toString().contains(mFilter, Qt::CaseInsensitive) &&
!data["message"].toString().contains(mFilter, Qt::CaseInsensitive) &&
!data["file"].toString().contains(mFilter, Qt::CaseInsensitive) &&
!data["id"].toString().contains(mFilter, Qt::CaseInsensitive))
{
!data["id"].toString().contains(mFilter, Qt::CaseInsensitive)) {
hide = true;
}
}
if (!hide)
{
if (!hide) {
mVisibleErrors = true;
}
@ -447,15 +418,13 @@ void ResultsTree::RefreshTree()
setRowHidden(j, file->index(), hide);
//If it was shown then the file itself has to be shown as well
if (!hide)
{
if (!hide) {
show = true;
}
}
//Hide the file if its "hide" attribute is set
if (file->data().toMap()["hide"].toBool())
{
if (file->data().toMap()["hide"].toBool()) {
show = false;
}
@ -471,8 +440,7 @@ QStandardItem *ResultsTree::EnsureFileItem(const QString &fullpath, bool hide)
// native separators to find it.
QStandardItem *item = FindFileItem(QDir::toNativeSeparators(name));
if (item)
{
if (item) {
return item;
}
@ -495,8 +463,7 @@ QStandardItem *ResultsTree::EnsureFileItem(const QString &fullpath, bool hide)
void ResultsTree::ShowFileItem(const QString &name)
{
QStandardItem *item = FindFileItem(name);
if (item)
{
if (item) {
setRowHidden(0, mModel.indexFromItem(item), false);
}
}
@ -504,8 +471,7 @@ void ResultsTree::ShowFileItem(const QString &name)
void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
{
QModelIndex index = indexAt(e->pos());
if (index.isValid())
{
if (index.isValid()) {
bool multipleSelection = false;
mSelectionModel = selectionModel();
if (mSelectionModel->selectedRows().count() > 1)
@ -523,11 +489,9 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
//member variables
QSignalMapper *signalMapper = new QSignalMapper(this);
if (mContextItem && mApplications->GetApplicationCount() > 0 && mContextItem->parent())
{
if (mContextItem && mApplications->GetApplicationCount() > 0 && mContextItem->parent()) {
//Go through all applications and add them to the context menu
for (int i = 0; i < mApplications->GetApplicationCount(); i++)
{
for (int i = 0; i < mApplications->GetApplicationCount(); i++) {
//Create an action for the application
const Application app = mApplications->GetApplication(i);
QAction *start = new QAction(app.getName(), &menu);
@ -552,10 +516,8 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
}
// Add menuitems to copy full path/filename to clipboard
if (mContextItem)
{
if (mApplications->GetApplicationCount() > 0)
{
if (mContextItem) {
if (mApplications->GetApplicationCount() > 0) {
menu.addSeparator();
}
@ -565,8 +527,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
QAction *copymessage = new QAction(tr("Copy message"), &menu);
QAction *hide = new QAction(tr("Hide"), &menu);
if (multipleSelection)
{
if (multipleSelection) {
copyfilename->setDisabled(true);
copypath->setDisabled(true);
copymessage->setDisabled(true);
@ -586,11 +547,9 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
//Start the menu
menu.exec(e->globalPos());
if (mContextItem && mApplications->GetApplicationCount() > 0 && mContextItem->parent())
{
if (mContextItem && mApplications->GetApplicationCount() > 0 && mContextItem->parent()) {
//Disconnect all signals
for (int i = 0; i < actions.size(); i++)
{
for (int i = 0; i < actions.size(); i++) {
disconnect(actions[i], SIGNAL(triggered()), signalMapper, SLOT(map()));
}
@ -607,8 +566,7 @@ void ResultsTree::contextMenuEvent(QContextMenuEvent * e)
void ResultsTree::StartApplication(QStandardItem *target, int application)
{
//If there are no applications specified, tell the user about it
if (mApplications->GetApplicationCount() == 0)
{
if (mApplications->GetApplicationCount() == 0) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("No editor application configured.\n\n"
@ -622,8 +580,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
if (application == -1)
application = mApplications->GetDefaultApplication();
if (application == -1)
{
if (application == -1) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("No default editor application selected.\n\n"
@ -635,8 +592,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
}
if (target && application >= 0 && application < mApplications->GetApplicationCount() && target->parent())
{
if (target && application >= 0 && application < mApplications->GetApplicationCount() && target->parent()) {
// Make sure we are working with the first column
if (target->column() != 0)
target = target->parent()->child(target->row(), 0);
@ -649,25 +605,18 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
qDebug() << "Opening file: " << file;
QFileInfo info(file);
if (!info.exists())
{
if (info.isAbsolute())
{
if (!info.exists()) {
if (info.isAbsolute()) {
QMessageBox msgbox(this);
msgbox.setWindowTitle("Cppcheck");
msgbox.setText(tr("Could not find the file!"));
msgbox.setIcon(QMessageBox::Critical);
msgbox.exec();
}
else
{
} else {
QDir checkdir(mCheckPath);
if (checkdir.isAbsolute() && checkdir.exists())
{
if (checkdir.isAbsolute() && checkdir.exists()) {
file = mCheckPath + "/" + file;
}
else
{
} else {
QString dir = AskFileDir(file);
dir += '/';
file = dir + file;
@ -675,8 +624,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
}
}
if (file.indexOf(" ") > -1)
{
if (file.indexOf(" ") > -1) {
file.insert(0, "\"");
file.append("\"");
}
@ -695,10 +643,8 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
// In Windows we must surround paths including spaces with quotation marks.
#ifdef Q_WS_WIN
if (program.indexOf(" ") > -1)
{
if (!program.startsWith('"') && !program.endsWith('"'))
{
if (program.indexOf(" ") > -1) {
if (!program.startsWith('"') && !program.endsWith('"')) {
program.insert(0, "\"");
program.append("\"");
}
@ -708,8 +654,7 @@ void ResultsTree::StartApplication(QStandardItem *target, int application)
const QString cmdLine = QString("%1 %2").arg(program).arg(params);
bool success = QProcess::startDetached(cmdLine);
if (!success)
{
if (!success) {
QString text = tr("Could not start %1\n\nPlease check the application path and parameters are correct.").arg(program);
QMessageBox msgbox(this);
@ -750,8 +695,7 @@ void ResultsTree::CopyFullPath()
void ResultsTree::CopyMessage()
{
if (mContextItem)
{
if (mContextItem) {
// Make sure we are working with the first column
if (mContextItem->column() != 0)
mContextItem = mContextItem->parent()->child(mContextItem->row(), 0);
@ -759,8 +703,7 @@ void ResultsTree::CopyMessage()
QVariantMap data = mContextItem->data().toMap();
QString message;
if (data["inconclusive"].toBool())
{
if (data["inconclusive"].toBool()) {
message = tr("[Inconclusive]");
message += " ";
}
@ -778,8 +721,7 @@ void ResultsTree::HideResult()
QModelIndexList selectedRows = mSelectionModel->selectedRows();
QModelIndex index;
foreach(index, selectedRows)
{
foreach(index, selectedRows) {
QStandardItem *item = mModel.itemFromIndex(index);
//Set the "hide" flag for this item
QVariantMap data = item->data().toMap();
@ -803,8 +745,7 @@ void ResultsTree::QuickStartApplication(const QModelIndex &index)
void ResultsTree::CopyPath(QStandardItem *target, bool fullPath)
{
if (target)
{
if (target) {
// Make sure we are working with the first column
if (target->column() != 0)
target = target->parent()->child(target->row(), 0);
@ -815,8 +756,7 @@ void ResultsTree::CopyPath(QStandardItem *target, bool fullPath)
//Replace (file) with filename
QString file = data["file"].toString();
pathStr = QDir::toNativeSeparators(file);
if (!fullPath)
{
if (!fullPath) {
QFileInfo fi(pathStr);
pathStr = fi.fileName();
}
@ -828,8 +768,7 @@ void ResultsTree::CopyPath(QStandardItem *target, bool fullPath)
QString ResultsTree::SeverityToIcon(Severity::SeverityType severity) const
{
switch (severity)
{
switch (severity) {
case Severity::error:
return ":images/dialog-error.png";
case Severity::style:
@ -852,8 +791,7 @@ void ResultsTree::SaveResults(Report *report)
{
report->WriteHeader();
for (int i = 0; i < mModel.rowCount(); i++)
{
for (int i = 0; i < mModel.rowCount(); i++) {
QStandardItem *item = mModel.item(i, 0);
if (!isRowHidden(i, item->index()))
SaveErrors(report, item);
@ -864,22 +802,18 @@ void ResultsTree::SaveResults(Report *report)
void ResultsTree::SaveErrors(Report *report, QStandardItem *item)
{
if (!item)
{
if (!item) {
return;
}
for (int i = 0; i < item->rowCount(); i++)
{
for (int i = 0; i < item->rowCount(); i++) {
QStandardItem *error = item->child(i, 0);
if (!error)
{
if (!error) {
continue;
}
if (isRowHidden(i, item->index()) && !mSaveAllErrors)
{
if (isRowHidden(i, item->index()) && !mSaveAllErrors) {
continue;
}
@ -900,8 +834,7 @@ void ResultsTree::SaveErrors(Report *report, QStandardItem *item)
item.files << file;
item.lines << line;
for (int j = 0; j < error->rowCount(); j++)
{
for (int j = 0; j < error->rowCount(); j++) {
QStandardItem *child_error = error->child(j, 0);
//Get error's user data
QVariant child_userdata = child_error->data();
@ -923,8 +856,7 @@ void ResultsTree::UpdateSettings(bool showFullPath,
bool saveFullPath,
bool saveAllErrors)
{
if (mShowFullPath != showFullPath)
{
if (mShowFullPath != showFullPath) {
mShowFullPath = showFullPath;
RefreshFilePaths();
}
@ -940,8 +872,7 @@ void ResultsTree::SetCheckDirectory(const QString &dir)
QString ResultsTree::StripPath(const QString &path, bool saving)
{
if ((!saving && mShowFullPath) || (saving && mSaveFullPath))
{
if ((!saving && mShowFullPath) || (saving && mSaveFullPath)) {
return QString(path);
}
@ -951,8 +882,7 @@ QString ResultsTree::StripPath(const QString &path, bool saving)
void ResultsTree::RefreshFilePaths(QStandardItem *item)
{
if (!item)
{
if (!item) {
return;
}
@ -960,13 +890,11 @@ void ResultsTree::RefreshFilePaths(QStandardItem *item)
bool updated = false;
//Loop through all errors within this file
for (int i = 0; i < item->rowCount(); i++)
{
for (int i = 0; i < item->rowCount(); i++) {
//Get error i
QStandardItem *error = item->child(i, 0);
if (!error)
{
if (!error) {
continue;
}
@ -982,15 +910,12 @@ void ResultsTree::RefreshFilePaths(QStandardItem *item)
error->setText(StripPath(file, false));
//If this error has backtraces make sure the files list has enough filenames
if (error->hasChildren())
{
if (error->hasChildren()) {
//Loop through all files within the error
for (int j = 0; j < error->rowCount(); j++)
{
for (int j = 0; j < error->rowCount(); j++) {
//Get file
QStandardItem *child = error->child(j, 0);
if (!child)
{
if (!child) {
continue;
}
//Get childs's user data
@ -1006,8 +931,7 @@ void ResultsTree::RefreshFilePaths(QStandardItem *item)
}
//if the main file hasn't been updated yet, update it now
if (!updated)
{
if (!updated) {
updated = true;
item->setText(error->text());
}
@ -1020,8 +944,7 @@ void ResultsTree::RefreshFilePaths()
qDebug("Refreshing file paths");
//Go through all file items (these are parent items that contain the errors)
for (int i = 0; i < mModel.rowCount(); i++)
{
for (int i = 0; i < mModel.rowCount(); i++) {
RefreshFilePaths(mModel.item(i, 0));
}
}

View File

@ -46,8 +46,7 @@ class QItemSelectionModel;
* @brief Cppcheck's results are shown in this tree
*
*/
class ResultsTree : public QTreeView
{
class ResultsTree : public QTreeView {
Q_OBJECT
public:
ResultsTree(QWidget * parent = 0);

View File

@ -121,8 +121,7 @@ void ResultsView::FilterResults(const QString& filter)
void ResultsView::Save(const QString &filename, Report::Type type)
{
if (!mErrorsFound)
{
if (!mErrorsFound) {
QMessageBox msgBox;
msgBox.setText(tr("No errors found, nothing to save."));
msgBox.setIcon(QMessageBox::Critical);
@ -131,8 +130,7 @@ void ResultsView::Save(const QString &filename, Report::Type type)
Report *report = NULL;
switch (type)
{
switch (type) {
case Report::CSV:
report = new CsvReport(filename, this);
break;
@ -147,12 +145,10 @@ void ResultsView::Save(const QString &filename, Report::Type type)
break;
}
if (report)
{
if (report) {
if (report->Create())
mUI.mTree->SaveResults(report);
else
{
else {
QMessageBox msgBox;
msgBox.setText(tr("Failed to save the report."));
msgBox.setIcon(QMessageBox::Critical);
@ -160,9 +156,7 @@ void ResultsView::Save(const QString &filename, Report::Type type)
}
delete report;
report = NULL;
}
else
{
} else {
QMessageBox msgBox;
msgBox.setText(tr("Failed to save the report."));
msgBox.setIcon(QMessageBox::Critical);
@ -198,11 +192,9 @@ void ResultsView::CheckingFinished()
mUI.mProgress->setFormat("%p%");
//Should we inform user of non visible/not found errors?
if (mShowNoErrorsMessage)
{
if (mShowNoErrorsMessage) {
//Tell user that we found no errors
if (!mErrorsFound)
{
if (!mErrorsFound) {
QMessageBox msg(QMessageBox::Information,
tr("Cppcheck"),
tr("No errors found."),
@ -211,8 +203,7 @@ void ResultsView::CheckingFinished()
msg.exec();
} //If we have errors but they aren't visible, tell user about it
else if (!mUI.mTree->HasVisibleResults())
{
else if (!mUI.mTree->HasVisibleResults()) {
QString text = tr("Errors were found, but they are configured to be hidden.\n"\
"To toggle what kind of errors are shown, open view menu.");
QMessageBox msg(QMessageBox::Information,
@ -257,8 +248,7 @@ void ResultsView::DisableProgressbar()
void ResultsView::ReadErrorsXml(const QString &filename)
{
const int version = XmlReport::determineVersion(filename);
if (version == 0)
{
if (version == 0) {
QMessageBox msgBox;
msgBox.setText(tr("Failed to read the report."));
msgBox.setIcon(QMessageBox::Critical);
@ -273,12 +263,10 @@ void ResultsView::ReadErrorsXml(const QString &filename)
report = new XmlReportV2(filename, this);
QList<ErrorItem> errors;
if (report)
{
if (report) {
if (report->Open())
errors = report->Read();
else
{
else {
QMessageBox msgBox;
msgBox.setText(tr("Failed to read the report."));
msgBox.setIcon(QMessageBox::Critical);
@ -286,9 +274,7 @@ void ResultsView::ReadErrorsXml(const QString &filename)
}
delete report;
report = NULL;
}
else
{
} else {
QMessageBox msgBox;
msgBox.setText(tr("Failed to read the report."));
msgBox.setIcon(QMessageBox::Critical);
@ -296,8 +282,7 @@ void ResultsView::ReadErrorsXml(const QString &filename)
}
ErrorItem item;
foreach(item, errors)
{
foreach(item, errors) {
mUI.mTree->AddErrorItem(item);
}
mUI.mTree->SetCheckDirectory("");
@ -308,8 +293,7 @@ void ResultsView::UpdateDetails(const QModelIndex &index)
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(mUI.mTree->model());
QStandardItem *item = model->itemFromIndex(index);
if (!item)
{
if (!item) {
mUI.mDetails->setText("");
return;
}
@ -321,8 +305,7 @@ void ResultsView::UpdateDetails(const QModelIndex &index)
QVariantMap data = item->data().toMap();
// If there is no severity data then it is a parent item without summary and message
if (!data.contains("severity"))
{
if (!data.contains("severity")) {
mUI.mDetails->setText("");
return;
}

View File

@ -41,8 +41,7 @@ class CheckStatistics;
* @brief Widget to show cppcheck progressbar and result
*
*/
class ResultsView : public QWidget
{
class ResultsView : public QWidget {
Q_OBJECT
public:
@ -147,8 +146,7 @@ public:
* @brief Return checking statistics.
* @return Pointer to checking statistics.
*/
CheckStatistics *GetStatistics() const
{
CheckStatistics *GetStatistics() const {
return mStatistics;
}
@ -156,8 +154,7 @@ public:
* @brief Return Showtypes.
* @return Pointer to Showtypes.
*/
ShowTypes * GetShowTypes() const
{
ShowTypes * GetShowTypes() const {
return &mUI.mTree->mShowSeverities;
}

View File

@ -108,8 +108,7 @@ void SettingsDialog::InitIncludepathsList()
QSettings settings;
const QString allPaths = settings.value(SETTINGS_GLOBAL_INCLUDE_PATHS).toString();
const QStringList paths = allPaths.split(";", QString::SkipEmptyParts);
foreach(QString path, paths)
{
foreach(QString path, paths) {
AddIncludePath(path);
}
}
@ -118,8 +117,7 @@ void SettingsDialog::InitTranslationsList()
{
const QString current = mTranslator->GetCurrentLanguage();
QList<TranslationInfo> translations = mTranslator->GetTranslations();
foreach(TranslationInfo translation, translations)
{
foreach(TranslationInfo translation, translations) {
QListWidgetItem *item = new QListWidgetItem;
item->setText(translation.mName);
item->setData(LangCodeRole, QVariant(translation.mCode));
@ -131,8 +129,7 @@ void SettingsDialog::InitTranslationsList()
Qt::CheckState SettingsDialog::BoolToCheckState(bool yes) const
{
if (yes)
{
if (yes) {
return Qt::Checked;
}
return Qt::Unchecked;
@ -140,8 +137,7 @@ Qt::CheckState SettingsDialog::BoolToCheckState(bool yes) const
bool SettingsDialog::CheckStateToBool(Qt::CheckState state) const
{
if (state == Qt::Checked)
{
if (state == Qt::Checked) {
return true;
}
return false;
@ -165,8 +161,7 @@ void SettingsDialog::SaveSettings()
void SettingsDialog::SaveSettingValues()
{
int jobs = mUI.mJobs->text().toInt();
if (jobs <= 0)
{
if (jobs <= 0) {
jobs = 1;
}
@ -187,8 +182,7 @@ void SettingsDialog::SaveSettingValues()
const int count = mUI.mListIncludePaths->count();
QString includePaths;
for (int i = 0; i < count; i++)
{
for (int i = 0; i < count; i++) {
QListWidgetItem *item = mUI.mListIncludePaths->item(i);
includePaths += item->text();
includePaths += ";";
@ -207,8 +201,7 @@ void SettingsDialog::AddApplication()
Application app;
ApplicationDialog dialog(tr("Add a new application"), app, this);
if (dialog.exec() == QDialog::Accepted)
{
if (dialog.exec() == QDialog::Accepted) {
const Application app = dialog.GetApplication();
mTempApplications->AddApplication(app);
mUI.mListWidget->addItem(app.getName());
@ -218,8 +211,7 @@ void SettingsDialog::AddApplication()
void SettingsDialog::RemoveApplication()
{
QList<QListWidgetItem *> selected = mUI.mListWidget->selectedItems();
foreach(QListWidgetItem *item, selected)
{
foreach(QListWidgetItem *item, selected) {
const int removeIndex = mUI.mListWidget->row(item);
const int currentDefault = mTempApplications->GetDefaultApplication();
mTempApplications->RemoveApplication(removeIndex);
@ -238,14 +230,12 @@ void SettingsDialog::EditApplication()
{
QList<QListWidgetItem *> selected = mUI.mListWidget->selectedItems();
QListWidgetItem *item = 0;
foreach(item, selected)
{
foreach(item, selected) {
int row = mUI.mListWidget->row(item);
const Application app = mTempApplications->GetApplication(row);
ApplicationDialog dialog(tr("Modify an application"), app, this);
if (dialog.exec() == QDialog::Accepted)
{
if (dialog.exec() == QDialog::Accepted) {
const Application app2 = dialog.GetApplication();
mTempApplications->SetApplication(row, app2);
item->setText(app2.getName());
@ -256,8 +246,7 @@ void SettingsDialog::EditApplication()
void SettingsDialog::DefaultApplication()
{
QList<QListWidgetItem *> selected = mUI.mListWidget->selectedItems();
if (selected.size() > 0)
{
if (selected.size() > 0) {
int index = mUI.mListWidget->row(selected[0]);
mTempApplications->SetDefault(index);
mUI.mListWidget->clear();
@ -268,12 +257,10 @@ void SettingsDialog::DefaultApplication()
void SettingsDialog::PopulateApplicationList()
{
const int defapp = mTempApplications->GetDefaultApplication();
for (int i = 0; i < mTempApplications->GetApplicationCount(); i++)
{
for (int i = 0; i < mTempApplications->GetApplicationCount(); i++) {
Application app = mTempApplications->GetApplication(i);
QString name = app.getName();
if (i == defapp)
{
if (i == defapp) {
name += " ";
name += tr("[Default]");
}
@ -284,8 +271,7 @@ void SettingsDialog::PopulateApplicationList()
// first item.
if (defapp == -1)
mUI.mListWidget->setCurrentRow(0);
else
{
else {
if (mTempApplications->GetApplicationCount() > defapp)
mUI.mListWidget->setCurrentRow(defapp);
else
@ -325,8 +311,7 @@ void SettingsDialog::AddIncludePath()
tr("Select include directory"),
QString());
if (!selectedDir.isEmpty())
{
if (!selectedDir.isEmpty()) {
AddIncludePath(selectedDir);
}
}

View File

@ -36,8 +36,7 @@ class TranslationHandler;
* @brief Settings dialog
*
*/
class SettingsDialog : public QDialog
{
class SettingsDialog : public QDialog {
Q_OBJECT
public:
SettingsDialog(ApplicationList *list,

View File

@ -33,8 +33,7 @@ ShowTypes::~ShowTypes()
ShowTypes::ShowType ShowTypes::SeverityToShowType(Severity::SeverityType severity)
{
switch (severity)
{
switch (severity) {
case Severity::none:
return ShowTypes::ShowNone;
case Severity::error:
@ -58,8 +57,7 @@ ShowTypes::ShowType ShowTypes::SeverityToShowType(Severity::SeverityType severit
Severity::SeverityType ShowTypes::ShowTypeToSeverity(ShowTypes::ShowType type)
{
switch (type)
{
switch (type) {
case ShowTypes::ShowStyle:
return Severity::style;
break;
@ -95,8 +93,7 @@ Severity::SeverityType ShowTypes::ShowTypeToSeverity(ShowTypes::ShowType type)
ShowTypes::ShowType ShowTypes::VariantToShowType(const QVariant &data)
{
const int value = data.toInt();
if (value < ShowTypes::ShowStyle || value > ShowTypes::ShowErrors)
{
if (value < ShowTypes::ShowStyle || value > ShowTypes::ShowErrors) {
return ShowTypes::ShowNone;
}
return (ShowTypes::ShowType)value;

View File

@ -34,15 +34,13 @@
* Notice that the "visibility" settings are automatically loaded when the
* class is constructed and saved when the class is destroyed.
*/
class ShowTypes
{
class ShowTypes {
public:
/**
* @brief Show types we have (i.e. severities in the GUI).
*/
enum ShowType
{
enum ShowType {
ShowStyle = 0,
ShowWarnings,
ShowPerformance,

View File

@ -38,15 +38,12 @@ StatsDialog::StatsDialog(QWidget *parent)
void StatsDialog::setProject(const Project& project)
{
ProjectFile *projectFile = project.GetProjectFile();
if (projectFile)
{
if (projectFile) {
mUI.mProject->setText(projectFile->GetRootPath());
mUI.mPaths->setText(projectFile->GetCheckPaths().join(";"));
mUI.mIncludePaths->setText(projectFile->GetIncludeDirs().join(";"));
mUI.mDefines->setText(projectFile->GetDefines().join(";"));
}
else
{
} else {
mUI.mProject->setText("");
mUI.mPaths->setText("");
mUI.mIncludePaths->setText("");
@ -96,8 +93,7 @@ void StatsDialog::setScanDuration(double seconds)
void StatsDialog::copyToClipboard()
{
QClipboard *clipboard = QApplication::clipboard();
if (clipboard)
{
if (clipboard) {
const QString projSettings(tr("Project Settings"));
const QString project(tr("Project"));
const QString paths(tr("Paths"));

View File

@ -32,8 +32,7 @@ class CheckStatistics;
* @brief A dialog that shows project and scan statistics.
*
*/
class StatsDialog : public QDialog
{
class StatsDialog : public QDialog {
Q_OBJECT
public:
StatsDialog(QWidget *parent = 0);

View File

@ -39,8 +39,7 @@ void BenchmarkSimple::tokenize()
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(data.constData());
QBENCHMARK
{
QBENCHMARK {
tokenizer.tokenize(istr, "test.cpp");
}
}
@ -57,8 +56,7 @@ void BenchmarkSimple::simplify()
Tokenizer tokenizer(&settings, this);
std::istringstream istr(data.constData());
tokenizer.tokenize(istr, "test.cpp");
QBENCHMARK
{
QBENCHMARK {
tokenizer.simplifyTokenList();
}
}
@ -74,8 +72,7 @@ void BenchmarkSimple::tokenizeAndSimplify()
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(data.constData());
QBENCHMARK
{
QBENCHMARK {
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
}

View File

@ -20,8 +20,7 @@
#include <QObject>
#include "errorlogger.h"
class BenchmarkSimple: public QObject, public ErrorLogger
{
class BenchmarkSimple: public QObject, public ErrorLogger {
Q_OBJECT
private slots:
@ -32,10 +31,8 @@ private slots:
private:
// Empty implementations of ErrorLogger methods.
// We don't care about the output in the benchmark tests.
void reportOut(const std::string & outmsg)
{
void reportOut(const std::string & outmsg) {
}
void reportErr(const ErrorLogger::ErrorMessage &msg)
{
void reportErr(const ErrorLogger::ErrorMessage &msg) {
}
};

File diff suppressed because it is too large Load Diff

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestFileList: public QObject
{
class TestFileList: public QObject {
Q_OBJECT
private slots:

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestProjectFile: public QObject
{
class TestProjectFile: public QObject {
Q_OBJECT
private slots:

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestTranslationHandler: public QObject
{
class TestTranslationHandler: public QObject {
Q_OBJECT
private slots:

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestXmlReport: public QObject
{
class TestXmlReport: public QObject {
Q_OBJECT
private slots:

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestXmlReportV1: public QObject
{
class TestXmlReportV1: public QObject {
Q_OBJECT
private slots:

View File

@ -19,8 +19,7 @@
#include <QtTest>
#include <QObject>
class TestXmlReportV2: public QObject
{
class TestXmlReportV2: public QObject {
Q_OBJECT
private slots:

View File

@ -50,13 +50,11 @@ void ThreadHandler::SetFiles(const QStringList &files)
void ThreadHandler::Check(const Settings &settings, bool recheck)
{
if (recheck && mRunningThreadCount == 0)
{
if (recheck && mRunningThreadCount == 0) {
mResults.SetFiles(mLastFiles);
}
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0)
{
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0) {
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
emit Done();
return;
@ -66,13 +64,11 @@ void ThreadHandler::Check(const Settings &settings, bool recheck)
mRunningThreadCount = mThreads.size();
if (mResults.GetFileCount() < mRunningThreadCount)
{
if (mResults.GetFileCount() < mRunningThreadCount) {
mRunningThreadCount = mResults.GetFileCount();
}
for (int i = 0; i < mRunningThreadCount; i++)
{
for (int i = 0; i < mRunningThreadCount; i++) {
mThreads[i]->Check(settings);
}
@ -88,16 +84,14 @@ void ThreadHandler::SetThreadCount(const int count)
{
if (mRunningThreadCount > 0 ||
count == mThreads.size() ||
count <= 0)
{
count <= 0) {
return;
}
//Remove unused old threads
RemoveThreads();
//Create new threads
for (int i = mThreads.size(); i < count; i++)
{
for (int i = mThreads.size(); i < count; i++) {
mThreads << new CheckThread(mResults);
connect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
@ -110,8 +104,7 @@ void ThreadHandler::SetThreadCount(const int count)
void ThreadHandler::RemoveThreads()
{
for (int i = 0; i < mThreads.size(); i++)
{
for (int i = 0; i < mThreads.size(); i++) {
mThreads[i]->terminate();
disconnect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
@ -127,8 +120,7 @@ void ThreadHandler::RemoveThreads()
void ThreadHandler::ThreadDone()
{
mRunningThreadCount--;
if (mRunningThreadCount == 0)
{
if (mRunningThreadCount == 0) {
emit Done();
mScanDuration = mTime.elapsed();
@ -137,8 +129,7 @@ void ThreadHandler::ThreadDone()
void ThreadHandler::Stop()
{
for (int i = 0; i < mThreads.size(); i++)
{
for (int i = 0; i < mThreads.size(); i++) {
mThreads[i]->stop();
}
}

View File

@ -37,8 +37,7 @@
* @brief This class handles creating threadresult and starting threads
*
*/
class ThreadHandler : public QObject
{
class ThreadHandler : public QObject {
Q_OBJECT
public:
ThreadHandler(QObject *parent = 0);

View File

@ -49,8 +49,7 @@ void ThreadResult::FileChecked(const QString &file)
mProgress += QFile(file).size();
mFilesChecked ++;
if (mMaxProgress > 0)
{
if (mMaxProgress > 0) {
const int value = static_cast<int>(PROGRESS_MAX * mProgress / mMaxProgress);
const QString description = tr("%1 of %2 files checked").arg(mFilesChecked).arg(mTotalFiles);
@ -67,8 +66,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = msg._callStack.begin();
tok != msg._callStack.end();
++tok)
{
++tok) {
files << QString((*tok).getfile(false).c_str());
lines << (*tok).line;
}
@ -92,8 +90,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
QString ThreadResult::GetNextFile()
{
QMutexLocker locker(&mutex);
if (mFiles.size() == 0)
{
if (mFiles.size() == 0) {
return "";
}
@ -111,8 +108,7 @@ void ThreadResult::SetFiles(const QStringList &files)
// Determine the total size of all of the files to check, so that we can
// show an accurate progress estimate
quint64 sizeOfFiles = 0;
foreach(const QString& file, files)
{
foreach(const QString& file, files) {
sizeOfFiles += QFile(file).size();
}
mMaxProgress = sizeOfFiles;

View File

@ -35,8 +35,7 @@ class ErrorItem;
* @brief Threads use this class to obtain new files to process and to publish results
*
*/
class ThreadResult : public QObject, public ErrorLogger
{
class ThreadResult : public QObject, public ErrorLogger {
Q_OBJECT
public:
ThreadResult();

View File

@ -43,12 +43,9 @@ TranslationHandler::TranslationHandler(QObject *parent) :
//Load English as a fallback language
QTranslator *english = new QTranslator();
if (english->load("cppcheck_en"))
{
if (english->load("cppcheck_en")) {
qApp->installTranslator(english);
}
else
{
} else {
qDebug() << "Failed to load English translation!";
delete english;
}
@ -61,8 +58,7 @@ TranslationHandler::~TranslationHandler()
const QStringList TranslationHandler::GetNames() const
{
QStringList names;
foreach(TranslationInfo translation, mTranslations)
{
foreach(TranslationInfo translation, mTranslations) {
names.append(translation.mName);
}
return names;
@ -71,11 +67,9 @@ const QStringList TranslationHandler::GetNames() const
bool TranslationHandler::SetLanguage(const QString &code, QString &error)
{
//If English is the language
if (code == "en")
{
if (code == "en") {
//Just remove all extra translators
if (mTranslator)
{
if (mTranslator) {
qApp->removeTranslator(mTranslator);
}
@ -85,18 +79,15 @@ bool TranslationHandler::SetLanguage(const QString &code, QString &error)
//Make sure the translator is otherwise valid
int index = GetLanguageIndexByCode(code);
if (index == -1)
{
if (index == -1) {
error = QObject::tr("Unknown language specified!");
return false;
}
//Load the new language
if (!mTranslator->load(mTranslations[index].mFilename))
{
if (!mTranslator->load(mTranslations[index].mFilename)) {
//If it failed, lets check if the default file exists
if (!QFile::exists(mTranslations[index].mFilename + ".qm"))
{
if (!QFile::exists(mTranslations[index].mFilename + ".qm")) {
error = QObject::tr("Language file %1 not found!");
error = error.arg(mTranslations[index].mFilename + ".qm");
return false;
@ -136,8 +127,7 @@ QString TranslationHandler::SuggestLanguage() const
int index = GetLanguageIndexByCode(language);
//If nothing found, return English
if (index < 0)
{
if (index < 0) {
return "en";
}
@ -156,10 +146,8 @@ void TranslationHandler::AddTranslation(const char *name, const char *filename)
int TranslationHandler::GetLanguageIndexByCode(const QString &code) const
{
int index = -1;
for (int i = 0; i < mTranslations.size(); i++)
{
if (mTranslations[i].mCode == code)
{
for (int i = 0; i < mTranslations.size(); i++) {
if (mTranslations[i].mCode == code) {
index = i;
break;
}

View File

@ -31,8 +31,7 @@
* @brief Information for one translation.
*
*/
struct TranslationInfo
{
struct TranslationInfo {
/**
* @brief Readable name for the translation (e.g. "English").
*
@ -59,8 +58,7 @@ struct TranslationInfo
* track which translation is the currently active translation.
*
*/
class TranslationHandler : QObject
{
class TranslationHandler : QObject {
Q_OBJECT
public:
TranslationHandler(QObject *parent = 0);
@ -78,8 +76,7 @@ public:
* @return List of available translations.
*
*/
QList<TranslationInfo> GetTranslations() const
{
QList<TranslationInfo> GetTranslations() const {
return mTranslations;
}

View File

@ -34,8 +34,7 @@ TxtReport::~TxtReport()
bool TxtReport::Create()
{
bool success = false;
if (Report::Create())
{
if (Report::Create()) {
mTxtWriter.setDevice(Report::GetFile());
success = true;
}
@ -61,23 +60,19 @@ void TxtReport::WriteError(const ErrorItem &error)
QString line;
for (int i = 0; i < error.lines.size(); i++)
{
for (int i = 0; i < error.lines.size(); i++) {
const QString file = QDir::toNativeSeparators(error.files[i]);
line += QString("[%1:%2]").arg(file).arg(error.lines[i]);
if (i < error.lines.size() - 1 && error.lines.size() > 0)
{
if (i < error.lines.size() - 1 && error.lines.size() > 0) {
line += " -> ";
}
if (i == error.lines.size() - 1)
{
if (i == error.lines.size() - 1) {
line += ": ";
}
}
line += QString("(%1) ").arg(GuiSeverity::toString(error.severity));
if (error.inconclusive)
{
if (error.inconclusive) {
line += tr("inconclusive");
line += " ";
}

View File

@ -34,8 +34,7 @@
* @brief Text file report.
* This report mimics the output of the command line cppcheck.
*/
class TxtReport : public Report
{
class TxtReport : public Report {
Q_OBJECT
public:

View File

@ -62,20 +62,15 @@ int XmlReport::determineVersion(const QString &filename)
return 0;
QXmlStreamReader reader(&file);
while (!reader.atEnd())
{
switch (reader.readNext())
{
while (!reader.atEnd()) {
switch (reader.readNext()) {
case QXmlStreamReader::StartElement:
if (reader.name() == ResultElementName)
{
if (reader.name() == ResultElementName) {
QXmlStreamAttributes attribs = reader.attributes();
if (attribs.hasAttribute(QString(VersionAttribute)))
{
if (attribs.hasAttribute(QString(VersionAttribute))) {
int ver = attribs.value("", VersionAttribute).toString().toInt();
return ver;
}
else
} else
return 1;
}
break;

View File

@ -33,8 +33,7 @@ class QObject;
/**
* @brief Base class for XML report classes.
*/
class XmlReport : public Report
{
class XmlReport : public Report {
public:
XmlReport(const QString &filename, QObject * parent = 0);

View File

@ -53,8 +53,7 @@ XmlReportV1::~XmlReportV1()
bool XmlReportV1::Create()
{
bool success = false;
if (Report::Create())
{
if (Report::Create()) {
mXmlWriter = new QXmlStreamWriter(Report::GetFile());
success = true;
}
@ -64,8 +63,7 @@ bool XmlReportV1::Create()
bool XmlReportV1::Open()
{
bool success = false;
if (Report::Open())
{
if (Report::Open()) {
mXmlReader = new QXmlStreamReader(Report::GetFile());
success = true;
}
@ -116,22 +114,18 @@ QList<ErrorItem> XmlReportV1::Read()
{
QList<ErrorItem> errors;
bool insideResults = false;
if (!mXmlReader)
{
if (!mXmlReader) {
qDebug() << "You must Open() the file before reading it!";
return errors;
}
while (!mXmlReader->atEnd())
{
switch (mXmlReader->readNext())
{
while (!mXmlReader->atEnd()) {
switch (mXmlReader->readNext()) {
case QXmlStreamReader::StartElement:
if (mXmlReader->name() == ResultElementName)
insideResults = true;
// Read error element from inside result element
if (insideResults && mXmlReader->name() == ErrorElementName)
{
if (insideResults && mXmlReader->name() == ErrorElementName) {
ErrorItem item = ReadError(mXmlReader);
errors.append(item);
}
@ -161,8 +155,7 @@ QList<ErrorItem> XmlReportV1::Read()
ErrorItem XmlReportV1::ReadError(QXmlStreamReader *reader)
{
ErrorItem item;
if (reader->name().toString() == ErrorElementName)
{
if (reader->name().toString() == ErrorElementName) {
QXmlStreamAttributes attribs = reader->attributes();
QString file = attribs.value("", FilenameAttribute).toString();
file = XmlReport::unquoteMessage(file);

View File

@ -36,8 +36,7 @@
* This report outputs XML-formatted report, version 1. The XML format must match command
* line version's XML output.
*/
class XmlReportV1 : public XmlReport
{
class XmlReportV1 : public XmlReport {
public:
XmlReportV1(const QString &filename, QObject * parent = 0);
virtual ~XmlReportV1();

View File

@ -59,8 +59,7 @@ XmlReportV2::~XmlReportV2()
bool XmlReportV2::Create()
{
bool success = false;
if (Report::Create())
{
if (Report::Create()) {
mXmlWriter = new QXmlStreamWriter(Report::GetFile());
success = true;
}
@ -70,8 +69,7 @@ bool XmlReportV2::Create()
bool XmlReportV2::Open()
{
bool success = false;
if (Report::Open())
{
if (Report::Open()) {
mXmlReader = new QXmlStreamReader(Report::GetFile());
success = true;
}
@ -122,8 +120,7 @@ void XmlReportV2::WriteError(const ErrorItem &error)
const QString message = XmlReport::quoteMessage(error.message);
mXmlWriter->writeAttribute(VerboseAttribute, message);
for (int i = 0; i < error.files.count(); i++)
{
for (int i = 0; i < error.files.count(); i++) {
mXmlWriter->writeStartElement(LocationElementName);
QString file = QDir::toNativeSeparators(error.files[i]);
@ -142,22 +139,18 @@ QList<ErrorItem> XmlReportV2::Read()
{
QList<ErrorItem> errors;
bool insideResults = false;
if (!mXmlReader)
{
if (!mXmlReader) {
qDebug() << "You must Open() the file before reading it!";
return errors;
}
while (!mXmlReader->atEnd())
{
switch (mXmlReader->readNext())
{
while (!mXmlReader->atEnd()) {
switch (mXmlReader->readNext()) {
case QXmlStreamReader::StartElement:
if (mXmlReader->name() == ResultElementName)
insideResults = true;
// Read error element from inside result element
if (insideResults && mXmlReader->name() == ErrorElementName)
{
if (insideResults && mXmlReader->name() == ErrorElementName) {
ErrorItem item = ReadError(mXmlReader);
errors.append(item);
}
@ -198,8 +191,7 @@ ErrorItem XmlReportV2::ReadError(QXmlStreamReader *reader)
ErrorItem item;
// Read error element from inside errors element
if (mXmlReader->name() == ErrorElementName)
{
if (mXmlReader->name() == ErrorElementName) {
QXmlStreamAttributes attribs = reader->attributes();
item.errorId = attribs.value("", IdAttribute).toString();
item.severity = GuiSeverity::fromString(attribs.value("", SeverityAttribute).toString());
@ -210,15 +202,12 @@ ErrorItem XmlReportV2::ReadError(QXmlStreamReader *reader)
}
bool errorRead = false;
while (!errorRead && !mXmlReader->atEnd())
{
switch (mXmlReader->readNext())
{
while (!errorRead && !mXmlReader->atEnd()) {
switch (mXmlReader->readNext()) {
case QXmlStreamReader::StartElement:
// Read location element from inside error element
if (mXmlReader->name() == LocationElementName)
{
if (mXmlReader->name() == LocationElementName) {
QXmlStreamAttributes attribs = mXmlReader->attributes();
QString file = attribs.value("", FilenameAttribute).toString();
file = XmlReport::unquoteMessage(file);

View File

@ -37,8 +37,7 @@
* This report outputs XML-formatted report. The XML format must match command
* line version's XML output.
*/
class XmlReportV2 : public XmlReport
{
class XmlReportV2 : public XmlReport {
public:
XmlReportV2(const QString &filename, QObject * parent = 0);
virtual ~XmlReportV2();

View File

@ -35,8 +35,7 @@
* @brief Interface class that cppcheck uses to communicate with the checks.
* All checking classes must inherit from this class
*/
class Check
{
class Check {
public:
/** This constructor is used when registering the CheckClass */
Check(const std::string &aname);
@ -46,16 +45,14 @@ public:
: _name(aname), _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger)
{ }
virtual ~Check()
{
virtual ~Check() {
#ifndef DJGPP
instances().remove(this);
#endif
}
/** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
static std::list<Check *> &instances()
{
static std::list<Check *> &instances() {
static std::list<Check *> _instances;
return _instances;
}
@ -65,8 +62,7 @@ public:
* @param tokens The tokens to analyse
* @param result container where results are stored
*/
virtual void analyse(const Token *tokens, std::set<std::string> &result) const
{
virtual void analyse(const Token *tokens, std::set<std::string> &result) const {
// suppress compiler warnings
(void)tokens;
(void)result;
@ -76,8 +72,7 @@ public:
* Save analysis data - the caller ensures thread safety
* @param data The data where the results are saved
*/
virtual void saveAnalysisData(const std::set<std::string> &data) const
{
virtual void saveAnalysisData(const std::set<std::string> &data) const {
// suppress compiler warnings
(void)data;
}
@ -93,8 +88,7 @@ public:
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) = 0;
/** class name, used to generate documentation */
std::string name() const
{
std::string name() const {
return _name;
}
@ -106,8 +100,7 @@ public:
* This is for for printout out the error list with --errorlist
* @param errmsg Error message to write
*/
static void reportError(const ErrorLogger::ErrorMessage &errmsg)
{
static void reportError(const ErrorLogger::ErrorMessage &errmsg) {
std::cout << errmsg.toXML(true, 1) << std::endl;
}
@ -118,8 +111,7 @@ protected:
ErrorLogger * const _errorLogger;
/** report an error */
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg)
{
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) {
std::list<const Token *> callstack;
if (tok)
callstack.push_back(tok);
@ -127,14 +119,12 @@ protected:
}
/** report an error */
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg)
{
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg) {
reportError(callstack, severity, id, msg, false);
}
/** report an inconclusive error */
void reportInconclusiveError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg)
{
void reportInconclusiveError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) {
std::list<const Token *> callstack;
if (tok)
callstack.push_back(tok);
@ -142,8 +132,7 @@ protected:
}
/** report an inconclusive error */
void reportInconclusiveError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg)
{
void reportInconclusiveError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg) {
reportError(callstack, severity, id, msg, true);
}
@ -153,11 +142,9 @@ private:
void operator=(const Check &);
/** report an error */
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg, bool inconclusive)
{
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, std::string msg, bool inconclusive) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
{
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
@ -179,16 +166,13 @@ private:
};
namespace std
{
/** compare the names of Check classes, used when sorting the Check descendants */
template <> struct less<Check *>
{
bool operator()(const Check *p1, const Check *p2) const
{
return (p1->name() < p2->name());
}
};
namespace std {
/** compare the names of Check classes, used when sorting the Check descendants */
template <> struct less<Check *> {
bool operator()(const Check *p1, const Check *p2) const {
return (p1->name() < p2->name());
}
};
}
inline Check::Check(const std::string &aname)

View File

@ -26,9 +26,8 @@
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace
{
Check64BitPortability instance;
namespace {
Check64BitPortability instance;
}
/** Is given variable a pointer or array? */
@ -49,10 +48,8 @@ void Check64BitPortability::pointerassignment()
if (!_settings->isEnabled("portability"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "[;{}] %var% = %var% [;+]"))
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% = %var% [;+]")) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId()));
@ -61,8 +58,7 @@ void Check64BitPortability::pointerassignment()
if (isaddr(var1) && isint(var2) && tok->strAt(4) != "+")
assignmentIntegerToAddressError(tok->next());
else if (isint(var1) && isaddr(var2) && !tok->tokAt(3)->isPointerCompare())
{
else if (isint(var1) && isaddr(var2) && !tok->tokAt(3)->isPointerCompare()) {
// assigning address => warning
// some trivial addition => warning
if (Token::Match(tok->tokAt(4), "+ %any% !!;"))

View File

@ -32,30 +32,25 @@
* @brief Check for 64-bit portability issues
*/
class Check64BitPortability : public Check
{
class Check64BitPortability : public Check {
public:
/** This constructor is used when registering the Check64BitPortability */
Check64BitPortability() : Check(myName())
{
Check64BitPortability() : Check(myName()) {
}
/** This constructor is used when running checks. */
Check64BitPortability(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{
: Check(myName(), tokenizer, settings, errorLogger) {
}
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
Check64BitPortability check64BitPortability(tokenizer, settings, errorLogger);
check64BitPortability.pointerassignment();
}
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
(void)tokenizer;
(void)settings;
(void)errorLogger;
@ -69,20 +64,17 @@ private:
void assignmentAddressToIntegerError(const Token *tok);
void assignmentIntegerToAddressError(const Token *tok);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
Check64BitPortability c(0, settings, errorLogger);
c.assignmentAddressToIntegerError(0);
c.assignmentIntegerToAddressError(0);
}
std::string myName() const
{
std::string myName() const {
return "64-bit portability";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Check if there is 64-bit portability issues:\n"
" * assign address to/from int/long";
}

View File

@ -26,9 +26,8 @@
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace
{
CheckAssignIf instance;
namespace {
CheckAssignIf instance;
}
@ -37,13 +36,11 @@ void CheckAssignIf::assignIf()
if (!_settings->isEnabled("style"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "=")
continue;
if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;"))
{
if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;")) {
const unsigned int varid(tok->previous()->varId());
if (varid == 0)
continue;
@ -54,12 +51,10 @@ void CheckAssignIf::assignIf()
if (num < 0)
continue;
for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next())
{
for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
break;
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid))
{
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) {
const std::string op(tok2->strAt(3));
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
@ -89,20 +84,17 @@ void CheckAssignIf::comparison()
if (!_settings->isEnabled("style"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "&")
continue;
if (Token::Match(tok, "& %num% )| ==|!= %num% &&|%oror%|)"))
{
if (Token::Match(tok, "& %num% )| ==|!= %num% &&|%oror%|)")) {
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1));
if (num1 < 0)
continue;
const Token *compareToken = tok->tokAt(2);
if (compareToken->str() == ")")
{
if (compareToken->str() == ")") {
if (!Token::Match(compareToken->link()->previous(), "(|%oror%|&&"))
continue;
compareToken = compareToken->next();
@ -112,8 +104,7 @@ void CheckAssignIf::comparison()
if (num2 < 0)
continue;
if ((num1 & num2) != num2)
{
if ((num1 & num2) != num2) {
const std::string op(compareToken->str());
comparisonError(tok, op=="==" ? false : true);
}
@ -145,10 +136,8 @@ void CheckAssignIf::multiCondition()
if (!_settings->isEnabled("style"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "if ( %var% & %num% ) {"))
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "if ( %var% & %num% ) {")) {
const unsigned int varid(tok->tokAt(2)->varId());
if (varid == 0)
continue;
@ -158,8 +147,7 @@ void CheckAssignIf::multiCondition()
continue;
const Token *tok2 = tok->tokAt(6)->link();
while (Token::simpleMatch(tok2, "} else { if ("))
{
while (Token::simpleMatch(tok2, "} else { if (")) {
// Goto '('
const Token * const opar = tok2->tokAt(4);
@ -169,14 +157,12 @@ void CheckAssignIf::multiCondition()
tok2 = tok2->next()->link();
// check condition..
if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid))
{
if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid)) {
const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3));
if (num2 < 0)
continue;
if ((num1 & num2) == num2)
{
if ((num1 & num2) == num2) {
multiConditionError(opar, tok->linenr());
}
}

View File

@ -32,23 +32,19 @@
* @brief Check for assignment / condition mismatches
*/
class CheckAssignIf : public Check
{
class CheckAssignIf : public Check {
public:
/** This constructor is used when registering the CheckAssignIf */
CheckAssignIf() : Check(myName())
{
CheckAssignIf() : Check(myName()) {
}
/** This constructor is used when running checks. */
CheckAssignIf(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{
: Check(myName(), tokenizer, settings, errorLogger) {
}
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckAssignIf checkAssignIf(tokenizer, settings, errorLogger);
checkAssignIf.assignIf();
checkAssignIf.comparison();
@ -70,21 +66,18 @@ private:
void comparisonError(const Token *tok, bool result);
void multiConditionError(const Token *tok, unsigned int line1);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckAssignIf c(0, settings, errorLogger);
c.assignIfError(0, false);
c.comparisonError(0, false);
c.multiConditionError(0,1);
}
std::string myName() const
{
std::string myName() const {
return "match assignments and conditions";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Match assignments and conditions:\n"
" * Mismatching assignment and comparison => comparison is always true/false\n"
" * Mismatching lhs and rhs in comparison => comparison is always true/false\n"

View File

@ -31,9 +31,8 @@
// Register this check class into cppcheck by creating a static instance of it..
namespace
{
static CheckAutoVariables instance;
namespace {
static CheckAutoVariables instance;
}
@ -56,8 +55,7 @@ bool CheckAutoVariables::isAutoVar(unsigned int varId)
if (!var || !var->isLocal() || var->isStatic() || var->isArray() || var->typeEndToken()->str() == "*")
return false;
if (Token::simpleMatch(var->nameToken()->previous(), "&"))
{
if (Token::simpleMatch(var->nameToken()->previous(), "&")) {
// address of reference variable can be taken if the address
// of the variable it points at is not a auto-var
// TODO: check what the reference variable references.
@ -83,97 +81,74 @@ void CheckAutoVariables::autoVariables()
std::list<Scope>::const_iterator scope;
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope)
{
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
unsigned int indentlevel = 0;
for (const Token *tok = scope->classDef->next()->link(); tok; tok = tok->next())
{
for (const Token *tok = scope->classDef->next()->link(); tok; tok = tok->next()) {
// indentlevel..
if (tok->str() == "{")
++indentlevel;
else if (tok->str() == "}")
{
else if (tok->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
//Critical assignment
if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5)))
{
if (Token::Match(tok, "[;{}] * %var% = & %var%") && errorAv(tok->tokAt(2), tok->tokAt(5))) {
const Variable * var = symbolDatabase->getVariableFromVarId(tok->tokAt(5)->varId());
if (var && (!var->isClass() || var->type()))
errorAutoVariableAssignment(tok->next(), false);
}
else if (Token::Match(tok, "[;{}] %var% . %var% = & %var%"))
{
} else if (Token::Match(tok, "[;{}] %var% . %var% = & %var%")) {
// TODO: check if the parameter is only changed temporarily (#2969)
if (_settings->inconclusive)
{
if (_settings->inconclusive) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *"))
{
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *")) {
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(6)->varId());
if (var2 && var2->isLocal() && !var2->isStatic() && !Token::simpleMatch(var2->typeEndToken(), "*"))
errorAutoVariableAssignment(tok->next(), _settings->inconclusive);
}
}
tok = tok->tokAt(6);
}
else if (Token::Match(tok, "[;{}] %var% . %var% = %var% ;"))
{
} else if (Token::Match(tok, "[;{}] %var% . %var% = %var% ;")) {
// TODO: check if the parameter is only changed temporarily (#2969)
if (_settings->inconclusive)
{
if (_settings->inconclusive) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *"))
{
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *")) {
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(5)->varId());
if (var2 && var2->isLocal() && var2->isArray() && !var2->isStatic())
errorAutoVariableAssignment(tok->next(), _settings->inconclusive);
}
}
tok = tok->tokAt(5);
}
else if (Token::Match(tok, "[;{}] * %var% = %var% ;"))
{
} else if (Token::Match(tok, "[;{}] * %var% = %var% ;")) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(2)->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-3), "%type% * *"))
{
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-3), "%type% * *")) {
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(4)->varId());
if (var2 && var2->isLocal() && var2->isArray() && !var2->isStatic())
errorAutoVariableAssignment(tok->next(), false);
}
tok = tok->tokAt(4);
}
else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(7)))
{
} else if (Token::Match(tok, "[;{}] %var% [ %any% ] = & %var%") && errorAv(tok->tokAt(1), tok->tokAt(7))) {
errorAutoVariableAssignment(tok->next(), false);
}
// Critical return
else if (Token::Match(tok, "return & %var% ;") && isAutoVar(tok->tokAt(2)->varId()))
{
else if (Token::Match(tok, "return & %var% ;") && isAutoVar(tok->tokAt(2)->varId())) {
errorReturnAddressToAutoVariable(tok);
}
else if (Token::Match(tok, "return & %var% [") &&
Token::Match(tok->tokAt(3)->link(), "] ;") &&
isAutoVarArray(tok->tokAt(2)->varId()))
{
} else if (Token::Match(tok, "return & %var% [") &&
Token::Match(tok->tokAt(3)->link(), "] ;") &&
isAutoVarArray(tok->tokAt(2)->varId())) {
errorReturnAddressToAutoVariable(tok);
}
else if (Token::Match(tok, "return & %var% ;") && tok->tokAt(2)->varId())
{
} else if (Token::Match(tok, "return & %var% ;") && tok->tokAt(2)->varId()) {
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(2)->varId());
if (var1 && var1->isArgument() && var1->typeEndToken()->str() != "&")
errorReturnAddressOfFunctionParameter(tok, tok->strAt(2));
}
// Invalid pointer deallocation
else if (Token::Match(tok, "free ( %var% ) ;") && isAutoVarArray(tok->tokAt(2)->varId()))
{
else if (Token::Match(tok, "free ( %var% ) ;") && isAutoVarArray(tok->tokAt(2)->varId())) {
errorInvalidDeallocation(tok);
}
}
@ -188,8 +163,7 @@ void CheckAutoVariables::returnPointerToLocalArray()
std::list<Scope>::const_iterator scope;
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope)
{
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
@ -201,8 +175,7 @@ void CheckAutoVariables::returnPointerToLocalArray()
tok = tok->tokAt(-2);
// have we reached a function that returns a pointer
if (Token::Match(tok->tokAt(-2), "%type% *"))
{
if (Token::Match(tok->tokAt(-2), "%type% *")) {
// go to the '('
const Token *tok2 = scope->classDef->next();
@ -210,26 +183,22 @@ void CheckAutoVariables::returnPointerToLocalArray()
tok2 = tok2->next()->link();
unsigned int indentlevel = 0;
for (; tok2; tok2 = tok2->next())
{
for (; tok2; tok2 = tok2->next()) {
// indentlevel..
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
// Return pointer to local array variable..
if (Token::Match(tok2, "return %var% ;"))
{
if (Token::Match(tok2, "return %var% ;")) {
const unsigned int varid = tok2->next()->varId();
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && var->isLocal() && !var->isStatic() && var->isArray())
{
if (var && var->isLocal() && !var->isStatic() && var->isArray()) {
errorReturnPointerToLocalArray(tok2);
}
}
@ -250,17 +219,14 @@ void CheckAutoVariables::errorReturnPointerToLocalArray(const Token *tok)
void CheckAutoVariables::errorAutoVariableAssignment(const Token *tok, bool inconclusive)
{
if (!inconclusive)
{
if (!inconclusive) {
reportError(tok, Severity::error, "autoVariables",
"Assigning address of local auto-variable to a function parameter.\n"
"Dangerous assignment - function parameter takes the address of a local "
"auto-variable. Local auto-variables are reserved from the stack. And the "
"stack is freed when the function ends. So the pointer to a local variable "
"is invalid after the function ends.");
}
else
{
} else {
reportInconclusiveError(tok, Severity::error, "autoVariables",
"Inconclusive: Assigning address of local auto-variable to a function parameter.\n"
"Inconclusive: function parameter takes the address of a local auto-variable. "
@ -296,8 +262,7 @@ void CheckAutoVariables::returnReference()
std::list<Scope>::const_iterator scope;
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope)
{
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
@ -310,8 +275,7 @@ void CheckAutoVariables::returnReference()
// have we reached a function that returns a reference?
if (Token::Match(tok->tokAt(-2), "%type% &") ||
Token::Match(tok->tokAt(-2), "> &"))
{
Token::Match(tok->tokAt(-2), "> &")) {
// go to the '('
const Token *tok2 = scope->classDef->next();
@ -319,35 +283,30 @@ void CheckAutoVariables::returnReference()
tok2 = tok2->link();
unsigned int indentlevel = 0;
for (; tok2; tok2 = tok2->next())
{
for (; tok2; tok2 = tok2->next()) {
// indentlevel..
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
// return..
if (Token::Match(tok2, "return %var% ;"))
{
if (Token::Match(tok2, "return %var% ;")) {
// is the returned variable a local variable?
const unsigned int varid = tok2->next()->varId();
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && var->isLocal() && !var->isStatic())
{
if (var && var->isLocal() && !var->isStatic()) {
// report error..
errorReturnReference(tok2);
}
}
// return reference to temporary..
else if (returnTemporary(tok2))
{
else if (returnTemporary(tok2)) {
// report error..
errorReturnTempReference(tok2);
}
@ -382,8 +341,7 @@ void CheckAutoVariables::returncstr()
std::list<Scope>::const_iterator scope;
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope)
{
for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
// only check functions
if (scope->type != Scope::eFunction)
continue;
@ -395,8 +353,7 @@ void CheckAutoVariables::returncstr()
tok = tok->tokAt(-2);
// have we reached a function that returns a const char *
if (Token::simpleMatch(tok->tokAt(-3), "const char *"))
{
if (Token::simpleMatch(tok->tokAt(-3), "const char *")) {
// go to the '('
const Token *tok2 = scope->classDef->next();
@ -404,35 +361,30 @@ void CheckAutoVariables::returncstr()
tok2 = tok2->next()->link();
unsigned int indentlevel = 0;
for (; tok2; tok2 = tok2->next())
{
for (; tok2; tok2 = tok2->next()) {
// indentlevel..
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
// return..
if (Token::Match(tok2, "return %var% . c_str ( ) ;"))
{
if (Token::Match(tok2, "return %var% . c_str ( ) ;")) {
// is the returned variable a local variable?
const unsigned int varid = tok2->next()->varId();
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (var && var->isLocal() && !var->isStatic())
{
if (var && var->isLocal() && !var->isStatic()) {
// report error..
errorReturnAutocstr(tok2);
}
}
// return pointer to temporary..
else if (returnTemporary(tok2))
{
else if (returnTemporary(tok2)) {
// report error..
errorReturnTempPointer(tok2);
}

View File

@ -30,8 +30,7 @@
/// @{
class CheckAutoVariables : public Check
{
class CheckAutoVariables : public Check {
public:
/** This constructor is used when registering the CheckClass */
CheckAutoVariables() : Check(myName())
@ -43,14 +42,12 @@ public:
{ }
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
checkAutoVariables.returnReference();
}
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
checkAutoVariables.autoVariables();
checkAutoVariables.returnPointerToLocalArray();
@ -91,8 +88,7 @@ private:
void errorInvalidDeallocation(const Token *tok);
void errorReturnAddressOfFunctionParameter(const Token *tok, const std::string &varname);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckAutoVariables c(0,settings,errorLogger);
c.errorAutoVariableAssignment(0, false);
c.errorReturnAddressToAutoVariable(0);
@ -105,13 +101,11 @@ private:
c.errorReturnAddressOfFunctionParameter(0, "parameter");
}
std::string myName() const
{
std::string myName() const {
return "Auto Variables";
}
std::string classInfo() const
{
std::string classInfo() const {
return "A pointer to a variable is only valid as long as the variable is in scope.\n"
"Check:\n"
"* returning a pointer to auto or temporary variable\n"

File diff suppressed because it is too large Load Diff

View File

@ -46,8 +46,7 @@ class Variable;
* I generally use 'buffer overrun' if you for example call a strcpy or
* other function and pass a buffer and reads or writes too much data.
*/
class CheckBufferOverrun : public Check
{
class CheckBufferOverrun : public Check {
public:
/** This constructor is used when registering the CheckClass */
@ -59,8 +58,7 @@ public:
: Check(myName(), tokenizer, settings, errorLogger)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckBufferOverrun checkBufferOverrun(tokenizer, settings, errorLogger);
checkBufferOverrun.bufferOverrun();
checkBufferOverrun.negativeIndex();
@ -111,8 +109,7 @@ public:
void negativeIndex();
/** Information about N-dimensional array */
class ArrayInfo
{
class ArrayInfo {
private:
/** number of elements of array */
std::vector<MathLib::bigint> _num;
@ -152,44 +149,36 @@ public:
bool declare(const Token *tok, const Tokenizer &tokenizer);
/** array sizes */
const std::vector<MathLib::bigint> &num() const
{
const std::vector<MathLib::bigint> &num() const {
return _num;
}
/** array size */
MathLib::bigint num(size_t index) const
{
MathLib::bigint num(size_t index) const {
return _num[index];
}
void num(size_t index, MathLib::bigint number)
{
void num(size_t index, MathLib::bigint number) {
_num[index] = number;
}
/** size of each element */
MathLib::bigint element_size() const
{
MathLib::bigint element_size() const {
return _element_size;
}
/** Variable name */
unsigned int varid() const
{
unsigned int varid() const {
return _varid;
}
void varid(unsigned int id)
{
void varid(unsigned int id) {
_varid = id;
}
/** Variable name */
const std::string &varname() const
{
const std::string &varname() const {
return _varname;
}
void varname(const std::string &name)
{
void varname(const std::string &name) {
_varname = name;
}
};
@ -235,8 +224,7 @@ public:
void arrayIndexThenCheckError(const Token *tok, const std::string &indexName);
void possibleBufferOverrunError(const Token *tok, const std::string &src, const std::string &dst, bool cat);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckBufferOverrun c(0, settings, errorLogger);
std::vector<MathLib::bigint> indexes;
indexes.push_back(2);
@ -254,13 +242,11 @@ public:
c.possibleBufferOverrunError(0, "source", "destination", false);
}
std::string myName() const
{
std::string myName() const {
return "Bounds checking";
}
std::string classInfo() const
{
std::string classInfo() const {
return "out of bounds checking";
}
};

File diff suppressed because it is too large Load Diff

View File

@ -32,8 +32,7 @@ class Token;
/** @brief %Check classes. Uninitialized member variables, non-conforming operators, missing virtual destructor, etc */
class CheckClass : public Check
{
class CheckClass : public Check {
public:
/** @brief This constructor is used when registering the CheckClass */
CheckClass() : Check(myName()), symbolDatabase(NULL)
@ -43,8 +42,7 @@ public:
CheckClass(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger);
/** @brief Run checks on the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckClass checkClass(tokenizer, settings, errorLogger);
// can't be a simplified check .. the 'sizeof' is used.
@ -52,8 +50,7 @@ public:
}
/** @brief Run checks on the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckClass checkClass(tokenizer, settings, errorLogger);
// Coding style checks
@ -132,8 +129,7 @@ private:
void checkConstError2(const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname);
void initializerListError(const Token *tok1,const Token *tok2, const std::string & classname, const std::string &varname);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckClass c(0, settings, errorLogger);
c.noConstructorError(0, "classname", false);
c.uninitVarError(0, "classname", "varname");
@ -149,13 +145,11 @@ private:
c.initializerListError(0, 0, "class", "variable");
}
std::string myName() const
{
std::string myName() const {
return "Class";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Check the code for each class.\n"
"* Missing constructors\n"
"* Are all variables initialized by the constructors?\n"
@ -183,8 +177,7 @@ private:
// constructors helper function
/** @brief Information about a member variable. Used when checking for uninitialized variables */
struct Usage
{
struct Usage {
Usage() : assign(false), init(false) { }
/** @brief has this variable been assigned? */

View File

@ -24,9 +24,8 @@
//---------------------------------------------------------------------------
// Register CheckExceptionSafety..
namespace
{
CheckExceptionSafety instance;
namespace {
CheckExceptionSafety instance;
}
@ -39,8 +38,7 @@ void CheckExceptionSafety::destructors()
return;
// Perform check..
for (const Token * tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token * tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// Skip executable scopes
if (Token::simpleMatch(tok, ") {"))
tok = tok->next()->link();
@ -51,23 +49,19 @@ void CheckExceptionSafety::destructors()
// Inspect this destructor..
unsigned int indentlevel = 0;
for (const Token *tok2 = tok->tokAt(5); tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
{
for (const Token *tok2 = tok->tokAt(5); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{") {
++indentlevel;
}
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel <= 1)
break;
--indentlevel;
}
// throw found within a destructor
else if (tok2->str() == "throw")
{
else if (tok2->str() == "throw") {
destructorsError(tok2);
break;
}
@ -82,8 +76,7 @@ void CheckExceptionSafety::deallocThrow()
{
// Deallocate a global/member pointer and then throw exception
// the pointer will be a dead pointer
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// only looking for delete now
if (tok->str() != "delete")
continue;
@ -104,16 +97,13 @@ void CheckExceptionSafety::deallocThrow()
{
// TODO: Isn't it better to use symbol database instead?
bool globalVar = false;
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next())
{
if (tok2->varId() == varid)
{
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) {
if (tok2->varId() == varid) {
globalVar = true;
break;
}
if (tok2->str() == "class")
{
if (tok2->str() == "class") {
while (tok2 && tok2->str() != ";" && tok2->str() != "{")
tok2 = tok2->next();
tok2 = tok2 ? tok2->next() : 0;
@ -121,8 +111,7 @@ void CheckExceptionSafety::deallocThrow()
break;
}
if (tok2->str() == "{")
{
if (tok2->str() == "{") {
tok2 = tok2->link();
if (!tok2)
break;
@ -141,12 +130,10 @@ void CheckExceptionSafety::deallocThrow()
const Token *ThrowToken = 0;
// is there a throw after the deallocation?
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel == 0)
break;
--indentlevel;
@ -157,8 +144,7 @@ void CheckExceptionSafety::deallocThrow()
// if the variable is not assigned after the throw then it
// is assumed that it is not the intention that it is a dead pointer.
else if (Token::Match(tok2, "%varid% =", varid))
{
else if (Token::Match(tok2, "%varid% =", varid)) {
if (ThrowToken)
deallocThrowError(ThrowToken, tok->str());
break;
@ -180,15 +166,13 @@ void CheckExceptionSafety::checkRethrowCopy()
const char catchPattern[] = "catch ( const| %type% &|*| %var% ) { %any%";
const Token *tok = Token::findmatch(_tokenizer->tokens(), catchPattern);
while (tok)
{
while (tok) {
const Token *startBlockTok = tok->next()->link()->next();
const Token *endBlockTok = startBlockTok->link();
const unsigned int varid = startBlockTok->tokAt(-2)->varId();
const Token* rethrowTok = Token::findmatch(startBlockTok, "throw %varid%", endBlockTok, varid);
if (rethrowTok)
{
if (rethrowTok) {
rethrowCopyError(rethrowTok, startBlockTok->tokAt(-2)->str());
}

View File

@ -39,8 +39,7 @@ class Token;
* that certain variable values are corrupt.
*/
class CheckExceptionSafety : public Check
{
class CheckExceptionSafety : public Check {
public:
/** This constructor is used when registering the CheckClass */
CheckExceptionSafety() : Check(myName())
@ -52,8 +51,7 @@ public:
{ }
/** Checks that uses the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckExceptionSafety checkExceptionSafety(tokenizer, settings, errorLogger);
checkExceptionSafety.destructors();
checkExceptionSafety.deallocThrow();
@ -71,18 +69,15 @@ public:
private:
/** Don't throw exceptions in destructors */
void destructorsError(const Token * const tok)
{
void destructorsError(const Token * const tok) {
reportError(tok, Severity::error, "exceptThrowInDestructor", "Throwing exception in destructor");
}
void deallocThrowError(const Token * const tok, const std::string &varname)
{
void deallocThrowError(const Token * const tok, const std::string &varname) {
reportError(tok, Severity::error, "exceptDeallocThrow", "Throwing exception in invalid state, " + varname + " points at deallocated memory");
}
void rethrowCopyError(const Token * const tok, const std::string &varname)
{
void rethrowCopyError(const Token * const tok, const std::string &varname) {
reportError(tok, Severity::style, "exceptRethrowCopy",
"Throwing a copy of the caught exception instead of rethrowing the original exception\n"
"Rethrowing an exception with 'throw " + varname + ";' makes an unnecessary copy of '" + varname + "'.\n"
@ -90,8 +85,7 @@ private:
}
/** Generate all possible errors (for --errorlist) */
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckExceptionSafety c(0, settings, errorLogger);
c.destructorsError(0);
c.deallocThrowError(0, "p");
@ -99,14 +93,12 @@ private:
}
/** Short description of class (for --doc) */
std::string myName() const
{
std::string myName() const {
return "Exception Safety";
}
/** wiki formatted description of the class (for --doc) */
std::string classInfo() const
{
std::string classInfo() const {
return "Checking exception safety\n"
"* Throwing exceptions in destructors\n"
"* Throwing exception during invalid state\n"

File diff suppressed because it is too large Load Diff

View File

@ -47,8 +47,7 @@ class Token;
/// @{
/** @brief Base class for memory leaks checking */
class CheckMemoryLeak
{
class CheckMemoryLeak {
private:
/** For access to the tokens */
const Tokenizer * const tokenizer;
@ -85,8 +84,7 @@ private:
public:
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e)
: tokenizer(t), errorLogger(e)
{
: tokenizer(t), errorLogger(e) {
}
@ -180,8 +178,7 @@ public:
* -# finally, check if the simplified token list contain any leaks.
*/
class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak
{
class CheckMemoryLeakInFunction : private Check, public CheckMemoryLeak {
public:
/** @brief This constructor is used when registering this class */
CheckMemoryLeakInFunction() : Check(myName()), CheckMemoryLeak(0, 0), symbolDatabase(NULL)
@ -189,8 +186,7 @@ public:
/** @brief This constructor is used when running checks */
CheckMemoryLeakInFunction(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
{
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog) {
// get the symbol database
if (tokenizr)
symbolDatabase = tokenizr->getSymbolDatabase();
@ -199,12 +195,10 @@ public:
}
/** @brief run all simplified checks */
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
{
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) {
// Don't use these check for Java and C# programs..
if (tokenizr->getFiles()->at(0).find(".java") != std::string::npos ||
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos)
{
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos) {
return;
}
@ -315,8 +309,7 @@ public:
void checkScope(const Token *Tok1, const std::string &varname, unsigned int varid, bool classmember, unsigned int sz);
/** Report all possible errors (for the --errorlist) */
void getErrorMessages(ErrorLogger *e, const Settings *settings)
{
void getErrorMessages(ErrorLogger *e, const Settings *settings) {
CheckMemoryLeakInFunction c(0, settings, e);
c.memleakError(0, "varname");
@ -334,8 +327,7 @@ public:
* Get name of class (--doc)
* @return name of class
*/
std::string myName() const
{
std::string myName() const {
return "Memory leaks (function variables)";
}
@ -343,8 +335,7 @@ public:
* Get class information (--doc)
* @return Wiki formatted information about this class
*/
std::string classInfo() const
{
std::string classInfo() const {
return "Is there any allocated memory when a function goes out of scope";
}
@ -366,8 +357,7 @@ public:
* @brief %Check class variables, variables that are allocated in the constructor should be deallocated in the destructor
*/
class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak
{
class CheckMemoryLeakInClass : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakInClass() : Check(myName()), CheckMemoryLeak(0, 0)
{ }
@ -376,12 +366,10 @@ public:
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
{
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) {
// Don't use these check for Java and C# programs..
if (tokenizr->getFiles()->at(0).find(".java") != std::string::npos ||
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos)
{
tokenizr->getFiles()->at(0).find(".cs") != std::string::npos) {
return;
}
@ -401,13 +389,11 @@ private:
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string myName() const
{
std::string myName() const {
return "Memory leaks (class variables)";
}
std::string classInfo() const
{
std::string classInfo() const {
return "If the constructor allocate memory then the destructor must deallocate it.";
}
};
@ -416,8 +402,7 @@ private:
/** @brief detect simple memory leaks for struct members */
class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak
{
class CheckMemoryLeakStructMember : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakStructMember() : Check(myName()), CheckMemoryLeak(0, 0)
{ }
@ -426,8 +411,7 @@ public:
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
{
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) {
CheckMemoryLeakStructMember checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.check();
}
@ -444,13 +428,11 @@ private:
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string myName() const
{
std::string myName() const {
return "Memory leaks (struct members)";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Don't forget to deallocate struct members";
}
};
@ -459,8 +441,7 @@ private:
/** @brief detect simple memory leaks (address not taken) */
class CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak
{
class CheckMemoryLeakNoVar : private Check, private CheckMemoryLeak {
public:
CheckMemoryLeakNoVar() : Check(myName()), CheckMemoryLeak(0, 0)
{ }
@ -469,8 +450,7 @@ public:
: Check(myName(), tokenizr, settings, errLog), CheckMemoryLeak(tokenizr, errLog)
{ }
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog)
{
void runSimplifiedChecks(const Tokenizer *tokenizr, const Settings *settings, ErrorLogger *errLog) {
CheckMemoryLeakNoVar checkMemoryLeak(tokenizr, settings, errLog);
checkMemoryLeak.check();
}
@ -484,13 +464,11 @@ private:
void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/)
{ }
std::string myName() const
{
std::string myName() const {
return "Memory leaks (address not taken)";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Not taking the address to allocated memory";
}
};

View File

@ -26,9 +26,8 @@
// Register this check class (by creating a static instance of it)
namespace
{
CheckNonReentrantFunctions instance;
namespace {
CheckNonReentrantFunctions instance;
}
void CheckNonReentrantFunctions::nonReentrantFunctions()
@ -40,13 +39,10 @@ void CheckNonReentrantFunctions::nonReentrantFunctions()
if (_tokenizer->isJavaOrCSharp())
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,"))
{
for (; it!=itend; ++it) {
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,")) {
// If checking code that is single threaded, this might be not interesing for all.
// Therefore this is "portabiblity"
reportError(tok->tokAt(1), Severity::portability, "nonreentrantFunctions"+it->first, it->second);

View File

@ -34,24 +34,20 @@
* @brief Using non reentrant functions that can be replaced by their reentrant versions
*/
class CheckNonReentrantFunctions : public Check
{
class CheckNonReentrantFunctions : public Check {
public:
/** This constructor is used when registering the CheckNonReentrantFunctions */
CheckNonReentrantFunctions() : Check(myName())
{
CheckNonReentrantFunctions() : Check(myName()) {
initNonReentrantFunctions();
}
/** This constructor is used when running checks. */
CheckNonReentrantFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{
: Check(myName(), tokenizer, settings, errorLogger) {
initNonReentrantFunctions();
}
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckNonReentrantFunctions checkNonReentrantFunctions(tokenizer, settings, errorLogger);
checkNonReentrantFunctions.nonReentrantFunctions();
}
@ -65,10 +61,8 @@ private:
std::list< std::pair< const std::string, const std::string> > _nonReentrantFunctions;
/** init nonreentrant functions list ' */
void initNonReentrantFunctions()
{
static const char * const non_reentrant_functions_list[] =
{
void initNonReentrantFunctions() {
static const char * const non_reentrant_functions_list[] = {
"ctime", "localtime", "gmtime", "asctime", "strtok", "gethostbyname", "gethostbyaddr", "getservbyname"
, "getservbyport", "crypt", "ttyname", "rand", "gethostbyname2"
, "getprotobyname", "getnetbyname", "getnetbyaddr", "getrpcbyname", "getrpcbynumber", "getrpcent"
@ -78,8 +72,7 @@ private:
};
// generate messages
for (unsigned int i = 0; i < (sizeof(non_reentrant_functions_list) / sizeof(char *)); ++i)
{
for (unsigned int i = 0; i < (sizeof(non_reentrant_functions_list) / sizeof(char *)); ++i) {
std::string strMsg("Found non reentrant function \'");
strMsg+=non_reentrant_functions_list[i];
strMsg+= "\'. For threadsafe applications it is recommended to use the reentrant replacement function \'";
@ -89,28 +82,23 @@ private:
}
}
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckNonReentrantFunctions c(0, settings, errorLogger);
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
for (; it!=itend; ++it) {
c.reportError(0, Severity::portability, "nonreentrantFunctions"+it->first, it->second);
}
}
std::string myName() const
{
std::string myName() const {
return "Non reentrant functions";
}
std::string classInfo() const
{
std::string classInfo() const {
std::string info = "Warn if any of these non reentrant functions are used:\n";
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_nonReentrantFunctions.begin()), itend(_nonReentrantFunctions.end());
for (; it!=itend; ++it)
{
for (; it!=itend; ++it) {
info += "* " + it->first + "\n";
}
return info;

View File

@ -26,9 +26,8 @@
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace
{
CheckNullPointer instance;
namespace {
CheckNullPointer instance;
}
//---------------------------------------------------------------------------
@ -37,8 +36,7 @@ CheckNullPointer instance;
/** Is string uppercase? */
bool CheckNullPointer::isUpper(const std::string &str)
{
for (unsigned int i = 0; i < str.length(); ++i)
{
for (unsigned int i = 0; i < str.length(); ++i) {
if (str[i] >= 'a' && str[i] <= 'z')
return false;
}
@ -57,8 +55,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
// standard functions that dereference first parameter..
// both uninitialized data and null pointers are invalid.
static std::set<std::string> functionNames1;
if (functionNames1.empty())
{
if (functionNames1.empty()) {
functionNames1.insert("memchr");
functionNames1.insert("memcmp");
functionNames1.insert("strcat");
@ -87,8 +84,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
// standard functions that dereference second parameter..
// both uninitialized data and null pointers are invalid.
static std::set<std::string> functionNames2;
if (functionNames2.empty())
{
if (functionNames2.empty()) {
functionNames2.insert("memcmp");
functionNames2.insert("memcpy");
functionNames2.insert("memmove");
@ -107,8 +103,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
// 1st parameter..
if ((Token::Match(&tok, "%var% ( %var% ,|)") && tok.tokAt(2)->varId() > 0) ||
(value == 0 && Token::Match(&tok, "%var% ( 0 ,|)")))
{
(value == 0 && Token::Match(&tok, "%var% ( 0 ,|)"))) {
if (functionNames1.find(tok.str()) != functionNames1.end())
var.push_back(tok.tokAt(2));
else if (value == 0 && Token::Match(&tok, "memcpy|memmove|memset|strcpy|printf|sprintf|vsprintf|vprintf|fprintf|vfprintf"))
@ -121,55 +116,39 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
// 2nd parameter..
if ((Token::Match(&tok, "%var% ( %any% , %var% ,|)") && tok.tokAt(4)->varId() > 0) ||
(value == 0 && Token::Match(&tok, "%var% ( %any% , 0 ,|)")))
{
(value == 0 && Token::Match(&tok, "%var% ( %any% , 0 ,|)"))) {
if (functionNames2.find(tok.str()) != functionNames2.end())
var.push_back(tok.tokAt(4));
}
if (Token::Match(&tok, "printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf"))
{
if (Token::Match(&tok, "printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf")) {
const Token* argListTok = 0; // Points to first va_list argument
std::string formatString;
bool scan = Token::Match(&tok, "scanf|sscanf|fscanf");
if (Token::Match(&tok, "printf|scanf ( %str% , %any%"))
{
if (Token::Match(&tok, "printf|scanf ( %str% , %any%")) {
formatString = tok.strAt(2);
argListTok = tok.tokAt(4);
}
else if (Token::Match(&tok, "sprintf|fprintf|sscanf|fscanf ( %var% , %str% , %any%"))
{
} else if (Token::Match(&tok, "sprintf|fprintf|sscanf|fscanf ( %var% , %str% , %any%")) {
formatString = tok.strAt(4);
argListTok = tok.tokAt(6);
}
else if (Token::Match(&tok, "snprintf|fnprintf ( %var% , %any% , %str% , %any%"))
{
} else if (Token::Match(&tok, "snprintf|fnprintf ( %var% , %any% , %str% , %any%")) {
formatString = tok.strAt(6);
argListTok = tok.tokAt(8);
}
if (argListTok)
{
if (argListTok) {
bool percent = false;
for (std::string::iterator i = formatString.begin(); i != formatString.end(); ++i)
{
if (*i == '%')
{
for (std::string::iterator i = formatString.begin(); i != formatString.end(); ++i) {
if (*i == '%') {
percent = !percent;
}
else if (percent && std::isalpha(*i))
{
if (*i == 'n' || *i == 's' || scan)
{
if ((value == 0 && argListTok->str() == "0") || (Token::Match(argListTok, "%var%") && argListTok->varId() > 0))
{
} else if (percent && std::isalpha(*i)) {
if (*i == 'n' || *i == 's' || scan) {
if ((value == 0 && argListTok->str() == "0") || (Token::Match(argListTok, "%var%") && argListTok->varId() > 0)) {
var.push_back(argListTok);
}
}
for (; argListTok; argListTok = argListTok->next()) // Find next argument
{
if (argListTok->str() == ",")
{
for (; argListTok; argListTok = argListTok->next()) { // Find next argument
if (argListTok->str() == ",") {
argListTok = argListTok->next();
break;
}
@ -244,8 +223,7 @@ bool CheckNullPointer::isPointer(const unsigned int varid)
// it is a pointer
if (!tok->previous() ||
Token::Match(tok->previous(), "[({};]") ||
tok->previous()->isName())
{
tok->previous()->isName()) {
return true;
}
@ -256,8 +234,7 @@ bool CheckNullPointer::isPointer(const unsigned int varid)
void CheckNullPointer::nullPointerAfterLoop()
{
// Locate insufficient null-pointer handling after loop
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// only interested in while ( %var% )
// TODO: Aren't there false negatives. Shouldn't other loops be handled such as:
// - while ( ! %var% )
@ -284,19 +261,16 @@ void CheckNullPointer::nullPointerAfterLoop()
bool inconclusive = false;
// Check if the variable is dereferenced after the while loop
while (0 != (tok2 = tok2 ? tok2->next() : 0))
{
while (0 != (tok2 = tok2 ? tok2->next() : 0)) {
// inner and outer scopes
if (tok2->str() == "{" || tok2->str() == "}")
{
if (tok2->str() == "{" || tok2->str() == "}") {
// Not inconclusive: bail out
if (!_settings->inconclusive)
break;
inconclusive = true;
if (tok2->str() == "}")
{
if (tok2->str() == "}") {
// "}" => leaving function? then break.
const Token *tok3 = tok2->link()->previous();
if (!tok3 || !Token::Match(tok3, "[);]"))
@ -311,14 +285,12 @@ void CheckNullPointer::nullPointerAfterLoop()
break;
// loop variable is found..
else if (tok2->varId() == varid)
{
else if (tok2->varId() == varid) {
// dummy variable.. is it unknown if pointer is dereferenced or not?
bool unknown = false;
// Is the loop variable dereferenced?
if (CheckNullPointer::isPointerDeRef(tok2, unknown))
{
if (CheckNullPointer::isPointerDeRef(tok2, unknown)) {
nullPointerError(tok2, varname, tok->linenr(), inconclusive);
}
break;
@ -335,8 +307,7 @@ void CheckNullPointer::nullPointerLinkedList()
// if (tok->str() == "hello")
// tok = tok->next; // <- tok might become a null pointer!
// }
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next())
{
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) {
// search for a "for" token..
if (!Token::simpleMatch(tok1, "for ("))
continue;
@ -344,21 +315,18 @@ void CheckNullPointer::nullPointerLinkedList()
// is there any dereferencing occurring in the for statement
// parlevel2 counts the parentheses when using tok2.
unsigned int parlevel2 = 1;
for (const Token *tok2 = tok1->tokAt(2); tok2; tok2 = tok2->next())
{
for (const Token *tok2 = tok1->tokAt(2); tok2; tok2 = tok2->next()) {
// Parentheses..
if (tok2->str() == "(")
++parlevel2;
else if (tok2->str() == ")")
{
else if (tok2->str() == ")") {
if (parlevel2 <= 1)
break;
--parlevel2;
}
// Dereferencing a variable inside the "for" parentheses..
else if (Token::Match(tok2, "%var% . %var%"))
{
else if (Token::Match(tok2, "%var% . %var%")) {
// Variable id for dereferenced variable
const unsigned int varid(tok2->varId());
if (varid == 0)
@ -372,33 +340,27 @@ void CheckNullPointer::nullPointerLinkedList()
// Check usage of dereferenced variable in the loop..
unsigned int indentlevel3 = 0;
for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next())
{
for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next()) {
if (tok3->str() == "{")
++indentlevel3;
else if (tok3->str() == "}")
{
else if (tok3->str() == "}") {
if (indentlevel3 <= 1)
break;
--indentlevel3;
}
// TODO: are there false negatives for "while ( %varid% ||"
else if (Token::Match(tok3, "while ( %varid% &&|)", varid))
{
else if (Token::Match(tok3, "while ( %varid% &&|)", varid)) {
// Make sure there is a "break" or "return" inside the loop.
// Without the "break" a null pointer could be dereferenced in the
// for statement.
// indentlevel4 is a counter for { and }. When scanning the code with tok4
unsigned int indentlevel4 = indentlevel3;
for (const Token *tok4 = tok3->next()->link(); tok4; tok4 = tok4->next())
{
for (const Token *tok4 = tok3->next()->link(); tok4; tok4 = tok4->next()) {
if (tok4->str() == "{")
++indentlevel4;
else if (tok4->str() == "}")
{
if (indentlevel4 <= 1)
{
else if (tok4->str() == "}") {
if (indentlevel4 <= 1) {
// Is this variable a pointer?
if (isPointer(varid))
nullPointerError(tok1, varname, tok3->linenr());
@ -430,21 +392,17 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
skipvar.insert(0);
// Scan through all tokens
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next())
{
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) {
// Checking if some pointer is null.
// then add the pointer to skipvar => is it known that it isn't NULL
if (Token::Match(tok1, "if|while ( !| %var% )"))
{
if (Token::Match(tok1, "if|while ( !| %var% )")) {
tok1 = tok1->tokAt(2);
if (tok1->str() == "!")
tok1 = tok1->next();
skipvar.insert(tok1->varId());
continue;
}
else if (Token::Match(tok1, "( ! %var% ||") ||
Token::Match(tok1, "( %var% &&"))
{
} else if (Token::Match(tok1, "( ! %var% ||") ||
Token::Match(tok1, "( %var% &&")) {
// TODO: there are false negatives caused by this. The
// variable should be removed from skipvar after the
// condition
@ -461,22 +419,19 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
*/
// dereference in assignment
if (Token::Match(tok1, "[;{}] %var% . %var%"))
{
if (Token::Match(tok1, "[;{}] %var% . %var%")) {
tok1 = tok1->next();
}
// dereference in assignment
else if (Token::Match(tok1, "[{};] %var% = %var% . %var%"))
{
else if (Token::Match(tok1, "[{};] %var% = %var% . %var%")) {
if (std::string(tok1->strAt(1)) == tok1->strAt(3))
continue;
tok1 = tok1->tokAt(3);
}
// dereference in condition
else if (Token::Match(tok1, "if ( !| %var% ."))
{
else if (Token::Match(tok1, "if ( !| %var% .")) {
tok1 = tok1->tokAt(2);
if (tok1->str() == "!")
tok1 = tok1->next();
@ -484,18 +439,15 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
// dereference in function call (but not sizeof|decltype)
else if ((Token::Match(tok1->tokAt(-2), "%var% ( %var% . %var%") && !Token::Match(tok1->tokAt(-2), "sizeof|decltype ( %var% . %var%")) ||
Token::Match(tok1->previous(), ", %var% . %var%"))
{
Token::Match(tok1->previous(), ", %var% . %var%")) {
// Is the function return value taken by the pointer?
bool assignment = false;
const unsigned int varid1(tok1->varId());
if (varid1 == 0)
continue;
const Token *tok2 = tok1->previous();
while (tok2 && !Token::Match(tok2, "[;{}]"))
{
if (Token::Match(tok2, "%varid% =", varid1))
{
while (tok2 && !Token::Match(tok2, "[;{}]")) {
if (Token::Match(tok2, "%varid% =", varid1)) {
assignment = true;
break;
}
@ -506,8 +458,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
}
// Goto next token
else
{
else {
continue;
}
@ -527,8 +478,7 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
isLocal = true;
// member function may or may not nullify the pointer if it's global (#2647)
if (!isLocal)
{
if (!isLocal) {
const Token *tok2 = tok1;
while (Token::Match(tok2, "%var% ."))
tok2 = tok2->tokAt(2);
@ -538,13 +488,11 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
// count { and } using tok2
unsigned int indentlevel2 = 0;
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next())
{
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel2;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel2 == 0)
break;
--indentlevel2;
@ -555,10 +503,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
break;
// Reassignment of the struct
else if (tok2->varId() == varid1)
{
if (tok2->next()->str() == "=")
{
else if (tok2->varId() == varid1) {
if (tok2->next()->str() == "=") {
// Avoid false positives when there is 'else if'
// TODO: can this be handled better?
if (tok1->strAt(-2) == "if")
@ -581,15 +527,13 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec()
// Function call: If the pointer is not a local variable it
// might be changed by the call.
else if (Token::Match(tok2, "[;{}] %var% (") &&
Token::simpleMatch(tok2->tokAt(2)->link(), ") ;") && !isLocal)
{
Token::simpleMatch(tok2->tokAt(2)->link(), ") ;") && !isLocal) {
break;
}
// Check if pointer is null.
// TODO: false negatives for "if (!p || .."
else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1))
{
else if (Token::Match(tok2, "if ( !| %varid% )|&&", varid1)) {
// Is this variable a pointer?
if (isPointer(varid1))
nullPointerError(tok1, varname, tok2->linenr());
@ -606,13 +550,11 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
// Dereferencing a pointer and then checking if it's NULL..
// This check will first scan for the check. And then scan backwards
// from the check, searching for dereferencing.
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// TODO: false negatives.
// - logical operators
// - while
if (tok->str() == "if" && Token::Match(tok->previous(), "; if ( !| %var% )|%oror%|&&"))
{
if (tok->str() == "if" && Token::Match(tok->previous(), "; if ( !| %var% )|%oror%|&&")) {
const Token * vartok = tok->tokAt(2);
if (vartok->str() == "!")
vartok = vartok->next();
@ -636,10 +578,8 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
const Token * const decltok = var->nameToken();
for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous())
{
if (tok1->str() == ")" && Token::Match(tok1->link()->previous(), "%var% ("))
{
for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous()) {
if (tok1->str() == ")" && Token::Match(tok1->link()->previous(), "%var% (")) {
const Token *tok2 = tok1->link();
while (tok2 && !Token::Match(tok2, "[;{}?:]"))
tok2 = tok2->previous();
@ -652,24 +592,20 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
break;
if (Token::Match(tok1->link(), "( ! %varid% ||", varid) ||
Token::Match(tok1->link(), "( %varid% &&", varid))
{
Token::Match(tok1->link(), "( %varid% &&", varid)) {
tok1 = tok1->link();
continue;
}
if (Token::simpleMatch(tok1->link()->previous(), "sizeof ("))
{
if (Token::simpleMatch(tok1->link()->previous(), "sizeof (")) {
tok1 = tok1->link()->previous();
continue;
}
if (Token::Match(tok2, "[;{}] %var% ( %varid% ,", varid))
{
if (Token::Match(tok2, "[;{}] %var% ( %varid% ,", varid)) {
std::list<const Token *> varlist;
parseFunctionCall(*(tok2->next()), varlist, 0);
if (!varlist.empty() && varlist.front() == tok2->tokAt(3))
{
if (!varlist.empty() && varlist.front() == tok2->tokAt(3)) {
nullPointerError(tok2->tokAt(3), varname, tok->linenr());
break;
}
@ -683,15 +619,12 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (tok1->str() == "break")
break;
if (tok1->varId() == varid)
{
if (tok1->varId() == varid) {
// Don't write warning if the dereferencing is
// guarded by ?:
const Token *tok2 = tok1->previous();
if (tok2 && (tok2->isArithmeticalOp() || tok2->str() == "("))
{
while (tok2 && !Token::Match(tok2, "[;{}?:]"))
{
if (tok2 && (tok2->isArithmeticalOp() || tok2->str() == "(")) {
while (tok2 && !Token::Match(tok2, "[;{}?:]")) {
if (tok2->str() == ")")
tok2 = tok2->link();
tok2 = tok2->previous();
@ -704,29 +637,18 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
// uncertain
bool unknown = false;
if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid))
{
if (Token::Match(tok1->tokAt(-2), "%varid% = %varid% .", varid)) {
break;
}
else if (Token::Match(tok1->previous(), "&&|%oror%"))
{
} else if (Token::Match(tok1->previous(), "&&|%oror%")) {
break;
}
else if (Token::Match(tok1->tokAt(-2), "&&|%oror% !"))
{
} else if (Token::Match(tok1->tokAt(-2), "&&|%oror% !")) {
break;
}
else if (CheckNullPointer::isPointerDeRef(tok1, unknown))
{
} else if (CheckNullPointer::isPointerDeRef(tok1, unknown)) {
nullPointerError(tok1, varname, tok->linenr());
break;
}
else if (Token::simpleMatch(tok1->previous(), "&"))
{
} else if (Token::simpleMatch(tok1->previous(), "&")) {
break;
}
else if (Token::simpleMatch(tok1->next(), "="))
{
} else if (Token::simpleMatch(tok1->next(), "=")) {
break;
}
}
@ -751,13 +673,11 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// TODO: Use isPointer?
std::set<unsigned int> pointerVariables;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "* %var% [;,)=]"))
pointerVariables.insert(tok->next()->varId());
else if (Token::simpleMatch(tok, "if ("))
{
else if (Token::simpleMatch(tok, "if (")) {
// TODO: investigate false negatives:
// - handle "while"?
// - if there are logical operators
@ -808,8 +728,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// indentlevel inside the if-body is 1
unsigned int indentlevel = 1;
if (Token::Match(tok, "if|while ( %var% )|&&"))
{
if (Token::Match(tok, "if|while ( %var% )|&&")) {
// pointer might be null
null = false;
@ -827,18 +746,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
const std::string &pointerName = vartok->str();
// Count { and } for tok2
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
{
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel == 0)
break;
--indentlevel;
if (null && indentlevel == 0)
{
if (null && indentlevel == 0) {
// skip all "else" blocks because they are not executed in this execution path
while (Token::simpleMatch(tok2, "} else {"))
tok2 = tok2->tokAt(2)->link();
@ -846,8 +762,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
}
}
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))
{
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) {
bool dummy = false;
if (Token::Match(tok2, "return * %varid%", varid))
nullPointerError(tok2, pointerName, linenr);
@ -858,21 +773,17 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
}
// parameters to sizeof are not dereferenced
if (Token::Match(tok2, "decltype|sizeof ("))
{
if (Token::Match(tok2, "decltype|sizeof (")) {
tok2 = tok2->next()->link();
continue;
}
// function call, check if pointer is dereferenced
if (Token::Match(tok2, "%var% ("))
{
if (Token::Match(tok2, "%var% (")) {
std::list<const Token *> var;
parseFunctionCall(*tok2, var, 0);
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it)
{
if ((*it)->varId() == varid)
{
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) {
if ((*it)->varId() == varid) {
nullPointerError(*it, pointerName, linenr);
break;
}
@ -882,8 +793,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// calling unknown function (abort/init)..
if (Token::simpleMatch(tok2, ") ;") &&
(Token::Match(tok2->link()->tokAt(-2), "[;{}.] %var% (") ||
Token::Match(tok2->link()->tokAt(-5), "[;{}] ( * %var% ) (")))
{
Token::Match(tok2->link()->tokAt(-5), "[;{}] ( * %var% ) ("))) {
// noreturn function?
if (tok2->strAt(2) == "}")
break;
@ -895,8 +805,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
break;
}
if (tok2->varId() == varid)
{
if (tok2->varId() == varid) {
// unknown: this is set to true by isPointerDeRef if
// the function fails to determine if there
// is a dereference or not
@ -932,19 +841,16 @@ void CheckNullPointer::nullConstantDereference()
// this is kept at 0 for all scopes that are not executing
unsigned int indentlevel = 0;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// start of executable scope..
if (indentlevel == 0 && Token::Match(tok, ") const| {"))
indentlevel = 1;
else if (indentlevel >= 1)
{
else if (indentlevel >= 1) {
if (tok->str() == "{")
++indentlevel;
else if (tok->str() == "}")
{
else if (tok->str() == "}") {
if (indentlevel <= 2)
indentlevel = 0;
else
@ -954,11 +860,9 @@ void CheckNullPointer::nullConstantDereference()
if (tok->str() == "(" && Token::Match(tok->previous(), "sizeof|decltype"))
tok = tok->link();
else if (Token::simpleMatch(tok, "exit ( )"))
{
else if (Token::simpleMatch(tok, "exit ( )")) {
// Goto end of scope
while (tok && tok->str() != "}")
{
while (tok && tok->str() != "}") {
if (tok->str() == "{")
tok = tok->link();
tok = tok->next();
@ -967,24 +871,19 @@ void CheckNullPointer::nullConstantDereference()
break;
}
else if (Token::simpleMatch(tok, "* 0"))
{
if (Token::Match(tok->previous(), "return|;|{|}|=|(|,|%op%"))
{
else if (Token::simpleMatch(tok, "* 0")) {
if (Token::Match(tok->previous(), "return|;|{|}|=|(|,|%op%")) {
nullPointerError(tok);
}
}
else if (indentlevel > 0 && Token::Match(tok->previous(), "[={};] %var% ("))
{
else if (indentlevel > 0 && Token::Match(tok->previous(), "[={};] %var% (")) {
std::list<const Token *> var;
parseFunctionCall(*tok, var, 0);
// is one of the var items a NULL pointer?
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it)
{
if ((*it)->str() == "0")
{
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it) {
if ((*it)->str() == "0") {
nullPointerError(*it);
}
}
@ -1002,12 +901,10 @@ void CheckNullPointer::nullConstantDereference()
* @brief %Check for null pointer usage (using ExecutionPath)
*/
class Nullpointer : public ExecutionPath
{
class Nullpointer : public ExecutionPath {
public:
/** Startup constructor */
Nullpointer(Check *c) : ExecutionPath(c, 0), null(false)
{
Nullpointer(Check *c) : ExecutionPath(c, 0), null(false) {
}
private:
@ -1015,13 +912,11 @@ private:
Nullpointer(Check *c, const unsigned int id, const std::string &name)
: ExecutionPath(c, id),
varname(name),
null(false)
{
null(false) {
}
/** Copy this check */
ExecutionPath *copy()
{
ExecutionPath *copy() {
return new Nullpointer(*this);
}
@ -1029,8 +924,7 @@ private:
void operator=(const Nullpointer &);
/** is other execution path equal? */
bool is_equal(const ExecutionPath *e) const
{
bool is_equal(const ExecutionPath *e) const {
const Nullpointer *c = static_cast<const Nullpointer *>(e);
return (varname == c->varname && null == c->null);
}
@ -1042,11 +936,9 @@ private:
bool null;
/** variable is set to null */
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid)
{
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid) {
std::list<ExecutionPath *>::iterator it;
for (it = checks.begin(); it != checks.end(); ++it)
{
for (it = checks.begin(); it != checks.end(); ++it) {
Nullpointer *c = dynamic_cast<Nullpointer *>(*it);
if (c && c->varId == varid)
c->null = true;
@ -1058,19 +950,15 @@ private:
* @param checks Checks
* @param tok token where dereferencing happens
*/
static void dereference(std::list<ExecutionPath *> &checks, const Token *tok)
{
static void dereference(std::list<ExecutionPath *> &checks, const Token *tok) {
const unsigned int varid(tok->varId());
std::list<ExecutionPath *>::iterator it;
for (it = checks.begin(); it != checks.end(); ++it)
{
for (it = checks.begin(); it != checks.end(); ++it) {
Nullpointer *c = dynamic_cast<Nullpointer *>(*it);
if (c && c->varId == varid && c->null)
{
if (c && c->varId == varid && c->null) {
CheckNullPointer *checkNullPointer = dynamic_cast<CheckNullPointer *>(c->owner);
if (checkNullPointer)
{
if (checkNullPointer) {
checkNullPointer->nullPointerError(tok, c->varname);
return;
}
@ -1079,10 +967,8 @@ private:
}
/** parse tokens */
const Token *parse(const Token &tok, std::list<ExecutionPath *> &checks) const
{
if (Token::Match(tok.previous(), "[;{}] const| struct| %type% * %var% ;"))
{
const Token *parse(const Token &tok, std::list<ExecutionPath *> &checks) const {
if (Token::Match(tok.previous(), "[;{}] const| struct| %type% * %var% ;")) {
const Token * vartok = tok.tokAt(2);
if (tok.str() == "const")
@ -1097,21 +983,18 @@ private:
}
// Template pointer variable..
if (Token::Match(tok.previous(), "[;{}] %type% ::|<"))
{
if (Token::Match(tok.previous(), "[;{}] %type% ::|<")) {
const Token * vartok = &tok;
while (Token::Match(vartok, "%type% ::"))
vartok = vartok->tokAt(2);
if (Token::Match(vartok, "%type% < %type%"))
{
if (Token::Match(vartok, "%type% < %type%")) {
vartok = vartok->tokAt(3);
while (vartok && (vartok->str() == "*" || vartok->isName()))
vartok = vartok->next();
}
if (vartok
&& (vartok->str() == ">" || vartok->isName())
&& Token::Match(vartok->next(), "* %var% ;|="))
{
&& Token::Match(vartok->next(), "* %var% ;|=")) {
vartok = vartok->tokAt(2);
checks.push_back(new Nullpointer(owner, vartok->varId(), vartok->str()));
if (Token::simpleMatch(vartok->next(), "= 0 ;"))
@ -1120,29 +1003,24 @@ private:
}
}
if (Token::simpleMatch(&tok, "try {"))
{
if (Token::simpleMatch(&tok, "try {")) {
// Bail out all used variables
unsigned int indentlevel = 0;
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next())
{
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
else if (tok2->str() == "}") {
if (indentlevel == 0)
break;
if (indentlevel == 1 && !Token::simpleMatch(tok2,"} catch ("))
return tok2;
--indentlevel;
}
else if (tok2->varId())
} else if (tok2->varId())
bailOutVar(checks,tok2->varId());
}
}
if (Token::Match(&tok, "%var% ("))
{
if (Token::Match(&tok, "%var% (")) {
if (tok.str() == "sizeof")
return tok.next()->link();
@ -1156,8 +1034,7 @@ private:
else if (Token::simpleMatch(&tok, "( 0 &&"))
return tok.link();
if (tok.varId() != 0)
{
if (tok.varId() != 0) {
// unknown : not really used. it is passed to isPointerDeRef.
// if isPointerDeRef fails to determine if there
// is a dereference the this will be set to true.
@ -1173,8 +1050,7 @@ private:
bailOutVar(checks, tok.varId());
}
else if (tok.str() == "delete")
{
else if (tok.str() == "delete") {
const Token *ret = tok.next();
if (Token::simpleMatch(ret, "[ ]"))
ret = ret->tokAt(2);
@ -1182,14 +1058,12 @@ private:
return ret->next();
}
else if (tok.str() == "return")
{
else if (tok.str() == "return") {
bool unknown = false;
const Token *vartok = tok.next();
if (vartok->str() == "*")
vartok = vartok->next();
if (vartok->varId() && CheckNullPointer::isPointerDeRef(vartok, unknown))
{
if (vartok->varId() && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
dereference(checks, vartok);
}
}
@ -1198,18 +1072,15 @@ private:
}
/** parse condition. @sa ExecutionPath::parseCondition */
bool parseCondition(const Token &tok, std::list<ExecutionPath *> &checks)
{
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next())
{
bool parseCondition(const Token &tok, std::list<ExecutionPath *> &checks) {
for (const Token *tok2 = &tok; tok2; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == ")")
break;
if (Token::Match(tok2, "[<>=] * %var%"))
dereference(checks, tok2->tokAt(2));
}
if (Token::Match(&tok, "!| %var% ("))
{
if (Token::Match(&tok, "!| %var% (")) {
std::list<const Token *> var;
CheckNullPointer::parseFunctionCall(tok.str() == "!" ? *tok.next() : tok, var, 0);
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it)
@ -1220,10 +1091,8 @@ private:
}
void parseLoopBody(const Token *tok, std::list<ExecutionPath *> &checks) const
{
while (tok)
{
void parseLoopBody(const Token *tok, std::list<ExecutionPath *> &checks) const {
while (tok) {
if (Token::Match(tok, "{|}|return|goto|break|if"))
return;
const Token *next = parse(*tok, checks);

View File

@ -33,8 +33,7 @@ class Token;
/** @brief check for null pointer dereferencing */
class CheckNullPointer : public Check
{
class CheckNullPointer : public Check {
public:
/** @brief This constructor is used when registering the CheckNullPointer */
CheckNullPointer() : Check(myName())
@ -46,15 +45,13 @@ public:
{ }
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
checkNullPointer.nullPointer();
}
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckNullPointer checkNullPointer(tokenizer, settings, errorLogger);
checkNullPointer.nullConstantDereference();
checkNullPointer.executionPaths();
@ -105,21 +102,18 @@ public:
void nullPointerError(const Token *tok, const std::string &varname, const unsigned int line, bool inconclusive = false);
/** Get error messages. Used by --errorlist */
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckNullPointer c(0, settings, errorLogger);
c.nullPointerError(0, "pointer");
}
/** Name of check */
std::string myName() const
{
std::string myName() const {
return "Null pointer";
}
/** class info in WIKI format. Used by --doc */
std::string classInfo() const
{
std::string classInfo() const {
return "Null pointers\n"
"* null pointer dereferencing\n";
}

View File

@ -27,9 +27,8 @@
// Register this check class (by creating a static instance of it)
namespace
{
CheckObsoleteFunctions instance;
namespace {
CheckObsoleteFunctions instance;
}
void CheckObsoleteFunctions::obsoleteFunctions()
@ -43,45 +42,35 @@ void CheckObsoleteFunctions::obsoleteFunctions()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" &&
(!Token::Match(tok->previous(), ".|::|:|,") || Token::simpleMatch(tok->previous()->previous(), "std :: gets")))
{
(!Token::Match(tok->previous(), ".|::|:|,") || Token::simpleMatch(tok->previous()->previous(), "std :: gets"))) {
// function declaration?
if ((tok->previous() && (tok->previous()->str() == "*" || tok->previous()->isName()))
|| symbolDatabase->findFunctionByToken(tok))
{
|| symbolDatabase->findFunctionByToken(tok)) {
_obsoleteStandardFunctions.erase(tok->str());
_obsoletePosixFunctions.erase(tok->str());
continue;
}
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
if (it != _obsoleteStandardFunctions.end())
{
if (it != _obsoleteStandardFunctions.end()) {
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
}
else
{
if (_settings->posix)
{
} else {
if (_settings->posix) {
it = _obsoletePosixFunctions.find(tok->str());
if (it != _obsoletePosixFunctions.end())
{
if (it != _obsoletePosixFunctions.end()) {
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
}
}
if (_settings->c99)
{
if (_settings->c99) {
it = _obsoleteC99Functions.find(tok->str());
if (it != _obsoleteC99Functions.end())
{
if (it != _obsoleteC99Functions.end()) {
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
}
}

View File

@ -34,24 +34,20 @@
* @brief Using obsolete functions that are always insecure to use.
*/
class CheckObsoleteFunctions : public Check
{
class CheckObsoleteFunctions : public Check {
public:
/** This constructor is used when registering the CheckObsoleteFunctions */
CheckObsoleteFunctions() : Check(myName())
{
CheckObsoleteFunctions() : Check(myName()) {
initObsoleteFunctions();
}
/** This constructor is used when running checks. */
CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{
: Check(myName(), tokenizer, settings, errorLogger) {
initObsoleteFunctions();
}
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckObsoleteFunctions checkObsoleteFunctions(tokenizer, settings, errorLogger);
checkObsoleteFunctions.obsoleteFunctions();
}
@ -66,8 +62,7 @@ private:
std::map<std::string, std::string> _obsoleteC99Functions;
/** init obsolete functions list ' */
void initObsoleteFunctions()
{
void initObsoleteFunctions() {
_obsoletePosixFunctions["bsd_signal"] = "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function";
_obsoletePosixFunctions["gethostbyaddr"] = "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function";
@ -116,28 +111,23 @@ private:
}
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckObsoleteFunctions c(0, settings, errorLogger);
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
for (; it!=itend; ++it)
{
for (; it!=itend; ++it) {
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
}
}
std::string myName() const
{
std::string myName() const {
return "Obsolete functions";
}
std::string classInfo() const
{
std::string classInfo() const {
std::string info = "Warn if any of these obsolete functions are used:\n";
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
for (; it!=itend; ++it)
{
for (; it!=itend; ++it) {
info += "* " + it->first + "\n";
}
return info;

File diff suppressed because it is too large Load Diff

View File

@ -33,8 +33,7 @@ class Token;
/** @brief Various small checks */
class CheckOther : public Check
{
class CheckOther : public Check {
public:
/** @brief This constructor is used when registering the CheckClass */
CheckOther() : Check(myName())
@ -46,8 +45,7 @@ public:
{ }
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckOther checkOther(tokenizer, settings, errorLogger);
// Coding style checks
@ -76,8 +74,7 @@ public:
}
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
CheckOther checkOther(tokenizer, settings, errorLogger);
checkOther.clarifyCalculation();
@ -278,8 +275,7 @@ public:
void comparisonOfBoolExpressionWithIntError(const Token *tok);
void SuspiciousSemicolonError(const Token *tok);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) {
CheckOther c(0, settings, errorLogger);
// error
@ -330,13 +326,11 @@ public:
c.SuspiciousSemicolonError(0);
}
std::string myName() const
{
std::string myName() const {
return "Other";
}
std::string classInfo() const
{
std::string classInfo() const {
return "Other checks\n"
// error
@ -391,11 +385,9 @@ private:
* @brief Used in warningRedundantCode()
* Iterates through the %var% tokens in a fully qualified name and concatenates them.
*/
std::string concatNames(const Token **tok) const
{
std::string concatNames(const Token **tok) const {
std::string varname;
while (Token::Match(*tok, "%var% ::|."))
{
while (Token::Match(*tok, "%var% ::|.")) {
varname.append((*tok)->str());
varname.append((*tok)->next()->str());
*tok = (*tok)->tokAt(2);

Some files were not shown because too many files have changed in this diff Show More