Skip to content

Commit 2ba2a4e

Browse files
committed
Some refactorizations
1 parent bfb4dd6 commit 2ba2a4e

5 files changed

Lines changed: 20 additions & 44 deletions

File tree

cli/cmdlineparser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CmdLineParser {
6868
/**
6969
* Return the path names user gave to command line.
7070
*/
71-
std::vector<std::string> GetPathNames() const {
71+
const std::vector<std::string>& GetPathNames() const {
7272
return _pathnames;
7373
}
7474

@@ -89,7 +89,7 @@ class CmdLineParser {
8989
/**
9090
* Return a list of paths user wants to ignore.
9191
*/
92-
std::vector<std::string> GetIgnoredPaths() const {
92+
const std::vector<std::string>& GetIgnoredPaths() const {
9393
return _ignoredPaths;
9494
}
9595

cli/cppcheckexecutor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
5454
const char * extraVersion = cppcheck->extraVersion();
5555
if (strlen(extraVersion) > 0)
5656
std::cout << "Cppcheck " << cppcheck->version() << " ("
57-
<< extraVersion << ")" << std::endl;
57+
<< extraVersion << ')' << std::endl;
5858
else
5959
std::cout << "Cppcheck " << cppcheck->version() << std::endl;
6060
}
@@ -84,7 +84,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
8484
else {
8585
// If the include path is not found, warn user and remove the
8686
// non-existing path from the list.
87-
std::cout << "cppcheck: warning: Couldn't find path given by -I '" + path + "'" << std::endl;
87+
std::cout << "cppcheck: warning: Couldn't find path given by -I '" << path << '\'' << std::endl;
8888
iter = _settings._includePaths.erase(iter);
8989
}
9090
}
@@ -262,7 +262,7 @@ void CppCheckExecutor::reportProgress(const std::string &filename, const char st
262262
std::ostringstream ostr;
263263
ostr << "progress: "
264264
<< stage
265-
<< " " << int(value) << "%";
265+
<< ' ' << int(value) << '%';
266266
if (_settings._verbose)
267267
ostr << " time=" << str.substr(11, 8);
268268

@@ -275,7 +275,7 @@ void CppCheckExecutor::reportStatus(size_t fileindex, size_t filecount, long siz
275275
{
276276
if (filecount > 1) {
277277
std::ostringstream oss;
278-
oss << fileindex << "/" << filecount
278+
oss << fileindex << '/' << filecount
279279
<< " files checked " <<
280280
(sizetotal > 0 ? static_cast<long>(static_cast<long double>(sizedone) / sizetotal*100) : 0)
281281
<< "% done";

lib/errorlogger.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string
277277
if (!_callStack.empty())
278278
text << callStackToString(_callStack) << ": ";
279279
if (_severity != Severity::none)
280-
text << "(" << Severity::toString(_severity) << ") ";
280+
text << '(' << Severity::toString(_severity) << ") ";
281281
text << (verbose ? _verboseMessage : _shortMessage);
282282
return text.str();
283283
}
@@ -316,10 +316,10 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
316316
{
317317
std::ostringstream ostr;
318318
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) {
319-
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).getfile();
319+
ostr << (tok == callStack.begin() ? "" : " -> ") << '[' << (*tok).getfile();
320320
if ((*tok).line != 0)
321-
ostr << ":" << (*tok).line;
322-
ostr << "]";
321+
ostr << ':' << (*tok).line;
322+
ostr << ']';
323323
}
324324
return ostr.str();
325325
}

lib/path.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ std::string Path::simplifyPath(const char *originalPath)
8080
if (subPath.length() > 0)
8181
pathParts.push_back(subPath);
8282

83-
for (unsigned int i = 0; i < pathParts.size(); ++i) {
83+
for (unsigned int i = 1; i < pathParts.size(); ++i) {
8484
if (i > 1 && pathParts[i-2] != ".." && pathParts[i] == ".." && pathParts.size() > i + 1) {
8585
pathParts.erase(pathParts.begin() + static_cast<int>(i) + 1);
8686
pathParts.erase(pathParts.begin() + static_cast<int>(i));
@@ -90,7 +90,7 @@ std::string Path::simplifyPath(const char *originalPath)
9090
} else if (i > 0 && pathParts[i] == ".") {
9191
pathParts.erase(pathParts.begin() + static_cast<int>(i));
9292
i = 0;
93-
} else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/") {
93+
} else if (i > 0 && pathParts[i] == "/" && pathParts[i-1] == "/") {
9494
pathParts.erase(pathParts.begin() + static_cast<int>(i) - 1);
9595
i = 0;
9696
}
@@ -158,11 +158,7 @@ bool Path::isC(const std::string &path)
158158
{
159159
// In unix, ".C" is concidered C++ file
160160
const std::string extension = getFilenameExtension(path);
161-
if (extension == ".c") {
162-
return true;
163-
}
164-
165-
return false;
161+
return(extension == ".c");
166162
}
167163

168164
bool Path::isCPP(const std::string &path)
@@ -178,40 +174,23 @@ bool Path::isCPP(const std::string &path)
178174
}
179175

180176
// In unix, ".C" is concidered C++ file
181-
if (getFilenameExtension(path) == ".C") {
182-
return true;
183-
}
184-
185-
return false;
177+
return(getFilenameExtension(path) == ".C");
186178
}
187179

188180
bool Path::isJava(const std::string &path)
189181
{
190182
const std::string extension = getFilenameExtensionInLowerCase(path);
191-
if (extension == ".java") {
192-
return true;
193-
}
194-
195-
return false;
183+
return(extension == ".java");
196184
}
197185

198186
bool Path::isCSharp(const std::string &path)
199187
{
200188
const std::string extension = getFilenameExtensionInLowerCase(path);
201-
if (extension == ".cs") {
202-
return true;
203-
}
204-
205-
return false;
189+
return(extension == ".cs");
206190
}
207191

208192
bool Path::acceptFile(const std::string &filename)
209193
{
210-
if (Path::isCPP(filename) ||
211-
Path::isC(filename)) {
212-
return true;
213-
}
214-
215-
return false;
194+
return(Path::isCPP(filename) || Path::isC(filename));
216195
}
217196

test/testutils.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,18 @@ class Token;
2727
class givenACodeSampleToTokenize {
2828
private:
2929
std::istringstream _sample;
30-
const Token* _tokens;
3130
Settings _settings;
3231
Tokenizer _tokenizer;
3332

3433
public:
3534
givenACodeSampleToTokenize(const std::string& sample)
36-
:_sample(sample)
37-
,_tokens(NULL) {
38-
_tokenizer.setSettings(&_settings);
35+
: _sample(sample)
36+
, _tokenizer(&_settings, 0) {
3937
_tokenizer.tokenize(_sample, "test.cpp");
40-
_tokens = _tokenizer.tokens();
4138
}
4239

4340
const Token* tokens() const {
44-
return _tokens;
41+
return _tokenizer.tokens();
4542
}
4643
};
4744

0 commit comments

Comments
 (0)