Skip to content

Commit bba87f1

Browse files
committed
move (most of the) file size logic to FileWithDetails
1 parent aa1f62b commit bba87f1

7 files changed

Lines changed: 86 additions & 20 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
231231
$(libcppdir)/ctu.o \
232232
$(libcppdir)/errorlogger.o \
233233
$(libcppdir)/errortypes.o \
234+
$(libcppdir)/filesettings.o \
234235
$(libcppdir)/forwardanalyzer.o \
235236
$(libcppdir)/fwdanalysis.o \
236237
$(libcppdir)/importproject.o \
@@ -606,6 +607,9 @@ $(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h li
606607
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
607608
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
608609

610+
$(libcppdir)/filesettings.o: lib/filesettings.cpp lib/config.h lib/filesettings.h lib/path.h lib/platform.h lib/standards.h lib/utils.h
611+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp
612+
609613
$(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/forwardanalyzer.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueptr.h lib/vfvalue.h
610614
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
611615

cli/filelister.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
230230
}
231231
} else {
232232
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
233-
if (stat(new_path.c_str(), &file_stat) == -1) {
234-
const int err = errno;
235-
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
233+
files.emplace_back(new_path);
234+
std::string err = files.back().updateSize();
235+
if (!err.empty()) {
236+
return err;
236237
}
237-
files.emplace_back(new_path, file_stat.st_size);
238238
}
239239
}
240240
}

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<ClCompile Include="ctu.cpp" />
6767
<ClCompile Include="errorlogger.cpp" />
6868
<ClCompile Include="errortypes.cpp" />
69+
<ClCompile Include="filesettings.cpp" />
6970
<ClCompile Include="forwardanalyzer.cpp" />
7071
<ClCompile Include="fwdanalysis.cpp" />
7172
<ClCompile Include="importproject.cpp" />
@@ -165,6 +166,7 @@
165166
<ClInclude Include="errorlogger.h" />
166167
<ClInclude Include="errortypes.h" />
167168
<ClInclude Include="filesettings.h" />
169+
<ClInclude Include="filesettings.h" />
168170
<ClInclude Include="findtoken.h" />
169171
<ClInclude Include="forwardanalyzer.h" />
170172
<ClInclude Include="fwdanalysis.h" />

lib/filesettings.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2024 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "filesettings.h"
20+
21+
#include <sys/types.h>
22+
#include <sys/stat.h>
23+
#include <cerrno>
24+
25+
#ifdef _WIN32
26+
27+
#ifdef _WIN64
28+
29+
std::string FileWithDetails::updateSize() {
30+
struct _stati64 buf;
31+
if (_stati64(mPath.c_str(), &buf) < 0) {
32+
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
33+
}
34+
mSize = buf.st_size;
35+
return "";
36+
}
37+
38+
#else
39+
40+
std::string FileWithDetails::updateSize() {
41+
struct _stat buf;
42+
if (_stat(mPath.c_str(), &buf) < 0) {
43+
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
44+
}
45+
mSize = buf.st_size;
46+
return "";
47+
}
48+
49+
#endif
50+
51+
#else
52+
53+
std::string FileWithDetails::updateSize() {
54+
struct stat buf;
55+
if (stat(mPath.c_str(), &buf) < 0) {
56+
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
57+
}
58+
mSize = buf.st_size;
59+
return "";
60+
}
61+
62+
#endif

lib/filesettings.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class FileWithDetails
5959
{
6060
return mSize;
6161
}
62+
63+
std::string updateSize();
64+
6265
private:
6366
std::string mPath;
6467
std::string mPathSimplified;
@@ -90,6 +93,12 @@ struct CPPCHECKLIB FileSettings {
9093
{
9194
return file.size();
9295
}
96+
97+
std::string updateFileSize()
98+
{
99+
return file.updateSize();
100+
}
101+
93102
std::string defines;
94103
// TODO: handle differently
95104
std::string cppcheckDefines() const {

lib/importproject.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,21 +1509,6 @@ bool ImportProject::sourceFileExists(const std::string &file)
15091509
void ImportProject::setFileSizes()
15101510
{
15111511
for (auto &fs : fileSettings) {
1512-
int statResult;
1513-
#ifdef _WIN32
1514-
#ifdef _WIN64
1515-
struct _stati64 statBuf;
1516-
statResult = _stati64(fs.filename().c_str(), &statBuf);
1517-
#else
1518-
struct _stat statBuf;
1519-
statResult = _stat(fs.filename().c_str(), &statBuf);
1520-
#endif
1521-
#else
1522-
struct stat statBuf;
1523-
statResult = stat(fs.filename().c_str(), &statBuf);
1524-
#endif
1525-
if (statResult == 0) {
1526-
fs.file = FileWithDetails(fs.file.path(), statBuf.st_size);
1527-
}
1512+
fs.updateFileSize();
15281513
}
15291514
}

oss-fuzz/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
7474
$(libcppdir)/ctu.o \
7575
$(libcppdir)/errorlogger.o \
7676
$(libcppdir)/errortypes.o \
77+
$(libcppdir)/filesettings.o \
7778
$(libcppdir)/forwardanalyzer.o \
7879
$(libcppdir)/fwdanalysis.o \
7980
$(libcppdir)/importproject.o \
@@ -293,6 +294,9 @@ $(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml
293294
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h
294295
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp
295296

297+
$(libcppdir)/filesettings.o: ../lib/filesettings.cpp ../lib/config.h ../lib/filesettings.h ../lib/path.h ../lib/platform.h ../lib/standards.h ../lib/utils.h
298+
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp
299+
296300
$(libcppdir)/forwardanalyzer.o: ../lib/forwardanalyzer.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/color.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/forwardanalyzer.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/valueptr.h ../lib/vfvalue.h
297301
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp
298302

0 commit comments

Comments
 (0)