@@ -50,12 +50,13 @@ class TestPreprocessor : public TestFixture {
5050
5151private:
5252 template <size_t size>
53- std::string expandMacros (const char (&code)[size], ErrorLogger &errorLogger) const {
53+ std::string expandMacros (const char (&code)[size], ErrorLogger &errorLogger, bool throwError = true ) const {
5454 simplecpp::OutputList outputList;
5555 std::vector<std::string> files;
5656 simplecpp::TokenList tokens1 = simplecpp::TokenList (code, files, " file.cpp" , &outputList);
5757 Preprocessor p (tokens1, settingsDefault, errorLogger, Path::identify (tokens1.getFiles ()[0 ], false ));
58- simplecpp::TokenList tokens2 = p.preprocess (" " , files, true );
58+ ASSERT (p.loadFiles (files));
59+ simplecpp::TokenList tokens2 = p.preprocess (" " , files, throwError);
5960 (void )p.reportOutput (outputList, true );
6061 return tokens2.stringify ();
6162 }
@@ -69,13 +70,14 @@ class TestPreprocessor : public TestFixture {
6970 if (tokenlist.front ())
7071 throw std::runtime_error (" token list not empty" );
7172
72- const simplecpp::TokenList tokens1 (code, files, file0);
73+ simplecpp::OutputList outputList;
74+ const simplecpp::TokenList tokens1 (code, files, file0, &outputList);
7375
7476 // Preprocess..
7577 simplecpp::TokenList tokens2 (files);
7678 simplecpp::FileDataCache cache;
77- // TODO: provide and handle outputList
78- simplecpp::preprocess (tokens2, tokens1, files, cache, dui);
79+ simplecpp::preprocess (tokens2, tokens1, files, cache, dui, outputList);
80+ // TODO: handle outputList
7981
8082 // Tokenizer..
8183 tokenlist.createTokens (std::move (tokens2));
@@ -135,7 +137,7 @@ class TestPreprocessor : public TestFixture {
135137 cfgs = preprocessor.getConfigs ();
136138 for (const std::string & config : cfgs) {
137139 try {
138- const bool writeLocations = (strstr (code, " #file " ) != nullptr ) || ( strstr (code, " # include" ) != nullptr );
140+ const bool writeLocations = (strstr (code, " #include" ) != nullptr );
139141 cfgcode[config] = preprocessor.getcode (config, files, writeLocations);
140142 } catch (const simplecpp::Output &) {
141143 cfgcode[config] = " " ;
@@ -368,9 +370,12 @@ class TestPreprocessor : public TestFixture {
368370 if (library)
369371 ASSERT (settings.library .load (" " , library, false ).errorcode == Library::ErrorCode::OK);
370372 std::vector<std::string> files;
373+ simplecpp::OutputList outputList;
371374 // TODO: this adds an empty filename
372- simplecpp::TokenList tokens (code,files);
373- Preprocessor preprocessor (tokens, settings, *this , Standards::Language::C); // TODO: do we need to consider #file?
375+ simplecpp::TokenList tokens (code,files," " ,&outputList);
376+ Preprocessor preprocessor (tokens, settings, *this , Standards::Language::C);
377+ ASSERT (preprocessor.loadFiles (files));
378+ ASSERT (!preprocessor.reportOutput (outputList, true ));
374379 preprocessor.removeComments ();
375380 const std::set<std::string> configs = preprocessor.getConfigs ();
376381 std::string ret;
@@ -383,8 +388,9 @@ class TestPreprocessor : public TestFixture {
383388 std::size_t getHash (const char (&code)[size]) {
384389 std::vector<std::string> files;
385390 // TODO: this adds an empty filename
386- simplecpp::TokenList tokens (code,files);
387- Preprocessor preprocessor (tokens, settingsDefault, *this , Standards::Language::C); // TODO: do we need to consider #file?
391+ simplecpp::TokenList tokens (code,files," " );
392+ Preprocessor preprocessor (tokens, settingsDefault, *this , Standards::Language::C);
393+ ASSERT (preprocessor.loadFiles (files));
388394 preprocessor.removeComments ();
389395 return preprocessor.calculateHash (" " );
390396 }
@@ -448,16 +454,19 @@ class TestPreprocessor : public TestFixture {
448454 void error4 () {
449455 // In included file
450456 {
457+ ScopedFile header (" ab.h" , " #error hello world!\n " );
451458 const auto settings = dinit (Settings, $.userDefines = " TEST" );
452- const char code[] = " #file \" ab.h\"\n #error hello world! \n #endfile " ;
459+ const char code[] = " #include \" ab.h\" " ;
453460 (void )getcodeforcfg (settings, *this , code, " TEST" , " test.c" );
454461 ASSERT_EQUALS (" [ab.h:1:2]: (error) #error hello world! [preprocessorErrorDirective]\n " , errout_str ());
455462 }
456463
457464 // After including a file
458465 {
466+ ScopedFile header (" ab.h" , " " );
459467 const auto settings = dinit (Settings, $.userDefines = " TEST" );
460- const char code[] = " #file \" ab.h\"\n\n #endfile\n #error aaa" ;
468+ const char code[] = " #include \" ab.h\"\n "
469+ " #error aaa" ;
461470 (void )getcodeforcfg (settings, *this , code, " TEST" , " test.c" );
462471 ASSERT_EQUALS (" [test.c:2:2]: (error) #error aaa [preprocessorErrorDirective]\n " , errout_str ());
463472 }
@@ -560,35 +569,35 @@ class TestPreprocessor : public TestFixture {
560569 }
561570
562571 void includeguard1 () {
572+ ScopedFile header (" abc.h" ,
573+ " #ifndef abcH\n "
574+ " #define abcH\n "
575+ " #endif\n " );
563576 // Handling include guards..
564- const char filedata[] = " #file \" abc.h\"\n "
565- " #ifndef abcH\n "
566- " #define abcH\n "
567- " #endif\n "
568- " #endfile\n "
577+ const char filedata[] = " #include \" abc.h\"\n "
569578 " #ifdef ABC\n "
570579 " #endif" ;
571580 ASSERT_EQUALS (" \n ABC\n " , getConfigsStr (filedata));
572581 }
573582
574583 void includeguard2 () {
584+ ScopedFile header (" abc.h" ,
585+ " foo\n "
586+ " #ifdef ABC\n "
587+ " \n "
588+ " #endif\n " );
575589 // Handling include guards..
576- const char filedata[] = " #file \" abc.h\"\n "
577- " foo\n "
578- " #ifdef ABC\n "
579- " \n "
580- " #endif\n "
581- " #endfile\n " ;
590+ const char filedata[] = " #include \" abc.h\"\n " ;
582591 ASSERT_EQUALS (" \n ABC\n " , getConfigsStr (filedata));
583592 }
584593
585594
586595 void ifdefwithfile () {
596+ ScopedFile header (" abc.h" , " class A{};/*\n\n\n\n\n\n\n */\n " );
597+
587598 // Handling include guards..
588599 const char filedata[] = " #ifdef ABC\n "
589- " #file \" abc.h\"\n "
590- " class A{};/*\n\n\n\n\n\n\n */\n "
591- " #endfile\n "
600+ " #include \" abc.h\"\n "
592601 " #endif\n "
593602 " int main() {}\n " ;
594603
@@ -1552,26 +1561,26 @@ class TestPreprocessor : public TestFixture {
15521561 }
15531562
15541563 {
1555- const char filedata[] = " #file \" abc.h\"\n "
1556- " #define a\n "
1557- " \"\n "
1558- " #endfile \n " ;
1564+ ScopedFile header ( " abc.h" ,
1565+ " #define a\n "
1566+ " \"\n " );
1567+ const char filedata[] = " #include \" abc.h \" " ;
15591568
15601569 // expand macros..
1561- const std::string actual (expandMacros (filedata, *this ));
1570+ const std::string actual (expandMacros (filedata, *this , false ));
15621571
15631572 ASSERT_EQUALS (" " , actual);
15641573 ASSERT_EQUALS (" [abc.h:2:1]: (error) No pair for character (\" ). Can't process file. File is either invalid or unicode, which is currently not supported. [syntaxError]\n " , errout_str ());
15651574 }
15661575
15671576 {
1568- const char filedata[] = " #file \" abc.h\"\n "
1569- " #define a\n "
1570- " #endfile \n "
1577+ ScopedFile header ( " abc.h" ,
1578+ " #define a\n " );
1579+ const char filedata[] = " #include \" abc.h \" \n"
15711580 " \"\n " ;
15721581
15731582 // expand macros..
1574- const std::string actual (expandMacros (filedata, *this ));
1583+ const std::string actual (expandMacros (filedata, *this , false ));
15751584
15761585 ASSERT_EQUALS (" " , actual);
15771586 ASSERT_EQUALS (" [file.cpp:2:1]: (error) No pair for character (\" ). Can't process file. File is either invalid or unicode, which is currently not supported. [syntaxError]\n " , errout_str ());
@@ -2230,14 +2239,14 @@ class TestPreprocessor : public TestFixture {
22302239 }
22312240
22322241 void getConfigs7e () {
2242+ ScopedFile header (" test.h" ,
2243+ " #ifndef test_h\n "
2244+ " #define test_h\n "
2245+ " #ifdef ABC\n "
2246+ " #endif\n "
2247+ " #endif\n " );
22332248 const char filedata[] = " #ifdef ABC\n "
2234- " #file \" test.h\"\n "
2235- " #ifndef test_h\n "
2236- " #define test_h\n "
2237- " #ifdef ABC\n "
2238- " #endif\n "
2239- " #endif\n "
2240- " #endfile\n "
2249+ " #include \" test.h\"\n "
22412250 " #endif\n " ;
22422251 ASSERT_EQUALS (" \n ABC\n " , getConfigsStr (filedata));
22432252 }
@@ -2259,12 +2268,12 @@ class TestPreprocessor : public TestFixture {
22592268 }
22602269
22612270 void getConfigs11 () { // #9832 - include guards
2262- const char filedata[] = " #file \" test.h\"\n "
2263- " #if !defined(test_h)\n "
2264- " #define test_h\n "
2265- " 123\n "
2266- " #endif\n "
2267- " #endfile \n " ;
2271+ ScopedFile header ( " test.h" ,
2272+ " #if !defined(test_h)\n "
2273+ " #define test_h\n "
2274+ " 123\n "
2275+ " #endif\n " );
2276+ const char filedata[] = " #include \" test.h \" \n" ;
22682277 ASSERT_EQUALS (" \n " , getConfigsStr (filedata));
22692278 }
22702279
0 commit comments