35
35
#include < list>
36
36
#include < map>
37
37
#include < set>
38
- #include < sstream>
39
38
#include < stdexcept>
40
39
#include < string>
41
40
#include < utility>
@@ -50,27 +49,27 @@ class TestPreprocessor : public TestFixture {
50
49
TestPreprocessor () : TestFixture(" TestPreprocessor" ) {}
51
50
52
51
private:
53
- std::string expandMacros ( const char code[], ErrorLogger &errorLogger) const {
54
- std::istringstream istr ( code);
52
+ template < size_t size>
53
+ std::string expandMacros ( const char (& code)[size], ErrorLogger &errorLogger) const {
55
54
simplecpp::OutputList outputList;
56
55
std::vector<std::string> files;
57
- const simplecpp::TokenList tokens1 = simplecpp::TokenList (istr , files, " file.cpp" , &outputList);
56
+ const simplecpp::TokenList tokens1 = simplecpp::TokenList (code, size- 1 , files, " file.cpp" , &outputList);
58
57
Preprocessor p (settingsDefault, errorLogger, Path::identify (tokens1.getFiles ()[0 ], false ));
59
58
simplecpp::TokenList tokens2 = p.preprocess (tokens1, " " , files, true );
60
59
p.reportOutput (outputList, true );
61
60
return tokens2.stringify ();
62
61
}
63
62
64
- static void preprocess (const char code[], std::vector<std::string> &files, const std::string& file0, TokenList& tokenlist, const simplecpp::DUI& dui)
63
+ template <size_t size>
64
+ static void preprocess (const char (&code)[size], std::vector<std::string> &files, const std::string& file0, TokenList& tokenlist, const simplecpp::DUI& dui)
65
65
{
66
66
if (!files.empty ())
67
67
throw std::runtime_error (" file list not empty" );
68
68
69
69
if (tokenlist.front ())
70
70
throw std::runtime_error (" token list not empty" );
71
71
72
- std::istringstream istr (code);
73
- const simplecpp::TokenList tokens1 (istr, files, file0);
72
+ const simplecpp::TokenList tokens1 (code, size-1 , files, file0);
74
73
75
74
// Preprocess..
76
75
simplecpp::TokenList tokens2 (files);
@@ -82,11 +81,11 @@ class TestPreprocessor : public TestFixture {
82
81
tokenlist.createTokens (std::move (tokens2));
83
82
}
84
83
85
- std::vector<RemarkComment> getRemarkComments (const char code[], ErrorLogger& errorLogger) const
84
+ template <size_t size>
85
+ std::vector<RemarkComment> getRemarkComments (const char (&code)[size], ErrorLogger& errorLogger) const
86
86
{
87
87
std::vector<std::string> files;
88
- std::istringstream istr (code);
89
- const simplecpp::TokenList tokens1 (istr, files, " test.cpp" );
88
+ const simplecpp::TokenList tokens1 (code, size-1 , files, " test.cpp" );
90
89
91
90
const Preprocessor preprocessor (settingsDefault, errorLogger, Path::identify (tokens1.getFiles ()[0 ], false ));
92
91
return preprocessor.getRemarkComments (tokens1);
@@ -301,16 +300,16 @@ class TestPreprocessor : public TestFixture {
301
300
TEST_CASE (standard);
302
301
}
303
302
304
- std::string getConfigsStr (const char filedata[], const char *arg = nullptr ) {
303
+ template <size_t size>
304
+ std::string getConfigsStr (const char (&code)[size], const char *arg = nullptr) {
305
305
Settings settings;
306
306
if (arg && std::strncmp (arg," -D" ,2 )==0 )
307
307
settings.userDefines = arg + 2 ;
308
308
if (arg && std::strncmp (arg," -U" ,2 )==0 )
309
309
settings.userUndefs .insert (arg+2 );
310
310
std::vector<std::string> files;
311
- std::istringstream istr (filedata);
312
311
// TODO: this adds an empty filename
313
- simplecpp::TokenList tokens (istr ,files);
312
+ simplecpp::TokenList tokens (code, size- 1 ,files);
314
313
tokens.removeComments ();
315
314
Preprocessor preprocessor (settings, *this , Standards::Language::C); // TODO: do we need to consider #file?
316
315
const std::set<std::string> configs = preprocessor.getConfigs (tokens);
@@ -320,11 +319,11 @@ class TestPreprocessor : public TestFixture {
320
319
return ret;
321
320
}
322
321
323
- std::size_t getHash (const char filedata[]) {
322
+ template <size_t size>
323
+ std::size_t getHash (const char (&code)[size]) {
324
324
std::vector<std::string> files;
325
- std::istringstream istr (filedata);
326
325
// TODO: this adds an empty filename
327
- simplecpp::TokenList tokens (istr ,files);
326
+ simplecpp::TokenList tokens (code,size- 1 ,files);
328
327
tokens.removeComments ();
329
328
Preprocessor preprocessor (settingsDefault, *this , Standards::Language::C); // TODO: do we need to consider #file?
330
329
return preprocessor.calculateHash (tokens, " " );
@@ -478,9 +477,8 @@ class TestPreprocessor : public TestFixture {
478
477
" #else\n "
479
478
" 2\n "
480
479
" #endif\n " ;
481
- std::istringstream istr (filedata);
482
480
std::vector<std::string> files;
483
- simplecpp::TokenList tokens (istr , files, " test.c" );
481
+ simplecpp::TokenList tokens (filedata, sizeof (filedata) , files, " test.c" );
484
482
485
483
// preprocess code with unix32 platform..
486
484
{
@@ -803,14 +801,14 @@ class TestPreprocessor : public TestFixture {
803
801
}
804
802
805
803
void if_macro_eq_macro () {
806
- const char * code = " #define A B\n "
807
- " #define B 1\n "
808
- " #define C 1\n "
809
- " #if A == C\n "
810
- " Wilma\n "
811
- " #else\n "
812
- " Betty\n "
813
- " #endif\n " ;
804
+ const char code[] = " #define A B\n "
805
+ " #define B 1\n "
806
+ " #define C 1\n "
807
+ " #if A == C\n "
808
+ " Wilma\n "
809
+ " #else\n "
810
+ " Betty\n "
811
+ " #endif\n " ;
814
812
ASSERT_EQUALS (" \n " , getConfigsStr (code));
815
813
}
816
814
0 commit comments