@@ -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
@@ -1905,11 +1915,11 @@ static void readfile_char_error()
19051915{
19061916 simplecpp::OutputList outputList;
19071917
1908- readfile (" A = L's" , - 1 , &outputList);
1918+ readfile (" A = L's" , &outputList);
19091919 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));
19101920 outputList.clear ();
19111921
1912- readfile (" A = 's\n '" , - 1 , &outputList);
1922+ readfile (" A = 's\n '" , &outputList);
19131923 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));
19141924}
19151925
@@ -1963,36 +1973,36 @@ static void readfile_string_error()
19631973{
19641974 simplecpp::OutputList outputList;
19651975
1966- readfile (" A = \" abs" , - 1 , &outputList);
1976+ readfile (" A = \" abs" , &outputList);
19671977 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));
19681978 outputList.clear ();
19691979
1970- readfile (" A = u8\" abs\n\" " , - 1 , &outputList);
1980+ readfile (" A = u8\" abs\n\" " , &outputList);
19711981 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));
19721982 outputList.clear ();
19731983
1974- readfile (" A = R\" as\n (abc)as\" " , - 1 , &outputList);
1984+ readfile (" A = R\" as\n (abc)as\" " , &outputList);
19751985 ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
19761986 outputList.clear ();
19771987
1978- readfile (" A = u8R\" as\n (abc)as\" " , - 1 , &outputList);
1988+ readfile (" A = u8R\" as\n (abc)as\" " , &outputList);
19791989 ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
19801990 outputList.clear ();
19811991
1982- readfile (" A = R\" as(abc)a\" " , - 1 , &outputList);
1992+ readfile (" A = R\" as(abc)a\" " , &outputList);
19831993 ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
19841994 outputList.clear ();
19851995
1986- readfile (" A = LR\" as(abc)a\" " , - 1 , &outputList);
1996+ readfile (" A = LR\" as(abc)a\" " , &outputList);
19871997 ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
19881998 outputList.clear ();
19891999
1990- readfile (" #define A \" abs" , - 1 , &outputList);
2000+ readfile (" #define A \" abs" , &outputList);
19912001 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));
19922002 outputList.clear ();
19932003
19942004 // Don't warn for a multiline define
1995- readfile (" #define A \" abs\\\n\" " , - 1 , &outputList);
2005+ readfile (" #define A \" abs\\\n\" " , &outputList);
19962006 ASSERT_EQUALS (" " , toString (outputList));
19972007}
19982008
@@ -2004,11 +2014,11 @@ static void readfile_cpp14_number()
20042014static void readfile_unhandled_chars ()
20052015{
20062016 simplecpp::OutputList outputList;
2007- readfile (" // 你好世界" , - 1 , &outputList);
2017+ readfile (" // 你好世界" , &outputList);
20082018 ASSERT_EQUALS (" " , toString (outputList));
2009- readfile (" s=\" 你好世界\" " , - 1 , &outputList);
2019+ readfile (" s=\" 你好世界\" " , &outputList);
20102020 ASSERT_EQUALS (" " , toString (outputList));
2011- readfile (" int 你好世界=0;" , - 1 , &outputList);
2021+ readfile (" int 你好世界=0;" , &outputList);
20122022 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));
20132023}
20142024
@@ -2153,13 +2163,34 @@ static void utf8()
21532163
21542164static void unicode ()
21552165{
2156- ASSERT_EQUALS (" 12" , readfile (" \xFE\xFF\x00\x31\x00\x32 " , 6 ));
2157- ASSERT_EQUALS (" 12" , readfile (" \xFF\xFE\x31\x00\x32\x00 " , 6 ));
2158- ASSERT_EQUALS (" //\n 1" , readfile (" \xFE\xFF\x00\x2f\x00\x2f\x00\x0a\x00\x31 " , 10 ));
2159- ASSERT_EQUALS (" //\n 1" , readfile (" \xFF\xFE\x2f\x00\x2f\x00\x0a\x00\x31\x00 " , 10 ));
2160- ASSERT_EQUALS (" \" a\" " , readfile (" \xFE\xFF\x00\x22\x00\x61\x00\x22 " , 8 ));
2161- ASSERT_EQUALS (" \" a\" " , readfile (" \xFF\xFE\x22\x00\x61\x00\x22\x00 " , 8 ));
2162- ASSERT_EQUALS (" \n //1" , readfile (" \xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00 " ,16 ));
2166+ {
2167+ const char code[] = " \xFE\xFF\x00\x31\x00\x32 " ;
2168+ ASSERT_EQUALS (" 12" , readfile (code, sizeof (code)));
2169+ }
2170+ {
2171+ const char code[] = " \xFF\xFE\x31\x00\x32\x00 " ;
2172+ ASSERT_EQUALS (" 12" , readfile (code, sizeof (code)));
2173+ }
2174+ {
2175+ const char code[] = " \xFE\xFF\x00\x2f\x00\x2f\x00\x0a\x00\x31 " ;
2176+ ASSERT_EQUALS (" //\n 1" , readfile (code, sizeof (code)));
2177+ }
2178+ {
2179+ const char code[] = " \xFF\xFE\x2f\x00\x2f\x00\x0a\x00\x31\x00 " ;
2180+ ASSERT_EQUALS (" //\n 1" , readfile (code, sizeof (code)));
2181+ }
2182+ {
2183+ const char code[] = " \xFE\xFF\x00\x22\x00\x61\x00\x22 " ;
2184+ ASSERT_EQUALS (" \" a\" " , readfile (code, sizeof (code)));
2185+ }
2186+ {
2187+ const char code[] = " \xFF\xFE\x22\x00\x61\x00\x22\x00 " ;
2188+ ASSERT_EQUALS (" \" a\" " , readfile (code, sizeof (code)));
2189+ }
2190+ {
2191+ const char code[] = " \xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00 " ;
2192+ ASSERT_EQUALS (" \n //1" , readfile (code, sizeof (code)));
2193+ }
21632194}
21642195
21652196static void warning ()
0 commit comments