Skip to content

Commit 44174cc

Browse files
authored
even more FileWithDetails usage (#6508)
1 parent 06ce840 commit 44174cc

10 files changed

+86
-75
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ $(libcppdir)/settings.o: lib/settings.cpp externals/picojson/picojson.h lib/addo
632632
$(libcppdir)/summaries.o: lib/summaries.cpp lib/addoninfo.h lib/analyzerinfo.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
633633
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/summaries.cpp
634634

635-
$(libcppdir)/suppressions.o: lib/suppressions.cpp externals/tinyxml2/tinyxml2.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/mathlib.h lib/path.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
635+
$(libcppdir)/suppressions.o: lib/suppressions.cpp externals/tinyxml2/tinyxml2.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/mathlib.h lib/path.h lib/platform.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
636636
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/suppressions.cpp
637637

638638
$(libcppdir)/templatesimplifier.o: lib/templatesimplifier.cpp lib/addoninfo.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h

Diff for: cli/cppcheckexecutor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ bool CppCheckExecutor::reportSuppressions(const Settings &settings, const Suppre
232232

233233
for (std::list<FileWithDetails>::const_iterator i = files.cbegin(); i != files.cend(); ++i) {
234234
err |= SuppressionList::reportUnmatchedSuppressions(
235-
suppressions.getUnmatchedLocalSuppressions(i->path(), unusedFunctionCheckEnabled), errorLogger);
235+
suppressions.getUnmatchedLocalSuppressions(*i, unusedFunctionCheckEnabled), errorLogger);
236236
}
237237

238238
for (std::list<FileSettings>::const_iterator i = fileSettings.cbegin(); i != fileSettings.cend(); ++i) {
239239
err |= SuppressionList::reportUnmatchedSuppressions(
240-
suppressions.getUnmatchedLocalSuppressions(i->filename(), unusedFunctionCheckEnabled), errorLogger);
240+
suppressions.getUnmatchedLocalSuppressions(i->file, unusedFunctionCheckEnabled), errorLogger);
241241
}
242242
}
243243
err |= SuppressionList::reportUnmatchedSuppressions(suppressions.getUnmatchedGlobalSuppressions(unusedFunctionCheckEnabled), errorLogger);

Diff for: lib/cppcheck.cpp

+54-51
Large diffs are not rendered by default.

Diff for: lib/cppcheck.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
172172

173173
/**
174174
* @brief Check a file using stream
175-
* @param filename file name
175+
* @param file the file
176176
* @param cfgname cfg name
177177
* @param fileStream stream the file content can be read from
178178
* @return number of errors found
179179
*/
180-
unsigned int checkFile(const std::string& filename, const std::string &cfgname, std::istream* fileStream = nullptr);
180+
unsigned int checkFile(const FileWithDetails& file, const std::string &cfgname, std::istream* fileStream = nullptr);
181181

182182
/**
183183
* @brief Check normal tokens
@@ -189,7 +189,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
189189
* Execute addons
190190
*/
191191
void executeAddons(const std::vector<std::string>& files, const std::string& file0);
192-
void executeAddons(const std::string &dumpFile, const std::string& file0);
192+
void executeAddons(const std::string &dumpFile, const FileWithDetails& file);
193193

194194
/**
195195
* Execute addons
@@ -205,7 +205,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
205205
void executeRules(const std::string &tokenlist, const TokenList &list);
206206
#endif
207207

208-
unsigned int checkClang(const std::string &path);
208+
unsigned int checkClang(const FileWithDetails &file);
209209

210210
/**
211211
* @brief Errors and warnings are directed here.

Diff for: lib/filesettings.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <list>
2727
#include <set>
28+
#include <stdexcept>
2829
#include <string>
2930
#include <utility>
3031

@@ -37,17 +38,21 @@ class FileWithDetails
3738

3839
FileWithDetails(std::string path, std::size_t size)
3940
: mPath(std::move(path))
41+
, mPathSimplified(Path::simplifyPath(mPath))
4042
, mSize(size)
41-
{}
43+
{
44+
if (mPath.empty())
45+
throw std::runtime_error("empty path specified");
46+
}
4247

4348
const std::string& path() const
4449
{
4550
return mPath;
4651
}
4752

48-
std::string spath() const
53+
const std::string& spath() const
4954
{
50-
return Path::simplifyPath(mPath);
55+
return mPathSimplified;
5156
}
5257

5358
std::size_t size() const
@@ -56,6 +61,7 @@ class FileWithDetails
5661
}
5762
private:
5863
std::string mPath;
64+
std::string mPathSimplified;
5965
std::size_t mSize;
6066
};
6167

@@ -75,7 +81,8 @@ struct CPPCHECKLIB FileSettings {
7581
{
7682
return file.path();
7783
}
78-
std::string sfilename() const
84+
// cppcheck-suppress unusedFunction
85+
const std::string& sfilename() const
7986
{
8087
return file.spath();
8188
}

Diff for: lib/suppressions.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "errorlogger.h"
2222
#include "errortypes.h"
23+
#include "filesettings.h"
2324
#include "path.h"
2425
#include "utils.h"
2526
#include "token.h"
@@ -475,9 +476,8 @@ void SuppressionList::dump(std::ostream & out) const
475476
out << " </suppressions>" << std::endl;
476477
}
477478

478-
std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
479+
std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppressions(const FileWithDetails &file, const bool unusedFunctionChecking) const
479480
{
480-
std::string tmpFile = Path::simplifyPath(file);
481481
std::list<Suppression> result;
482482
for (const Suppression &s : mSuppressions) {
483483
if (s.matched || ((s.lineNumber != Suppression::NO_LINE) && !s.checked))
@@ -490,7 +490,7 @@ std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppre
490490
continue;
491491
if (!unusedFunctionChecking && s.errorId == ID_UNUSEDFUNCTION)
492492
continue;
493-
if (tmpFile.empty() || !s.isLocal() || s.fileName != tmpFile)
493+
if (!s.isLocal() || s.fileName != file.spath())
494494
continue;
495495
result.push_back(s);
496496
}

Diff for: lib/suppressions.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Tokenizer;
3737
class ErrorMessage;
3838
class ErrorLogger;
3939
enum class Certainty;
40+
class FileWithDetails;
4041

4142
/** @brief class for handling suppressions */
4243
class CPPCHECKLIB SuppressionList {
@@ -227,7 +228,7 @@ class CPPCHECKLIB SuppressionList {
227228
* @brief Returns list of unmatched local (per-file) suppressions.
228229
* @return list of unmatched suppressions
229230
*/
230-
std::list<Suppression> getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const;
231+
std::list<Suppression> getUnmatchedLocalSuppressions(const FileWithDetails &file, const bool unusedFunctionChecking) const;
231232

232233
/**
233234
* @brief Returns list of unmatched global (glob pattern) suppressions.

Diff for: oss-fuzz/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ $(libcppdir)/settings.o: ../lib/settings.cpp ../externals/picojson/picojson.h ..
311311
$(libcppdir)/summaries.o: ../lib/summaries.cpp ../lib/addoninfo.h ../lib/analyzerinfo.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/settings.h ../lib/sourcelocation.h ../lib/standards.h ../lib/summaries.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h
312312
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/summaries.cpp
313313

314-
$(libcppdir)/suppressions.o: ../lib/suppressions.cpp ../externals/tinyxml2/tinyxml2.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/mathlib.h ../lib/path.h ../lib/standards.h ../lib/suppressions.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
314+
$(libcppdir)/suppressions.o: ../lib/suppressions.cpp ../externals/tinyxml2/tinyxml2.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/filesettings.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/standards.h ../lib/suppressions.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
315315
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/suppressions.cpp
316316

317317
$(libcppdir)/templatesimplifier.o: ../lib/templatesimplifier.cpp ../lib/addoninfo.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/settings.h ../lib/standards.h ../lib/suppressions.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h

Diff for: test/testimportproject.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class TestImportProject : public TestFixture {
6969
}
7070

7171
void setDefines() const {
72-
FileSettings fs{""};
72+
FileSettings fs{"test.cpp"};
7373

7474
ImportProject::fsSetDefines(fs, "A");
7575
ASSERT_EQUALS("A=1", fs.defines);
@@ -85,7 +85,7 @@ class TestImportProject : public TestFixture {
8585
}
8686

8787
void setIncludePaths1() const {
88-
FileSettings fs{""};
88+
FileSettings fs{"test.cpp"};
8989
std::list<std::string> in(1, "../include");
9090
std::map<std::string, std::string, cppcheck::stricmp> variables;
9191
ImportProject::fsSetIncludePaths(fs, "abc/def/", in, variables);
@@ -94,7 +94,7 @@ class TestImportProject : public TestFixture {
9494
}
9595

9696
void setIncludePaths2() const {
97-
FileSettings fs{""};
97+
FileSettings fs{"test.cpp"};
9898
std::list<std::string> in(1, "$(SolutionDir)other");
9999
std::map<std::string, std::string, cppcheck::stricmp> variables;
100100
variables["SolutionDir"] = "c:/abc/";
@@ -104,7 +104,7 @@ class TestImportProject : public TestFixture {
104104
}
105105

106106
void setIncludePaths3() const { // macro names are case insensitive
107-
FileSettings fs{""};
107+
FileSettings fs{"test.cpp"};
108108
std::list<std::string> in(1, "$(SOLUTIONDIR)other");
109109
std::map<std::string, std::string, cppcheck::stricmp> variables;
110110
variables["SolutionDir"] = "c:/abc/";

Diff for: test/testsuppressions.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1194,18 +1194,18 @@ class TestSuppressions : public TestFixture {
11941194
SuppressionList::Suppression suppression("unusedFunction", "test.c", 3);
11951195
suppression.checked = true; // have to do this because fixes for #5704
11961196
ASSERT_EQUALS("", suppressions.addSuppression(std::move(suppression)));
1197-
ASSERT_EQUALS(true, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty());
1197+
ASSERT_EQUALS(true, !suppressions.getUnmatchedLocalSuppressions(FileWithDetails("test.c"), true).empty());
11981198
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(true).empty());
1199-
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty());
1199+
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions(FileWithDetails("test.c"), false).empty());
12001200
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(false).empty());
12011201
}
12021202

12031203
void globalsuppress_unusedFunction() const { // #4946 - wrong report of "unmatchedSuppression" for "unusedFunction"
12041204
SuppressionList suppressions;
12051205
ASSERT_EQUALS("", suppressions.addSuppressionLine("unusedFunction:*"));
1206-
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", true).empty());
1206+
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions(FileWithDetails("test.c"), true).empty());
12071207
ASSERT_EQUALS(true, !suppressions.getUnmatchedGlobalSuppressions(true).empty());
1208-
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions("test.c", false).empty());
1208+
ASSERT_EQUALS(false, !suppressions.getUnmatchedLocalSuppressions(FileWithDetails("test.c"), false).empty());
12091209
ASSERT_EQUALS(false, !suppressions.getUnmatchedGlobalSuppressions(false).empty());
12101210
}
12111211

0 commit comments

Comments
 (0)