Refactorization: Improved handling of preprocessor in test suite

- Set Preprocessor::macroChar to '$' once in the beginning to avoid problems when changing the order of tests
- Removed Preprocessor usage from where it is not required
- Fixed a TODO in testuninitvar.cpp
This commit is contained in:
PKEuS 2015-10-07 14:05:55 +02:00
parent 50ed47c725
commit 07b661ef62
6 changed files with 80 additions and 125 deletions

View File

@ -6523,15 +6523,9 @@ private:
// Clear the error buffer..
errout.str("");
// Preprocess...
Preprocessor preprocessor(settings, this);
std::istringstream istrpreproc(code);
std::map<std::string, std::string> actual;
preprocessor.preprocess(istrpreproc, actual, "test.c");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(actual[""]);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.c");
tokenizer.simplifyTokenList2();

View File

@ -4006,8 +4006,6 @@ private:
}
void duplicateBranch2() {
Preprocessor::macroChar = '$';
check("void f(int x) {\n" // #4329
" if (x)\n"
" $;\n"
@ -4428,7 +4426,6 @@ private:
}
void duplicateExpression5() { // #3749 - macros with same values
Preprocessor::macroChar = '$';
check("void f() {\n"
" if ($a == $a) { }\n"
"}");

View File

@ -41,7 +41,6 @@
class TestPreprocessor : public TestFixture {
public:
TestPreprocessor() : TestFixture("TestPreprocessor") {
Preprocessor::macroChar = '$';
}
class OurPreprocessor : public Preprocessor {
@ -4010,9 +4009,9 @@ private:
void macroChar() const {
const char filedata[] = "#define X 1\nX\n";
ASSERT_EQUALS("\n$1\n", OurPreprocessor::expandMacros(filedata,nullptr));
OurPreprocessor::macroChar = char(1);
Preprocessor::macroChar = char(1);
ASSERT_EQUALS("\n" + std::string(char(1),1U) + "1\n", OurPreprocessor::expandMacros(filedata,nullptr));
OurPreprocessor::macroChar = '$';
Preprocessor::macroChar = '$';
}
void validateCfg() {

View File

@ -16,10 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "testsuite.h"
#include "preprocessor.h"
#include "options.h"
#include <cstdio>
#include <cstdlib>
#include "testsuite.h"
#include "options.h"
#include <ctime>
int main(int argc, char *argv[])
{
@ -31,6 +33,8 @@ int main(int argc, char *argv[])
#ifdef NDEBUG
try {
#endif
Preprocessor::macroChar = '$'; // While macroChar is char(1) per default outside test suite, we require it to be a human-readable character here.
options args(argc, const_cast<const char**>(argv));
std::size_t failedTestsCount = TestFixture::runTests(args);

View File

@ -20,7 +20,6 @@
#include "tokenize.h"
#include "checkstring.h"
#include "testsuite.h"
#include "preprocessor.h"
#include "testutils.h"
@ -72,31 +71,6 @@ private:
checkString.runSimplifiedChecks(&tokenizer, &settings, this);
}
void check_preprocess_suppress(const char precode[]) {
// Clear the error buffer..
errout.str("");
Settings settings;
settings.addEnabled("warning");
// Preprocess file..
Preprocessor preprocessor(settings, this);
std::list<std::string> configurations;
std::string filedata;
std::istringstream fin(precode);
preprocessor.preprocess(fin, filedata, configurations, "test.cpp", settings._includePaths);
const std::string code = preprocessor.getcode(filedata, "", "test.cpp");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check..
CheckString checkString(&tokenizer, &settings, this);
checkString.checkAlwaysTrueOrFalseStringCompare();
}
void stringLiteralWrite() {
check("void f() {\n"
" char *abc = \"abc\";\n"
@ -151,7 +125,7 @@ private:
" if (!memcmp(p + offset, p, 42)){}\n"
" if (!memcmp(offset + p, p, 42)){}\n"
" if (!memcmp(p, offset + p, 42)){}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// avoid false positives when the address is modified #6415
@ -160,7 +134,7 @@ private:
" if (!memcmp(c + offset, c, 42)){}\n"
" if (!memcmp(offset + c, c, 42)){}\n"
" if (!memcmp(c, offset + c, 42)){}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
// avoid false positives when the address is modified #6415
@ -169,103 +143,91 @@ private:
" if (!memcmp(s.c_str() + offset, s.c_str(), 42)){}\n"
" if (!memcmp(offset + s.c_str(), s.c_str(), 42)){}\n"
" if (!memcmp(s.c_str(), offset + s.c_str(), 42)){}\n"
"}\n");
"}");
ASSERT_EQUALS("", errout.str());
check_preprocess_suppress(
"#define MACRO \"00FF00\"\n"
"int main()\n"
"{\n"
" if (strcmp(MACRO,\"00FF00\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check_preprocess_suppress(
"int main()\n"
"{\n"
" if (stricmp(\"hotdog\",\"HOTdog\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
check("int main()\n"
"{\n"
" if (strcmp(\"00FF00\", \"00FF00\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check_preprocess_suppress(
"#define MACRO \"Hotdog\"\n"
"int main()\n"
"{\n"
" if (QString::compare(\"Hamburger\", MACRO) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check("int main()\n"
"{\n"
" if (stricmp(\"hotdog\",\"HOTdog\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check_preprocess_suppress(
"int main()\n"
"{\n"
" if (QString::compare(argv[2], \"hotdog\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
check("int main()\n"
"{\n"
" if (QString::compare(\"Hamburger\", \"Hotdog\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check("int main()\n"
"{\n"
" if (QString::compare(argv[2], \"hotdog\") == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("", errout.str());
check_preprocess_suppress(
"int main()\n"
"{\n"
" if (strncmp(\"hotdog\",\"hotdog\", 6) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
check("int main()\n"
"{\n"
" if (strncmp(\"hotdog\",\"hotdog\", 6) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check(
"int foo(const char *buf)\n"
"{\n"
" if (strcmp(buf, buf) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
check("int foo(const char *buf)\n"
"{\n"
" if (strcmp(buf, buf) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str());
check(
"int foo(const std::string& buf)\n"
"{\n"
" if (stricmp(buf.c_str(), buf.c_str()) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
check("int foo(const std::string& buf)\n"
"{\n"
" if (stricmp(buf.c_str(), buf.c_str()) == 0)"
" {"
" std::cout << \"Equal\n\""
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str());
check_preprocess_suppress(
"int main() {\n"
" if (\"str\" == \"str\") {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
check("int main() {\n"
" if (\"str\" == \"str\") {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check_preprocess_suppress(
"int main() {\n"
" if (\"str\" != \"str\") {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
check("int main() {\n"
" if (\"str\" != \"str\") {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout.str());
check_preprocess_suppress(
"int main() {\n"
" if (a+\"str\" != \"str\"+b) {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
check("int main() {\n"
" if (a+\"str\" != \"str\"+b) {\n"
" std::cout << \"Equal\n\"\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}

View File

@ -1989,8 +1989,7 @@ private:
TODO_ASSERT_EQUALS("", "[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str());
checkUninitVar("void f() {\n"
" #define w(x) ({ x z; (x*)z; })\n" // TODO: Preprocessor?
" int *n = w(typeof(*n));\n"
" int *n = ({ typeof(*n) z; (typeof(*n)*)z; })\n"
"}", "test.cpp", false);
ASSERT_EQUALS("", errout.str());
}