Skip to content

Commit a14449f

Browse files
committed
more FileWithDetails usage
1 parent 019a240 commit a14449f

15 files changed

+109
-48
lines changed

Diff for: Makefile

+14-14
Large diffs are not rendered by default.

Diff for: cli/processexecutor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ unsigned int ProcessExecutor::check()
286286
fileChecker.analyseClangTidy(*iFileSettings);
287287
} else {
288288
// Read file from a file
289-
resultOfCheck = fileChecker.check(iFile->path());
289+
resultOfCheck = fileChecker.check(*iFile);
290290
// TODO: call analyseClangTidy()?
291291
}
292292

Diff for: cli/singleexecutor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ unsigned int SingleExecutor::check()
5050
unsigned int c = 0;
5151

5252
for (std::list<FileWithDetails>::const_iterator i = mFiles.cbegin(); i != mFiles.cend(); ++i) {
53-
result += mCppcheck.check(i->path());
53+
result += mCppcheck.check(*i);
5454
processedsize += i->size();
5555
++c;
5656
if (!mSettings.quiet)

Diff for: cli/threadexecutor.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ class ThreadData
9494
});
9595
}
9696

97-
bool next(const std::string *&file, const FileSettings *&fs, std::size_t &fileSize) {
97+
bool next(const FileWithDetails *&file, const FileSettings *&fs, std::size_t &fileSize) {
9898
std::lock_guard<std::mutex> l(mFileSync);
9999
if (mItNextFile != mFiles.end()) {
100-
file = &mItNextFile->path();
100+
file = &(*mItNextFile);
101101
fs = nullptr;
102102
fileSize = mItNextFile->size();
103103
++mItNextFile;
@@ -114,7 +114,7 @@ class ThreadData
114114
return false;
115115
}
116116

117-
unsigned int check(ErrorLogger &errorLogger, const std::string *file, const FileSettings *fs) const {
117+
unsigned int check(ErrorLogger &errorLogger, const FileWithDetails *file, const FileSettings *fs) const {
118118
CppCheck fileChecker(errorLogger, false, mExecuteCommand);
119119
fileChecker.settings() = mSettings; // this is a copy
120120

@@ -163,7 +163,7 @@ static unsigned int STDCALL threadProc(ThreadData *data)
163163
{
164164
unsigned int result = 0;
165165

166-
const std::string *file;
166+
const FileWithDetails *file;
167167
const FileSettings *fs;
168168
std::size_t fileSize;
169169

Diff for: democlient/democlient.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <iostream>
2424

2525
#include "cppcheck.h"
26+
#include "filesettings.h"
2627
#include "version.h"
2728

2829
static void unencode(const char *src, char *dest)
@@ -59,7 +60,7 @@ class CppcheckExecutor : public ErrorLogger {
5960
}
6061

6162
void run(const char code[]) {
62-
cppcheck.check("test.cpp", code);
63+
cppcheck.check(FileWithDetails("test.cpp"), code);
6364
}
6465

6566
void reportOut(const std::string & /*outmsg*/, Color /*c*/) override {}

Diff for: gui/checkthread.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void CheckThread::run()
124124
QString file = mResult.getNextFile();
125125
while (!file.isEmpty() && mState == Running) {
126126
qDebug() << "Checking file" << file;
127-
mCppcheck.check(file.toStdString());
127+
mCppcheck.check(FileWithDetails(file.toStdString()));
128128
runAddonsAndTools(nullptr, file);
129129
emit fileChecked(file);
130130

Diff for: gui/mainwindow.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
662662
checkLockDownUI();
663663
clearResults();
664664
mUI->mResults->checkingStarted(1);
665-
cppcheck.check(filename.toStdString(), code.toStdString());
665+
cppcheck.check(FileWithDetails(filename.toStdString()), code.toStdString());
666666
analysisDone();
667667

668668
// Expand results

Diff for: lib/cppcheck.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -539,18 +539,18 @@ unsigned int CppCheck::checkClang(const std::string &path)
539539
return mExitCode;
540540
}
541541

542-
unsigned int CppCheck::check(const std::string &path)
542+
unsigned int CppCheck::check(const FileWithDetails &file)
543543
{
544544
if (mSettings.clang)
545-
return checkClang(Path::simplifyPath(path));
545+
return checkClang(file.spath());
546546

547-
return checkFile(Path::simplifyPath(path), emptyString);
547+
return checkFile(file.spath(), emptyString);
548548
}
549549

550-
unsigned int CppCheck::check(const std::string &path, const std::string &content)
550+
unsigned int CppCheck::check(const FileWithDetails &file, const std::string &content)
551551
{
552552
std::istringstream iss(content);
553-
return checkFile(Path::simplifyPath(path), emptyString, &iss);
553+
return checkFile(file.spath(), emptyString, &iss);
554554
}
555555

556556
unsigned int CppCheck::check(const FileSettings &fs)
@@ -578,12 +578,12 @@ unsigned int CppCheck::check(const FileSettings &fs)
578578
if (mSettings.clang) {
579579
temp.mSettings.includePaths.insert(temp.mSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());
580580
// TODO: propagate back suppressions
581-
const unsigned int returnValue = temp.check(Path::simplifyPath(fs.filename()));
581+
const unsigned int returnValue = temp.check(fs.file);
582582
if (mUnusedFunctionsCheck)
583583
mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
584584
return returnValue;
585585
}
586-
const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename()), fs.cfg);
586+
const unsigned int returnValue = temp.checkFile(fs.sfilename(), fs.cfg);
587587
mSettings.supprs.nomsg.addSuppressions(temp.mSettings.supprs.nomsg.getSuppressions());
588588
if (mUnusedFunctionsCheck)
589589
mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
@@ -612,8 +612,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
612612
const Timer fileTotalTimer(mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL, filename);
613613

614614
if (!mSettings.quiet) {
615-
std::string fixedpath = Path::simplifyPath(filename);
616-
fixedpath = Path::toNativeSeparators(std::move(fixedpath));
615+
std::string fixedpath = Path::toNativeSeparators(filename);
617616
mErrorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("..."), Color::FgGreen);
618617

619618
if (mSettings.verbose) {
@@ -867,8 +866,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
867866

868867
// If only errors are printed, print filename after the check
869868
if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) {
870-
std::string fixedpath = Path::simplifyPath(filename);
871-
fixedpath = Path::toNativeSeparators(std::move(fixedpath));
869+
std::string fixedpath = Path::toNativeSeparators(filename);
872870
mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen);
873871
}
874872

@@ -973,7 +971,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
973971
fdump.close();
974972
}
975973

976-
executeAddons(dumpFile, Path::simplifyPath(filename));
974+
executeAddons(dumpFile, filename);
977975

978976
} catch (const TerminateException &) {
979977
// Analysis is terminated

Diff for: lib/cppcheck.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,26 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
8383
/**
8484
* @brief Check the file.
8585
* This function checks one given file for errors.
86-
* @param path Path to the file to check.
86+
* @param file The file to check.
8787
* @return amount of errors found or 0 if none were found.
8888
* @note You must set settings before calling this function (by calling
8989
* settings()).
9090
*/
91-
unsigned int check(const std::string &path);
91+
unsigned int check(const FileWithDetails &file);
9292
unsigned int check(const FileSettings &fs);
9393

9494
/**
9595
* @brief Check the file.
9696
* This function checks one "virtual" file. The file is not read from
9797
* the disk but the content is given in @p content. In errors the @p path
9898
* is used as a filename.
99-
* @param path Path to the file to check.
99+
* @param file The file to check.
100100
* @param content File content as a string.
101101
* @return amount of errors found or 0 if none were found.
102102
* @note You must set settings before calling this function (by calling
103103
* settings()).
104104
*/
105-
unsigned int check(const std::string &path, const std::string &content);
105+
unsigned int check(const FileWithDetails &file, const std::string &content);
106106

107107
/**
108108
* @brief Get reference to current settings.

Diff for: lib/filesettings.h

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define fileSettingsH
2121

2222
#include "config.h"
23+
#include "path.h"
2324
#include "platform.h"
2425

2526
#include <list>
@@ -44,6 +45,11 @@ class FileWithDetails
4445
return mPath;
4546
}
4647

48+
std::string spath() const
49+
{
50+
return Path::simplifyPath(mPath);
51+
}
52+
4753
std::size_t size() const
4854
{
4955
return mSize;
@@ -69,6 +75,10 @@ struct CPPCHECKLIB FileSettings {
6975
{
7076
return file.path();
7177
}
78+
std::string sfilename() const
79+
{
80+
return file.spath();
81+
}
7282
std::string defines;
7383
// TODO: handle differently
7484
std::string cppcheckDefines() const {

Diff for: oss-fuzz/main.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "cppcheck.h"
20+
#include "filesettings.h"
2021
#include "type2.h"
2122

2223
#ifdef NO_FUZZ
@@ -37,13 +38,14 @@ class DummyErrorLogger : public ErrorLogger {
3738
};
3839

3940
static DummyErrorLogger s_errorLogger;
41+
static const FileWithDetails s_file("test.cpp");
4042

4143
static void doCheck(const std::string& code)
4244
{
4345
CppCheck cppcheck(s_errorLogger, false, nullptr);
4446
cppcheck.settings().addEnabled("all");
4547
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
46-
cppcheck.check("test.cpp", code);
48+
cppcheck.check(s_file, code);
4749
}
4850

4951
#ifndef NO_FUZZ

Diff for: test/testcppcheck.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class TestCppcheck : public TestFixture {
102102

103103
ErrorLogger2 errorLogger;
104104
CppCheck cppcheck(errorLogger, false, {});
105-
ASSERT_EQUALS(1, cppcheck.check(file.path()));
105+
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(file.path())));
106106
// TODO: how to properly disable these warnings?
107107
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
108108
return id == "logChecker";
@@ -146,7 +146,7 @@ class TestCppcheck : public TestFixture {
146146
const char xmldata[] = R"(<def format="2"><markup ext=".cpp" reporterrors="false"/></def>)";
147147
const Settings s = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
148148
cppcheck.settings() = s;
149-
ASSERT_EQUALS(0, cppcheck.check(file.path()));
149+
ASSERT_EQUALS(0, cppcheck.check(FileWithDetails(file.path())));
150150
// TODO: how to properly disable these warnings?
151151
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
152152
return id == "logChecker";
@@ -169,8 +169,8 @@ class TestCppcheck : public TestFixture {
169169

170170
ErrorLogger2 errorLogger;
171171
CppCheck cppcheck(errorLogger, false, {});
172-
ASSERT_EQUALS(1, cppcheck.check(test_file_a.path()));
173-
ASSERT_EQUALS(1, cppcheck.check(test_file_b.path()));
172+
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file_a.path())));
173+
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file_b.path())));
174174
// TODO: how to properly disable these warnings?
175175
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& msg) {
176176
return msg.id == "logChecker";

Diff for: test/testfilesettings.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,44 @@ class TestFileSettings : public TestFixture {
2525

2626
private:
2727
void run() override {
28-
TEST_CASE(path);
28+
TEST_CASE(test);
2929
}
3030

31-
void path() const {
31+
void test() const {
3232
{
3333
const FileWithDetails p{"file.cpp"};
3434
ASSERT_EQUALS("file.cpp", p.path());
35+
ASSERT_EQUALS("file.cpp", p.spath());
3536
ASSERT_EQUALS(0, p.size());
3637
}
3738
{
3839
const FileWithDetails p{"file.cpp", 123};
3940
ASSERT_EQUALS("file.cpp", p.path());
41+
ASSERT_EQUALS("file.cpp", p.spath());
4042
ASSERT_EQUALS(123, p.size());
4143
}
4244
{
4345
const FileWithDetails p{"in/file.cpp"};
4446
ASSERT_EQUALS("in/file.cpp", p.path());
47+
ASSERT_EQUALS("in/file.cpp", p.spath());
4548
ASSERT_EQUALS(0, p.size());
4649
}
4750
{
4851
const FileWithDetails p{"in\\file.cpp"};
4952
ASSERT_EQUALS("in\\file.cpp", p.path());
53+
ASSERT_EQUALS("in/file.cpp", p.spath());
5054
ASSERT_EQUALS(0, p.size());
5155
}
5256
{
5357
const FileWithDetails p{"in/../file.cpp"};
5458
ASSERT_EQUALS("in/../file.cpp", p.path());
59+
ASSERT_EQUALS("file.cpp", p.spath());
5560
ASSERT_EQUALS(0, p.size());
5661
}
5762
{
5863
const FileWithDetails p{"in\\..\\file.cpp"};
5964
ASSERT_EQUALS("in\\..\\file.cpp", p.path());
65+
ASSERT_EQUALS("file.cpp", p.spath());
6066
ASSERT_EQUALS(0, p.size());
6167
}
6268
}

Diff for: test/testpath.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestPath : public TestFixture {
4747
TEST_CASE(identify);
4848
TEST_CASE(identifyWithCppProbe);
4949
TEST_CASE(is_header);
50+
TEST_CASE(simplifyPath);
5051
}
5152

5253
void removeQuotationMarks() const {
@@ -376,6 +377,49 @@ class TestPath : public TestFixture {
376377
ASSERT(Path::isHeader("index.htm")==false);
377378
ASSERT(Path::isHeader("index.html")==false);
378379
}
380+
381+
void simplifyPath() const {
382+
ASSERT_EQUALS("file.cpp", Path::simplifyPath("file.cpp"));
383+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("../file.cpp"));
384+
ASSERT_EQUALS("file.cpp", Path::simplifyPath("test/../file.cpp"));
385+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("../test/../file.cpp"));
386+
387+
ASSERT_EQUALS("file.cpp", Path::simplifyPath("./file.cpp"));
388+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("./../file.cpp"));
389+
ASSERT_EQUALS("file.cpp", Path::simplifyPath("./test/../file.cpp"));
390+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("./../test/../file.cpp"));
391+
392+
ASSERT_EQUALS("test/", Path::simplifyPath("test/"));
393+
ASSERT_EQUALS("../test/", Path::simplifyPath("../test/"));
394+
ASSERT_EQUALS("../", Path::simplifyPath("../test/.."));
395+
ASSERT_EQUALS("../", Path::simplifyPath("../test/../"));
396+
397+
ASSERT_EQUALS("/home/file.cpp", Path::simplifyPath("/home/test/../file.cpp"));
398+
ASSERT_EQUALS("/file.cpp", Path::simplifyPath("/home/../test/../file.cpp"));
399+
400+
ASSERT_EQUALS("C:/home/file.cpp", Path::simplifyPath("C:/home/test/../file.cpp"));
401+
ASSERT_EQUALS("C:/file.cpp", Path::simplifyPath("C:/home/../test/../file.cpp"));
402+
403+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("..\\file.cpp"));
404+
ASSERT_EQUALS("file.cpp", Path::simplifyPath("test\\..\\file.cpp"));
405+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath("..\\test\\..\\file.cpp"));
406+
407+
ASSERT_EQUALS("file.cpp", Path::simplifyPath(".\\file.cpp"));
408+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath(".\\..\\file.cpp"));
409+
ASSERT_EQUALS("file.cpp", Path::simplifyPath(".\\test\\..\\file.cpp"));
410+
ASSERT_EQUALS("../file.cpp", Path::simplifyPath(".\\..\\test\\..\\file.cpp"));
411+
412+
ASSERT_EQUALS("test/", Path::simplifyPath("test\\"));
413+
ASSERT_EQUALS("../test/", Path::simplifyPath("..\\test\\"));
414+
ASSERT_EQUALS("../", Path::simplifyPath("..\\test\\.."));
415+
ASSERT_EQUALS("../", Path::simplifyPath("..\\test\\..\\"));
416+
417+
ASSERT_EQUALS("C:/home/file.cpp", Path::simplifyPath("C:\\home\\test\\..\\file.cpp"));
418+
ASSERT_EQUALS("C:/file.cpp", Path::simplifyPath("C:\\home\\..\\test\\..\\file.cpp"));
419+
420+
ASSERT_EQUALS("//home/file.cpp", Path::simplifyPath("\\\\home\\test\\..\\file.cpp"));
421+
ASSERT_EQUALS("//file.cpp", Path::simplifyPath("\\\\home\\..\\test\\..\\file.cpp"));
422+
}
379423
};
380424

381425
REGISTER_TEST(TestPath)

Diff for: test/testsuppressions.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ class TestSuppressions : public TestFixture {
11851185
settings.exitCode = 1;
11861186

11871187
const char code[] = "int f() { int a; return a; }";
1188-
ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen
1188+
ASSERT_EQUALS(0, cppCheck.check(FileWithDetails("test.c"), code)); // <- no unsuppressed error is seen
11891189
ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout_str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched.
11901190
}
11911191

@@ -1225,7 +1225,7 @@ class TestSuppressions : public TestFixture {
12251225
" // cppcheck-suppress unusedStructMember\n"
12261226
" int y;\n"
12271227
"};";
1228-
ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code));
1228+
ASSERT_EQUALS(0, cppCheck.check(FileWithDetails("/somewhere/test.cpp"), code));
12291229
ASSERT_EQUALS("",errout_str());
12301230
}
12311231

0 commit comments

Comments
 (0)