Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testrunner: got rid of some redundant preprocessing code / added some missing asserts #5521

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Makefile

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions test/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,34 @@ std::string PreprocessorHelper::getcode(Preprocessor &preprocessor, const std::s

return ret;
}

void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer)
{
// Raw Tokens..
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

// Tokenizer..
tokenizer.createTokens(std::move(tokens2));
}

void PreprocessorHelper::preprocess(Preprocessor &preprocessor, const char code[], std::vector<std::string> &files, Tokenizer& tokenizer)
{
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

// Tokenizer..
tokenizer.createTokens(std::move(tokens2));

preprocessor.setDirectives(tokens1);
}
3 changes: 3 additions & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class PreprocessorHelper
* @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);

static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer);
static void preprocess(Preprocessor &preprocessor, const char code[], std::vector<std::string> &files, Tokenizer& tokenizer);
};

namespace cppcheck {
Expand Down
5 changes: 2 additions & 3 deletions test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, isReturnScope("void positiveTokenOffset() { return; }", 7));
}

#define isSameExpression(code, tokStr1, tokStr2) isSameExpression_(code, tokStr1, tokStr2, __FILE__, __LINE__)
bool isSameExpression_(const char code[], const char tokStr1[], const char tokStr2[], const char* file, int line) {
#define isSameExpression(...) isSameExpression_(__FILE__, __LINE__, __VA_ARGS__)
bool isSameExpression_(const char* file, int line, const char code[], const char tokStr1[], const char tokStr2[]) {
const Settings settings;
Library library;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
tokenizer.simplifyTokens1("");
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1, strlen(tokStr1));
const Token * const tok2 = Token::findsimplematch(tok1->next(), tokStr2, strlen(tokStr2));
return (isSameExpression)(false, false, tok1, tok2, library, false, true, nullptr);
Expand Down
18 changes: 6 additions & 12 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "checkbufferoverrun.h"
#include "ctu.h"
#include "errortypes.h"
#include "helpers.h"
#include "standards.h"
#include "platform.h"
#include "settings.h"
Expand Down Expand Up @@ -71,28 +72,21 @@ class TestBufferOverrun : public TestFixture {
runChecks<CheckBufferOverrun>(tokenizer, this);
}

void checkP(const char code[], const char* filename = "test.cpp")
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp")
{
// Clear the error buffer..
errout.str("");

const Settings settings = settingsBuilder(settings0).severity(Severity::performance)
.c(Standards::CLatest).cpp(Standards::CPPLatest).certainty(Certainty::inconclusive).build();

// Raw tokens..
std::vector<std::string> files(1, filename);
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Tokenizer tokenizer(&settings, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

// Tokenizer..
Tokenizer tokenizer(&settings, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check for buffer overruns..
runChecks<CheckBufferOverrun>(tokenizer, this);
Expand Down
18 changes: 5 additions & 13 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "check.h"
#include "checkclass.h"
#include "errortypes.h"
#include "helpers.h"
#include "preprocessor.h"
#include "settings.h"
#include "fixture.h"
Expand Down Expand Up @@ -8409,26 +8410,17 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

#define checkUselessOverride(code) checkUselessOverride_(code, __FILE__, __LINE__)
void checkUselessOverride_(const char code[], const char* file, int line) {
#define checkUselessOverride(...) checkUselessOverride_(__FILE__, __LINE__, __VA_ARGS__)
void checkUselessOverride_(const char* file, int line, const char code[]) {
// Clear the error log
errout.str("");

const Settings settings = settingsBuilder().severity(Severity::style).build();

// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

// Tokenize..
Tokenizer tokenizer(&settings, this);
tokenizer.createTokens(std::move(tokens2));
PreprocessorHelper::preprocess(code, files, tokenizer);

ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check..
Expand Down
26 changes: 9 additions & 17 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "checkcondition.h"
#include "errortypes.h"
#include "helpers.h"
#include "library.h"
#include "platform.h"
#include "preprocessor.h"
Expand Down Expand Up @@ -127,35 +128,26 @@ class TestCondition : public TestFixture {
TEST_CASE(knownConditionIncrementLoop); // #9808
}

void check(const char code[], const Settings &settings, const char* filename = "test.cpp") {
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") {
// Clear the error buffer..
errout.str("");

// Raw tokens..
std::vector<std::string> files(1, filename);
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

Preprocessor preprocessor(settings);
preprocessor.setDirectives(tokens1);
std::vector<std::string> files(1, filename);
Tokenizer tokenizer(&settings, this, &preprocessor);
PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer);

// Tokenizer..
Tokenizer tokenizer(&settings, this, &preprocessor);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Run checks..
runChecks<CheckCondition>(tokenizer, this);
}

void check(const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
void check_(const char* file, int line, const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
check(code, settings, filename);
check_(file, line, code, settings, filename);
}

void assignAndCompare() {
Expand Down
18 changes: 6 additions & 12 deletions test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "checkother.h"
#include "errortypes.h"
#include "helpers.h"
#include "settings.h"
#include "fixture.h"
#include "tokenize.h"
Expand All @@ -37,26 +38,19 @@ class TestIncompleteStatement : public TestFixture {
private:
const Settings settings = settingsBuilder().severity(Severity::warning).build();

void check(const char code[], bool inconclusive = false) {
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
// Clear the error buffer..
errout.str("");

const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();

// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Tokenizer tokenizer(&settings1, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

// Tokenize..
Tokenizer tokenizer(&settings1, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check for incomplete statements..
CheckOther checkOther(&tokenizer, &settings1, this);
Expand Down
18 changes: 6 additions & 12 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "checkleakautovar.h"
#include "errortypes.h"
#include "helpers.h"
#include "library.h"
#include "settings.h"
#include "fixture.h"
Expand Down Expand Up @@ -2837,24 +2838,17 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
private:
const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();

void checkP(const char code[], bool cpp = false) {
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
// Clear the error buffer..
errout.str("");

// Raw tokens..
std::vector<std::string> files(1, cpp?"test.cpp":"test.c");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Tokenizer tokenizer(&settings, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

// Tokenizer..
Tokenizer tokenizer(&settings, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check for leaks..
runChecks<CheckLeakAutoVar>(tokenizer, this);
Expand Down
18 changes: 6 additions & 12 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "checknullpointer.h"
#include "ctu.h"
#include "errortypes.h"
#include "helpers.h"
#include "library.h"
#include "settings.h"
#include "fixture.h"
Expand Down Expand Up @@ -192,26 +193,19 @@ class TestNullPointer : public TestFixture {
runChecks<CheckNullPointer>(tokenizer, this);
}

void checkP(const char code[]) {
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[]) {
// Clear the error buffer..
errout.str("");

const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, false).build();

// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Tokenizer tokenizer(&settings1, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

// Tokenizer..
Tokenizer tokenizer(&settings1, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check for null pointer dereferences..
runChecks<CheckNullPointer>(tokenizer, this);
Expand Down
22 changes: 7 additions & 15 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "checkother.h"
#include "errortypes.h"
#include "helpers.h"
#include "library.h"
#include "platform.h"
#include "preprocessor.h"
Expand Down Expand Up @@ -330,7 +331,8 @@ class TestOther : public TestFixture {
check_(file, line, code, "test.cpp", true, true, false, s);
}

void checkP(const char code[], const char *filename = "test.cpp") {
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], const char *filename = "test.cpp") {
// Clear the error buffer..
errout.str("");

Expand All @@ -343,23 +345,13 @@ class TestOther : public TestFixture {
settings->standards.cpp = Standards::CPPLatest;
settings->certainty.enable(Certainty::inconclusive);

// Raw tokens..
std::vector<std::string> files(1, filename);
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

Preprocessor preprocessor(*settings);
preprocessor.setDirectives(tokens1);
std::vector<std::string> files(1, filename);
Tokenizer tokenizer(settings, this, &preprocessor);
PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer);

// Tokenizer..
Tokenizer tokenizer(settings, this, &preprocessor);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Check..
runChecks<CheckOther>(tokenizer, this);
Expand Down
13 changes: 3 additions & 10 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "errortypes.h"
#include "helpers.h"
#include "platform.h"
#include "settings.h"
#include "fixture.h"
Expand Down Expand Up @@ -263,19 +264,11 @@ class TestSimplifyTypedef : public TestFixture {
// Clear the error buffer..
errout.str("");

// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Tokenizer tokenizer(&settings0, this);
PreprocessorHelper::preprocess(code, files, tokenizer);

// Tokenize..
Tokenizer tokenizer(&settings0, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.createLinks();
tokenizer.simplifyTypedef();

Expand Down
Loading
Loading