Skip to content

Commit e9411e0

Browse files
committed
Refactorized inefficient usage of std::string and const char[].
1 parent 3c64c70 commit e9411e0

13 files changed

Lines changed: 265 additions & 268 deletions

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
483483

484484
if (!path.empty()) {
485485
path = Path::fromNativeSeparators(path);
486-
path = Path::simplifyPath(path.c_str());
486+
path = Path::simplifyPath(path);
487487
path = Path::removeQuotationMarks(path);
488488

489489
if (FileLister::isDirectory(path)) {

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
138138
return exitcode;
139139

140140
if (_settings._errorsOnly == false) {
141-
std::string fixedpath = Path::simplifyPath(filename.c_str());
141+
std::string fixedpath = Path::simplifyPath(filename);
142142
fixedpath = Path::toNativeSeparators(fixedpath);
143143
_errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("..."));
144144
}
@@ -209,7 +209,7 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
209209

210210
// If only errors are printed, print filename after the check
211211
if (_settings._errorsOnly == false && it != configurations.begin()) {
212-
std::string fixedpath = Path::simplifyPath(filename.c_str());
212+
std::string fixedpath = Path::simplifyPath(filename);
213213
fixedpath = Path::toNativeSeparators(fixedpath);
214214
_errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("..."));
215215
}

lib/errorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
365365
{
366366
_file = file;
367367
_file = Path::fromNativeSeparators(_file);
368-
_file = Path::simplifyPath(_file.c_str());
368+
_file = Path::simplifyPath(_file);
369369
}
370370

371371
std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const

lib/library.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,23 @@ class CPPCHECKLIB Library {
203203
}
204204

205205
std::string blockstart(const std::string &file) const {
206-
std::string start;
207206
const std::map<std::string, CodeBlock>::const_iterator map_it
208207
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
209208

210209
if (map_it != _executableblocks.end()) {
211-
start = map_it->second.start();
210+
return map_it->second.start();
212211
}
213-
return start;
212+
return std::string();
214213
}
215214

216215
std::string blockend(const std::string &file) const {
217-
std::string end;
218216
const std::map<std::string, CodeBlock>::const_iterator map_it
219217
= _executableblocks.find(Path::getFilenameExtensionInLowerCase(file));
220218

221219
if (map_it != _executableblocks.end()) {
222-
end = map_it->second.end();
220+
return map_it->second.end();
223221
}
224-
return end;
222+
return std::string();
225223
}
226224

227225
bool iskeyword(const std::string &file, const std::string &keyword) const {
@@ -305,10 +303,10 @@ class CPPCHECKLIB Library {
305303
void addBlock(const std::string& blockName) {
306304
_blocks.insert(blockName);
307305
}
308-
std::string start() const {
306+
const std::string& start() const {
309307
return _start;
310308
}
311-
std::string end() const {
309+
const std::string& end() const {
312310
return _end;
313311
}
314312
int offset() const {

lib/path.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ std::string Path::fromNativeSeparators(std::string path)
5858
return path;
5959
}
6060

61-
std::string Path::simplifyPath(const char *originalPath)
61+
std::string Path::simplifyPath(std::string originalPath)
6262
{
6363
// Skip ./ at the beginning
64-
if (std::strlen(originalPath) > 2 && originalPath[0] == '.' &&
64+
if (originalPath.size() > 2 && originalPath[0] == '.' &&
6565
originalPath[1] == '/') {
66-
originalPath += 2;
66+
originalPath = originalPath.erase(0, 2);
6767
}
6868

6969
std::string subPath = "";
7070
std::vector<std::string> pathParts;
71-
for (; *originalPath; ++originalPath) {
72-
if (*originalPath == '/' || *originalPath == '\\') {
71+
for (std::size_t i = 0; i < originalPath.size(); ++i) {
72+
if (originalPath[i] == '/' || originalPath[i] == '\\') {
7373
if (subPath.length() > 0) {
7474
pathParts.push_back(subPath);
7575
subPath = "";
7676
}
7777

78-
pathParts.push_back(std::string(1 , *originalPath));
78+
pathParts.push_back(std::string(1 , originalPath[i]));
7979
} else
80-
subPath.append(1, *originalPath);
80+
subPath.append(1, originalPath[i]);
8181
}
8282

8383
if (subPath.length() > 0)

lib/path.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CPPCHECKLIB Path {
5757
* @param originalPath path to be simplified, must have / -separators.
5858
* @return simplified path
5959
*/
60-
static std::string simplifyPath(const char *originalPath);
60+
static std::string simplifyPath(std::string originalPath);
6161

6262
/**
6363
* @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')

lib/preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
23082308
const bool fileOpened(openHeader(filename, includePaths, filepath, fin));
23092309

23102310
if (fileOpened) {
2311-
filename = Path::simplifyPath(filename.c_str());
2311+
filename = Path::simplifyPath(filename);
23122312
std::string tempFile = filename;
23132313
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
23142314
if (handledFiles.find(tempFile) != handledFiles.end()) {

lib/suppressions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ std::string Suppressions::FileMatcher::addFile(const std::string &name, unsigned
169169
} else if (name.empty()) {
170170
_globs["*"][0U] = false;
171171
} else {
172-
_files[Path::simplifyPath(name.c_str())][line] = false;
172+
_files[Path::simplifyPath(name)][line] = false;
173173
}
174174
return "";
175175
}

lib/tokenlist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ unsigned int TokenList::appendFileIfNew(const std::string &fileName)
6262
return i;
6363

6464
// The "_files" vector remembers what files have been tokenized..
65-
_files.push_back(Path::simplifyPath(fileName.c_str()));
65+
_files.push_back(Path::simplifyPath(fileName));
6666
return static_cast<unsigned int>(_files.size() - 1);
6767
}
6868

test/testmemleak.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,19 +1372,18 @@ class TestMemleakInFunction : public TestFixture {
13721372

13731373

13741374
void switch2() {
1375-
const std::string code("void f()\n"
1376-
"{\n"
1377-
" char *str = new char[10];\n"
1378-
" switch (abc)\n"
1379-
" {\n"
1380-
" case 1:\n"
1381-
" delete [] str;\n"
1382-
" break;\n"
1383-
" default:\n"
1384-
" break;\n"
1385-
" };\n"
1386-
"}");
1387-
check(code.c_str());
1375+
check("void f()\n"
1376+
"{\n"
1377+
" char *str = new char[10];\n"
1378+
" switch (abc)\n"
1379+
" {\n"
1380+
" case 1:\n"
1381+
" delete [] str;\n"
1382+
" break;\n"
1383+
" default:\n"
1384+
" break;\n"
1385+
" };\n"
1386+
"}");
13881387
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: str\n", errout.str());
13891388
}
13901389

0 commit comments

Comments
 (0)