From 1fa58042fae3a7bf612262598a13a7aa2ec8ee12 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 24 Sep 2024 09:22:03 +0200 Subject: [PATCH 1/3] TestPreprocessor: removed strange `Preprocessor` inheritance and use actual implementation --- test/testpreprocessor.cpp | 260 ++++++++++++++++++-------------------- 1 file changed, 125 insertions(+), 135 deletions(-) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 257749f882b..0352333f85d 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -45,29 +45,19 @@ class TestPreprocessor : public TestFixture { public: TestPreprocessor() : TestFixture("TestPreprocessor") {} - class OurPreprocessor : public Preprocessor { - public: - - static std::string expandMacros(const char code[], ErrorLogger *errorLogger = nullptr) { - std::istringstream istr(code); - simplecpp::OutputList outputList; - std::vector files; - const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList); - std::map filedata; - simplecpp::TokenList tokens2(files); - simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI(), &outputList); - - if (errorLogger) { - const Settings settings; - Preprocessor p(settings, *errorLogger); - p.reportOutput(outputList, true); - } - - return tokens2.stringify(); - } - }; - private: + static std::string expandMacros(const char code[], ErrorLogger &errorLogger) { + std::istringstream istr(code); + simplecpp::OutputList outputList; + std::vector files; + const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList); + const Settings settings; + Preprocessor p(settings, errorLogger); + simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true); + p.reportOutput(outputList, true); + return tokens2.stringify(); + } + const Settings settings0 = settingsBuilder().severity(Severity::information).build(); void run() override { @@ -815,39 +805,39 @@ class TestPreprocessor : public TestFixture { (void)PreprocessorHelper::getcode(settings0, *this, code); } - void macro_simple1() const { + void macro_simple1() { { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA(5);\n"; - ASSERT_EQUALS("\nf ( 5 ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA (5);\n"; - ASSERT_EQUALS("\nf ( 5 ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata, *this)); } } - void macro_simple2() const { + void macro_simple2() { const char filedata[] = "#define min(x,y) x 0 ) return 1 ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nif ( temp > 0 ) return 1 ;", expandMacros(filedata, *this)); } - void macro_simple5() const { + void macro_simple5() { const char filedata[] = "#define ABC if( temp > 0 ) return 1;\n" "\n" "void foo()\n" @@ -855,132 +845,132 @@ class TestPreprocessor : public TestFixture { " int temp = 0;\n" " ABC\n" "}\n"; - ASSERT_EQUALS("\n\nvoid foo ( )\n{\nint temp = 0 ;\nif ( temp > 0 ) return 1 ;\n}", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nvoid foo ( )\n{\nint temp = 0 ;\nif ( temp > 0 ) return 1 ;\n}", expandMacros(filedata, *this)); } - void macro_simple6() const { + void macro_simple6() { const char filedata[] = "#define ABC (a+b+c)\n" "ABC\n"; - ASSERT_EQUALS("\n( a + b + c )", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n( a + b + c )", expandMacros(filedata, *this)); } - void macro_simple7() const { + void macro_simple7() { const char filedata[] = "#define ABC(str) str\n" "ABC(\"(\")\n"; - ASSERT_EQUALS("\n\"(\"", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\"(\"", expandMacros(filedata, *this)); } - void macro_simple8() const { + void macro_simple8() { const char filedata[] = "#define ABC 123\n" "#define ABCD 1234\n" "ABC ABCD\n"; - ASSERT_EQUALS("\n\n123 1234", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n123 1234", expandMacros(filedata, *this)); } - void macro_simple9() const { + void macro_simple9() { const char filedata[] = "#define ABC(a) f(a)\n" "ABC( \"\\\"\" );\n" "ABC( \"g\" );\n"; - ASSERT_EQUALS("\nf ( \"\\\"\" ) ;\nf ( \"g\" ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nf ( \"\\\"\" ) ;\nf ( \"g\" ) ;", expandMacros(filedata, *this)); } - void macro_simple10() const { + void macro_simple10() { const char filedata[] = "#define ABC(t) t x\n" "ABC(unsigned long);\n"; - ASSERT_EQUALS("\nunsigned long x ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nunsigned long x ;", expandMacros(filedata, *this)); } - void macro_simple11() const { + void macro_simple11() { const char filedata[] = "#define ABC(x) delete x\n" "ABC(a);\n"; - ASSERT_EQUALS("\ndelete a ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\ndelete a ;", expandMacros(filedata, *this)); } - void macro_simple12() const { + void macro_simple12() { const char filedata[] = "#define AB ab.AB\n" "AB.CD\n"; - ASSERT_EQUALS("\nab . AB . CD", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nab . AB . CD", expandMacros(filedata, *this)); } - void macro_simple13() const { + void macro_simple13() { const char filedata[] = "#define TRACE(x)\n" "TRACE(;if(a))\n"; - ASSERT_EQUALS("", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("", expandMacros(filedata, *this)); } - void macro_simple14() const { + void macro_simple14() { const char filedata[] = "#define A \" a \"\n" "printf(A);\n"; - ASSERT_EQUALS("\nprintf ( \" a \" ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nprintf ( \" a \" ) ;", expandMacros(filedata, *this)); } - void macro_simple15() const { + void macro_simple15() { const char filedata[] = "#define FOO\"foo\"\n" "FOO\n"; - ASSERT_EQUALS("\n\"foo\"", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\"foo\"", expandMacros(filedata, *this)); } - void macro_simple16() const { // # 4703 + void macro_simple16() { // # 4703 const char filedata[] = "#define MACRO( A, B, C ) class A##B##C##Creator {};\n" "MACRO( B\t, U , G )"; - ASSERT_EQUALS("\nclass BUGCreator { } ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nclass BUGCreator { } ;", expandMacros(filedata, *this)); } - void macro_simple17() const { // # 5074 - the Token::isExpandedMacro() doesn't always indicate properly if token comes from macro + void macro_simple17() { // # 5074 - the Token::isExpandedMacro() doesn't always indicate properly if token comes from macro // It would probably be OK if the generated code was // "\n123+$123" since the first 123 comes from the source code const char filedata[] = "#define MACRO(A) A+123\n" "MACRO(123)"; - ASSERT_EQUALS("\n123 + 123", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n123 + 123", expandMacros(filedata, *this)); } - void macro_simple18() const { // (1e-7) + void macro_simple18() { // (1e-7) const char filedata1[] = "#define A (1e-7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1e-7 ) ;", OurPreprocessor::expandMacros(filedata1)); + ASSERT_EQUALS("\na = ( 1e-7 ) ;", expandMacros(filedata1, *this)); const char filedata2[] = "#define A (1E-7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1E-7 ) ;", OurPreprocessor::expandMacros(filedata2)); + ASSERT_EQUALS("\na = ( 1E-7 ) ;", expandMacros(filedata2, *this)); const char filedata3[] = "#define A (1e+7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1e+7 ) ;", OurPreprocessor::expandMacros(filedata3)); + ASSERT_EQUALS("\na = ( 1e+7 ) ;", expandMacros(filedata3, *this)); const char filedata4[] = "#define A (1.e+7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1.e+7 ) ;", OurPreprocessor::expandMacros(filedata4)); + ASSERT_EQUALS("\na = ( 1.e+7 ) ;", expandMacros(filedata4, *this)); const char filedata5[] = "#define A (1.7f)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1.7f ) ;", OurPreprocessor::expandMacros(filedata5)); + ASSERT_EQUALS("\na = ( 1.7f ) ;", expandMacros(filedata5, *this)); const char filedata6[] = "#define A (.1)\n" "a=A;"; - ASSERT_EQUALS("\na = ( .1 ) ;", OurPreprocessor::expandMacros(filedata6)); + ASSERT_EQUALS("\na = ( .1 ) ;", expandMacros(filedata6, *this)); const char filedata7[] = "#define A (1.)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1. ) ;", OurPreprocessor::expandMacros(filedata7)); + ASSERT_EQUALS("\na = ( 1. ) ;", expandMacros(filedata7, *this)); const char filedata8[] = "#define A (8.0E+007)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 8.0E+007 ) ;", OurPreprocessor::expandMacros(filedata8)); + ASSERT_EQUALS("\na = ( 8.0E+007 ) ;", expandMacros(filedata8, *this)); } - void macroInMacro1() const { + void macroInMacro1() { { const char filedata[] = "#define A(m) long n = m; n++;\n" "#define B(n) A(n)\n" "B(0)\n"; - ASSERT_EQUALS("\n\nlong n = 0 ; n ++ ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nlong n = 0 ; n ++ ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define A B\n" "#define B 3\n" "A\n"; - ASSERT_EQUALS("\n\n3", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n3", expandMacros(filedata, *this)); } { @@ -991,34 +981,34 @@ class TestPreprocessor : public TestFixture { "ABC(2,3);\n" "ABC(4,5,6);\n"; - ASSERT_EQUALS("\n\n\n1 + 0 * 0 ;\n2 + 03 * 0 ;\n4 + 05 * 06 ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n1 + 0 * 0 ;\n2 + 03 * 0 ;\n4 + 05 * 06 ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define A 4\n" "#define B(a) a,A\n" "B(2);\n"; - ASSERT_EQUALS("\n\n2 , 4 ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n2 , 4 ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define A(x) (x)\n" "#define B )A(\n" "#define C )A(\n"; - ASSERT_EQUALS("", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("", expandMacros(filedata, *this)); } { const char filedata[] = "#define A(x) (x*2)\n" "#define B A(\n" "foo B(i));\n"; - ASSERT_EQUALS("\n\nfoo ( ( i ) * 2 ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nfoo ( ( i ) * 2 ) ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define foo foo\n" "foo\n"; - ASSERT_EQUALS("\nfoo", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nfoo", expandMacros(filedata, *this)); } { @@ -1027,7 +1017,7 @@ class TestPreprocessor : public TestFixture { "#define A(name) void foo##name() { do { B(1, 2); }\n" "A(0)\n" "A(1)\n"; - ASSERT_EQUALS("\n\nvoid foo0 ( ) { do { } while ( 0 ) ; }\nvoid foo1 ( ) { do { } while ( 0 ) ; }", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nvoid foo0 ( ) { do { } while ( 0 ) ; }\nvoid foo1 ( ) { do { } while ( 0 ) ; }", expandMacros(filedata, *this)); } { @@ -1035,7 +1025,7 @@ class TestPreprocessor : public TestFixture { "#define B(x) (\n" "#define A() B(xx)\n" "B(1) A() ) )\n"; - ASSERT_EQUALS("\n\n( ( ) )", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n( ( ) )", expandMacros(filedata, *this)); } { @@ -1043,25 +1033,25 @@ class TestPreprocessor : public TestFixture { "#define PTR1 (\n" "#define PTR2 PTR1 PTR1\n" "int PTR2 PTR2 foo )))) = 0;\n"; - ASSERT_EQUALS("\n\nint ( ( ( ( foo ) ) ) ) = 0 ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\nint ( ( ( ( foo ) ) ) ) = 0 ;", expandMacros(filedata, *this)); } { const char filedata[] = "#define PTR1 (\n" "PTR1 PTR1\n"; - ASSERT_EQUALS("\n( (", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n( (", expandMacros(filedata, *this)); } } - void macroInMacro2() const { + void macroInMacro2() { const char filedata[] = "#define A(x) a##x\n" "#define B 0\n" "A(B)\n"; - ASSERT_EQUALS("\n\naB", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\naB", expandMacros(filedata, *this)); } - void macro_linenumbers() const { + void macro_linenumbers() { const char filedata[] = "#define AAA(a)\n" "AAA(5\n" "\n" @@ -1072,23 +1062,23 @@ class TestPreprocessor : public TestFixture { "\n" "\n" "int a ;", - OurPreprocessor::expandMacros(filedata)); + expandMacros(filedata, *this)); } - void macro_nopar() const { + void macro_nopar() { const char filedata[] = "#define AAA( ) { NULL }\n" "AAA()\n"; - ASSERT_EQUALS("\n{ NULL }", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n{ NULL }", expandMacros(filedata, *this)); } - void macro_incdec() const { + void macro_incdec() { const char filedata[] = "#define M1(X) 1+X\n" "#define M2(X) 2-X\n" "M1(+1) M2(-1)\n"; - ASSERT_EQUALS("\n\n1 + + 1 2 - - 1", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n1 + + 1 2 - - 1", expandMacros(filedata, *this)); } - void macro_switchCase() const { + void macro_switchCase() { { // Make sure "case 2" doesn't become "case2" const char filedata[] = "#define A( b ) " @@ -1097,14 +1087,14 @@ class TestPreprocessor : public TestFixture { " break; " "}\n" "A( 5 );\n"; - ASSERT_EQUALS("\nswitch ( a ) { case 2 : break ; } ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nswitch ( a ) { case 2 : break ; } ;", expandMacros(filedata, *this)); } { // Make sure "2 BB" doesn't become "2BB" const char filedata[] = "#define A() AA : 2 BB\n" "A();\n"; - ASSERT_EQUALS("\nAA : 2 BB ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nAA : 2 BB ;", expandMacros(filedata, *this)); } { @@ -1112,7 +1102,7 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() break;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } break ; ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n{ } break ; ;", expandMacros(filedata, *this)); } @@ -1121,7 +1111,7 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() _break;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } _break ; ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n{ } _break ; ;", expandMacros(filedata, *this)); } @@ -1130,14 +1120,14 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() 5;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } 5 ; ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n{ } 5 ; ;", expandMacros(filedata, *this)); } } - void macro_NULL() const { + void macro_NULL() { // See ticket #4482 - UB when passing NULL to variadic function - ASSERT_EQUALS("\n0", OurPreprocessor::expandMacros("#define null 0\nnull")); - TODO_ASSERT_EQUALS("\nNULL", "\n0", OurPreprocessor::expandMacros("#define NULL 0\nNULL")); // TODO: Let the tokenizer handle NULL? + ASSERT_EQUALS("\n0", expandMacros("#define null 0\nnull", *this)); + TODO_ASSERT_EQUALS("\nNULL", "\n0", expandMacros("#define NULL 0\nNULL", *this)); // TODO: Let the tokenizer handle NULL? } void string1() { @@ -1154,19 +1144,19 @@ class TestPreprocessor : public TestFixture { ASSERT_EQUALS("int main ( ) { const char * a = \"#define A\" ; }", actual.at("")); } - void string2() const { + void string2() { const char filedata[] = "#define AAA 123\n" "str = \"AAA\"\n"; // Compare results.. - ASSERT_EQUALS("\nstr = \"AAA\"", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nstr = \"AAA\"", expandMacros(filedata, *this)); } - void string3() const { + void string3() { const char filedata[] = "str(\";\");\n"; // Compare results.. - ASSERT_EQUALS("str ( \";\" ) ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("str ( \";\" ) ;", expandMacros(filedata, *this)); } @@ -1178,7 +1168,7 @@ class TestPreprocessor : public TestFixture { "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\nchar b = 0 ;", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\nchar b = 0 ;", expandMacros(filedata, *this)); } { @@ -1191,44 +1181,44 @@ class TestPreprocessor : public TestFixture { } } - void defdef() const { + void defdef() { const char filedata[] = "#define AAA 123\n" "#define AAA 456\n" "#define AAA 789\n" "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\n789", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n\n789", expandMacros(filedata, *this)); } - void preprocessor_doublesharp() const { + void preprocessor_doublesharp() { // simple testcase without ## const char filedata1[] = "#define TEST(var,val) var = val\n" "TEST(foo,20);\n"; - ASSERT_EQUALS("\nfoo = 20 ;", OurPreprocessor::expandMacros(filedata1)); + ASSERT_EQUALS("\nfoo = 20 ;", expandMacros(filedata1, *this)); // simple testcase with ## const char filedata2[] = "#define TEST(var,val) var##_##val = val\n" "TEST(foo,20);\n"; - ASSERT_EQUALS("\nfoo_20 = 20 ;", OurPreprocessor::expandMacros(filedata2)); + ASSERT_EQUALS("\nfoo_20 = 20 ;", expandMacros(filedata2, *this)); // concat macroname const char filedata3[] = "#define ABCD 123\n" "#define A(B) A##B\n" "A(BCD)\n"; - ASSERT_EQUALS("\n\n123", OurPreprocessor::expandMacros(filedata3)); + ASSERT_EQUALS("\n\n123", expandMacros(filedata3, *this)); // Ticket #1802 - inner ## must be expanded before outer macro const char filedata4[] = "#define A(B) A##B\n" "#define a(B) A(B)\n" "a(A(B))\n"; - ASSERT_EQUALS("\n\nAAB", OurPreprocessor::expandMacros(filedata4)); + ASSERT_EQUALS("\n\nAAB", expandMacros(filedata4, *this)); // Ticket #1802 - inner ## must be expanded before outer macro const char filedata5[] = "#define AB(A,B) A##B\n" "#define ab(A,B) AB(A,B)\n" "ab(a,AB(b,c))\n"; - ASSERT_EQUALS("\n\nabc", OurPreprocessor::expandMacros(filedata5)); + ASSERT_EQUALS("\n\nabc", expandMacros(filedata5, *this)); // Ticket #1802 const char filedata6[] = "#define AB_(A,B) A ## B\n" @@ -1236,7 +1226,7 @@ class TestPreprocessor : public TestFixture { "#define ab(suf) AB(X, AB_(_, suf))\n" "#define X x\n" "ab(y)\n"; - ASSERT_EQUALS("\n\n\n\nx_y", OurPreprocessor::expandMacros(filedata6)); + ASSERT_EQUALS("\n\n\n\nx_y", expandMacros(filedata6, *this)); } @@ -1256,46 +1246,46 @@ class TestPreprocessor : public TestFixture { ASSERT_EQUALS("int main ( )\n{\nconst char * a = \"#include \" ;\nreturn 0 ;\n}", actual.at("")); } - void va_args_1() const { + void va_args_1() { const char filedata[] = "#define DBG(fmt...) printf(fmt)\n" "DBG(\"[0x%lx-0x%lx)\", pstart, pend);\n"; // Preprocess.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); ASSERT_EQUALS("\nprintf ( \"[0x%lx-0x%lx)\" , pstart , pend ) ;", actual); } /* - void va_args_2() const { + void va_args_2() { const char filedata[] = "#define DBG(fmt, args...) printf(fmt, ## args)\n" "DBG(\"hello\");\n"; // Preprocess.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); // invalid code ASSERT_EQUALS("\nprintf ( \"hello\" ) ;", actual); } */ - void va_args_3() const { + void va_args_3() { const char filedata[] = "#define FRED(...) { fred(__VA_ARGS__); }\n" "FRED(123)\n"; - ASSERT_EQUALS("\n{ fred ( 123 ) ; }", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n{ fred ( 123 ) ; }", expandMacros(filedata, *this)); } - void va_args_4() const { + void va_args_4() { const char filedata[] = "#define FRED(name, ...) name (__VA_ARGS__)\n" "FRED(abc, 123)\n"; - ASSERT_EQUALS("\nabc ( 123 )", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\nabc ( 123 )", expandMacros(filedata, *this)); } - void va_args_5() const { + void va_args_5() { const char filedata1[] = "#define A(...) #__VA_ARGS__\n" "A(123)\n"; - ASSERT_EQUALS("\n\"123\"", OurPreprocessor::expandMacros(filedata1)); + ASSERT_EQUALS("\n\"123\"", expandMacros(filedata1, *this)); const char filedata2[] = "#define A(X,...) X(#__VA_ARGS__)\n" "A(f,123)\n"; - ASSERT_EQUALS("\nf ( \"123\" )", OurPreprocessor::expandMacros(filedata2)); + ASSERT_EQUALS("\nf ( \"123\" )", expandMacros(filedata2, *this)); } @@ -1317,52 +1307,52 @@ class TestPreprocessor : public TestFixture { } - void stringify() const { + void stringify() { const char filedata[] = "#define STRINGIFY(x) #x\n" "STRINGIFY(abc)\n"; // expand macros.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); ASSERT_EQUALS("\n\"abc\"", actual); } - void stringify2() const { + void stringify2() { const char filedata[] = "#define A(x) g(#x)\n" "A(abc);\n"; // expand macros.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); ASSERT_EQUALS("\ng ( \"abc\" ) ;", actual); } - void stringify3() const { + void stringify3() { const char filedata[] = "#define A(x) g(#x)\n" "A( abc);\n"; // expand macros.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); ASSERT_EQUALS("\ng ( \"abc\" ) ;", actual); } - void stringify4() const { + void stringify4() { const char filedata[] = "#define A(x) #x\n" "1 A(\n" "abc\n" ") 2\n"; // expand macros.. - std::string actual = OurPreprocessor::expandMacros(filedata); + std::string actual = expandMacros(filedata, *this); ASSERT_EQUALS("\n1 \"abc\"\n\n2", actual); } - void stringify5() const { + void stringify5() { const char filedata[] = "#define A(x) a(#x,x)\n" "A(foo(\"\\\"\"))\n"; - ASSERT_EQUALS("\na ( \"foo(\\\"\\\\\\\"\\\")\" , foo ( \"\\\"\" ) )", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\na ( \"foo(\\\"\\\\\\\"\\\")\" , foo ( \"\\\"\" ) )", expandMacros(filedata, *this)); } void pragma() { @@ -1453,7 +1443,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // expand macros.. - const std::string actual(OurPreprocessor::expandMacros(filedata, this)); + const std::string actual(expandMacros(filedata, *this)); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[file.cpp:3]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); @@ -1466,7 +1456,7 @@ class TestPreprocessor : public TestFixture { "#endfile\n"; // expand macros.. - const std::string actual(OurPreprocessor::expandMacros(filedata, this)); + const std::string actual(expandMacros(filedata, *this)); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[abc.h:2]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); @@ -1479,7 +1469,7 @@ class TestPreprocessor : public TestFixture { "\"\n"; // expand macros.. - const std::string actual(OurPreprocessor::expandMacros(filedata, this)); + const std::string actual(expandMacros(filedata, *this)); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[file.cpp:2]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); @@ -1491,7 +1481,7 @@ class TestPreprocessor : public TestFixture { "int a = A;\n"; // expand macros.. - const std::string actual(OurPreprocessor::expandMacros(filedata, this)); + const std::string actual(expandMacros(filedata, *this)); ASSERT_EQUALS("", actual); ASSERT_EQUALS("[file.cpp:2]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); @@ -1508,7 +1498,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // expand macros.. - (void)OurPreprocessor::expandMacros(filedata, this); + (void)expandMacros(filedata, *this); ASSERT_EQUALS("[file.cpp:7]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout_str()); } From 8cc377075845def7aadfc728f0d365dce98f0dad Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 24 Sep 2024 09:25:25 +0200 Subject: [PATCH 2/3] PreprocessHelper: use `Preprocessor` instead of hand-rolling the logic --- test/helpers.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/helpers.cpp b/test/helpers.cpp index 633becd1e9b..83825ce3a29 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -165,9 +165,23 @@ std::map PreprocessorHelper::getcode(const Settings& s void PreprocessorHelper::preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger) { - preprocess(code, files, tokenizer, errorlogger, simplecpp::DUI()); + // TODO: make sure the given Tokenizer has not been used yet + + // TODO: get rid of stream + std::istringstream istr(code); + const simplecpp::TokenList tokens1(istr, files, files[0]); + + Preprocessor preprocessor(tokenizer.getSettings(), errorlogger); + simplecpp::TokenList tokens2 = preprocessor.preprocess(tokens1, "", files, true); + + // Tokenizer.. + tokenizer.list.createTokens(std::move(tokens2)); + + std::list directives = preprocessor.createDirectives(tokens1); + tokenizer.setDirectives(std::move(directives)); } +// TODO: get rid of this void PreprocessorHelper::preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui) { // TODO: make sure the given Tokenizer has not been used yet From c556434e65bea8b89c850fb5281d5291ddaeba86 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 24 Sep 2024 09:38:14 +0200 Subject: [PATCH 3/3] TestType: removed `DUI` usage from `integerOverflow()` --- test/testtype.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/testtype.cpp b/test/testtype.cpp index fb9f6fee71a..f9e5a5359e4 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -76,12 +76,12 @@ class TestType : public TestFixture { #define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__) template - void checkP_(const char* file, int line, const char (&code)[size], const Settings& settings, const char filename[] = "test.cpp", const simplecpp::DUI& dui = simplecpp::DUI()) { + void checkP_(const char* file, int line, const char (&code)[size], const Settings& settings, const char filename[] = "test.cpp") { const Settings settings1 = settingsBuilder(settings).severity(Severity::warning).severity(Severity::portability).build(); std::vector files(1, filename); Tokenizer tokenizer(settings1, *this); - PreprocessorHelper::preprocess(code, files, tokenizer, *this, dui); + PreprocessorHelper::preprocess(code, files, tokenizer, *this); // Tokenizer.. ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line); @@ -501,13 +501,9 @@ class TestType : public TestFixture { } void integerOverflow() { // #11794 - // TODO: needs to use preprocessing production code - simplecpp::DUI dui; - dui.std = "c++11"; - // this is set by the production code via cppcheck::Platform::getLimitDefines() - dui.defines.emplace_back("INT_MIN=-2147483648"); - dui.defines.emplace_back("INT32_MAX=2147483647"); - dui.defines.emplace_back("int32_t=int"); + // std.cfg for int32_t + // Platform::Unix32 for INT_MIN=-2147483648 and INT32_MAX=2147483647 + const Settings s = settingsBuilder().library("std.cfg").cpp(Standards::CPP11).platform(Platform::Unix32).build(); checkP("int fun(int x)\n" "{\n" @@ -517,14 +513,14 @@ class TestType : public TestFixture { "int f()\n" "{\n" " fun(INT_MIN);\n" - "}", settingsDefault, "test.cpp", dui); + "}", s, "test.cpp"); ASSERT_EQUALS("[test.cpp:3]: (error) Signed integer overflow for expression '-x'.\n", errout_str()); checkP("void f() {\n" // #8399 " int32_t i = INT32_MAX;\n" " i << 1;\n" " i << 2;\n" - "}", settingsDefault, "test.cpp", dui); + "}", s, "test.cpp"); ASSERT_EQUALS("[test.cpp:4]: (error) Signed integer overflow for expression 'i<<2'.\n", errout_str()); } };