From 2ba2a4e6ae52e2fb7442c57b03093cc6a64fc461 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 18 Feb 2012 11:55:05 +0100 Subject: [PATCH] Some refactorizations --- cli/cmdlineparser.h | 4 ++-- cli/cppcheckexecutor.cpp | 8 ++++---- lib/errorlogger.cpp | 8 ++++---- lib/path.cpp | 35 +++++++---------------------------- test/testutils.h | 9 +++------ 5 files changed, 20 insertions(+), 44 deletions(-) diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index e17bd1dce..533ebd2a9 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -68,7 +68,7 @@ public: /** * Return the path names user gave to command line. */ - std::vector GetPathNames() const { + const std::vector& GetPathNames() const { return _pathnames; } @@ -89,7 +89,7 @@ public: /** * Return a list of paths user wants to ignore. */ - std::vector GetIgnoredPaths() const { + const std::vector& GetIgnoredPaths() const { return _ignoredPaths; } diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index ad95b0eeb..2ab79b846 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -54,7 +54,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c const char * extraVersion = cppcheck->extraVersion(); if (strlen(extraVersion) > 0) std::cout << "Cppcheck " << cppcheck->version() << " (" - << extraVersion << ")" << std::endl; + << extraVersion << ')' << std::endl; else std::cout << "Cppcheck " << cppcheck->version() << std::endl; } @@ -84,7 +84,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c 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; + std::cout << "cppcheck: warning: Couldn't find path given by -I '" << path << '\'' << std::endl; iter = _settings._includePaths.erase(iter); } } @@ -262,7 +262,7 @@ void CppCheckExecutor::reportProgress(const std::string &filename, const char st std::ostringstream ostr; ostr << "progress: " << stage - << " " << int(value) << "%"; + << ' ' << int(value) << '%'; if (_settings._verbose) ostr << " time=" << str.substr(11, 8); @@ -275,7 +275,7 @@ void CppCheckExecutor::reportStatus(size_t fileindex, size_t filecount, long siz { if (filecount > 1) { std::ostringstream oss; - oss << fileindex << "/" << filecount + oss << fileindex << '/' << filecount << " files checked " << (sizetotal > 0 ? static_cast(static_cast(sizedone) / sizetotal*100) : 0) << "% done"; diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index ebadf30a1..858ac5e55 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -277,7 +277,7 @@ std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string if (!_callStack.empty()) text << callStackToString(_callStack) << ": "; if (_severity != Severity::none) - text << "(" << Severity::toString(_severity) << ") "; + text << '(' << Severity::toString(_severity) << ") "; text << (verbose ? _verboseMessage : _shortMessage); return text.str(); } @@ -316,10 +316,10 @@ std::string ErrorLogger::callStackToString(const std::list::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) { - ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile(); + ostr << (tok == callStack.begin() ? "" : " -> ") << '[' << (*tok).getfile(); if ((*tok).line != 0) - ostr << ":" << (*tok).line; - ostr << "]"; + ostr << ':' << (*tok).line; + ostr << ']'; } return ostr.str(); } diff --git a/lib/path.cpp b/lib/path.cpp index 86215e276..bbd1c83bb 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -80,7 +80,7 @@ std::string Path::simplifyPath(const char *originalPath) if (subPath.length() > 0) pathParts.push_back(subPath); - for (unsigned int i = 0; i < pathParts.size(); ++i) { + for (unsigned int i = 1; i < pathParts.size(); ++i) { if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) { pathParts.erase(pathParts.begin() + static_cast(i) + 1); pathParts.erase(pathParts.begin() + static_cast(i)); @@ -90,7 +90,7 @@ std::string Path::simplifyPath(const char *originalPath) } else if (i > 0 && pathParts[i] == ".") { pathParts.erase(pathParts.begin() + static_cast(i)); i = 0; - } else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/") { + } else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") { pathParts.erase(pathParts.begin() + static_cast(i) - 1); i = 0; } @@ -158,11 +158,7 @@ bool Path::isC(const std::string &path) { // In unix, ".C" is concidered C++ file const std::string extension = getFilenameExtension(path); - if (extension == ".c") { - return true; - } - - return false; + return(extension == ".c"); } bool Path::isCPP(const std::string &path) @@ -178,40 +174,23 @@ bool Path::isCPP(const std::string &path) } // In unix, ".C" is concidered C++ file - if (getFilenameExtension(path) == ".C") { - return true; - } - - return false; + return(getFilenameExtension(path) == ".C"); } bool Path::isJava(const std::string &path) { const std::string extension = getFilenameExtensionInLowerCase(path); - if (extension == ".java") { - return true; - } - - return false; + return(extension == ".java"); } bool Path::isCSharp(const std::string &path) { const std::string extension = getFilenameExtensionInLowerCase(path); - if (extension == ".cs") { - return true; - } - - return false; + return(extension == ".cs"); } bool Path::acceptFile(const std::string &filename) { - if (Path::isCPP(filename) || - Path::isC(filename)) { - return true; - } - - return false; + return(Path::isCPP(filename) || Path::isC(filename)); } diff --git a/test/testutils.h b/test/testutils.h index 4bf2db297..1fa7086de 100644 --- a/test/testutils.h +++ b/test/testutils.h @@ -27,21 +27,18 @@ class Token; class givenACodeSampleToTokenize { private: std::istringstream _sample; - const Token* _tokens; Settings _settings; Tokenizer _tokenizer; public: givenACodeSampleToTokenize(const std::string& sample) - :_sample(sample) - ,_tokens(NULL) { - _tokenizer.setSettings(&_settings); + : _sample(sample) + , _tokenizer(&_settings, 0) { _tokenizer.tokenize(_sample, "test.cpp"); - _tokens = _tokenizer.tokens(); } const Token* tokens() const { - return _tokens; + return _tokenizer.tokens(); } };