Skip to content

Commit 06817e0

Browse files
committed
improved testing of missing includes / made it possible to clear the include cache in simplecpp
1 parent b097eca commit 06817e0

File tree

8 files changed

+401
-10
lines changed

8 files changed

+401
-10
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ CLIOBJ = cli/cmdlineparser.o \
264264
cli/threadexecutor.o
265265

266266
TESTOBJ = test/fixture.o \
267+
test/helpers.o \
267268
test/main.o \
268269
test/options.o \
269270
test/test64bit.o \
@@ -659,6 +660,9 @@ cli/threadexecutor.o: cli/threadexecutor.cpp cli/cppcheckexecutor.h cli/executor
659660
test/fixture.o: test/fixture.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h test/fixture.h test/options.h test/redirect.h
660661
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp
661662

663+
test/helpers.o: test/helpers.cpp lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.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/vfvalue.h test/helpers.h
664+
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/helpers.cpp
665+
662666
test/main.o: test/main.cpp externals/simplecpp/simplecpp.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/preprocessor.h lib/suppressions.h test/fixture.h test/options.h
663667
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/main.cpp
664668

@@ -770,7 +774,7 @@ test/testplatform.o: test/testplatform.cpp externals/tinyxml2/tinyxml2.h lib/col
770774
test/testpostfixoperator.o: test/testpostfixoperator.cpp lib/check.h lib/checkpostfixoperator.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/vfvalue.h test/fixture.h
771775
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testpostfixoperator.cpp
772776

773-
test/testpreprocessor.o: test/testpreprocessor.cpp externals/simplecpp/simplecpp.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/preprocessor.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/vfvalue.h test/fixture.h test/helpers.h
777+
test/testpreprocessor.o: test/testpreprocessor.cpp externals/simplecpp/simplecpp.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.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/vfvalue.h test/fixture.h test/helpers.h
774778
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testpreprocessor.cpp
775779

776780
test/testprocessexecutor.o: test/testprocessexecutor.cpp cli/executor.h cli/processexecutor.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/vfvalue.h test/fixture.h test/helpers.h test/redirect.h

lib/preprocessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf
626626
dui.std = mSettings.standards.getCPP();
627627
else
628628
dui.std = mSettings.standards.getC();
629+
dui.clearIncludeCache = mSettings.clearIncludeCache;
629630
return dui;
630631
}
631632

lib/settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Settings::Settings()
4444
clang(false),
4545
clangExecutable("clang"),
4646
clangTidy(false),
47+
clearIncludeCache(false),
4748
daca(false),
4849
debugnormal(false),
4950
debugSimplified(false),

lib/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
150150
/** Use clang-tidy */
151151
bool clangTidy;
152152

153+
/** Internal: Clear the simplecpp non-existing include cache */
154+
bool clearIncludeCache;
155+
153156
/** @brief include paths excluded from checking the configuration */
154157
std::set<std::string> configExcludePaths;
155158

test/helpers.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 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 "helpers.h"
20+
21+
#include "path.h"
22+
23+
#ifdef _WIN32
24+
#include <windows.h>
25+
#else
26+
#include <sys/stat.h>
27+
#include <sys/types.h>
28+
#include <unistd.h>
29+
#endif
30+
31+
ScopedFile::ScopedFile(std::string name, const std::string &content, std::string path)
32+
: mName(std::move(name))
33+
, mPath(Path::toNativeSeparators(std::move(path)))
34+
, mFullPath(Path::join(mPath, mName))
35+
{
36+
if (!mPath.empty() && mPath != Path::getCurrentPath()) {
37+
#ifdef _WIN32
38+
if (!CreateDirectoryA(mPath.c_str(), nullptr))
39+
throw std::runtime_error("ScopedFile(" + mFullPath + ") - could not create directory");
40+
#else
41+
if (mkdir(mPath.c_str(), S_IRUSR | S_IWUSR) != 0)
42+
throw std::runtime_error("ScopedFile(" + mFullPath + ") - could not create directory");
43+
#endif
44+
}
45+
46+
std::ofstream of(mFullPath);
47+
if (!of.is_open())
48+
throw std::runtime_error("ScopedFile(" + mFullPath + ") - could not open file");
49+
of << content;
50+
}
51+
52+
ScopedFile::~ScopedFile() {
53+
std::remove(mFullPath.c_str());
54+
if (!mPath.empty() && mPath != Path::getCurrentPath()) {
55+
#ifdef _WIN32
56+
RemoveDirectoryA(mPath.c_str());
57+
#else
58+
rmdir(mPath.c_str());
59+
#endif
60+
}
61+
}

test/helpers.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ class SimpleSuppressor : public ErrorLogger {
7474

7575
class ScopedFile {
7676
public:
77-
ScopedFile(std::string name, const std::string &content) : mName(std::move(name)) {
78-
std::ofstream of(mName);
79-
of << content;
80-
}
77+
ScopedFile(std::string name, const std::string &content, std::string path = "");
78+
~ScopedFile();
8179

82-
~ScopedFile() {
83-
remove(mName.c_str());
80+
const std::string& path() const
81+
{
82+
return mFullPath;
8483
}
8584
private:
86-
std::string mName;
85+
const std::string mName;
86+
const std::string mPath;
87+
const std::string mFullPath;
8788
};
8889

8990
#endif // helpersH

0 commit comments

Comments
 (0)