Small refactorizations:

- #include cleanup
- Use std::array instead of std::vector
- Do not create a stringstream to concatenate 4 strings
- Use std::cout instead of printf
This commit is contained in:
PKEuS 2015-11-29 10:49:10 +01:00
parent e8decd925b
commit e8522c7883
32 changed files with 42 additions and 56 deletions

View File

@ -26,7 +26,6 @@
#include "threadexecutor.h" #include "threadexecutor.h"
#include "utils.h" #include "utils.h"
#include <climits>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE #include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>

View File

@ -21,7 +21,6 @@
#include "pathmatch.h" #include "pathmatch.h"
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <sstream>
#ifdef _WIN32 #ifdef _WIN32
@ -114,7 +113,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
continue; continue;
const char* ansiFfd = ffd.cFileName; const char* ansiFfd = ffd.cFileName;
if (strchr(ansiFfd,'?')) { if (std::strchr(ansiFfd,'?')) {
ansiFfd = ffd.cAlternateFileName; ansiFfd = ffd.cAlternateFileName;
} }
@ -122,9 +121,9 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
// File // File
const std::string nativename = Path::fromNativeSeparators(fname);
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) { if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
const std::string nativename = Path::fromNativeSeparators(fname);
// Limitation: file sizes are assumed to fit in a 'size_t' // Limitation: file sizes are assumed to fit in a 'size_t'
#ifdef _WIN64 #ifdef _WIN64
files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow; files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
@ -168,6 +167,7 @@ bool FileLister::fileExists(const std::string &path)
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sstream>
// Get absolute path. Returns empty string if path does not exist or other error. // Get absolute path. Returns empty string if path does not exist or other error.
std::string FileLister::getAbsolutePath(const std::string& path) std::string FileLister::getAbsolutePath(const std::string& path)

View File

@ -102,7 +102,7 @@ void CheckOther::checkZeroDivision()
#include "cppcheckexecutor.h" #include "cppcheckexecutor.h"
#include <cstdio> #include <iostream>
#include <cstdlib> #include <cstdlib>
#ifdef _WIN32 #ifdef _WIN32
@ -136,11 +136,11 @@ int main(int argc, char* argv[])
return exec.check(argc, argv); return exec.check(argc, argv);
#ifdef NDEBUG #ifdef NDEBUG
} catch (const InternalError& e) { } catch (const InternalError& e) {
printf("%s\n", e.errorMessage.c_str()); std::cout << e.errorMessage << std::endl;
} catch (const std::exception& error) { } catch (const std::exception& error) {
printf("%s\n", error.what()); std::cout << error.what() << std::endl;
} catch (...) { } catch (...) {
printf("Unknown exception\n"); std::cout << "Unknown exception" << std::endl;
} }
return EXIT_FAILURE; return EXIT_FAILURE;
#endif #endif

View File

@ -17,8 +17,6 @@
*/ */
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// 64-bit portability
//---------------------------------------------------------------------------
#include "check.h" #include "check.h"

View File

@ -75,9 +75,6 @@ public:
/** @brief negative size for array */ /** @brief negative size for array */
void negativeArraySize(); void negativeArraySize();
/** @brief %Check for buffer overruns by inspecting execution paths */
void executionPaths();
/** /**
* @brief Get minimum length of format string result * @brief Get minimum length of format string result
* @param input_string format string * @param input_string format string

View File

@ -23,10 +23,10 @@
#include "token.h" #include "token.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <cctype>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -23,7 +23,6 @@
#include <string> #include <string>
#include <set> #include <set>
#include <cstring> #include <cstring>
#include <cctype>
// Register this check class (by creating a static instance of it). // Register this check class (by creating a static instance of it).
// Disabled in release builds // Disabled in release builds

View File

@ -21,6 +21,7 @@
#include "tokenize.h" #include "tokenize.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <cctype> #include <cctype>
#include <cstdlib> #include <cstdlib>

View File

@ -22,9 +22,9 @@
#include "mathlib.h" #include "mathlib.h"
#include "tokenize.h" #include "tokenize.h"
#include "astutils.h" #include "astutils.h"
#include "utils.h"
#include <algorithm> #include <algorithm>
#include <sstream>
#include <set> #include <set>
#include <stack> #include <stack>
@ -2060,12 +2060,9 @@ void CheckMemoryLeakInFunction::checkScope(const Token *startTok, const std::str
noerr = noerr || Token::simpleMatch(first, "alloc ; if return ; dealloc; }"); noerr = noerr || Token::simpleMatch(first, "alloc ; if return ; dealloc; }");
// Unhandled case.. // Unhandled case..
if (!noerr) { if (!noerr)
std::ostringstream errmsg; reportError(first, Severity::debug, "debug",
errmsg << "inconclusive leak of " << varname << ": "; "inconclusive leak of " + varname + ": " + tok->stringifyList(false, false, false, false, false, 0, 0));
errmsg << tok->stringifyList(false, false, false, false, false, 0, 0);
reportError(first, Severity::debug, "debug", errmsg.str());
}
} }
TokenList::deleteTokens(tok); TokenList::deleteTokens(tok);

View File

@ -21,6 +21,7 @@
#include "checknullpointer.h" #include "checknullpointer.h"
#include "mathlib.h" #include "mathlib.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <cctype> #include <cctype>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -22,8 +22,8 @@
#include "astutils.h" #include "astutils.h"
#include "mathlib.h" #include "mathlib.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <cmath> // fabs()
#include <stack> #include <stack>
#include <algorithm> // find_if() #include <algorithm> // find_if()
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -19,6 +19,7 @@
#include "checkstl.h" #include "checkstl.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "checknullpointer.h" #include "checknullpointer.h"
#include "utils.h"
#include <sstream> #include <sstream>
// Register this check class (by creating a static instance of it) // Register this check class (by creating a static instance of it)
@ -1476,7 +1477,7 @@ void CheckStl::readingEmptyStlContainer()
bool insert = false; bool insert = false;
if (var->nameToken() == tok && var->isLocal() && !var->isStatic()) { // Local variable declared if (var->nameToken() == tok && var->isLocal() && !var->isStatic()) { // Local variable declared
insert = !Token::Match(tok->tokAt(1), "[(=]"); // Only if not initialized insert = !Token::Match(tok->tokAt(1), "[(=]"); // Only if not initialized
} else if (Token::Match(tok, "%name% . clear ( ) ;")) { } else if (Token::Match(tok, "%var% . clear ( ) ;")) {
insert = true; insert = true;
} }

View File

@ -21,9 +21,8 @@
#include "checkuninitvar.h" #include "checkuninitvar.h"
#include "astutils.h" #include "astutils.h"
#include "mathlib.h" #include "mathlib.h"
#include "checknullpointer.h" // CheckNullPointer::parseFunctionCall #include "checknullpointer.h" // CheckNullPointer::isPointerDeref
#include "symboldatabase.h" #include "symboldatabase.h"
#include <algorithm>
#include <map> #include <map>
#include <cassert> #include <cassert>
#include <stack> #include <stack>
@ -172,10 +171,9 @@ void CheckUninitVar::checkStruct(const Token *tok, const Variable &structvar)
} }
struct VariableValue { struct VariableValue {
VariableValue() : notEqual(false), value(0) {} explicit VariableValue(MathLib::bigint val = 0) : value(val), notEqual(false) {}
explicit VariableValue(MathLib::bigint val) : notEqual(false), value(val) {}
bool notEqual;
MathLib::bigint value; MathLib::bigint value;
bool notEqual;
}; };
static VariableValue operator!(VariableValue v) static VariableValue operator!(VariableValue v)
{ {

View File

@ -21,7 +21,6 @@
#include "checkunusedvar.h" #include "checkunusedvar.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include <algorithm> #include <algorithm>
#include <cctype>
#include <utility> #include <utility>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -27,7 +27,7 @@
#include <cassert> #include <cassert>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <vector> #include <array>
InternalError::InternalError(const Token *tok, const std::string &errorMsg, Type type) : InternalError::InternalError(const Token *tok, const std::string &errorMsg, Type type) :
token(tok), errorMessage(errorMsg) token(tok), errorMessage(errorMsg)
@ -131,7 +131,8 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
_inconclusive = false; _inconclusive = false;
_callStack.clear(); _callStack.clear();
std::istringstream iss(data); std::istringstream iss(data);
std::vector<std::string> results; std::array<std::string, 5> results;
std::size_t elem = 0;
while (iss.good()) { while (iss.good()) {
unsigned int len = 0; unsigned int len = 0;
if (!(iss >> len)) if (!(iss >> len))
@ -149,12 +150,12 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
continue; continue;
} }
results.push_back(temp); results[elem++] = temp;
if (results.size() == 5) if (elem == 5)
break; break;
} }
if (results.size() != 5) if (elem != 5)
throw InternalError(0, "Internal Error: Deserialization of error message failed"); throw InternalError(0, "Internal Error: Deserialization of error message failed");
_id = results[0]; _id = results[0];

View File

@ -21,12 +21,12 @@
#define errorloggerH #define errorloggerH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include <list>
#include <string>
#include "config.h" #include "config.h"
#include "suppressions.h" #include "suppressions.h"
#include <list>
#include <string>
class Token; class Token;
class TokenList; class TokenList;

View File

@ -26,7 +26,6 @@
#include "astutils.h" #include "astutils.h"
#include <string> #include <string>
#include <algorithm>
static std::vector<std::string> getnames(const char *names) static std::vector<std::string> getnames(const char *names)
{ {

View File

@ -23,7 +23,6 @@
#include "config.h" #include "config.h"
#include "mathlib.h" #include "mathlib.h"
#include "token.h"
#include "standards.h" #include "standards.h"
#include "errorlogger.h" #include "errorlogger.h"
@ -31,8 +30,8 @@
#include <set> #include <set>
#include <string> #include <string>
#include <list> #include <list>
#include <vector>
class TokenList;
namespace tinyxml2 { namespace tinyxml2 {
class XMLDocument; class XMLDocument;
class XMLElement; class XMLElement;

View File

@ -23,6 +23,7 @@
#include <cmath> #include <cmath>
#include <cctype> #include <cctype>
#include <cstdlib>
#include <limits> #include <limits>

View File

@ -21,7 +21,6 @@
#define mathlibH #define mathlibH
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include <cstdlib>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "config.h" #include "config.h"

View File

@ -17,7 +17,6 @@
*/ */
#include "settings.h" #include "settings.h"
#include "path.h"
#include "preprocessor.h" // Preprocessor #include "preprocessor.h" // Preprocessor
#include "utils.h" #include "utils.h"

View File

@ -17,7 +17,6 @@
*/ */
#include "suppressions.h" #include "suppressions.h"
#include "settings.h"
#include "path.h" #include "path.h"
#include <algorithm> #include <algorithm>

View File

@ -23,6 +23,7 @@
#include "token.h" #include "token.h"
#include "settings.h" #include "settings.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "utils.h"
#include <string> #include <string>
#include <ostream> #include <ostream>

View File

@ -31,7 +31,6 @@
#include "config.h" #include "config.h"
#include "token.h" #include "token.h"
#include "mathlib.h" #include "mathlib.h"
#include "utils.h"
class Tokenizer; class Tokenizer;
class Settings; class Settings;

View File

@ -18,9 +18,9 @@
#include "token.h" #include "token.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "check.h"
#include "settings.h" #include "settings.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>

View File

@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "tokenize.h" #include "tokenize.h"
#include "mathlib.h" #include "mathlib.h"
@ -26,6 +25,7 @@
#include "symboldatabase.h" #include "symboldatabase.h"
#include "templatesimplifier.h" #include "templatesimplifier.h"
#include "timer.h" #include "timer.h"
#include "utils.h"
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>

View File

@ -29,7 +29,6 @@
#include <sstream> #include <sstream>
#include <cctype> #include <cctype>
#include <stack> #include <stack>
#include <cassert>
// How many compileExpression recursions are allowed? // How many compileExpression recursions are allowed?
// For practical code this could be endless. But in some special torture test // For practical code this could be endless. But in some special torture test

View File

@ -19,9 +19,8 @@
#include "testsuite.h" #include "testsuite.h"
#include "preprocessor.h" #include "preprocessor.h"
#include "options.h" #include "options.h"
#include <cstdio> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <ctime>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -41,11 +40,11 @@ int main(int argc, char *argv[])
return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE; return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
#ifdef NDEBUG #ifdef NDEBUG
} catch (const InternalError& e) { } catch (const InternalError& e) {
printf("%s\n", e.errorMessage.c_str()); std::cout << e.errorMessage << std::endl;
} catch (const std::exception& error) { } catch (const std::exception& error) {
printf("%s\n", error.what()); std::cout << error.what() << std::endl;
} catch (...) { } catch (...) {
printf("Unknown exception\n"); std::cout << "Unknown exception" << std::endl;
} }
return EXIT_FAILURE; return EXIT_FAILURE;
#endif #endif

View File

@ -21,6 +21,7 @@
#include "cppcheckexecutor.h" #include "cppcheckexecutor.h"
#include "path.h" #include "path.h"
#include "pathmatch.h" #include "pathmatch.h"
#include "redirect.h"
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>

View File

@ -18,9 +18,9 @@
#include "testsuite.h" #include "testsuite.h"
#include "options.h" #include "options.h"
#include "redirect.h"
#include <iostream> #include <iostream>
#include <cstdio>
#include <list> #include <list>
std::ostringstream errout; std::ostringstream errout;

View File

@ -23,7 +23,6 @@
#include <sstream> #include <sstream>
#include <set> #include <set>
#include "errorlogger.h" #include "errorlogger.h"
#include "redirect.h"
class options; class options;

View File

@ -19,6 +19,7 @@
#include "testsuite.h" #include "testsuite.h"
#include "testutils.h" #include "testutils.h"
#include "symboldatabase.h" #include "symboldatabase.h"
#include "utils.h"
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>