moved test-only `Preprocessor::getcode()` out of `lib` (#4993)

This commit is contained in:
Oliver Stöneberg 2023-04-22 10:22:00 +02:00 committed by GitHub
parent 5c7914aaa6
commit db955a13a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 128 additions and 99 deletions

View File

@ -670,7 +670,7 @@ cli/threadexecutor.o: cli/threadexecutor.cpp cli/cppcheckexecutor.h cli/executor
test/fixture.o: test/fixture.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h test/fixture.h test/options.h test/redirect.h test/fixture.o: test/fixture.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/suppressions.h test/fixture.h test/options.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp
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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/helpers.h test/helpers.o: test/helpers.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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/helpers.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/helpers.cpp
test/main.o: test/main.cpp externals/simplecpp/simplecpp.h lib/check.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 test/main.o: test/main.cpp externals/simplecpp/simplecpp.h lib/check.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
@ -838,7 +838,7 @@ test/testtimer.o: test/testtimer.cpp lib/check.h lib/color.h lib/config.h lib/er
test/testtoken.o: test/testtoken.cpp lib/check.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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h test/testtoken.o: test/testtoken.cpp lib/check.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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtoken.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtoken.cpp
test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/check.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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/check.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/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenize.cpp $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenize.cpp
test/testtokenlist.o: test/testtokenlist.cpp lib/check.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/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/testtokenlist.o: test/testtokenlist.cpp lib/check.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/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h

View File

@ -627,7 +627,7 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf
return dui; return dui;
} }
static bool hasErrors(const simplecpp::OutputList &outputList) bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
{ {
for (simplecpp::OutputList::const_iterator it = outputList.cbegin(); it != outputList.cend(); ++it) { for (simplecpp::OutputList::const_iterator it = outputList.cbegin(); it != outputList.cend(); ++it) {
switch (it->type) { switch (it->type) {
@ -752,36 +752,6 @@ std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std
return ret.str(); return ret.str();
} }
std::string Preprocessor::getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression)
{
simplecpp::OutputList outputList;
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens1(istr, files, Path::simplifyPath(filename), &outputList);
if (inlineSuppression)
inlineSuppressions(tokens1, *inlineSuppression);
tokens1.removeComments();
removeComments();
setDirectives(tokens1);
reportOutput(outputList, true);
if (hasErrors(outputList))
return "";
std::string ret;
try {
ret = getcode(tokens1, cfg, files, filedata.find("#file") != std::string::npos);
// Since "files" is a local variable the tracking info must be cleared..
mMacroUsage.clear();
mIfCond.clear();
} catch (const simplecpp::Output &) {
ret.clear();
}
return ret;
}
void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror) void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror)
{ {
for (const simplecpp::Output &out : outputList) { for (const simplecpp::Output &out : outputList) {

View File

@ -70,6 +70,10 @@ public:
* configurations that exist in a source file. * configurations that exist in a source file.
*/ */
class CPPCHECKLIB Preprocessor { class CPPCHECKLIB Preprocessor {
// TODO: get rid of this
friend class PreprocessorHelper;
friend class TestPreprocessor;
public: public:
/** /**
@ -145,18 +149,6 @@ public:
std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations); std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector<std::string> &files, const bool writeLocations);
/**
* Get preprocessed code for a given configuration
*
* Note: for testing only.
*
* @param filedata file data including preprocessing 'if', 'define', etc
* @param cfg configuration to read out
* @param filename name of source file
* @param inlineSuppression the inline suppressions
*/
std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression = nullptr);
/** /**
* Calculate HASH. Using toolinfo, tokens1, filedata. * Calculate HASH. Using toolinfo, tokens1, filedata.
* *
@ -192,6 +184,8 @@ private:
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType); void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
void error(const std::string &filename, unsigned int linenr, const std::string &msg); void error(const std::string &filename, unsigned int linenr, const std::string &msg);
static bool hasErrors(const simplecpp::OutputList &outputList);
const Settings& mSettings; const Settings& mSettings;
ErrorLogger *mErrorLogger; ErrorLogger *mErrorLogger;

View File

@ -19,6 +19,7 @@
#include "helpers.h" #include "helpers.h"
#include "path.h" #include "path.h"
#include "preprocessor.h"
#include <cstdio> #include <cstdio>
#include <stdexcept> #include <stdexcept>
@ -31,6 +32,8 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include <simplecpp.h>
ScopedFile::ScopedFile(std::string name, const std::string &content, std::string path) ScopedFile::ScopedFile(std::string name, const std::string &content, std::string path)
: mName(std::move(name)) : mName(std::move(name))
, mPath(Path::toNativeSeparators(std::move(path))) , mPath(Path::toNativeSeparators(std::move(path)))
@ -62,3 +65,37 @@ ScopedFile::~ScopedFile() {
#endif #endif
} }
} }
// TODO: we should be using the actual Preprocessor implementation
std::string PreprocessorHelper::getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression)
{
simplecpp::OutputList outputList;
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens1(istr, files, Path::simplifyPath(filename), &outputList);
if (inlineSuppression)
preprocessor.inlineSuppressions(tokens1, *inlineSuppression);
tokens1.removeComments();
preprocessor.simplifyPragmaAsm(&tokens1);
preprocessor.removeComments();
preprocessor.setDirectives(tokens1);
preprocessor.reportOutput(outputList, true);
if (Preprocessor::hasErrors(outputList))
return "";
std::string ret;
try {
ret = preprocessor.getcode(tokens1, cfg, files, filedata.find("#file") != std::string::npos);
} catch (const simplecpp::Output &) {
ret.clear();
}
// Since "files" is a local variable the tracking info must be cleared..
preprocessor.mMacroUsage.clear();
preprocessor.mIfCond.clear();
return ret;
}

View File

@ -31,8 +31,8 @@
#include <sstream> // IWYU pragma: keep #include <sstream> // IWYU pragma: keep
#include <string> #include <string>
class Token; class Token;
class Processor;
class givenACodeSampleToTokenize { class givenACodeSampleToTokenize {
private: private:
@ -92,4 +92,20 @@ private:
const std::string mFullPath; const std::string mFullPath;
}; };
class PreprocessorHelper
{
public:
/**
* Get preprocessed code for a given configuration
*
* Note: for testing only.
*
* @param filedata file data including preprocessing 'if', 'define', etc
* @param cfg configuration to read out
* @param filename name of source file
* @param inlineSuppression the inline suppressions
*/
static std::string getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression = nullptr);
};
#endif // helpersH #endif // helpersH

View File

@ -268,25 +268,36 @@ private:
TEST_CASE(testMissingIncludeCheckConfig); TEST_CASE(testMissingIncludeCheckConfig);
} }
// TODO: we should be calling the actual Preprocessor::preprocess() implementation // TODO: merge with `PreprocessorHelper::getcode()`
void preprocess(const char* code, std::map<std::string, std::string>& actual, const char filename[] = "file.c") { void preprocess(const char* code, std::map<std::string, std::string>& actual, const char filename[] = "file.c") {
errout.str(""); errout.str("");
std::istringstream istr(code); std::istringstream istr(code);
simplecpp::OutputList outputList; simplecpp::OutputList outputList;
std::vector<std::string> files; std::vector<std::string> files;
simplecpp::TokenList tokens(istr, files, filename, &outputList); simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
tokens.removeComments(); tokens.removeComments();
preprocessor0.simplifyPragmaAsm(&tokens); preprocessor0.simplifyPragmaAsm(&tokens);
const std::set<std::string> configs(preprocessor0.getConfigs(tokens)); preprocessor0.removeComments();
preprocessor0.setDirectives(tokens); preprocessor0.setDirectives(tokens);
preprocessor0.reportOutput(outputList, true);
if (Preprocessor::hasErrors(outputList))
return;
const std::set<std::string> configs(preprocessor0.getConfigs(tokens));
for (const std::string & config : configs) { for (const std::string & config : configs) {
try { try {
const std::string &cfgcode = preprocessor0.getcode(tokens, config, files, std::string(code).find("#file") != std::string::npos); const std::string &cfgcode = preprocessor0.getcode(tokens, config, files, std::string(code).find("#file") != std::string::npos);
actual[config] = cfgcode; actual[config] = cfgcode;
} catch (const simplecpp::Output &) { } catch (const simplecpp::Output &) {
actual[config] = ""; actual[config] = "";
} catch (...) {} }
} }
// Since "files" is a local variable the tracking info must be cleared..
preprocessor0.mMacroUsage.clear();
preprocessor0.mIfCond.clear();
} }
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) { std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
@ -363,7 +374,7 @@ private:
settings.userDefines = "__cplusplus"; settings.userDefines = "__cplusplus";
Preprocessor preprocessor(settings, this); Preprocessor preprocessor(settings, this);
const std::string code("#error hello world!\n"); const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "X", "test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str()); ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
} }
@ -376,7 +387,7 @@ private:
settings.userDefines = "TEST"; settings.userDefines = "TEST";
Preprocessor preprocessor(settings, this); Preprocessor preprocessor(settings, this);
const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile"); const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile");
preprocessor.getcode(code, "TEST", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "TEST", "test.c");
ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout.str()); ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout.str());
} }
@ -387,7 +398,7 @@ private:
settings.userDefines = "TEST"; settings.userDefines = "TEST";
Preprocessor preprocessor(settings, this); Preprocessor preprocessor(settings, this);
const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa"); const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa");
preprocessor.getcode(code, "TEST", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "TEST", "test.c");
ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout.str()); ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout.str());
} }
} }
@ -399,7 +410,7 @@ private:
settings.force = true; // No message if --force is given settings.force = true; // No message if --force is given
Preprocessor preprocessor(settings, this); Preprocessor preprocessor(settings, this);
const std::string code("#error hello world!\n"); const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "X", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -1212,7 +1223,7 @@ private:
"#undef z\n" "#undef z\n"
"int z;\n" "int z;\n"
"z = 0;\n"; "z = 0;\n";
ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", preprocessor0.getcode(filedata, "", "")); ASSERT_EQUALS("\n\nint z ;\nz = 0 ;", PreprocessorHelper::getcode(preprocessor0, filedata, "", ""));
} }
} }
@ -1476,7 +1487,7 @@ private:
preprocess(filedata, actual); preprocess(filedata, actual);
ASSERT_EQUALS("", actual[""]); ASSERT_EQUALS("", actual[""]);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[file.c:2]: (error) No pair for character ('). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
} }
} }
@ -1651,14 +1662,14 @@ private:
"#if A\n" "#if A\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
{ {
const char filedata[] = "#define A 1\n" const char filedata[] = "#define A 1\n"
"#if A==1\n" "#if A==1\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
} }
@ -1668,7 +1679,7 @@ private:
"#if (B==A) || (B==C)\n" "#if (B==A) || (B==C)\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
void define_if3() { void define_if3() {
@ -1676,7 +1687,7 @@ private:
"#if (A==0)\n" "#if (A==0)\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
void define_if4() { void define_if4() {
@ -1684,7 +1695,7 @@ private:
"#if X==123\n" "#if X==123\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
void define_if5() { // #4516 - #define B (A & 0x00f0) void define_if5() { // #4516 - #define B (A & 0x00f0)
@ -1694,7 +1705,7 @@ private:
"#if B==0x0010\n" "#if B==0x0010\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
{ {
const char filedata[] = "#define A 0x00f0\n" const char filedata[] = "#define A 0x00f0\n"
@ -1703,14 +1714,14 @@ private:
"#if C==0x0010\n" "#if C==0x0010\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\n\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\n\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
{ {
const char filedata[] = "#define A (1+A)\n" // don't hang for recursive macros const char filedata[] = "#define A (1+A)\n" // don't hang for recursive macros
"#if A==1\n" "#if A==1\n"
"FOO\n" "FOO\n"
"#endif"; "#endif";
ASSERT_EQUALS("\n\nFOO", preprocessor0.getcode(filedata,"","")); ASSERT_EQUALS("\n\nFOO", PreprocessorHelper::getcode(preprocessor0, filedata,"",""));
} }
} }
@ -1726,10 +1737,10 @@ private:
"#if B >= 0\n" "#if B >= 0\n"
"456\n" "456\n"
"#endif\n"; "#endif\n";
const std::string actualA0 = preprocessor0.getcode(filedata, "A=0", "test.c"); const std::string actualA0 = PreprocessorHelper::getcode(preprocessor0, filedata, "A=0", "test.c");
ASSERT_EQUALS(true, actualA0.find("123") != std::string::npos); ASSERT_EQUALS(true, actualA0.find("123") != std::string::npos);
ASSERT_EQUALS(false, actualA0.find("456") != std::string::npos); ASSERT_EQUALS(false, actualA0.find("456") != std::string::npos);
const std::string actualA1 = preprocessor0.getcode(filedata, "A=1", "test.c"); const std::string actualA1 = PreprocessorHelper::getcode(preprocessor0, filedata, "A=1", "test.c");
ASSERT_EQUALS(false, actualA1.find("123") != std::string::npos); ASSERT_EQUALS(false, actualA1.find("123") != std::string::npos);
ASSERT_EQUALS(true, actualA1.find("456") != std::string::npos); ASSERT_EQUALS(true, actualA1.find("456") != std::string::npos);
} }
@ -1839,8 +1850,8 @@ private:
"B me;\n"; "B me;\n";
// Preprocess => actual result.. // Preprocess => actual result..
ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", preprocessor0.getcode(filedata, "", "a.cpp")); ASSERT_EQUALS("\n\n\n\n\n\n$int me ;", PreprocessorHelper::getcode(preprocessor0, filedata, "", "a.cpp"));
ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", preprocessor0.getcode(filedata, "A", "a.cpp")); ASSERT_EQUALS("\n\n\n\n\n\n$char me ;", PreprocessorHelper::getcode(preprocessor0, filedata, "A", "a.cpp"));
} }
void ifndef_define() { void ifndef_define() {
@ -1864,8 +1875,8 @@ private:
"#endif\n"; "#endif\n";
// Preprocess => actual result.. // Preprocess => actual result..
ASSERT_EQUALS("", preprocessor0.getcode(filedata, "", "a.cpp")); ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata, "", "a.cpp"));
ASSERT_EQUALS("", preprocessor0.getcode(filedata, "A", "a.cpp")); ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, filedata, "A", "a.cpp"));
} }
void redundant_config() { void redundant_config() {
@ -1958,7 +1969,7 @@ private:
"// cppcheck-suppress missingIncludeSystem\n" "// cppcheck-suppress missingIncludeSystem\n"
"#include <missing2.h>\n"); "#include <missing2.h>\n");
Suppressions inlineSuppr; Suppressions inlineSuppr;
preprocessor.getcode(code, "", "test.c", &inlineSuppr); PreprocessorHelper::getcode(preprocessor, code, "", "test.c", &inlineSuppr);
auto suppressions = inlineSuppr.getSuppressions(); auto suppressions = inlineSuppr.getSuppressions();
ASSERT_EQUALS(2, suppressions.size()); ASSERT_EQUALS(2, suppressions.size());
@ -1984,7 +1995,7 @@ private:
const std::string src("#if defined X || Y\n" const std::string src("#if defined X || Y\n"
"Fred & Wilma\n" "Fred & Wilma\n"
"#endif\n"); "#endif\n");
std::string actual = preprocessor0.getcode(src, "X=1", "test.c"); std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1", "test.c");
ASSERT_EQUALS("\nFred & Wilma", actual); ASSERT_EQUALS("\nFred & Wilma", actual);
} }
@ -1994,12 +2005,12 @@ private:
"Fred & Wilma\n" "Fred & Wilma\n"
"#endif\n"); "#endif\n");
{ {
std::string actual = preprocessor0.getcode(src, "X=1", "test.c"); std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1", "test.c");
ASSERT_EQUALS("", actual); ASSERT_EQUALS("", actual);
} }
{ {
std::string actual = preprocessor0.getcode(src, "X=1;Y=2", "test.c"); std::string actual = PreprocessorHelper::getcode(preprocessor0, src, "X=1;Y=2", "test.c");
ASSERT_EQUALS("\nFred & Wilma", actual); ASSERT_EQUALS("\nFred & Wilma", actual);
} }
} }
@ -2011,34 +2022,34 @@ private:
"#if (X == Y)\n" "#if (X == Y)\n"
"Fred & Wilma\n" "Fred & Wilma\n"
"#endif\n"; "#endif\n";
const std::string actual = preprocessor0.getcode(code, "TEST", "test.c"); const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "TEST", "test.c");
ASSERT_EQUALS("\n\n\nFred & Wilma", actual); ASSERT_EQUALS("\n\n\nFred & Wilma", actual);
} }
void predefine4() { void predefine4() {
// #3577 // #3577
const char code[] = "char buf[X];\n"; const char code[] = "char buf[X];\n";
const std::string actual = preprocessor0.getcode(code, "X=123", "test.c"); const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "X=123", "test.c");
ASSERT_EQUALS("char buf [ $123 ] ;", actual); ASSERT_EQUALS("char buf [ $123 ] ;", actual);
} }
void predefine5() { // #3737, #5119 - automatically define __cplusplus void predefine5() { // #3737, #5119 - automatically define __cplusplus
// #3737... // #3737...
const char code[] = "#ifdef __cplusplus\n123\n#endif"; const char code[] = "#ifdef __cplusplus\n123\n#endif";
ASSERT_EQUALS("", preprocessor0.getcode(code, "", "test.c")); ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, code, "", "test.c"));
ASSERT_EQUALS("\n123", preprocessor0.getcode(code, "", "test.cpp")); ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(preprocessor0, code, "", "test.cpp"));
} }
void predefine6() { // automatically define __STDC_VERSION__ void predefine6() { // automatically define __STDC_VERSION__
const char code[] = "#ifdef __STDC_VERSION__\n123\n#endif"; const char code[] = "#ifdef __STDC_VERSION__\n123\n#endif";
ASSERT_EQUALS("\n123", preprocessor0.getcode(code, "", "test.c")); ASSERT_EQUALS("\n123", PreprocessorHelper::getcode(preprocessor0, code, "", "test.c"));
ASSERT_EQUALS("", preprocessor0.getcode(code, "", "test.cpp")); ASSERT_EQUALS("", PreprocessorHelper::getcode(preprocessor0, code, "", "test.cpp"));
} }
void invalidElIf() { void invalidElIf() {
// #2942 - segfault // #2942 - segfault
const char code[] = "#elif (){\n"; const char code[] = "#elif (){\n";
const std::string actual = preprocessor0.getcode(code, "TEST", "test.c"); const std::string actual = PreprocessorHelper::getcode(preprocessor0, code, "TEST", "test.c");
ASSERT_EQUALS("", actual); ASSERT_EQUALS("", actual);
} }
@ -2307,7 +2318,7 @@ private:
settings.userDefines = "foo"; settings.userDefines = "foo";
Preprocessor preprocessor(settings, this); Preprocessor preprocessor(settings, this);
const std::string code("#error hello world!\n"); const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "./././test.c"); PreprocessorHelper::getcode(preprocessor, code, "X", "./././test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str()); ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
} }
@ -2344,7 +2355,7 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
Preprocessor preprocessor(settings0, this); Preprocessor preprocessor(settings0, this);
preprocessor.getcode(filedata, "", "test.c"); PreprocessorHelper::getcode(preprocessor, filedata, "", "test.c");
preprocessor.dump(ostr); preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str()); ASSERT_EQUALS(dumpdata, ostr.str());
} }
@ -2371,7 +2382,7 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
Preprocessor preprocessor(settings0, this); Preprocessor preprocessor(settings0, this);
preprocessor.getcode(filedata, "", "test.c"); PreprocessorHelper::getcode(preprocessor, filedata, "", "test.c");
preprocessor.dump(ostr); preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str()); ASSERT_EQUALS(dumpdata, ostr.str());
} }
@ -2388,7 +2399,7 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
Preprocessor preprocessor(settings0, this); Preprocessor preprocessor(settings0, this);
preprocessor.getcode(filedata, "", "test.c"); PreprocessorHelper::getcode(preprocessor, filedata, "", "test.c");
preprocessor.dump(ostr); preprocessor.dump(ostr);
ASSERT_EQUALS(dumpdata, ostr.str()); ASSERT_EQUALS(dumpdata, ostr.str());
} }
@ -2407,7 +2418,7 @@ private:
std::string code("#include \"header.h\""); std::string code("#include \"header.h\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2424,7 +2435,7 @@ private:
std::string code("#include \"header.h\""); std::string code("#include \"header.h\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout.str());
} }
@ -2443,7 +2454,7 @@ private:
std::string code("#include \"header.h\""); std::string code("#include \"header.h\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout.str());
} }
@ -2463,7 +2474,7 @@ private:
std::string code("#include \"inc/header.h\""); std::string code("#include \"inc/header.h\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2483,7 +2494,7 @@ private:
std::string code("#include \"" + header.path() + "\""); std::string code("#include \"" + header.path() + "\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2502,7 +2513,7 @@ private:
std::string code("#include \"" + header + "\""); std::string code("#include \"" + header + "\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout.str());
} }
@ -2521,7 +2532,7 @@ private:
std::string code("#include <header.h>"); std::string code("#include <header.h>");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str());
} }
@ -2538,7 +2549,7 @@ private:
std::string code("#include <header.h>"); std::string code("#include <header.h>");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str());
} }
@ -2559,7 +2570,7 @@ private:
std::string code("#include <header.h>"); std::string code("#include <header.h>");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2579,7 +2590,7 @@ private:
std::string code("#include <" + header.path() + ">"); std::string code("#include <" + header.path() + ">");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -2598,7 +2609,7 @@ private:
std::string code("#include <" + header + ">"); std::string code("#include <" + header + ">");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str()); ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout.str());
} }
@ -2621,7 +2632,7 @@ private:
"#include <missing2.h>\n" "#include <missing2.h>\n"
"#include \"header2.h\""); "#include \"header2.h\"");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n"
"test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" "test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
@ -2661,7 +2672,7 @@ private:
"#include <" + header6.path() + ">\n" "#include <" + header6.path() + ">\n"
"#include <" + missing4 + ">\n"); "#include <" + missing4 + ">\n");
errout.str(""); errout.str("");
preprocessor.getcode(code, "", "test.c"); PreprocessorHelper::getcode(preprocessor, code, "", "test.c");
ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n" ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n"
"test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n" "test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"

View File

@ -18,6 +18,7 @@
#include "config.h" #include "config.h"
#include "errortypes.h" #include "errortypes.h"
#include "helpers.h"
#include "platform.h" #include "platform.h"
#include "preprocessor.h" // usually tests here should not use preprocessor... #include "preprocessor.h" // usually tests here should not use preprocessor...
#include "settings.h" #include "settings.h"
@ -6652,7 +6653,7 @@ private:
std::string filedata; std::string filedata;
std::istringstream fin(raw_code); std::istringstream fin(raw_code);
preprocessor.preprocess(fin, filedata, configurations, emptyString, settings0.includePaths); preprocessor.preprocess(fin, filedata, configurations, emptyString, settings0.includePaths);
const std::string code = preprocessor.getcode(filedata, emptyString, emptyString); const std::string code = PreprocessorHelper::getcode(preprocessor, filedata, emptyString, emptyString);
ASSERT_THROW(tokenizeAndStringify(code.c_str()), InternalError); ASSERT_THROW(tokenizeAndStringify(code.c_str()), InternalError);
} }