removed ThreadExecutor::addFileContent() (#3865)

This commit is contained in:
Oliver Stöneberg 2022-03-02 11:10:29 +01:00 committed by GitHub
parent 4a63af02ed
commit de728f472c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 35 deletions

View File

@ -740,13 +740,13 @@ test/testsuite.o: test/testsuite.cpp lib/color.h lib/config.h lib/errorlogger.h
test/testsummaries.o: test/testsummaries.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testsummaries.o: test/testsummaries.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsummaries.o test/testsummaries.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsummaries.o test/testsummaries.cpp
test/testsuppressions.o: test/testsuppressions.cpp cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/testsuite.h test/testsuppressions.o: test/testsuppressions.cpp cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuppressions.o test/testsuppressions.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsuppressions.o test/testsuppressions.cpp
test/testsymboldatabase.o: test/testsymboldatabase.cpp externals/tinyxml2/tinyxml2.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h test/testsymboldatabase.o: test/testsymboldatabase.cpp externals/tinyxml2/tinyxml2.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsymboldatabase.o test/testsymboldatabase.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testsymboldatabase.o test/testsymboldatabase.cpp
test/testthreadexecutor.o: test/testthreadexecutor.cpp cli/threadexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h test/testsuite.h test/testthreadexecutor.o: test/testthreadexecutor.cpp cli/threadexecutor.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h test/testsuite.h test/testutils.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testthreadexecutor.o test/testthreadexecutor.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CPPFILESDIR) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o test/testthreadexecutor.o test/testthreadexecutor.cpp
test/testtimer.o: test/testtimer.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h lib/timer.h test/testsuite.h test/testtimer.o: test/testtimer.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h lib/timer.h test/testsuite.h

View File

@ -67,12 +67,6 @@ ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files,
ThreadExecutor::~ThreadExecutor() ThreadExecutor::~ThreadExecutor()
{} {}
// cppcheck-suppress unusedFunction - only used in unit tests
void ThreadExecutor::addFileContent(const std::string &path, const std::string &content)
{
mFileContents[path] = content;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
////// This code is for platforms that support fork() only //////////////////// ////// This code is for platforms that support fork() only ////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -285,9 +279,6 @@ unsigned int ThreadExecutor::check()
if (iFileSettings != mSettings.project.fileSettings.end()) { if (iFileSettings != mSettings.project.fileSettings.end()) {
resultOfCheck = fileChecker.check(*iFileSettings); resultOfCheck = fileChecker.check(*iFileSettings);
} else if (!mFileContents.empty() && mFileContents.find(iFile->first) != mFileContents.end()) {
// File content was given as a string
resultOfCheck = fileChecker.check(iFile->first, mFileContents[iFile->first]);
} else { } else {
// Read file from a file // Read file from a file
resultOfCheck = fileChecker.check(iFile->first); resultOfCheck = fileChecker.check(iFile->first);
@ -538,14 +529,8 @@ unsigned int STDCALL ThreadExecutor::threadProc(LogWriter* logWriter)
logWriter->mFileSync.unlock(); logWriter->mFileSync.unlock();
const std::map<std::string, std::string>::const_iterator fileContent = logWriter->mThreadExecutor.mFileContents.find(file);
if (fileContent != logWriter->mThreadExecutor.mFileContents.end()) {
// File content was given as a string
result += fileChecker.check(file, fileContent->second);
} else {
// Read file from a file // Read file from a file
result += fileChecker.check(file); result += fileChecker.check(file);
}
} else { // file settings.. } else { // file settings..
const ImportProject::FileSettings &fs = *itFileSettings; const ImportProject::FileSettings &fs = *itFileSettings;
++itFileSettings; ++itFileSettings;

View File

@ -44,24 +44,12 @@ public:
void operator=(const ThreadExecutor &) = delete; void operator=(const ThreadExecutor &) = delete;
unsigned int check(); unsigned int check();
/**
* @brief Add content to a file, to be used in unit testing.
*
* @param path File name (used as a key to link with real file).
* @param content If the file would be a real file, this should be
* the content of the file.
*/
void addFileContent(const std::string &path, const std::string &content);
private: private:
const std::map<std::string, std::size_t> &mFiles; const std::map<std::string, std::size_t> &mFiles;
Settings &mSettings; Settings &mSettings;
ErrorLogger &mErrorLogger; ErrorLogger &mErrorLogger;
std::list<std::string> mErrorList; std::list<std::string> mErrorList;
/** @brief Key is file name, and value is the content of the file */
std::map<std::string, std::string> mFileContents;
#if defined(THREADING_MODEL_FORK) #if defined(THREADING_MODEL_FORK)
/** /**

View File

@ -22,10 +22,12 @@
#include "settings.h" #include "settings.h"
#include "suppressions.h" #include "suppressions.h"
#include "testsuite.h" #include "testsuite.h"
#include "testutils.h"
#include "threadexecutor.h" #include "threadexecutor.h"
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstring>
#include <functional> #include <functional>
#include <iosfwd> #include <iosfwd>
#include <list> #include <list>
@ -216,7 +218,7 @@ private:
output.str(""); output.str("");
std::map<std::string, std::size_t> files; std::map<std::string, std::size_t> files;
files["test.cpp"] = 1; files["test.cpp"] = strlen(code);
Settings settings; Settings settings;
settings.jobs = 1; settings.jobs = 1;
@ -226,8 +228,10 @@ private:
EXPECT_EQ("", settings.nomsg.addSuppressionLine(suppression)); EXPECT_EQ("", settings.nomsg.addSuppressionLine(suppression));
} }
ThreadExecutor executor(files, settings, *this); ThreadExecutor executor(files, settings, *this);
std::vector<ScopedFile> scopedfiles;
scopedfiles.reserve(files.size());
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i)
executor.addFileContent(i->first, code); scopedfiles.emplace_back(i->first, code);
const unsigned int exitCode = executor.check(); const unsigned int exitCode = executor.check();

View File

@ -18,6 +18,7 @@
#include "settings.h" #include "settings.h"
#include "testsuite.h" #include "testsuite.h"
#include "testutils.h"
#include "threadexecutor.h" #include "threadexecutor.h"
#include <cstddef> #include <cstddef>
@ -49,13 +50,15 @@ private:
for (int i = 1; i <= files; ++i) { for (int i = 1; i <= files; ++i) {
std::ostringstream oss; std::ostringstream oss;
oss << "file_" << i << ".cpp"; oss << "file_" << i << ".cpp";
filemap[oss.str()] = 1; filemap[oss.str()] = data.size();
} }
settings.jobs = jobs; settings.jobs = jobs;
ThreadExecutor executor(filemap, settings, *this); ThreadExecutor executor(filemap, settings, *this);
std::vector<ScopedFile> scopedfiles;
scopedfiles.reserve(filemap.size());
for (std::map<std::string, std::size_t>::const_iterator i = filemap.begin(); i != filemap.end(); ++i) for (std::map<std::string, std::size_t>::const_iterator i = filemap.begin(); i != filemap.end(); ++i)
executor.addFileContent(i->first, data); scopedfiles.emplace_back(i->first, data);
ASSERT_EQUALS(result, executor.check()); ASSERT_EQUALS(result, executor.check());
} }

View File

@ -26,6 +26,7 @@
#include "tokenize.h" #include "tokenize.h"
#include "tokenlist.h" #include "tokenlist.h"
#include <cstdio>
#include <iosfwd> #include <iosfwd>
#include <list> #include <list>
#include <string> #include <string>
@ -69,4 +70,18 @@ private:
ErrorLogger *next; ErrorLogger *next;
}; };
class ScopedFile {
public:
ScopedFile(const std::string &name, const std::string &content) : mName(name) {
std::ofstream of(mName);
of << content;
}
~ScopedFile() {
remove(mName.c_str());
}
private:
std::string mName;
};
#endif // TestUtilsH #endif // TestUtilsH