From 66c8a453924a0f935770943d50eac607411828e1 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 26 Sep 2024 11:38:25 +0200 Subject: [PATCH 1/3] PreprocessorHelper: moved `getRemarkComments()` to `TestPreprocessor` --- test/helpers.cpp | 12 ------------ test/helpers.h | 3 --- test/testpreprocessor.cpp | 18 +++++++++++++++--- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/test/helpers.cpp b/test/helpers.cpp index 1030fe5ef69..719b2cf1df1 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -202,18 +202,6 @@ void PreprocessorHelper::preprocess(const char code[], std::vector tokenizer.setDirectives(std::move(directives)); } -std::vector PreprocessorHelper::getRemarkComments(const char code[], ErrorLogger& errorLogger) -{ - std::vector files{"test.cpp"}; - std::istringstream istr(code); - const simplecpp::TokenList tokens1(istr, files, files[0]); - - const Settings settings; - - const Preprocessor preprocessor(settings, errorLogger); - return preprocessor.getRemarkComments(tokens1); -} - bool LibraryHelper::loadxmldata(Library &lib, const char xmldata[], std::size_t len) { tinyxml2::XMLDocument doc; diff --git a/test/helpers.h b/test/helpers.h index f85f5a01fe7..880ea7ae09b 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -175,9 +175,6 @@ class PreprocessorHelper static void preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger); static void preprocess(const char code[], std::vector &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui); - /** get remark comments */ - static std::vector getRemarkComments(const char code[], ErrorLogger& errorLogger); - private: static std::map getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::set cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr); }; diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index e73d6373f6c..d6746ecd2d3 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -60,6 +60,18 @@ class TestPreprocessor : public TestFixture { return tokens2.stringify(); } + static std::vector getRemarkComments(const char code[], ErrorLogger& errorLogger) + { + std::vector files{"test.cpp"}; + std::istringstream istr(code); + const simplecpp::TokenList tokens1(istr, files, files[0]); + + const Settings settings; + + const Preprocessor preprocessor(settings, errorLogger); + return preprocessor.getRemarkComments(tokens1); + } + const Settings settings0 = settingsBuilder().severity(Severity::information).build(); void run() override { @@ -1917,7 +1929,7 @@ class TestPreprocessor : public TestFixture { void remarkComment1() { const char code[] = "// REMARK: assignment with 1\n" "x=1;\n"; - const auto remarkComments = PreprocessorHelper::getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code, *this); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(2, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1", remarkComments[0].str); @@ -1925,7 +1937,7 @@ class TestPreprocessor : public TestFixture { void remarkComment2() { const char code[] = "x=1; ///REMARK assignment with 1\n"; - const auto remarkComments = PreprocessorHelper::getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code, *this); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(1, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1", remarkComments[0].str); @@ -1934,7 +1946,7 @@ class TestPreprocessor : public TestFixture { void remarkComment3() { const char code[] = "/** REMARK: assignment with 1 */\n" "x=1;\n"; - const auto remarkComments = PreprocessorHelper::getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code, *this); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(2, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1 ", remarkComments[0].str); From 33d4de3cd150f6abf7937339002163e805d72228 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 26 Sep 2024 11:40:58 +0200 Subject: [PATCH 2/3] TestPreprocessor: made some member functions non-static --- test/testpreprocessor.cpp | 162 +++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index d6746ecd2d3..64f22a0874b 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -48,19 +48,19 @@ class TestPreprocessor : public TestFixture { TestPreprocessor() : TestFixture("TestPreprocessor") {} private: - static std::string expandMacros(const char code[], ErrorLogger &errorLogger) { + std::string expandMacros(const char code[]) { 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); + Preprocessor p(settings, *this); simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true); p.reportOutput(outputList, true); return tokens2.stringify(); } - static std::vector getRemarkComments(const char code[], ErrorLogger& errorLogger) + std::vector getRemarkComments(const char code[]) { std::vector files{"test.cpp"}; std::istringstream istr(code); @@ -68,7 +68,7 @@ class TestPreprocessor : public TestFixture { const Settings settings; - const Preprocessor preprocessor(settings, errorLogger); + const Preprocessor preprocessor(settings, *this); return preprocessor.getRemarkComments(tokens1); } @@ -823,32 +823,32 @@ class TestPreprocessor : public TestFixture { { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA(5);\n"; - ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata)); } { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA (5);\n"; - ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nf ( 5 ) ;", expandMacros(filedata)); } } void macro_simple2() { const char filedata[] = "#define min(x,y) x 0 ) return 1 ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nif ( temp > 0 ) return 1 ;", expandMacros(filedata)); } void macro_simple5() { @@ -859,75 +859,75 @@ 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}", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\nvoid foo ( )\n{\nint temp = 0 ;\nif ( temp > 0 ) return 1 ;\n}", expandMacros(filedata)); } void macro_simple6() { const char filedata[] = "#define ABC (a+b+c)\n" "ABC\n"; - ASSERT_EQUALS("\n( a + b + c )", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n( a + b + c )", expandMacros(filedata)); } void macro_simple7() { const char filedata[] = "#define ABC(str) str\n" "ABC(\"(\")\n"; - ASSERT_EQUALS("\n\"(\"", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\"(\"", expandMacros(filedata)); } void macro_simple8() { const char filedata[] = "#define ABC 123\n" "#define ABCD 1234\n" "ABC ABCD\n"; - ASSERT_EQUALS("\n\n123 1234", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n123 1234", expandMacros(filedata)); } void macro_simple9() { const char filedata[] = "#define ABC(a) f(a)\n" "ABC( \"\\\"\" );\n" "ABC( \"g\" );\n"; - ASSERT_EQUALS("\nf ( \"\\\"\" ) ;\nf ( \"g\" ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nf ( \"\\\"\" ) ;\nf ( \"g\" ) ;", expandMacros(filedata)); } void macro_simple10() { const char filedata[] = "#define ABC(t) t x\n" "ABC(unsigned long);\n"; - ASSERT_EQUALS("\nunsigned long x ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nunsigned long x ;", expandMacros(filedata)); } void macro_simple11() { const char filedata[] = "#define ABC(x) delete x\n" "ABC(a);\n"; - ASSERT_EQUALS("\ndelete a ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\ndelete a ;", expandMacros(filedata)); } void macro_simple12() { const char filedata[] = "#define AB ab.AB\n" "AB.CD\n"; - ASSERT_EQUALS("\nab . AB . CD", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nab . AB . CD", expandMacros(filedata)); } void macro_simple13() { const char filedata[] = "#define TRACE(x)\n" "TRACE(;if(a))\n"; - ASSERT_EQUALS("", expandMacros(filedata, *this)); + ASSERT_EQUALS("", expandMacros(filedata)); } void macro_simple14() { const char filedata[] = "#define A \" a \"\n" "printf(A);\n"; - ASSERT_EQUALS("\nprintf ( \" a \" ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nprintf ( \" a \" ) ;", expandMacros(filedata)); } void macro_simple15() { const char filedata[] = "#define FOO\"foo\"\n" "FOO\n"; - ASSERT_EQUALS("\n\"foo\"", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\"foo\"", expandMacros(filedata)); } 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 { } ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nclass BUGCreator { } ;", expandMacros(filedata)); } void macro_simple17() { // # 5074 - the Token::isExpandedMacro() doesn't always indicate properly if token comes from macro @@ -935,41 +935,41 @@ class TestPreprocessor : public TestFixture { // "\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", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n123 + 123", expandMacros(filedata)); } void macro_simple18() { // (1e-7) const char filedata1[] = "#define A (1e-7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1e-7 ) ;", expandMacros(filedata1, *this)); + ASSERT_EQUALS("\na = ( 1e-7 ) ;", expandMacros(filedata1)); const char filedata2[] = "#define A (1E-7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1E-7 ) ;", expandMacros(filedata2, *this)); + ASSERT_EQUALS("\na = ( 1E-7 ) ;", expandMacros(filedata2)); const char filedata3[] = "#define A (1e+7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1e+7 ) ;", expandMacros(filedata3, *this)); + ASSERT_EQUALS("\na = ( 1e+7 ) ;", expandMacros(filedata3)); const char filedata4[] = "#define A (1.e+7)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1.e+7 ) ;", expandMacros(filedata4, *this)); + ASSERT_EQUALS("\na = ( 1.e+7 ) ;", expandMacros(filedata4)); const char filedata5[] = "#define A (1.7f)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1.7f ) ;", expandMacros(filedata5, *this)); + ASSERT_EQUALS("\na = ( 1.7f ) ;", expandMacros(filedata5)); const char filedata6[] = "#define A (.1)\n" "a=A;"; - ASSERT_EQUALS("\na = ( .1 ) ;", expandMacros(filedata6, *this)); + ASSERT_EQUALS("\na = ( .1 ) ;", expandMacros(filedata6)); const char filedata7[] = "#define A (1.)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 1. ) ;", expandMacros(filedata7, *this)); + ASSERT_EQUALS("\na = ( 1. ) ;", expandMacros(filedata7)); const char filedata8[] = "#define A (8.0E+007)\n" "a=A;"; - ASSERT_EQUALS("\na = ( 8.0E+007 ) ;", expandMacros(filedata8, *this)); + ASSERT_EQUALS("\na = ( 8.0E+007 ) ;", expandMacros(filedata8)); } void macroInMacro1() { @@ -977,14 +977,14 @@ class TestPreprocessor : public TestFixture { 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 ++ ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\nlong n = 0 ; n ++ ;", expandMacros(filedata)); } { const char filedata[] = "#define A B\n" "#define B 3\n" "A\n"; - ASSERT_EQUALS("\n\n3", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n3", expandMacros(filedata)); } { @@ -995,34 +995,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 ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\n1 + 0 * 0 ;\n2 + 03 * 0 ;\n4 + 05 * 06 ;", expandMacros(filedata)); } { const char filedata[] = "#define A 4\n" "#define B(a) a,A\n" "B(2);\n"; - ASSERT_EQUALS("\n\n2 , 4 ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n2 , 4 ;", expandMacros(filedata)); } { const char filedata[] = "#define A(x) (x)\n" "#define B )A(\n" "#define C )A(\n"; - ASSERT_EQUALS("", expandMacros(filedata, *this)); + ASSERT_EQUALS("", expandMacros(filedata)); } { const char filedata[] = "#define A(x) (x*2)\n" "#define B A(\n" "foo B(i));\n"; - ASSERT_EQUALS("\n\nfoo ( ( i ) * 2 ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\nfoo ( ( i ) * 2 ) ;", expandMacros(filedata)); } { const char filedata[] = "#define foo foo\n" "foo\n"; - ASSERT_EQUALS("\nfoo", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nfoo", expandMacros(filedata)); } { @@ -1031,7 +1031,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 ) ; }", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\nvoid foo0 ( ) { do { } while ( 0 ) ; }\nvoid foo1 ( ) { do { } while ( 0 ) ; }", expandMacros(filedata)); } { @@ -1039,7 +1039,7 @@ class TestPreprocessor : public TestFixture { "#define B(x) (\n" "#define A() B(xx)\n" "B(1) A() ) )\n"; - ASSERT_EQUALS("\n\n( ( ) )", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n( ( ) )", expandMacros(filedata)); } { @@ -1047,14 +1047,14 @@ class TestPreprocessor : public TestFixture { "#define PTR1 (\n" "#define PTR2 PTR1 PTR1\n" "int PTR2 PTR2 foo )))) = 0;\n"; - ASSERT_EQUALS("\n\nint ( ( ( ( foo ) ) ) ) = 0 ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\nint ( ( ( ( foo ) ) ) ) = 0 ;", expandMacros(filedata)); } { const char filedata[] = "#define PTR1 (\n" "PTR1 PTR1\n"; - ASSERT_EQUALS("\n( (", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n( (", expandMacros(filedata)); } } @@ -1062,7 +1062,7 @@ class TestPreprocessor : public TestFixture { const char filedata[] = "#define A(x) a##x\n" "#define B 0\n" "A(B)\n"; - ASSERT_EQUALS("\n\naB", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\naB", expandMacros(filedata)); } void macro_linenumbers() { @@ -1076,20 +1076,20 @@ class TestPreprocessor : public TestFixture { "\n" "\n" "int a ;", - expandMacros(filedata, *this)); + expandMacros(filedata)); } void macro_nopar() { const char filedata[] = "#define AAA( ) { NULL }\n" "AAA()\n"; - ASSERT_EQUALS("\n{ NULL }", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n{ NULL }", expandMacros(filedata)); } 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", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n1 + + 1 2 - - 1", expandMacros(filedata)); } void macro_switchCase() { @@ -1101,14 +1101,14 @@ class TestPreprocessor : public TestFixture { " break; " "}\n" "A( 5 );\n"; - ASSERT_EQUALS("\nswitch ( a ) { case 2 : break ; } ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nswitch ( a ) { case 2 : break ; } ;", expandMacros(filedata)); } { // Make sure "2 BB" doesn't become "2BB" const char filedata[] = "#define A() AA : 2 BB\n" "A();\n"; - ASSERT_EQUALS("\nAA : 2 BB ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nAA : 2 BB ;", expandMacros(filedata)); } { @@ -1116,7 +1116,7 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() break;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } break ; ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\n{ } break ; ;", expandMacros(filedata)); } @@ -1125,7 +1125,7 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() _break;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } _break ; ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\n{ } _break ; ;", expandMacros(filedata)); } @@ -1134,14 +1134,14 @@ class TestPreprocessor : public TestFixture { "#define B() A\n" "#define C( a ) B() 5;\n" "{C( 2 );\n"; - ASSERT_EQUALS("\n\n\n{ } 5 ; ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\n{ } 5 ; ;", expandMacros(filedata)); } } void macro_NULL() { // See ticket #4482 - UB when passing NULL to variadic function - 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? + ASSERT_EQUALS("\n0", expandMacros("#define null 0\nnull")); + TODO_ASSERT_EQUALS("\nNULL", "\n0", expandMacros("#define NULL 0\nNULL")); // TODO: Let the tokenizer handle NULL? } void string1() { @@ -1163,14 +1163,14 @@ class TestPreprocessor : public TestFixture { "str = \"AAA\"\n"; // Compare results.. - ASSERT_EQUALS("\nstr = \"AAA\"", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nstr = \"AAA\"", expandMacros(filedata)); } void string3() { const char filedata[] = "str(\";\");\n"; // Compare results.. - ASSERT_EQUALS("str ( \";\" ) ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("str ( \";\" ) ;", expandMacros(filedata)); } @@ -1182,7 +1182,7 @@ class TestPreprocessor : public TestFixture { "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\nchar b = 0 ;", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\nchar b = 0 ;", expandMacros(filedata)); } { @@ -1202,37 +1202,37 @@ class TestPreprocessor : public TestFixture { "AAA\n"; // Compare results.. - ASSERT_EQUALS("\n\n\n789", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n\n\n789", expandMacros(filedata)); } void preprocessor_doublesharp() { // simple testcase without ## const char filedata1[] = "#define TEST(var,val) var = val\n" "TEST(foo,20);\n"; - ASSERT_EQUALS("\nfoo = 20 ;", expandMacros(filedata1, *this)); + ASSERT_EQUALS("\nfoo = 20 ;", expandMacros(filedata1)); // simple testcase with ## const char filedata2[] = "#define TEST(var,val) var##_##val = val\n" "TEST(foo,20);\n"; - ASSERT_EQUALS("\nfoo_20 = 20 ;", expandMacros(filedata2, *this)); + ASSERT_EQUALS("\nfoo_20 = 20 ;", expandMacros(filedata2)); // concat macroname const char filedata3[] = "#define ABCD 123\n" "#define A(B) A##B\n" "A(BCD)\n"; - ASSERT_EQUALS("\n\n123", expandMacros(filedata3, *this)); + ASSERT_EQUALS("\n\n123", expandMacros(filedata3)); // 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", expandMacros(filedata4, *this)); + ASSERT_EQUALS("\n\nAAB", expandMacros(filedata4)); // 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", expandMacros(filedata5, *this)); + ASSERT_EQUALS("\n\nabc", expandMacros(filedata5)); // Ticket #1802 const char filedata6[] = "#define AB_(A,B) A ## B\n" @@ -1240,7 +1240,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", expandMacros(filedata6, *this)); + ASSERT_EQUALS("\n\n\n\nx_y", expandMacros(filedata6)); } @@ -1265,7 +1265,7 @@ class TestPreprocessor : public TestFixture { "DBG(\"[0x%lx-0x%lx)\", pstart, pend);\n"; // Preprocess.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); ASSERT_EQUALS("\nprintf ( \"[0x%lx-0x%lx)\" , pstart , pend ) ;", actual); } @@ -1275,7 +1275,7 @@ class TestPreprocessor : public TestFixture { "DBG(\"hello\");\n"; // Preprocess.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); // invalid code ASSERT_EQUALS("\nprintf ( \"hello\" ) ;", actual); } @@ -1283,23 +1283,23 @@ class TestPreprocessor : public TestFixture { void va_args_3() { const char filedata[] = "#define FRED(...) { fred(__VA_ARGS__); }\n" "FRED(123)\n"; - ASSERT_EQUALS("\n{ fred ( 123 ) ; }", expandMacros(filedata, *this)); + ASSERT_EQUALS("\n{ fred ( 123 ) ; }", expandMacros(filedata)); } void va_args_4() { const char filedata[] = "#define FRED(name, ...) name (__VA_ARGS__)\n" "FRED(abc, 123)\n"; - ASSERT_EQUALS("\nabc ( 123 )", expandMacros(filedata, *this)); + ASSERT_EQUALS("\nabc ( 123 )", expandMacros(filedata)); } void va_args_5() { const char filedata1[] = "#define A(...) #__VA_ARGS__\n" "A(123)\n"; - ASSERT_EQUALS("\n\"123\"", expandMacros(filedata1, *this)); + ASSERT_EQUALS("\n\"123\"", expandMacros(filedata1)); const char filedata2[] = "#define A(X,...) X(#__VA_ARGS__)\n" "A(f,123)\n"; - ASSERT_EQUALS("\nf ( \"123\" )", expandMacros(filedata2, *this)); + ASSERT_EQUALS("\nf ( \"123\" )", expandMacros(filedata2)); } @@ -1326,7 +1326,7 @@ class TestPreprocessor : public TestFixture { "STRINGIFY(abc)\n"; // expand macros.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); ASSERT_EQUALS("\n\"abc\"", actual); } @@ -1336,7 +1336,7 @@ class TestPreprocessor : public TestFixture { "A(abc);\n"; // expand macros.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); ASSERT_EQUALS("\ng ( \"abc\" ) ;", actual); } @@ -1346,7 +1346,7 @@ class TestPreprocessor : public TestFixture { "A( abc);\n"; // expand macros.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); ASSERT_EQUALS("\ng ( \"abc\" ) ;", actual); } @@ -1358,7 +1358,7 @@ class TestPreprocessor : public TestFixture { ") 2\n"; // expand macros.. - std::string actual = expandMacros(filedata, *this); + std::string actual = expandMacros(filedata); ASSERT_EQUALS("\n1 \"abc\"\n\n2", actual); } @@ -1366,7 +1366,7 @@ class TestPreprocessor : public TestFixture { void stringify5() { const char filedata[] = "#define A(x) a(#x,x)\n" "A(foo(\"\\\"\"))\n"; - ASSERT_EQUALS("\na ( \"foo(\\\"\\\\\\\"\\\")\" , foo ( \"\\\"\" ) )", expandMacros(filedata, *this)); + ASSERT_EQUALS("\na ( \"foo(\\\"\\\\\\\"\\\")\" , foo ( \"\\\"\" ) )", expandMacros(filedata)); } void pragma() { @@ -1457,7 +1457,7 @@ class TestPreprocessor : public TestFixture { "#endif\n"; // expand macros.. - const std::string actual(expandMacros(filedata, *this)); + const std::string actual(expandMacros(filedata)); 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()); @@ -1470,7 +1470,7 @@ class TestPreprocessor : public TestFixture { "#endfile\n"; // expand macros.. - const std::string actual(expandMacros(filedata, *this)); + const std::string actual(expandMacros(filedata)); 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()); @@ -1483,7 +1483,7 @@ class TestPreprocessor : public TestFixture { "\"\n"; // expand macros.. - const std::string actual(expandMacros(filedata, *this)); + const std::string actual(expandMacros(filedata)); 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()); @@ -1495,7 +1495,7 @@ class TestPreprocessor : public TestFixture { "int a = A;\n"; // expand macros.. - const std::string actual(expandMacros(filedata, *this)); + const std::string actual(expandMacros(filedata)); 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()); @@ -1512,7 +1512,7 @@ class TestPreprocessor : public TestFixture { "}\n"; // expand macros.. - (void)expandMacros(filedata, *this); + (void)expandMacros(filedata); 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()); } @@ -1929,7 +1929,7 @@ class TestPreprocessor : public TestFixture { void remarkComment1() { const char code[] = "// REMARK: assignment with 1\n" "x=1;\n"; - const auto remarkComments = getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(2, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1", remarkComments[0].str); @@ -1937,7 +1937,7 @@ class TestPreprocessor : public TestFixture { void remarkComment2() { const char code[] = "x=1; ///REMARK assignment with 1\n"; - const auto remarkComments = getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(1, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1", remarkComments[0].str); @@ -1946,7 +1946,7 @@ class TestPreprocessor : public TestFixture { void remarkComment3() { const char code[] = "/** REMARK: assignment with 1 */\n" "x=1;\n"; - const auto remarkComments = getRemarkComments(code, *this); + const auto remarkComments = getRemarkComments(code); ASSERT_EQUALS(1, remarkComments.size()); ASSERT_EQUALS(2, remarkComments[0].lineNumber); ASSERT_EQUALS("assignment with 1 ", remarkComments[0].str); From 922432bdea3e75ee0060518c186a59d17027f455 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 26 Sep 2024 11:44:52 +0200 Subject: [PATCH 3/3] TestPreprocessor: removed usage of `std::istringstream` --- test/testpreprocessor.cpp | 44 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 64f22a0874b..66bd18108f0 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -48,11 +47,11 @@ class TestPreprocessor : public TestFixture { TestPreprocessor() : TestFixture("TestPreprocessor") {} private: - std::string expandMacros(const char code[]) { - std::istringstream istr(code); + template + std::string expandMacros(const char (&code)[size]) { simplecpp::OutputList outputList; std::vector files; - const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList); + const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, size-1, files, "file.cpp", &outputList); const Settings settings; Preprocessor p(settings, *this); simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true); @@ -60,11 +59,11 @@ class TestPreprocessor : public TestFixture { return tokens2.stringify(); } - std::vector getRemarkComments(const char code[]) + template + std::vector getRemarkComments(const char (&code)[size]) { std::vector files{"test.cpp"}; - std::istringstream istr(code); - const simplecpp::TokenList tokens1(istr, files, files[0]); + const simplecpp::TokenList tokens1(code, size-1, files, files[0]); const Settings settings; @@ -274,7 +273,8 @@ class TestPreprocessor : public TestFixture { TEST_CASE(standard); } - std::string getConfigsStr(const char filedata[], const char *arg = nullptr) { + template + std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) { Settings settings; if (arg && std::strncmp(arg,"-D",2)==0) settings.userDefines = arg + 2; @@ -282,8 +282,7 @@ class TestPreprocessor : public TestFixture { settings.userUndefs.insert(arg+2); Preprocessor preprocessor(settings, *this); std::vector files; - std::istringstream istr(filedata); - simplecpp::TokenList tokens(istr,files); + simplecpp::TokenList tokens(code, size-1,files); tokens.removeComments(); const std::set configs = preprocessor.getConfigs(tokens); std::string ret; @@ -292,12 +291,12 @@ class TestPreprocessor : public TestFixture { return ret; } - std::size_t getHash(const char filedata[]) { + template + std::size_t getHash(const char (&code)[size]) { Settings settings; Preprocessor preprocessor(settings, *this); std::vector files; - std::istringstream istr(filedata); - simplecpp::TokenList tokens(istr,files); + simplecpp::TokenList tokens(code,size-1,files); tokens.removeComments(); return preprocessor.calculateHash(tokens, ""); } @@ -450,9 +449,8 @@ class TestPreprocessor : public TestFixture { "#else\n" "2\n" "#endif\n"; - std::istringstream istr(filedata); std::vector files; - simplecpp::TokenList tokens(istr, files, "test.c"); + simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c"); // preprocess code with unix32 platform.. { @@ -775,14 +773,14 @@ class TestPreprocessor : public TestFixture { } void if_macro_eq_macro() { - const char *code = "#define A B\n" - "#define B 1\n" - "#define C 1\n" - "#if A == C\n" - "Wilma\n" - "#else\n" - "Betty\n" - "#endif\n"; + const char code[] = "#define A B\n" + "#define B 1\n" + "#define C 1\n" + "#if A == C\n" + "Wilma\n" + "#else\n" + "Betty\n" + "#endif\n"; ASSERT_EQUALS("\n", getConfigsStr(code)); }