Refactorization: Improved usage of Settings instances in test suite

This commit is contained in:
PKEuS 2015-10-07 18:33:57 +02:00
parent 1d7c3c3db0
commit 3a5cef8a7e
36 changed files with 553 additions and 892 deletions

View File

@ -27,9 +27,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("portability");
TEST_CASE(novardecl);
TEST_CASE(functionpar);
TEST_CASE(structmember);
@ -42,9 +44,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("portability");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -26,13 +26,12 @@ public:
TestAssert() : TestFixture("TestAssert") {}
private:
Settings settings;
void check(const char code[], const char *filename = "test.cpp") {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -44,6 +43,8 @@ private:
}
void run() {
settings.addEnabled("warning");
TEST_CASE(assignmentInAssert);
TEST_CASE(functionCallInAssert);
TEST_CASE(memberFunctionCallInAssert);

View File

@ -27,9 +27,13 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
settings.addEnabled("warning");
settings.inconclusive = true;
TEST_CASE(bitwiseOnBoolean); // if (bool & bool)
TEST_CASE(incrementBoolean);
TEST_CASE(assignBoolToPointer);
@ -64,10 +68,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.addEnabled("warning");
settings.inconclusive = true;
settings.experimental = experimental;
// Tokenize..

View File

@ -27,7 +27,12 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
settings.addEnabled("performance");
TEST_CASE(BoostForeachContainerModification)
}
@ -35,10 +40,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.addEnabled("performance");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -30,27 +30,24 @@ public:
}
private:
Settings settings0;
void check(const char code[], bool experimental = true, const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.inconclusive = true;
settings.experimental = experimental;
settings.addEnabled("warning");
settings.addEnabled("style");
settings.addEnabled("portability");
settings0.inconclusive = true;
settings0.experimental = experimental;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
tokenizer.simplifyTokenList2();
// Check for buffer overruns..
CheckBufferOverrun checkBufferOverrun;
checkBufferOverrun.runSimplifiedChecks(&tokenizer, &settings, this);
checkBufferOverrun.runSimplifiedChecks(&tokenizer, &settings0, this);
}
void check(const char code[], const Settings &settings, const char filename[] = "test.cpp") {
@ -70,6 +67,10 @@ private:
}
void run() {
settings0.addEnabled("warning");
settings0.addEnabled("style");
settings0.addEnabled("portability");
TEST_CASE(noerr1);
TEST_CASE(noerr2);
TEST_CASE(noerr3);
@ -3431,16 +3432,14 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
tokenizer.simplifyTokenList2();
// Check for buffer overruns..
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings0, this);
checkBufferOverrun.bufferOverrun();
}

View File

@ -27,9 +27,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("warning");
TEST_CASE(array_index_1);
TEST_CASE(array_index_2);
TEST_CASE(bitop);
@ -39,9 +41,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -28,8 +28,13 @@ public:
}
private:
Settings settings0;
Settings settings1;
void run() {
settings0.addEnabled("style");
settings1.addEnabled("warning");
TEST_CASE(virtualDestructor1); // Base class not found => no error
TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor
TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual
@ -183,17 +188,15 @@ private:
void checkExplicitConstructors(const char code[]) {
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.checkExplicitConstructors();
}
@ -258,17 +261,15 @@ private:
void checkDuplInheritedMembers(const char code[]) {
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings1, this);
checkClass.checkDuplInheritedMembers();
}
@ -370,17 +371,15 @@ private:
void checkCopyConstructor(const char code[]) {
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.copyconstructors();
}
@ -607,18 +606,16 @@ private:
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.inconclusive = true;
settings0.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.operatorEq();
}
@ -768,17 +765,14 @@ private:
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.operatorEqRetRefThis();
}
@ -1055,17 +1049,14 @@ private:
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings1, this);
checkClass.operatorEqToSelf();
}
@ -1878,17 +1869,16 @@ private:
// Clear the error log
errout.str("");
Settings settings;
settings.inconclusive = inconclusive;
settings0.inconclusive = inconclusive;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.virtualDestructor();
}
@ -2732,17 +2722,14 @@ private:
// Clear the error log
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// Check..
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings1, this);
checkClass.thisSubtraction();
}
@ -2764,25 +2751,22 @@ private:
"[test.cpp:3]: (warning) Suspicious pointer subtraction. Did you intend to write '->'?\n", errout.str());
}
void checkConst(const char code[], const Settings *s = 0, bool inconclusive = true) {
void checkConst(const char code[], Settings *s = 0, bool inconclusive = true) {
// Clear the error log
errout.str("");
// Check..
Settings settings;
if (s)
settings = *s;
else
settings.addEnabled("style");
settings.inconclusive = inconclusive;
if (!s)
s = &settings0;
s->inconclusive = inconclusive;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(s, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, s, this);
checkClass.checkConst();
}
@ -5750,13 +5734,10 @@ private:
" }\n"
"};";
Settings settings;
settings.addEnabled("style");
checkConst(code, &settings, true);
checkConst(code, &settings0, true);
ASSERT_EQUALS("[test.cpp:3]: (performance, inconclusive) Technically the member function 'foo::f' can be static.\n", errout.str());
checkConst(code, &settings, false); // TODO: Set inconclusive to true (preprocess it)
checkConst(code, &settings0, false); // TODO: Set inconclusive to true (preprocess it)
ASSERT_EQUALS("", errout.str());
}
@ -5804,17 +5785,15 @@ private:
errout.str("");
// Check..
Settings settings;
settings.addEnabled("style");
settings.inconclusive = true;
settings0.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.initializerListOrder();
}
@ -5993,16 +5972,13 @@ private:
// Clear the error log
errout.str("");
// Check..
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, &settings0, this);
checkClass.checkSelfInitialization();
}
@ -6088,25 +6064,25 @@ private:
ASSERT_EQUALS("", errout.str());
}
void checkPureVirtualFunctionCall(const char code[], const Settings *s = 0, bool inconclusive = true) {
void checkPureVirtualFunctionCall(const char code[], Settings *s = 0, bool inconclusive = true) {
// Clear the error log
errout.str("");
// Check..
Settings settings;
if (s)
settings = *s;
else
settings.addEnabled("warning");
settings.inconclusive = inconclusive;
if (!s) {
static Settings settings_;
s = &settings_;
s->addEnabled("warning");
}
s->inconclusive = inconclusive;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(s, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
CheckClass checkClass(&tokenizer, &settings, this);
CheckClass checkClass(&tokenizer, s, this);
checkClass.checkPureVirtualFunctionCall();
}

View File

@ -28,9 +28,23 @@ public:
}
private:
Settings settings0;
Settings settings1;
void run() {
settings0.addEnabled("style");
settings0.addEnabled("warning");
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"bar\"> <pure/> </function>\n"
"</def>";
tinyxml2::XMLDocument xmldoc;
xmldoc.Parse(cfg, sizeof(cfg));
settings1.addEnabled("style");
settings1.addEnabled("warning");
settings1.library.load(xmldoc);
TEST_CASE(assignAndCompare); // assignment and comparison don't match
TEST_CASE(mismatchingBitAnd); // overlapping bitmasks
TEST_CASE(compare); // mismatching LHS/RHS in comparison
@ -71,19 +85,15 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.addEnabled("warning");
CheckCondition checkCondition;
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
checkCondition.runChecks(&tokenizer, &settings, this);
checkCondition.runChecks(&tokenizer, &settings0, this);
tokenizer.simplifyTokenList2();
checkCondition.runSimplifiedChecks(&tokenizer, &settings, this);
checkCondition.runSimplifiedChecks(&tokenizer, &settings0, this);
}
void assignAndCompare() {
@ -355,27 +365,15 @@ private:
// Clear the error buffer..
errout.str("");
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"bar\"> <pure/> </function>\n"
"</def>";
tinyxml2::XMLDocument xmldoc;
xmldoc.Parse(cfg, sizeof(cfg));
Settings settings;
settings.addEnabled("style");
settings.addEnabled("warning");
settings.library.load(xmldoc);
// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
CheckCondition checkCondition;
checkCondition.runChecks(&tokenizer, &settings, this);
checkCondition.runChecks(&tokenizer, &settings1, this);
tokenizer.simplifyTokenList2();
checkCondition.runSimplifiedChecks(&tokenizer, &settings, this);
checkCondition.runSimplifiedChecks(&tokenizer, &settings1, this);
}
void duplicateIf() {
check("void f(int a, int &b) {\n"

View File

@ -27,16 +27,13 @@ public:
}
private:
Settings settings;
void check(const char code[], bool showAll = false) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.inconclusive = showAll;
settings.addEnabled("style");
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -50,6 +47,9 @@ private:
}
void run() {
settings.addEnabled("style");
settings.addEnabled("warning");
TEST_CASE(simple1);
TEST_CASE(simple2);
TEST_CASE(simple3);

View File

@ -27,8 +27,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("all");
TEST_CASE(destructors);
TEST_CASE(deallocThrow1);
TEST_CASE(deallocThrow2);
@ -52,8 +55,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("all");
settings.inconclusive = inconclusive;
// Tokenize..

View File

@ -30,8 +30,18 @@ public:
}
private:
Settings settings;
void run() {
settings.debugwarnings = true;
settings.addEnabled("style");
settings.addEnabled("warning");
settings.addEnabled("portability");
settings.addEnabled("performance");
settings.addEnabled("information");
settings.inconclusive = true;
settings.experimental = true;
// don't freak out when the syntax is wrong
TEST_CASE(wrong_syntax1);
TEST_CASE(wrong_syntax2);
@ -196,15 +206,6 @@ private:
std::string checkCodeInternal(const char code[], const char* filename) {
errout.str("");
Settings settings;
settings.debugwarnings = true;
settings.addEnabled("style");
settings.addEnabled("warning");
settings.addEnabled("portability");
settings.addEnabled("performance");
settings.inconclusive = true;
settings.experimental = true;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -263,7 +264,6 @@ private:
" )\n"
"}";
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
try {
@ -297,9 +297,6 @@ private:
// #3585
const char code[] = "class x y { };";
Settings settings;
settings.addEnabled("information");
{
errout.str("");
Tokenizer tokenizer(&settings, this);

View File

@ -27,13 +27,12 @@ public:
}
private:
Settings settings;
void check(const char code[]) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -46,6 +45,8 @@ private:
}
void run() {
settings.addEnabled("warning");
TEST_CASE(test1);
TEST_CASE(test2);
TEST_CASE(test3);

View File

@ -29,7 +29,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("internal");
TEST_CASE(simplePatternInTokenMatch)
TEST_CASE(complexPatternInTokenSimpleMatch)
TEST_CASE(simplePatternSquareBrackets)
@ -46,9 +50,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("internal");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -27,8 +27,17 @@ public:
}
private:
Settings settings;
void run() {
int id = 0;
while (!settings.library.ismemory(++id));
settings.library.setalloc("malloc", id);
settings.library.setdealloc("free", id);
while (!settings.library.isresource(++id));
settings.library.setalloc("fopen", id);
settings.library.setdealloc("fclose", id);
// Assign
TEST_CASE(assign1);
TEST_CASE(assign2);
@ -117,14 +126,6 @@ private:
errout.str("");
// Tokenize..
Settings settings;
int id = 0;
while (!settings.library.ismemory(++id));
settings.library.setalloc("malloc", id);
settings.library.setdealloc("free", id);
while (!settings.library.isresource(++id));
settings.library.setalloc("fopen", id);
settings.library.setdealloc("fclose", id);
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, cpp?"test.cpp":"test.c");

View File

@ -30,6 +30,8 @@ public:
}
private:
Settings settings;
void run() {
TEST_CASE(testFunctionReturnType);
TEST_CASE(open);
@ -39,8 +41,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -94,8 +94,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -119,15 +117,18 @@ public:
}
private:
Settings settings0;
Settings settings1;
Settings settings2;
void check(const char code[], const Settings *settings = nullptr, bool c = false) {
void check(const char code[], bool c = false, bool posix = false, bool experimental = false, Settings *settings = nullptr) {
// Clear the error buffer..
errout.str("");
if (!settings)
settings = &settings1;
settings->experimental = experimental;
settings->standards.posix = posix;
// Tokenize..
Tokenizer tokenizer(settings, this);
@ -652,16 +653,13 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
// Tokenize..
std::istringstream istr(code);
TokenList list(&settings);
list.createTokens(istr,"test.cpp");
Token *tokens=list.front();
Tokenizer tokenizer(&settings0, this);
tokenizer.list.createTokens(istr, "test.cpp");
// replace "if ( ! var )" => "if(!var)"
for (Token *tok = tokens; tok; tok = tok->next()) {
for (Token *tok = tokenizer.list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "if|while ( var )")) {
Token::eraseTokens(tok, tok->tokAt(4));
tok->str(tok->str() + "(var)");
@ -673,11 +671,10 @@ private:
}
}
Tokenizer tokenizer;
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings, this);
checkMemoryLeak.simplifycode(tokens);
CheckMemoryLeakInFunction checkMemoryLeak(&tokenizer, &settings0, this);
checkMemoryLeak.simplifycode(tokenizer.list.front());
return list.front()->stringifyList(0, false);
return tokenizer.tokens()->stringifyList(0, false);
}
@ -818,16 +815,15 @@ private:
// is there a leak in given code? if so, return the linenr
static unsigned int dofindleak(const char code[]) {
unsigned int dofindleak(const char code[]) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.debug = settings.debugwarnings = true;
settings0.debug = settings0.debugwarnings = true;
// Tokenize..
std::istringstream istr(code);
TokenList list(&settings);
TokenList list(&settings0);
list.createTokens(istr,"test.cpp");
Token *tokens=list.front();
@ -849,10 +845,13 @@ private:
}
const Token *tok = CheckMemoryLeakInFunction::findleak(tokens);
settings0.debug = settings0.debugwarnings = false;
return (tok ? tok->linenr() : (unsigned int)(-1));
}
void findleak() const {
void findleak() {
static const unsigned int notfound = (unsigned int)(-1);
ASSERT_EQUALS(1, dofindleak("alloc;"));
@ -1142,8 +1141,6 @@ private:
}
void ifelse10() {
Settings settings;
settings.experimental = true;
check("static char *f()\n"
"{\n"
" char *s = new char[10];\n"
@ -1155,7 +1152,7 @@ private:
" {\n"
" str[0] = s;\n"
" }\n"
"}\n", &settings);
"}\n", false, false, true);
ASSERT_EQUALS("", errout.str());
}
@ -1262,8 +1259,6 @@ private:
}
void if11() {
Settings settings;
settings.experimental = true;
check("void foo()\n"
"{\n"
" int *x = new int[10];\n"
@ -1272,7 +1267,7 @@ private:
" return 1;\n"
" }\n"
" delete [] x;\n"
"}\n", &settings);
"}", false, false, true);
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n",
"", errout.str());
}
@ -1331,8 +1326,6 @@ private:
void forwhile9() {
Settings settings;
settings.experimental = true;
check("char *f()\n"
"{\n"
" char *a = 0;\n"
@ -1347,14 +1340,12 @@ private:
" }\n"
"\n"
" return a;\n"
"}\n", &settings);
"}\n", false, false, true);
ASSERT_EQUALS("[test.cpp:9]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n", errout.str());
}
void forwhile10() {
Settings settings;
settings.experimental = true;
check("char *f()\n"
"{\n"
" char *a = 0;\n"
@ -1369,7 +1360,7 @@ private:
" }\n"
"\n"
" return a;\n"
"}\n", &settings);
"}", false, false, true);
ASSERT_EQUALS("[test.cpp:9]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n"
"[test.cpp:11]: (error) Memory leak: a\n", errout.str());
}
@ -1460,14 +1451,11 @@ private:
void mismatch1() {
Settings settings;
settings.experimental = true;
check("void f()\n"
"{\n"
" int *a = new int[10];\n"
" free(a);\n"
"}\n", &settings);
"}\n", false, false, true);
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: a\n", errout.str());
// ticket #2971
@ -1475,14 +1463,14 @@ private:
"{\n"
" Fred *a = new Fred[10];\n"
" free(a);\n"
"}\n", &settings);
"}\n", false, false, true);
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: a\n", errout.str());
check("void f()\n"
"{\n"
" struct Fred *a = new struct Fred[10];\n"
" free(a);\n"
"}\n", &settings);
"}\n", false, false, true);
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: a\n", errout.str());
}
@ -1608,9 +1596,6 @@ private:
void func5() {
Settings settings;
settings.experimental = true;
check("static void foo(char *str)\n"
"{\n"
" delete str;\n"
@ -1620,7 +1605,7 @@ private:
"{\n"
" char *p = new char[100];\n"
" foo(p);\n"
"}\n", &settings);
"}", false, false, true);
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Mismatching allocation and deallocation: str\n",
errout.str());
}
@ -3671,9 +3656,6 @@ private:
}
void if_with_and() {
Settings settings;
settings.experimental = true;
check("void f()\n"
"{\n"
" char *a = new char[10];\n"
@ -3681,7 +3663,7 @@ private:
" return;\n"
"\n"
" delete [] a;\n"
"}\n", &settings);
"}", false, false, true);
ASSERT_EQUALS("", errout.str());
check("void f()\n"
@ -3691,18 +3673,15 @@ private:
" return;\n"
"\n"
" delete [] a;\n"
"}\n", &settings);
"}", false, false, true);
ASSERT_EQUALS("", errout.str());
}
void assign_pclose() {
Settings settings;
settings.standards.posix = true;
check("void f() {\n"
" FILE *f = popen (\"test\", \"w\");\n"
" int a = pclose(f);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("", errout.str());
}
@ -3865,14 +3844,10 @@ private:
}
void open_function() {
Settings settings;
settings.experimental = true;
settings.standards.posix = true;
check("void f(const char *path)\n"
"{\n"
" int fd = open(path, O_RDONLY);\n"
"}\n", &settings);
"}", false, true, true);
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
check("void f(const char *path)\n"
@ -3881,7 +3856,7 @@ private:
" if (fd == -1)\n"
" return;\n"
" close(fd);\n"
"}\n", &settings);
"}", false, true, true);
ASSERT_EQUALS("", errout.str());
check("void f(const char *path)\n"
@ -3890,7 +3865,7 @@ private:
" if (fd < 0)\n"
" return;\n"
" close(fd);\n"
"}\n", &settings);
"}", false, true, true);
ASSERT_EQUALS("", errout.str());
check("void f(const char *path)\n"
@ -3899,35 +3874,31 @@ private:
" if (-1 == fd)\n"
" return;\n"
" close(fd);\n"
"}\n", &settings);
"}", false, true, true);
ASSERT_EQUALS("", errout.str());
}
void creat_function() {
Settings settings;
settings.standards.posix = true;
check("void f(const char *path)\n"
"{\n"
" int fd = creat(path, S_IRWXU);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
}
void close_function() {
Settings settings;
settings.standards.posix = true;
check("void f(const char *path)\n"
"{\n"
" int fd = open(path, O_RDONLY);\n"
" close(fd);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("", errout.str());
check("void f(const char *path)\n"
"{\n"
" int fd = creat(path, S_IRWXU);\n"
" close(fd);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("", errout.str());
check("void f(const char *path)\n"
@ -3936,7 +3907,7 @@ private:
" if (close(fd) < 0) {\n"
" perror(\"close\");\n"
" }\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("", errout.str());
//#ticket 1401
@ -3954,7 +3925,7 @@ private:
" return 3;\n"
" }\n"
" close(handle);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("", errout.str());
//#ticket 1401
@ -3971,13 +3942,11 @@ private:
" return 3;\n"
" }\n"
" close(handle);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("[test.cpp:11]: (error) Resource leak: handle\n", errout.str());
}
void fd_functions() {
Settings settings;
settings.standards.posix = true;
check("void f(const char *path)\n"
"{\n"
" int fd = open(path, O_RDONLY);\n"
@ -4001,7 +3970,7 @@ private:
" ftruncate(fd, len);\n"
" fstat(fd, buf);\n"
" fchmod(fd, mode);\n"
"}", &settings);
"}", false, true);
ASSERT_EQUALS("[test.cpp:24]: (error) Resource leak: fd\n", errout.str());
}
@ -4252,13 +4221,12 @@ private:
" free(ll);\n"
" ll = NULL;\n"
" delete(ll, ll->top);\n"
"}", nullptr, true);
"}", true);
ASSERT_EQUALS("", errout.str());
}
void gnucfg() {
Settings settings;
settings.standards.posix = true;
LOAD_LIB_2(settings.library, "gnu.cfg");
const char code[] = "void leak() {\n"
" char * p = get_current_dir_name();\n" // memory leak
@ -4267,7 +4235,7 @@ private:
" char * p = get_current_dir_name();\n"
" free(p)\n;"
"}";
check(code, &settings);
check(code, false, true, false, &settings);
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout.str());
}
@ -4283,7 +4251,7 @@ private:
" int avierr = read_chunk_data(&data);\n"
" if (avierr == 0)\n"
" free(data);\n"
"}", nullptr, true);
"}", true);
ASSERT_EQUALS("", errout.str());
}
};
@ -4303,6 +4271,8 @@ public:
}
private:
Settings settings;
/**
* Tokenize and execute leak check for given code
* @param code Source code
@ -4311,10 +4281,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -4327,6 +4293,9 @@ private:
}
void run() {
settings.addEnabled("warning");
settings.addEnabled("style");
TEST_CASE(class1);
TEST_CASE(class2);
TEST_CASE(class3);
@ -5564,12 +5533,12 @@ public:
}
private:
Settings settings;
void check(const char code[], const char fname[] = 0, bool isCPP = true) {
// Clear the error buffer..
errout.str("");
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -27,8 +27,12 @@ public:
}
private:
Settings settings;
void run() {
settings.standards.posix = true;
settings.addEnabled("portability");
TEST_CASE(test_crypt);
TEST_CASE(test_namespace_handling);
}
@ -37,10 +41,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.standards.posix = true;
settings.addEnabled("portability");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -44,6 +44,7 @@ private:
doc.Parse(xmldata, sizeof(xmldata));
settings.library.load(doc);
}
settings.addEnabled("warning");
TEST_CASE(nullpointerAfterLoop);
TEST_CASE(nullpointer1);
@ -101,7 +102,6 @@ private:
// Clear the error buffer..
errout.str("");
settings.addEnabled("warning");
settings.inconclusive = inconclusive;
// Tokenize..

View File

@ -27,8 +27,13 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
settings.standards.posix = true;
settings.standards.c = Standards::C11;
TEST_CASE(testbsd_signal);
TEST_CASE(testgethostbyname);
TEST_CASE(testgethostbyaddr);
@ -68,11 +73,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.standards.posix = true;
settings.standards.c = Standards::C11;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -27,17 +27,13 @@ public:
}
private:
Settings settings;
void check(const char code[]) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("performance");
//settings.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -50,6 +46,8 @@ private:
}
void run() {
settings.addEnabled("performance");
TEST_CASE(testsimple);
TEST_CASE(testfor);
TEST_CASE(testvolatile);

File diff suppressed because it is too large Load Diff

View File

@ -29,8 +29,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("portability");
TEST_CASE(template1);
TEST_CASE(template2);
TEST_CASE(template3);
@ -101,8 +104,6 @@ private:
std::string tok(const char code[], bool simplify = true, bool debugwarnings = false, Settings::PlatformType type = Settings::Unspecified) {
errout.str("");
Settings settings;
settings.addEnabled("portability");
settings.debugwarnings = debugwarnings;
settings.platform(type);
Tokenizer tokenizer(&settings, this);
@ -119,7 +120,7 @@ private:
std::string tok(const char code[], const char filename[]) {
errout.str("");
Settings settings;
settings.debugwarnings = false;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -1177,7 +1178,6 @@ private:
}
unsigned int templateParameters(const char code[]) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -1208,11 +1208,10 @@ private:
// Helper function to unit test TemplateSimplifier::getTemplateNamePosition
int templateNamePositionHelper(const char code[], unsigned offset = 0) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp", "", true);
tokenizer.tokenize(istr, "test.cpp", emptyString, true);
const Token *_tok = tokenizer.tokens();
for (unsigned i = 0 ; i < offset ; ++i)

View File

@ -29,12 +29,17 @@ public:
private:
Settings settings0;
Settings settings1;
Settings settings_std;
Settings settings_windows;
void run() {
LOAD_LIB_2(settings_std.library, "std.cfg");
LOAD_LIB_2(settings_windows.library, "windows.cfg");
settings0.addEnabled("portability");
settings1.addEnabled("style");
settings_windows.addEnabled("portability");
// Make sure the Tokenizer::simplifyTokenList works.
// The order of the simplifications is important. So this test
@ -281,10 +286,8 @@ private:
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) {
errout.str("");
Settings settings;
settings.addEnabled("portability");
settings.platform(type);
Tokenizer tokenizer(&settings, this);
settings0.platform(type);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -298,7 +301,6 @@ private:
std::string tokWithWindows(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) {
errout.str("");
settings_windows.addEnabled("portability");
settings_windows.platform(type);
Tokenizer tokenizer(&settings_windows, this);
@ -314,8 +316,7 @@ private:
std::string tok(const char code[], const char filename[], bool simplify = true) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
@ -340,9 +341,7 @@ private:
std::string tokenizeDebugListing(const char code[], bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
@ -492,9 +491,7 @@ private:
"};\n"
"}\n";
Settings settings;
settings.platform(Settings::Unspecified);
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code1);
tokenizer.tokenize(istr, "test.cpp");
@ -787,11 +784,7 @@ private:
std::string elseif(const char code[]) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.elseif();
@ -855,11 +848,7 @@ private:
unsigned int sizeofFromTokenizer(const char type[]) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("");
tokenizer.tokenize(istr, "test.cpp");
Token tok1(0);
@ -1563,10 +1552,8 @@ private:
std::string simplifyIfAndWhileAssign(const char code[]) {
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -1651,8 +1638,7 @@ private:
void whileAssign4() {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("; while (!(m = q->push<Message>(x))) {}");
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
@ -3000,9 +2986,7 @@ private:
std::string checkSimplifyEnum(const char code[], bool cpp = true) {
errout.str("");
// Tokenize..
Settings settings;
settings.addEnabled("style");
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, cpp?"test.cpp":"test.c");
return tokenizer.tokens()->stringifyList(0, true);
@ -3433,8 +3417,7 @@ private:
}
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
const Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr("x ; return a not_eq x;");
tokenizer.tokenize(istr, "test.c");
Token *x_token = tokenizer.list.front()->tokAt(5);

View File

@ -29,8 +29,14 @@ public:
private:
Settings settings0;
Settings settings1;
Settings settings2;
void run() {
settings0.addEnabled("style");
settings2.addEnabled("style");
TEST_CASE(simplifyTypedef1)
TEST_CASE(simplifyTypedef2)
TEST_CASE(simplifyTypedef3)
@ -161,12 +167,10 @@ private:
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified, bool debugwarnings = true) {
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.inconclusive = true;
settings.debugwarnings = debugwarnings; // show warnings about unhandled typedef
settings.platform(type);
Tokenizer tokenizer(&settings, this);
settings0.inconclusive = true;
settings0.debugwarnings = debugwarnings; // show warnings about unhandled typedef
settings0.platform(type);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -180,8 +184,7 @@ private:
std::string simplifyTypedef(const char code[]) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.list.createTokens(istr);
@ -191,6 +194,16 @@ private:
return tokenizer.tokens()->stringifyList(0, false);
}
void checkSimplifyTypedef(const char code[]) {
errout.str("");
// Tokenize..
settings2.inconclusive = true;
settings2.debugwarnings = true; // show warnings about unhandled typedef
Tokenizer tokenizer(&settings2, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
}
void simplifyTypedef1() {
@ -532,8 +545,7 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -595,8 +607,7 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -1015,19 +1026,6 @@ private:
ASSERT_EQUALS(expected, tok(code, false));
}
// Check simplifyTypedef
void checkSimplifyTypedef(const char code[]) {
errout.str("");
// Tokenize..
Settings settings;
settings.inconclusive = true;
settings.addEnabled("style");
settings.debugwarnings = true; // show warnings about unhandled typedef
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
}
void simplifyTypedef35() {
const char code[] = "typedef int A;\n"
"class S\n"

View File

@ -27,9 +27,13 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("warning");
settings.addEnabled("portability");
settings.inconclusive = true;
TEST_CASE(sizeofsizeof);
TEST_CASE(sizeofCalculation);
TEST_CASE(checkPointerSizeof);
@ -46,11 +50,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
settings.addEnabled("portability");
settings.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -30,6 +30,9 @@ private:
Settings settings;
void run() {
settings.addEnabled("warning");
settings.addEnabled("style");
settings.addEnabled("performance");
LOAD_LIB_2(settings.library, "std.cfg");
TEST_CASE(iterator1);
@ -134,9 +137,6 @@ private:
// Clear the error buffer..
errout.str("");
settings.addEnabled("warning");
settings.addEnabled("style");
settings.addEnabled("performance");
settings.inconclusive = inconclusive;
settings.standards.cpp = cppstandard;
@ -147,7 +147,7 @@ private:
tokenizer.simplifyTokenList2();
// Check..
CheckStl checkStl;
CheckStl checkStl(&tokenizer, &settings, this);
checkStl.runSimplifiedChecks(&tokenizer, &settings, this);
}
void check(const std::string &code, const bool inconclusive=false) {

View File

@ -29,8 +29,12 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("warning");
settings.addEnabled("style");
TEST_CASE(stringLiteralWrite);
TEST_CASE(alwaysTrueFalseStringCompare);
@ -54,10 +58,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -734,8 +734,7 @@ private:
" foo[1].x = 123;\n" // <- x should get a variable() pointer
"}";
Settings localsettings;
Tokenizer tokenizer(&localsettings, this);
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -754,8 +753,7 @@ private:
" foo[1][2].x = 123;\n" // <- x should get a variable() pointer
"}";
Settings localsettings;
Tokenizer tokenizer(&localsettings, this);
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -774,8 +772,7 @@ private:
" (foo[1]).x = 123;\n" // <- x should get a variable() pointer
"}";
Settings localsettings;
Tokenizer tokenizer(&localsettings, this);
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -1349,17 +1346,18 @@ private:
errout.str("");
// Check..
Settings localsettings;
localsettings.debugwarnings = debug;
settings.debugwarnings = debug;
// Tokenize..
Tokenizer tokenizer(&localsettings, this);
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
// force symbol database creation
tokenizer.createSymbolDatabase();
settings.debugwarnings = false;
}
void functionArgs1() {

View File

@ -32,6 +32,7 @@ public:
}
private:
Settings settings;
/**
* Execute check using n jobs for y files which are have
@ -52,7 +53,6 @@ private:
filemap[oss.str()] = 1;
}
Settings settings;
settings._jobs = jobs;
ThreadExecutor executor(filemap, settings, *this);
for (std::map<std::string, std::size_t>::const_iterator i = filemap.begin(); i != filemap.end(); ++i)

View File

@ -31,6 +31,9 @@ public:
}
private:
Settings settings0;
Settings settings1;
Settings settings2;
Settings settings_windows;
void run() {
@ -469,13 +472,12 @@ private:
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Unspecified, const char* filename = "test.cpp", bool cpp11 = true) {
errout.str("");
Settings settings;
settings.debugwarnings = true;
settings.platform(platform);
settings.standards.cpp = cpp11 ? Standards::CPP11 : Standards::CPP03;
settings1.debugwarnings = true;
settings1.platform(platform);
settings1.standards.cpp = cpp11 ? Standards::CPP11 : Standards::CPP03;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
if (simplify)
@ -527,6 +529,23 @@ private:
return "";
}
std::string tokenizeDebugListing(const char code[], bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
settings2.standards.c = Standards::C89;
settings2.standards.cpp = Standards::CPP03;
Tokenizer tokenizer(&settings2, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
if (simplify)
tokenizer.simplifyTokenList2();
// result..
return tokenizer.tokens()->stringifyList(true);
}
void tokenize1() {
const char code[] = "void f ( )\n"
@ -869,10 +888,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(filedata);
tokenizer.tokenize(istr, "test.cpp");
@ -888,10 +905,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -906,10 +921,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -1370,9 +1383,7 @@ private:
std::string simplifyKnownVariables(const char code[]) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -2929,24 +2940,6 @@ private:
}
std::string tokenizeDebugListing(const char code[], bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
Settings settings;
settings.standards.c = Standards::C89;
settings.standards.cpp = Standards::CPP03;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
if (simplify)
tokenizer.simplifyTokenList2();
// result..
return tokenizer.tokens()->stringifyList(true);
}
void file1() {
const char code[] = "a1\n"
"#file \"b\"\n"
@ -2957,10 +2950,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "a");
@ -2991,10 +2982,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "a");
@ -3014,10 +3003,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "a.cpp");
@ -3026,32 +3013,12 @@ private:
void doublesharp() {
const char code[] = "a##_##b TEST(var,val) var##_##val = val\n";
errout.str("");
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "");
ASSERT_EQUALS("a_b TEST ( var , val ) var_val = val", tokenizer.tokens()->stringifyList(0, false));
ASSERT_EQUALS("a_b TEST ( var , val ) var_val = val", tokenizeAndStringify(code));
}
void macrodoublesharp() {
const char code[] = "DBG(fmt,args...) printf(fmt, ## args)\n";
errout.str("");
Settings settings;
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "");
ASSERT_EQUALS("DBG ( fmt , args . . . ) printf ( fmt , ## args )", tokenizer.tokens()->stringifyList(0, false));
ASSERT_EQUALS("DBG ( fmt , args . . . ) printf ( fmt , ## args )", tokenizeAndStringify(code));
}
void simplifyFunctionParameters() {
@ -3362,99 +3329,73 @@ private:
}
void tokenize_double() {
const char code[] = "void f()\n"
"{\n"
const char code[] = "void f() {\n"
" double a = 4.2;\n"
" float b = 4.2f;\n"
" double c = 4.2e+10;\n"
" double d = 4.2e-10;\n"
" int e = 4+2;\n"
"}\n";
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("void f ( ) { double a ; a = 4.2 ; float b ; b = 4.2f ; double c ; c = 4.2e+10 ; double d ; d = 4.2e-10 ; int e ; e = 6 ; }", tokenizer.tokens()->stringifyList(0, false));
"}";
ASSERT_EQUALS("void f ( ) {\n"
"double a ; a = 4.2 ;\n"
"float b ; b = 4.2f ;\n"
"double c ; c = 4.2e+10 ;\n"
"double d ; d = 4.2e-10 ;\n"
"int e ; e = 6 ;\n"
"}", tokenizeAndStringify(code));
}
void tokenize_strings() {
const char code[] = "void f()\n"
"{\n"
const char code[] = "void f() {\n"
"const char *a =\n"
"{\n"
"\"hello \"\n"
"\"more \"\n"
"\"world\"\n"
"};\n"
"}\n";
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
ASSERT_EQUALS("void f ( ) { const char * a ; a = { \"hello more world\" } ; }", tokenizer.tokens()->stringifyList(0, false));
"}";
ASSERT_EQUALS("void f ( ) {\n"
"const char * a ; a =\n"
"{\n"
"\"hello more world\"\n"
"\n"
"\n"
"} ;\n"
"}", tokenizeAndStringify(code));
}
void simplify_constants() {
const char code[] =
"void f()\n"
"{\n"
"void f() {\n"
"const int a = 45;\n"
"if( a )\n"
"{ int b = a; }\n"
"}\n"
"void g()\n"
"{\n"
"void g() {\n"
"int a = 2;\n"
"}\n";
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
ASSERT_EQUALS("void f ( ) { } void g ( ) { }", tokenizer.tokens()->stringifyList(0, false));
"}";
ASSERT_EQUALS("void f ( ) {\n"
"\n"
"\n"
"\n"
"}\n"
"void g ( ) {\n"
"\n"
"}", tokenizeAndStringify(code, true));
}
void simplify_constants2() {
const char code[] =
"void f( Foo &foo, Foo *foo2 )\n"
"{\n"
"void f( Foo &foo, Foo *foo2 ) {\n"
"const int a = 45;\n"
"foo.a=a+a;\n"
"foo2->a=a;\n"
"}\n";
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
ASSERT_EQUALS("void f ( Foo & foo , Foo * foo2 ) { foo . a = 90 ; foo2 . a = 45 ; }", tokenizer.tokens()->stringifyList(0, false));
"}";
ASSERT_EQUALS("void f ( Foo & foo , Foo * foo2 ) {\n"
"\n"
"foo . a = 90 ;\n"
"foo2 . a = 45 ;\n"
"}", tokenizeAndStringify(code, true));
}
void simplify_constants3() {
@ -3674,8 +3615,7 @@ private:
// ticket #2743 - set links if variable type contains parentheses
const char code[] = "Fred<int(*)()> fred1=a, fred2=b;";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.validate();
@ -3685,8 +3625,7 @@ private:
// ticket #3912 - set correct links
const char code[] = "function<void (shared_ptr<MyClass>)> v;";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.validate();
@ -4061,8 +4000,7 @@ private:
{
errout.str("");
const char code[] = "void f() {}";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_EQUALS(true, tokenizer.tokenize(istr, "test.cpp"));
ASSERT_EQUALS("", errout.str());
@ -4071,8 +4009,7 @@ private:
{
errout.str("");
const char code[] = "void f() {{}";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4080,8 +4017,7 @@ private:
{
errout.str("");
const char code[] = "void f()) {}";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4089,8 +4025,7 @@ private:
{
errout.str("");
const char code[] = "namespace extract{\nB(weighted_moment)\n}\nusing extract::weighted_moment;\n";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_EQUALS(true, tokenizer.tokenize(istr, "test.cpp"));
tokenizer.simplifyTokenList2();
@ -4103,8 +4038,7 @@ private:
"{\n"
" foo(;\n"
"}\n";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp", "ABC"), InternalError);
}
@ -4115,8 +4049,7 @@ private:
"{\n"
" for(;;){ foo();\n"
"}\n";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4127,8 +4060,7 @@ private:
"{\n"
" a[10;\n"
"}\n";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4141,8 +4073,7 @@ private:
"{\n"
" b());\n"
"}\n";
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
try {
tokenizer.tokenize(istr, "test.cpp");
@ -4161,8 +4092,7 @@ private:
{
errout.str("");
std::istringstream istr("x<y>z> xyz;\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4171,8 +4101,7 @@ private:
{
errout.str("");
std::istringstream istr("template<class T> operator<(T a, T b) { }\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4182,8 +4111,7 @@ private:
errout.str("");
std::istringstream istr("void f(a) int a;\n"
"{ ;x<y; }");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4193,8 +4121,7 @@ private:
errout.str("");
std::istringstream istr("void f()\n"
"try { ;x<y; }");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4203,8 +4130,7 @@ private:
{
errout.str();
std::istringstream istr("MACRO(({ i < x }))");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4213,8 +4139,7 @@ private:
{
errout.str("");
std::istringstream istr("x<y<int> xyz;\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4227,8 +4152,7 @@ private:
" , ConcreteVisitable\n"
" , Dummy< _visitableIndex >\n"
" >::type ConcreteVisitableOrDummy;\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
ASSERT_THROW(tokenizer.tokenize(istr, "test.cpp"), InternalError);
}
@ -4244,8 +4168,7 @@ private:
"private:\n"
" A a;\n"
"};\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_EQUALS("", errout.str());
}
@ -4253,8 +4176,7 @@ private:
void syntax_error_templates_2() {
std::istringstream istr("template<>\n");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
tokenizer.tokenize(istr, "test.cpp"); // shouldn't segfault
}
@ -4629,8 +4551,7 @@ private:
" void f() {}\n"
"};";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4655,8 +4576,7 @@ private:
" char *b ; b = new char[a[0]];\n"
"};";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4680,8 +4600,7 @@ private:
" foo(g());\n"
"};";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4701,8 +4620,7 @@ private:
" return(a<b && b>f);\n"
"}";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4730,8 +4648,7 @@ private:
" return static_cast<bar>(a);\n"
"}";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4748,8 +4665,7 @@ private:
" nvwa<(x > y)> ERROR_nnn;\n"
"}";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4765,8 +4681,7 @@ private:
// #4860
const char code[] = "class A : public B<int> {};";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4782,8 +4697,7 @@ private:
// #4860
const char code[] = "Bar<Typelist< int, Typelist< int, Typelist< int, FooNullType>>>>::set(1, 2, 3);";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4800,8 +4714,7 @@ private:
// #5627
const char code[] = "new Foo<Bar>[10];";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4817,8 +4730,7 @@ private:
// #6242
const char code[] = "func = integral_<uchar, int, double>;";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4833,8 +4745,7 @@ private:
// if (a < b || c > d) { }
const char code[] = "if (a < b || c > d);";
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
@ -4845,8 +4756,7 @@ private:
void simplifyString() {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
ASSERT_EQUALS("\"abc\"", tokenizer.simplifyString("\"abc\""));
ASSERT_EQUALS("\"\n\"", tokenizer.simplifyString("\"\\xa\""));
ASSERT_EQUALS("\"3\"", tokenizer.simplifyString("\"\\x33\""));
@ -4923,21 +4833,11 @@ private:
tokenizeAndStringify("foo data[100]; something(&foo[0]);"));
}
std::string simplifyFunctionPointers(const char code[]) {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyFunctionPointers();
return tokenizer.tokens()->stringifyList(0, true);
}
void functionpointer1() {
ASSERT_EQUALS("void * f ;", simplifyFunctionPointers("void (*f)();"));
ASSERT_EQUALS("void * * f ;", simplifyFunctionPointers("void *(*f)();"));
ASSERT_EQUALS("unsigned int * f ;", simplifyFunctionPointers("unsigned int (*f)();"));
ASSERT_EQUALS("unsigned int * * f ;", simplifyFunctionPointers("unsigned int * (*f)();"));
ASSERT_EQUALS("void * f ;", tokenizeAndStringify("void (*f)();"));
ASSERT_EQUALS("void * * f ;", tokenizeAndStringify("void *(*f)();"));
ASSERT_EQUALS("unsigned int * f ;", tokenizeAndStringify("unsigned int (*f)();"));
ASSERT_EQUALS("unsigned int * * f ;", tokenizeAndStringify("unsigned int * (*f)();"));
}
void functionpointer2() {
@ -4948,7 +4848,7 @@ private:
const char expected[] = "void f1 ( ) { } "
"void * pf ; pf = & f1 ; "
"void * pfs [ 2 ] = { & f1 , & f1 } ;";
ASSERT_EQUALS(expected, simplifyFunctionPointers(code));
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}
void functionpointer3() {
@ -4956,10 +4856,10 @@ private:
const char code[] = "void f() {\n"
"(void)(xy(*p)(0);)"
"\n}";
const char expected[] = "void f ( ) { "
"( void ) ( xy ( * p ) ( 0 ) ; ) "
const char expected[] = "void f ( ) {\n"
"( void ) ( xy ( * p ) ( 0 ) ; )\n"
"}";
ASSERT_EQUALS(expected, simplifyFunctionPointers(code));
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
}
void functionpointer4() {
@ -5047,10 +4947,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -5080,10 +4978,8 @@ private:
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -5168,9 +5064,9 @@ private:
std::string arraySize_(const std::string &code) {
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -8347,10 +8243,9 @@ private:
tokenizeAndStringify("[[deprecated]] int f();", false, true, Settings::Unspecified, "test.c", true));
}
static std::string testAst(const char code[],bool verbose=false) {
std::string testAst(const char code[],bool verbose=false) {
// tokenize given code..
const Settings settings;
Tokenizer tokenList(&settings, nullptr);
Tokenizer tokenList(&settings0, nullptr);
std::istringstream istr(code);
if (!tokenList.list.createTokens(istr,"test.cpp"))
return "ERROR";
@ -8385,7 +8280,7 @@ private:
return ret;
}
void astexpr() const { // simple expressions with arithmetical ops
void astexpr() { // simple expressions with arithmetical ops
ASSERT_EQUALS("12+3+", testAst("1+2+3"));
ASSERT_EQUALS("12*3+", testAst("1*2+3"));
ASSERT_EQUALS("123*+", testAst("1+2*3"));
@ -8469,7 +8364,7 @@ private:
ASSERT_EQUALS("ifCA_FarReadfilenew(,sizeofobjtype(,(!(", testAst("if (!CA_FarRead(file, (void far *)new, sizeof(objtype)))")); // #5910 - don't hang if C code is parsed as C++
}
void astnewdelete() const {
void astnewdelete() {
ASSERT_EQUALS("aintnew=", testAst("a = new int;"));
ASSERT_EQUALS("aint4[new=", testAst("a = new int[4];"));
ASSERT_EQUALS("aFoobar(new=", testAst("a = new Foo(bar);"));
@ -8505,7 +8400,7 @@ private:
ASSERT_EQUALS("char10[new(", testAst("(void)new(char*)[10];"));
}
void astpar() const { // parentheses
void astpar() { // parentheses
ASSERT_EQUALS("12+3*", testAst("(1+2)*3"));
ASSERT_EQUALS("123+*", testAst("1*(2+3)"));
ASSERT_EQUALS("123+*4*", testAst("1*(2+3)*4"));
@ -8570,7 +8465,7 @@ private:
ASSERT_EQUALS("stdfabs::m_similarity(numeric_limitsepsilon::(<=return", testAst("return std::fabs(m_similarity) <= numeric_limits<double>::epsilon();")); // #6195
}
void astbrackets() const { // []
void astbrackets() { // []
ASSERT_EQUALS("a23+[4+", testAst("a[2+3]+4"));
ASSERT_EQUALS("a1[0[", testAst("a[1][0]"));
ASSERT_EQUALS("ab0[=", testAst("a=(b)[0];"));
@ -8578,7 +8473,7 @@ private:
ASSERT_EQUALS("ab0[1[=", testAst("a=b[0][1];"));
}
void astunaryop() const { // unary operators
void astunaryop() { // unary operators
ASSERT_EQUALS("1a--+", testAst("1 + --a"));
ASSERT_EQUALS("1a--+", testAst("1 + a--"));
ASSERT_EQUALS("ab+!", testAst("!(a+b)"));
@ -8597,7 +8492,7 @@ private:
ASSERT_EQUALS("ai[i= i--", testAst("a[i]=i; --i;"));
}
void astfunction() const { // function calls
void astfunction() { // function calls
ASSERT_EQUALS("1f(+2+", testAst("1+f()+2"));
ASSERT_EQUALS("1f2(+3+", testAst("1+f(2)+3"));
ASSERT_EQUALS("1f23,(+4+", testAst("1+f(2,3)+4"));
@ -8610,7 +8505,7 @@ private:
ASSERT_EQUALS("xsizeofvoid(=", testAst("x=sizeof(void*)"));
}
void asttemplate() const { // uninstantiated templates will have <,>,etc..
void asttemplate() { // uninstantiated templates will have <,>,etc..
ASSERT_EQUALS("a(3==", testAst("a<int>()==3"));
ASSERT_EQUALS("ab(== f(", testAst("a == b<c>(); f();"));
ASSERT_EQUALS("static_casta(i[", testAst("; static_cast<char*>(a)[i];")); // #6203
@ -8620,7 +8515,7 @@ private:
ASSERT_EQUALS("AB: f( abc+=", testAst("struct A : public B<C*> { void f() { a=b+c; } };"));
}
void astcast() const {
void astcast() {
ASSERT_EQUALS("ac&(=", testAst("a = (long)&c;"));
ASSERT_EQUALS("ac*(=", testAst("a = (Foo*)*c;"));
ASSERT_EQUALS("ac-(=", testAst("a = (long)-c;"));
@ -8631,7 +8526,7 @@ private:
ASSERT_EQUALS("xdouble123(i*(=", testAst("x = (int)(double(123)*i);"));
}
void astlambda() const {
void astlambda() {
// a lambda expression '[x](y){}' is compiled as:
// [
// `-(
@ -8664,20 +8559,18 @@ private:
"int PTR4 q4_var RBR4 = 0;\n";
// Preprocess file..
Settings settings;
Preprocessor preprocessor(settings);
Preprocessor preprocessor(settings0);
std::list<std::string> configurations;
std::string filedata = "";
std::istringstream fin(raw_code);
preprocessor.preprocess(fin, filedata, configurations, emptyString, settings._includePaths);
preprocessor.preprocess(fin, filedata, configurations, emptyString, settings0._includePaths);
const std::string code = preprocessor.getcode(filedata, emptyString, emptyString);
tokenizeAndStringify(code.c_str()); // just survive...
}
bool isStartOfExecutableScope(int offset, const char code[]) {
const Settings settings;
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -8756,10 +8649,9 @@ private:
" }\n"
" free(buf);\n"
"}\n";
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");

View File

@ -31,6 +31,7 @@ public:
}
private:
Settings settings;
void run() {
TEST_CASE(line1); // Ticket #4408
@ -41,7 +42,6 @@ private:
// inspired by #5895
void testaddtoken() {
const std::string code = "0x89504e470d0a1a0a";
Settings settings;
TokenList tokenlist(&settings);
tokenlist.addtoken(code, 1, 1, false);
ASSERT_EQUALS("9894494448401390090", tokenlist.front()->str());
@ -69,8 +69,6 @@ private:
errout.str("");
Settings settings;
TokenList tokenList(&settings);
std::istringstream istr(code);
bool res = tokenList.createTokens(istr, "a.cpp");
@ -96,8 +94,6 @@ private:
errout.str("");
const Settings settings;
// tokenize..
TokenList tokenlist(&settings);
std::istringstream istr(code);
@ -106,8 +102,6 @@ private:
ASSERT_EQUALS(Path::toNativeSeparators("[c:\\a.h:8]"), tokenlist.fileLine(tokenlist.front()));
}
};
REGISTER_TEST(TestTokenList)

View File

@ -28,8 +28,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
TEST_CASE(incondition);
TEST_CASE(return1);
TEST_CASE(return2);
@ -61,9 +64,8 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.platform(platform);
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -333,7 +335,6 @@ private:
}
void multipleFiles() {
Settings settings;
Tokenizer tokenizer(&settings, this);
CheckUnusedFunctions c(&tokenizer, &settings, nullptr);

View File

@ -28,7 +28,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
TEST_CASE(test1);
TEST_CASE(test2);
TEST_CASE(test3);
@ -79,8 +83,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
settings.platform(platform);
// Tokenize..

View File

@ -27,7 +27,11 @@ public:
}
private:
Settings settings;
void run() {
settings.addEnabled("style");
TEST_CASE(emptyclass); // #5355 - False positive: Variable is not assigned a value.
TEST_CASE(emptystruct); // #5355 - False positive: Variable is not assigned a value.
@ -164,9 +168,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -479,9 +480,6 @@ private:
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("style");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);

View File

@ -26,13 +26,12 @@ public:
TestVaarg() : TestFixture("TestVaarg") {}
private:
Settings settings;
void check(const char code[]) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -44,6 +43,8 @@ private:
}
void run() {
settings.addEnabled("warning");
TEST_CASE(wrongParameterTo_va_start);
TEST_CASE(referenceAs_va_start);
TEST_CASE(va_end_missing);

View File

@ -32,8 +32,16 @@ public:
}
private:
Settings settings;
void run() {
// strcpy cfg
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
"</def>";
settings.library.loadxmldata(cfg, sizeof(cfg));
TEST_CASE(valueFlowNumber);
TEST_CASE(valueFlowString);
TEST_CASE(valueFlowPointerAlias);
@ -72,15 +80,6 @@ private:
}
bool testValueOfX(const char code[], unsigned int linenr, int value) {
Settings settings;
// strcpy cfg
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
"</def>";
settings.library.loadxmldata(cfg, sizeof(cfg));
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -101,15 +100,6 @@ private:
bool testValueOfX(const char code[], unsigned int linenr, const char value[]) {
Settings settings;
// strcpy cfg
const char cfg[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"strcpy\"> <arg nr=\"1\"><not-null/></arg> </function>\n"
"</def>";
settings.library.loadxmldata(cfg, sizeof(cfg));
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
@ -130,7 +120,6 @@ private:
bool testConditionalValueOfX(const char code[], unsigned int linenr, int value) {
// Tokenize..
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
@ -149,7 +138,6 @@ private:
}
void bailout(const char code[]) {
Settings settings;
settings.debugwarnings = true;
// Tokenize..
@ -157,10 +145,11 @@ private:
std::istringstream istr(code);
errout.str("");
tokenizer.tokenize(istr, "test.cpp");
settings.debugwarnings = false;
}
std::list<ValueFlow::Value> tokenValues(const char code[], const char tokstr[]) {
const Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
errout.str("");

View File

@ -28,6 +28,7 @@ public:
}
private:
Settings settings;
void run() {
TEST_CASE(varid1);
@ -165,7 +166,6 @@ private:
std::string tokenize(const char code[], bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
Settings settings;
settings.standards.c = Standards::C89;
settings.standards.cpp = Standards::CPP11;