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 "utils.h"
#include <climits>
#include <cstdlib> // EXIT_SUCCESS and EXIT_FAILURE
#include <cstring>
#include <algorithm>

View File

@ -21,7 +21,6 @@
#include "pathmatch.h"
#include <cstring>
#include <string>
#include <sstream>
#ifdef _WIN32
@ -114,7 +113,7 @@ void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, co
continue;
const char* ansiFfd = ffd.cFileName;
if (strchr(ansiFfd,'?')) {
if (std::strchr(ansiFfd,'?')) {
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) {
// File
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
const std::string nativename = Path::fromNativeSeparators(fname);
if ((!checkAllFilesInDir || Path::acceptFile(fname, extra)) && !ignored.Match(fname)) {
// Limitation: file sizes are assumed to fit in a 'size_t'
#ifdef _WIN64
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 <limits.h>
#include <sys/stat.h>
#include <sstream>
// Get absolute path. Returns empty string if path does not exist or other error.
std::string FileLister::getAbsolutePath(const std::string& path)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,6 +19,7 @@
#include "checkstl.h"
#include "symboldatabase.h"
#include "checknullpointer.h"
#include "utils.h"
#include <sstream>
// Register this check class (by creating a static instance of it)
@ -1476,7 +1477,7 @@ void CheckStl::readingEmptyStlContainer()
bool insert = false;
if (var->nameToken() == tok && var->isLocal() && !var->isStatic()) { // Local variable declared
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;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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