Skip to content

Commit

Permalink
move (most of the) file size logic to FileWithDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Nov 11, 2024
1 parent 1cffe12 commit de9c5a7
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/ctu.o \
$(libcppdir)/errorlogger.o \
$(libcppdir)/errortypes.o \
$(libcppdir)/filesettings.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/importproject.o \
Expand Down Expand Up @@ -605,6 +606,9 @@ $(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h li
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp

$(libcppdir)/filesettings.o: lib/filesettings.cpp lib/config.h lib/filesettings.h lib/path.h lib/platform.h lib/standards.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp

$(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
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp

Expand Down
8 changes: 4 additions & 4 deletions cli/filelister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ static std::string addFiles2(std::list<FileWithDetails> &files,
}
} else {
if (Path::acceptFile(new_path, extra) && !ignored.match(new_path)) {
if (stat(new_path.c_str(), &file_stat) == -1) {
const int err = errno;
return "could not stat file '" + new_path + "' (errno: " + std::to_string(err) + ")";
files.emplace_back(new_path);
std::string err = files.back().updateSize();
if (!err.empty()) {
return err;
}
files.emplace_back(new_path, file_stat.st_size);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ClCompile Include="ctu.cpp" />
<ClCompile Include="errorlogger.cpp" />
<ClCompile Include="errortypes.cpp" />
<ClCompile Include="filesettings.cpp" />
<ClCompile Include="forwardanalyzer.cpp" />
<ClCompile Include="fwdanalysis.cpp" />
<ClCompile Include="importproject.cpp" />
Expand Down Expand Up @@ -164,6 +165,7 @@
<ClInclude Include="errorlogger.h" />
<ClInclude Include="errortypes.h" />
<ClInclude Include="filesettings.h" />
<ClInclude Include="filesettings.h" />
<ClInclude Include="findtoken.h" />
<ClInclude Include="forwardanalyzer.h" />
<ClInclude Include="fwdanalysis.h" />
Expand Down
62 changes: 62 additions & 0 deletions lib/filesettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "filesettings.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>

#ifdef _WIN32

#ifdef _WIN64

std::string FileWithDetails::updateSize() {
struct _stati64 buf;
if (_stati64(mPath.c_str(), &buf) < 0) {
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
}
mSize = buf.st_size;
return "";
}

#else

std::string FileWithDetails::updateSize() {
struct _stat buf;
if (_stat(mPath.c_str(), &buf) < 0) {
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
}
mSize = buf.st_size;
return "";
}

#endif

#else

std::string FileWithDetails::updateSize() {
struct stat buf;
if (stat(mPath.c_str(), &buf) < 0) {
return "could not stat file '" + mPath + "': (errno: " + std::to_string(errno) + ")";
}
mSize = buf.st_size;
return "";
}

#endif
9 changes: 9 additions & 0 deletions lib/filesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class FileWithDetails
{
return mSize;
}

std::string updateSize();

private:
std::string mPath;
std::string mPathSimplified;
Expand Down Expand Up @@ -90,6 +93,12 @@ struct CPPCHECKLIB FileSettings {
{
return file.size();
}

std::string updateFileSize()
{
return file.updateSize();
}

std::string defines;
// TODO: handle differently
std::string cppcheckDefines() const {
Expand Down
17 changes: 1 addition & 16 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,21 +1509,6 @@ bool ImportProject::sourceFileExists(const std::string &file)
void ImportProject::setFileSizes()
{
for (auto &fs : fileSettings) {
int statResult;
#ifdef _WIN32
#ifdef _WIN64
struct _stati64 statBuf;
statResult = _stati64(fs.filename().c_str(), &statBuf);
#else
struct _stat statBuf;
statResult = _stat(fs.filename().c_str(), &statBuf);
#endif
#else
struct stat statBuf;
statResult = stat(fs.filename().c_str(), &statBuf);
#endif
if (statResult == 0) {
fs.file = FileWithDetails(fs.file.path(), statBuf.st_size);
}
fs.updateFileSize();
}
}
2 changes: 2 additions & 0 deletions lib/lib.pri
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ HEADERS += $${PWD}/addoninfo.h \
$${PWD}/errorlogger.h \
$${PWD}/errortypes.h \
$${PWD}/filesettings.h \
$${PWD}/filesettings.h \
$${PWD}/findtoken.h \
$${PWD}/forwardanalyzer.h \
$${PWD}/fwdanalysis.h \
Expand Down Expand Up @@ -154,6 +155,7 @@ SOURCES += $${PWD}/valueflow.cpp \
$${PWD}/ctu.cpp \
$${PWD}/errorlogger.cpp \
$${PWD}/errortypes.cpp \
$${PWD}/filesettings.cpp \
$${PWD}/forwardanalyzer.cpp \
$${PWD}/fwdanalysis.cpp \
$${PWD}/importproject.cpp \
Expand Down
4 changes: 4 additions & 0 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/ctu.o \
$(libcppdir)/errorlogger.o \
$(libcppdir)/errortypes.o \
$(libcppdir)/filesettings.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/importproject.o \
Expand Down Expand Up @@ -292,6 +293,9 @@ $(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errortypes.cpp

$(libcppdir)/filesettings.o: ../lib/filesettings.cpp ../lib/config.h ../lib/filesettings.h ../lib/path.h ../lib/platform.h ../lib/standards.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/filesettings.cpp

$(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
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/forwardanalyzer.cpp

Expand Down

0 comments on commit de9c5a7

Please sign in to comment.