33
33
#include < list>
34
34
#include < map>
35
35
#include < set>
36
- #include < sstream>
37
36
#include < string>
38
37
#include < vector>
39
38
@@ -48,11 +47,11 @@ class TestPreprocessor : public TestFixture {
48
47
class OurPreprocessor : public Preprocessor {
49
48
public:
50
49
51
- static std::string expandMacros ( const char code[], ErrorLogger *errorLogger = nullptr ) {
52
- std::istringstream istr ( code);
50
+ template < size_t size>
51
+ static std::string expandMacros ( const char (& code)[size], ErrorLogger *errorLogger = nullptr) {
53
52
simplecpp::OutputList outputList;
54
53
std::vector<std::string> files;
55
- const simplecpp::TokenList tokens1 = simplecpp::TokenList (istr , files, " file.cpp" , &outputList);
54
+ const simplecpp::TokenList tokens1 = simplecpp::TokenList (code, size- 1 , files, " file.cpp" , &outputList);
56
55
std::map<std::string, simplecpp::TokenList*> filedata;
57
56
simplecpp::TokenList tokens2 (files);
58
57
simplecpp::preprocess (tokens2, tokens1, files, filedata, simplecpp::DUI (), &outputList);
@@ -268,16 +267,16 @@ class TestPreprocessor : public TestFixture {
268
267
TEST_CASE (hashCalculation);
269
268
}
270
269
271
- std::string getConfigsStr (const char filedata[], const char *arg = nullptr ) {
270
+ template <size_t size>
271
+ std::string getConfigsStr (const char (&code)[size], const char *arg = nullptr) {
272
272
Settings settings;
273
273
if (arg && std::strncmp (arg," -D" ,2 )==0 )
274
274
settings.userDefines = arg + 2 ;
275
275
if (arg && std::strncmp (arg," -U" ,2 )==0 )
276
276
settings.userUndefs .insert (arg+2 );
277
277
Preprocessor preprocessor (settings, *this );
278
278
std::vector<std::string> files;
279
- std::istringstream istr (filedata);
280
- simplecpp::TokenList tokens (istr,files);
279
+ simplecpp::TokenList tokens (code,size-1 ,files);
281
280
tokens.removeComments ();
282
281
const std::set<std::string> configs = preprocessor.getConfigs (tokens);
283
282
std::string ret;
@@ -286,12 +285,12 @@ class TestPreprocessor : public TestFixture {
286
285
return ret;
287
286
}
288
287
289
- std::size_t getHash (const char filedata[]) {
288
+ template <size_t size>
289
+ std::size_t getHash (const char (&code)[size]) {
290
290
Settings settings;
291
291
Preprocessor preprocessor (settings, *this );
292
292
std::vector<std::string> files;
293
- std::istringstream istr (filedata);
294
- simplecpp::TokenList tokens (istr,files);
293
+ simplecpp::TokenList tokens (code,size-1 ,files);
295
294
tokens.removeComments ();
296
295
return preprocessor.calculateHash (tokens, " " );
297
296
}
@@ -444,9 +443,8 @@ class TestPreprocessor : public TestFixture {
444
443
" #else\n "
445
444
" 2\n "
446
445
" #endif\n " ;
447
- std::istringstream istr (filedata);
448
446
std::vector<std::string> files;
449
- simplecpp::TokenList tokens (istr , files, " test.c" );
447
+ simplecpp::TokenList tokens (filedata, sizeof (filedata) , files, " test.c" );
450
448
451
449
// preprocess code with unix32 platform..
452
450
{
@@ -769,47 +767,47 @@ class TestPreprocessor : public TestFixture {
769
767
}
770
768
771
769
void if_macro_eq_macro () {
772
- const char * code = " #define A B\n "
773
- " #define B 1\n "
774
- " #define C 1\n "
775
- " #if A == C\n "
776
- " Wilma\n "
777
- " #else\n "
778
- " Betty\n "
779
- " #endif\n " ;
770
+ const char code[] = " #define A B\n "
771
+ " #define B 1\n "
772
+ " #define C 1\n "
773
+ " #if A == C\n "
774
+ " Wilma\n "
775
+ " #else\n "
776
+ " Betty\n "
777
+ " #endif\n " ;
780
778
ASSERT_EQUALS (" \n " , getConfigsStr (code));
781
779
}
782
780
783
781
void ticket_3675 () {
784
- const char * code = " #ifdef YYSTACKSIZE\n "
785
- " #define YYMAXDEPTH YYSTACKSIZE\n "
786
- " #else\n "
787
- " #define YYSTACKSIZE YYMAXDEPTH\n "
788
- " #endif\n "
789
- " #if YYDEBUG\n "
790
- " #endif\n " ;
782
+ const char code[] = " #ifdef YYSTACKSIZE\n "
783
+ " #define YYMAXDEPTH YYSTACKSIZE\n "
784
+ " #else\n "
785
+ " #define YYSTACKSIZE YYMAXDEPTH\n "
786
+ " #endif\n "
787
+ " #if YYDEBUG\n "
788
+ " #endif\n " ;
791
789
(void )PreprocessorHelper::getcode (settings0, *this , code);
792
790
793
791
// There's nothing to assert. It just needs to not hang.
794
792
}
795
793
796
794
void ticket_3699 () {
797
- const char * code = " #define INLINE __forceinline\n "
798
- " #define inline __forceinline\n "
799
- " #define __forceinline inline\n "
800
- " #if !defined(_WIN32)\n "
801
- " #endif\n "
802
- " INLINE inline __forceinline\n " ;
795
+ const char code[] = " #define INLINE __forceinline\n "
796
+ " #define inline __forceinline\n "
797
+ " #define __forceinline inline\n "
798
+ " #if !defined(_WIN32)\n "
799
+ " #endif\n "
800
+ " INLINE inline __forceinline\n " ;
803
801
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode (settings0, *this , code);
804
802
805
803
// First, it must not hang. Second, inline must becomes inline, and __forceinline must become __forceinline.
806
804
ASSERT_EQUALS (" \n\n\n\n\n $__forceinline $inline $__forceinline" , actual.at (" " ));
807
805
}
808
806
809
807
void ticket_4922 () { // #4922
810
- const char * code = " __asm__ \n "
811
- " { int extern __value) 0; (double return (\"\" } extern\n "
812
- " __typeof __finite (__finite) __finite __inline \" __GI___finite\" );" ;
808
+ const char code[] = " __asm__ \n "
809
+ " { int extern __value) 0; (double return (\"\" } extern\n "
810
+ " __typeof __finite (__finite) __finite __inline \" __GI___finite\" );" ;
813
811
(void )PreprocessorHelper::getcode (settings0, *this , code);
814
812
}
815
813
@@ -2229,12 +2227,12 @@ class TestPreprocessor : public TestFixture {
2229
2227
}
2230
2228
2231
2229
void if_sizeof () { // #4071
2232
- static const char * code = " #if sizeof(unsigned short) == 2\n "
2233
- " Fred & Wilma\n "
2234
- " #elif sizeof(unsigned short) == 4\n "
2235
- " Fred & Wilma\n "
2236
- " #else\n "
2237
- " #endif" ;
2230
+ const char code[] = " #if sizeof(unsigned short) == 2\n "
2231
+ " Fred & Wilma\n "
2232
+ " #elif sizeof(unsigned short) == 4\n "
2233
+ " Fred & Wilma\n "
2234
+ " #else\n "
2235
+ " #endif" ;
2238
2236
2239
2237
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode (settings0, *this , code);
2240
2238
ASSERT_EQUALS (" \n Fred & Wilma" , actual.at (" " ));
0 commit comments