@@ -78,24 +78,34 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
78
78
#define TEST_CASE (F ) (testcase(#F, F, argc, argv))
79
79
80
80
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)
83
82
{
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);
87
85
}
88
86
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)
90
88
{
91
- std::istringstream istr (code);
89
+ std::istringstream istr (std::string ( code, size) );
92
90
return simplecpp::TokenList (istr,filenames,filename,outputList);
93
91
}
94
92
95
93
static simplecpp::TokenList makeTokenList (const char code[])
96
94
{
97
95
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 ();
99
109
}
100
110
101
111
static std::string preprocess (const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
@@ -166,15 +176,15 @@ static void backslash()
166
176
// <backslash><space><newline> preprocessed differently
167
177
simplecpp::OutputList outputList;
168
178
169
- readfile (" //123 \\\n 456" , - 1 , &outputList);
179
+ readfile (" //123 \\\n 456" , &outputList);
170
180
ASSERT_EQUALS (" " , toString (outputList));
171
- readfile (" //123 \\ \n 456" , - 1 , &outputList);
181
+ readfile (" //123 \\ \n 456" , &outputList);
172
182
ASSERT_EQUALS (" file0,1,portability_backslash,Combination 'backslash space newline' is not portable.\n " , toString (outputList));
173
183
174
184
outputList.clear ();
175
- readfile (" #define A \\\n 123" , - 1 , &outputList);
185
+ readfile (" #define A \\\n 123" , &outputList);
176
186
ASSERT_EQUALS (" " , toString (outputList));
177
- readfile (" #define A \\ \n 123" , - 1 , &outputList);
187
+ readfile (" #define A \\ \n 123" , &outputList);
178
188
ASSERT_EQUALS (" file0,1,portability_backslash,Combination 'backslash space newline' is not portable.\n " , toString (outputList));
179
189
}
180
190
@@ -811,26 +821,26 @@ static void error3()
811
821
static void error4 ()
812
822
{
813
823
// "#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 " ;
816
825
std::vector<std::string> files;
817
826
std::map<std::string, simplecpp::TokenList*> filedata;
818
827
simplecpp::OutputList outputList;
819
828
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);
821
831
ASSERT_EQUALS (" file0,1,#error,#error x\n " , toString (outputList));
822
832
}
823
833
824
834
static void error5 ()
825
835
{
826
836
// "#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 " ;
829
838
std::vector<std::string> files;
830
839
std::map<std::string, simplecpp::TokenList*> filedata;
831
840
simplecpp::OutputList outputList;
832
841
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);
834
844
ASSERT_EQUALS (" file0,1,#error,#error x\n " , toString (outputList));
835
845
}
836
846
@@ -1905,11 +1915,11 @@ static void readfile_char_error()
1905
1915
{
1906
1916
simplecpp::OutputList outputList;
1907
1917
1908
- readfile (" A = L's" , - 1 , &outputList);
1918
+ readfile (" A = L's" , &outputList);
1909
1919
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));
1910
1920
outputList.clear ();
1911
1921
1912
- readfile (" A = 's\n '" , - 1 , &outputList);
1922
+ readfile (" A = 's\n '" , &outputList);
1913
1923
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));
1914
1924
}
1915
1925
@@ -1963,36 +1973,36 @@ static void readfile_string_error()
1963
1973
{
1964
1974
simplecpp::OutputList outputList;
1965
1975
1966
- readfile (" A = \" abs" , - 1 , &outputList);
1976
+ readfile (" A = \" abs" , &outputList);
1967
1977
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));
1968
1978
outputList.clear ();
1969
1979
1970
- readfile (" A = u8\" abs\n\" " , - 1 , &outputList);
1980
+ readfile (" A = u8\" abs\n\" " , &outputList);
1971
1981
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));
1972
1982
outputList.clear ();
1973
1983
1974
- readfile (" A = R\" as\n (abc)as\" " , - 1 , &outputList);
1984
+ readfile (" A = R\" as\n (abc)as\" " , &outputList);
1975
1985
ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
1976
1986
outputList.clear ();
1977
1987
1978
- readfile (" A = u8R\" as\n (abc)as\" " , - 1 , &outputList);
1988
+ readfile (" A = u8R\" as\n (abc)as\" " , &outputList);
1979
1989
ASSERT_EQUALS (" file0,1,syntax_error,Invalid newline in raw string delimiter.\n " , toString (outputList));
1980
1990
outputList.clear ();
1981
1991
1982
- readfile (" A = R\" as(abc)a\" " , - 1 , &outputList);
1992
+ readfile (" A = R\" as(abc)a\" " , &outputList);
1983
1993
ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
1984
1994
outputList.clear ();
1985
1995
1986
- readfile (" A = LR\" as(abc)a\" " , - 1 , &outputList);
1996
+ readfile (" A = LR\" as(abc)a\" " , &outputList);
1987
1997
ASSERT_EQUALS (" file0,1,syntax_error,Raw string missing terminating delimiter.\n " , toString (outputList));
1988
1998
outputList.clear ();
1989
1999
1990
- readfile (" #define A \" abs" , - 1 , &outputList);
2000
+ readfile (" #define A \" abs" , &outputList);
1991
2001
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));
1992
2002
outputList.clear ();
1993
2003
1994
2004
// Don't warn for a multiline define
1995
- readfile (" #define A \" abs\\\n\" " , - 1 , &outputList);
2005
+ readfile (" #define A \" abs\\\n\" " , &outputList);
1996
2006
ASSERT_EQUALS (" " , toString (outputList));
1997
2007
}
1998
2008
@@ -2004,11 +2014,11 @@ static void readfile_cpp14_number()
2004
2014
static void readfile_unhandled_chars ()
2005
2015
{
2006
2016
simplecpp::OutputList outputList;
2007
- readfile (" // 你好世界" , - 1 , &outputList);
2017
+ readfile (" // 你好世界" , &outputList);
2008
2018
ASSERT_EQUALS (" " , toString (outputList));
2009
- readfile (" s=\" 你好世界\" " , - 1 , &outputList);
2019
+ readfile (" s=\" 你好世界\" " , &outputList);
2010
2020
ASSERT_EQUALS (" " , toString (outputList));
2011
- readfile (" int 你好世界=0;" , - 1 , &outputList);
2021
+ readfile (" int 你好世界=0;" , &outputList);
2012
2022
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));
2013
2023
}
2014
2024
@@ -2153,13 +2163,34 @@ static void utf8()
2153
2163
2154
2164
static void unicode ()
2155
2165
{
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
+ }
2163
2194
}
2164
2195
2165
2196
static void warning ()
0 commit comments