@@ -78,24 +78,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
7878#define TEST_CASE (F ) (testcase(#F, F, argc, argv))
7979
8080
81-
82- static std::string readfile (const char code[], int sz=-1 , simplecpp::OutputList *outputList=nullptr )
81+ static simplecpp::TokenList makeTokenList (const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
8382{
84- std::istringstream istr (sz == -1 ? std::string (code) : std::string (code,sz));
85- std::vector<std::string> files;
86- return simplecpp::TokenList (istr,files,std::string (),outputList).stringify ();
83+ std::istringstream istr (code);
84+ return simplecpp::TokenList (istr,filenames,filename,outputList);
8785}
8886
89- static simplecpp::TokenList makeTokenList (const char code[], std::vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
87+ static simplecpp::TokenList makeTokenList (const char code[], std::size_t size, std:: vector<std::string> &filenames, const std::string &filename=std::string(), simplecpp::OutputList *outputList=nullptr)
9088{
91- std::istringstream istr (code);
89+ std::istringstream istr (std::string ( code, size) );
9290 return simplecpp::TokenList (istr,filenames,filename,outputList);
9391}
9492
9593static simplecpp::TokenList makeTokenList (const char code[])
9694{
9795 std::vector<std::string> files;
98- return makeTokenList (code, files, std::string ());
96+ return makeTokenList (code, files);
97+ }
98+
99+ static std::string readfile (const char code[], simplecpp::OutputList *outputList=nullptr )
100+ {
101+ std::vector<std::string> files;
102+ return makeTokenList (code,files,std::string (),outputList).stringify ();
103+ }
104+
105+ static std::string readfile (const char code[], std::size_t size, simplecpp::OutputList *outputList=nullptr )
106+ {
107+ std::vector<std::string> files;
108+ return makeTokenList (code,size,files,std::string (),outputList).stringify ();
99109}
100110
101111static std::string preprocess (const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
@@ -166,15 +176,15 @@ static void backslash()
166176 // <backslash><space><newline> preprocessed differently
167177 simplecpp::OutputList outputList;
168178
169- readfile (" //123 \\\n 456" , - 1 , &outputList);
179+ readfile (" //123 \\\n 456" , &outputList);
170180 ASSERT_EQUALS (" " , toString (outputList));
171- readfile (" //123 \\ \n 456" , - 1 , &outputList);
181+ readfile (" //123 \\ \n 456" , &outputList);
172182 ASSERT_EQUALS (" file0,1,portability_backslash,Combination 'backslash space newline' is not portable.\n " , toString (outputList));
173183
174184 outputList.clear ();
175- readfile (" #define A \\\n 123" , - 1 , &outputList);
185+ readfile (" #define A \\\n 123" , &outputList);
176186 ASSERT_EQUALS (" " , toString (outputList));
177- readfile (" #define A \\ \n 123" , - 1 , &outputList);
187+ readfile (" #define A \\ \n 123" , &outputList);
178188 ASSERT_EQUALS (" file0,1,portability_backslash,Combination 'backslash space newline' is not portable.\n " , toString (outputList));
179189}
180190
@@ -811,26 +821,26 @@ static void error3()
811821static void error4 ()
812822{
813823 // "#error x\n1"
814- const std::string code (" \xFE\xFF\x00\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x78\x00\x0a\x00\x31 " , 22 );
815- std::istringstream istr (code);
824+ const char code[] = " \xFE\xFF\x00\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x78\x00\x0a\x00\x31 " ;
816825 std::vector<std::string> files;
817826 std::map<std::string, simplecpp::TokenList*> filedata;
818827 simplecpp::OutputList outputList;
819828 simplecpp::TokenList tokens2 (files);
820- simplecpp::preprocess (tokens2, simplecpp::TokenList (istr,files," test.c" ), files, filedata, simplecpp::DUI (), &outputList);
829+ const simplecpp::TokenList rawtoken = makeTokenList (code, sizeof (code),files," test.c" );
830+ simplecpp::preprocess (tokens2, rawtoken, files, filedata, simplecpp::DUI (), &outputList);
821831 ASSERT_EQUALS (" file0,1,#error,#error x\n " , toString (outputList));
822832}
823833
824834static void error5 ()
825835{
826836 // "#error x\n1"
827- const std::string code (" \xFF\xFE\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x78\x00\x0a\x00\x78\x00\x31\x00 " , 22 );
828- std::istringstream istr (code);
837+ const char code[] = " \xFF\xFE\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x78\x00\x0a\x00\x78\x00\x31\x00 " ;
829838 std::vector<std::string> files;
830839 std::map<std::string, simplecpp::TokenList*> filedata;
831840 simplecpp::OutputList outputList;
832841 simplecpp::TokenList tokens2 (files);
833- simplecpp::preprocess (tokens2, simplecpp::TokenList (istr,files," test.c" ), files, filedata, simplecpp::DUI (), &outputList);
842+ const simplecpp::TokenList rawtokens = makeTokenList (code, sizeof (code),files," test.c" );
843+ simplecpp::preprocess (tokens2, rawtokens, files, filedata, simplecpp::DUI (), &outputList);
834844 ASSERT_EQUALS (" file0,1,#error,#error x\n " , toString (outputList));
835845}
836846
@@ -1786,11 +1796,11 @@ static void readfile_char_error()
17861796{
17871797 simplecpp::OutputList outputList;
17881798
1789- readfile (" A = L's" , - 1 , &outputList);
1799+ readfile (" A = L's" , &outputList);
17901800 ASSERT_EQUALS (" file0,1,syntax_error,No pair for character (\' ). Can't process file. File is either invalid or unicode, which is currently not supported.\n " , toString (outputList));
17911801 outputList.clear ();
17921802
1793- readfile (" A = 's\n '" , - 1 , &outputList);
1803+ readfile (" A = 's\n '" , &outputList);
17941804 ASSERT_EQUALS (" file0,1,syntax_error,No pair for character (\' ). Can't process file. File is either invalid or unicode, which is currently not supported.\n " , toString (outputList));
17951805}
17961806
@@ -1844,36 +1854,36 @@ static void readfile_string_error()
18441854{
18451855 simplecpp::OutputList outputList;
18461856
1847- readfile (" A = \" abs" , - 1 , &outputList);
1857+ readfile (" A = \" abs" , &outputList);
18481858 ASSERT_EQUALS (" file0,1,syntax_error,No pair for character (\" ). Can't process file. File is either invalid or unicode, which is currently not supported.\n " , toString (outputList));
18491859 outputList.clear ();
18501860
1851- readfile (" A = u8\" abs\n\" " , - 1 , &outputList);
1861+ readfile (" A = u8\" abs\n\" " , &outputList);
18521862 ASSERT_EQUALS (" file0,1,syntax_error,No pair for character (\" ). Can't process file. File is either invalid or unicode, which is currently not supported.\n " , toString (outputList));
18531863 outputList.clear ();
18541864
1855- readfile (" A = R\" as\n (abc)as\" " , - 1 , &outputList);
1865+ readfile (" A = R\" as\n (abc)as\" " , &outputList);
18561866 ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
18571867 outputList.clear ();
18581868
1859- readfile (" A = u8R\" as\n (abc)as\" " , - 1 , &outputList);
1869+ readfile (" A = u8R\" as\n (abc)as\" " , &outputList);
18601870 ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
18611871 outputList.clear ();
18621872
1863- readfile (" A = R\" as(abc)a\" " , - 1 , &outputList);
1873+ readfile (" A = R\" as(abc)a\" " , &outputList);
18641874 ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
18651875 outputList.clear ();
18661876
1867- readfile (" A = LR\" as(abc)a\" " , - 1 , &outputList);
1877+ readfile (" A = LR\" as(abc)a\" " , &outputList);
18681878 ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
18691879 outputList.clear ();
18701880
1871- readfile (" #define A \" abs" , - 1 , &outputList);
1881+ readfile (" #define A \" abs" , &outputList);
18721882 ASSERT_EQUALS (" file0,1,syntax_error,No pair for character (\" ). Can't process file. File is either invalid or unicode, which is currently not supported.\n " , toString (outputList));
18731883 outputList.clear ();
18741884
18751885 // Don't warn for a multiline define
1876- readfile (" #define A \" abs\\\n\" " , - 1 , &outputList);
1886+ readfile (" #define A \" abs\\\n\" " , &outputList);
18771887 ASSERT_EQUALS (" " , toString (outputList));
18781888}
18791889
@@ -1885,11 +1895,11 @@ static void readfile_cpp14_number()
18851895static void readfile_unhandled_chars ()
18861896{
18871897 simplecpp::OutputList outputList;
1888- readfile (" // 你好世界" , - 1 , &outputList);
1898+ readfile (" // 你好世界" , &outputList);
18891899 ASSERT_EQUALS (" " , toString (outputList));
1890- readfile (" s=\" 你好世界\" " , - 1 , &outputList);
1900+ readfile (" s=\" 你好世界\" " , &outputList);
18911901 ASSERT_EQUALS (" " , toString (outputList));
1892- readfile (" int 你好世界=0;" , - 1 , &outputList);
1902+ readfile (" int 你好世界=0;" , &outputList);
18931903 ASSERT_EQUALS (" file0,1,unhandled_char_error,The code contains unhandled character(s) (character code=228). Neither unicode nor extended ascii is supported.\n " , toString (outputList));
18941904}
18951905
@@ -2034,13 +2044,34 @@ static void utf8()
20342044
20352045static void unicode ()
20362046{
2037- ASSERT_EQUALS (" 12" , readfile (" \xFE\xFF\x00\x31\x00\x32 " , 6 ));
2038- ASSERT_EQUALS (" 12" , readfile (" \xFF\xFE\x31\x00\x32\x00 " , 6 ));
2039- ASSERT_EQUALS (" //\n 1" , readfile (" \xFE\xFF\x00\x2f\x00\x2f\x00\x0a\x00\x31 " , 10 ));
2040- ASSERT_EQUALS (" //\n 1" , readfile (" \xFF\xFE\x2f\x00\x2f\x00\x0a\x00\x31\x00 " , 10 ));
2041- ASSERT_EQUALS (" \" a\" " , readfile (" \xFE\xFF\x00\x22\x00\x61\x00\x22 " , 8 ));
2042- ASSERT_EQUALS (" \" a\" " , readfile (" \xFF\xFE\x22\x00\x61\x00\x22\x00 " , 8 ));
2043- ASSERT_EQUALS (" \n //1" , readfile (" \xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00 " ,16 ));
2047+ {
2048+ const char code[] = " \xFE\xFF\x00\x31\x00\x32 " ;
2049+ ASSERT_EQUALS (" 12" , readfile (code, sizeof (code)));
2050+ }
2051+ {
2052+ const char code[] = " \xFF\xFE\x31\x00\x32\x00 " ;
2053+ ASSERT_EQUALS (" 12" , readfile (code, sizeof (code)));
2054+ }
2055+ {
2056+ const char code[] = " \xFE\xFF\x00\x2f\x00\x2f\x00\x0a\x00\x31 " ;
2057+ ASSERT_EQUALS (" //\n 1" , readfile (code, sizeof (code)));
2058+ }
2059+ {
2060+ const char code[] = " \xFF\xFE\x2f\x00\x2f\x00\x0a\x00\x31\x00 " ;
2061+ ASSERT_EQUALS (" //\n 1" , readfile (code, sizeof (code)));
2062+ }
2063+ {
2064+ const char code[] = " \xFE\xFF\x00\x22\x00\x61\x00\x22 " ;
2065+ ASSERT_EQUALS (" \" a\" " , readfile (code, sizeof (code)));
2066+ }
2067+ {
2068+ const char code[] = " \xFF\xFE\x22\x00\x61\x00\x22\x00 " ;
2069+ ASSERT_EQUALS (" \" a\" " , readfile (code, sizeof (code)));
2070+ }
2071+ {
2072+ const char code[] = " \xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00 " ;
2073+ ASSERT_EQUALS (" \n //1" , readfile (code, sizeof (code)));
2074+ }
20442075}
20452076
20462077static void warning ()
0 commit comments