@@ -50,13 +50,14 @@ class TestPreprocessor : public TestFixture {
5050 TestPreprocessor () : TestFixture(" TestPreprocessor" ) {}
5151
5252private:
53+ #define expandMacros (...) expandMacros_(__FILE__, __LINE__, __VA_ARGS__)
5354 template <size_t size>
54- std::string expandMacros ( const char (&code)[size], ErrorLogger &errorLogger) const {
55+ std::string expandMacros_ ( const char * file, int line, const char (&code)[size], ErrorLogger &errorLogger) const {
5556 simplecpp::OutputList outputList;
5657 std::vector<std::string> files;
5758 simplecpp::TokenList tokens1 = simplecpp::TokenList (code, files, " file.cpp" , &outputList);
5859 Preprocessor p (tokens1, settingsDefault, errorLogger, Path::identify (tokens1.getFiles ()[0 ], false ));
59- ASSERT (p.loadFiles (files));
60+ ASSERT_LOC (p.loadFiles (files), file, line );
6061 simplecpp::TokenList tokens2 = p.preprocess (" " , files, outputList);
6162 (void )p.reportOutput (outputList, true );
6263 return tokens2.stringify ();
@@ -140,7 +141,7 @@ class TestPreprocessor : public TestFixture {
140141 cfgs = preprocessor.getConfigs ();
141142 for (const std::string & config : cfgs) {
142143 try {
143- const bool writeLocations = (strstr (code, " #file " ) != nullptr ) || ( strstr (code, " # include" ) != nullptr );
144+ const bool writeLocations = (strstr (code, " #include" ) != nullptr );
144145 cfgcode[config] = preprocessor.getcode (config, files, writeLocations);
145146 } catch (const simplecpp::Output &) {
146147 cfgcode[config] = " " ;
@@ -392,7 +393,7 @@ class TestPreprocessor : public TestFixture {
392393 std::vector<std::string> files;
393394 simplecpp::OutputList outputList;
394395 simplecpp::TokenList tokens (code,files," test.c" ,&outputList);
395- Preprocessor preprocessor (tokens, settings, *this , Standards::Language::C); // TODO: do we need to consider #file?
396+ Preprocessor preprocessor (tokens, settings, *this , Standards::Language::C);
396397 ASSERT (preprocessor.loadFiles (files));
397398 ASSERT (!preprocessor.reportOutput (outputList, true ));
398399 preprocessor.removeComments ();
@@ -407,7 +408,7 @@ class TestPreprocessor : public TestFixture {
407408 std::size_t getHash (const char (&code)[size]) {
408409 std::vector<std::string> files;
409410 simplecpp::TokenList tokens (code,files," test.c" );
410- Preprocessor preprocessor (tokens, settingsDefault, *this , Standards::Language::C); // TODO: do we need to consider #file?
411+ Preprocessor preprocessor (tokens, settingsDefault, *this , Standards::Language::C);
411412 ASSERT (preprocessor.loadFiles (files));
412413 preprocessor.removeComments ();
413414 return preprocessor.calculateHash (" " );
@@ -472,16 +473,19 @@ class TestPreprocessor : public TestFixture {
472473 void error4 () {
473474 // In included file
474475 {
476+ ScopedFile header (" ab.h" , " #error hello world!\n " );
475477 const auto settings = dinit (Settings, $.userDefines = " TEST" );
476- const char code[] = " #file \" ab.h\"\n #error hello world! \n #endfile " ;
478+ const char code[] = " #include \" ab.h\" " ;
477479 (void )getcodeforcfg (settings, *this , code, " TEST" , " test.c" );
478480 ASSERT_EQUALS (" [ab.h:1:2]: (error) #error hello world! [preprocessorErrorDirective]\n " , errout_str ());
479481 }
480482
481483 // After including a file
482484 {
485+ ScopedFile header (" ab.h" , " " );
483486 const auto settings = dinit (Settings, $.userDefines = " TEST" );
484- const char code[] = " #file \" ab.h\"\n\n #endfile\n #error aaa" ;
487+ const char code[] = " #include \" ab.h\"\n "
488+ " #error aaa" ;
485489 (void )getcodeforcfg (settings, *this , code, " TEST" , " test.c" );
486490 ASSERT_EQUALS (" [test.c:2:2]: (error) #error aaa [preprocessorErrorDirective]\n " , errout_str ());
487491 }
@@ -584,35 +588,35 @@ class TestPreprocessor : public TestFixture {
584588 }
585589
586590 void includeguard1 () {
591+ ScopedFile header (" abc.h" ,
592+ " #ifndef abcH\n "
593+ " #define abcH\n "
594+ " #endif\n " );
587595 // Handling include guards..
588- const char filedata[] = " #file \" abc.h\"\n "
589- " #ifndef abcH\n "
590- " #define abcH\n "
591- " #endif\n "
592- " #endfile\n "
596+ const char filedata[] = " #include \" abc.h\"\n "
593597 " #ifdef ABC\n "
594598 " #endif" ;
595599 ASSERT_EQUALS (" \n ABC=ABC\n " , getConfigsStr (filedata));
596600 }
597601
598602 void includeguard2 () {
603+ ScopedFile header (" abc.h" ,
604+ " foo\n "
605+ " #ifdef ABC\n "
606+ " \n "
607+ " #endif\n " );
599608 // Handling include guards..
600- const char filedata[] = " #file \" abc.h\"\n "
601- " foo\n "
602- " #ifdef ABC\n "
603- " \n "
604- " #endif\n "
605- " #endfile\n " ;
609+ const char filedata[] = " #include \" abc.h\"\n " ;
606610 ASSERT_EQUALS (" \n ABC=ABC\n " , getConfigsStr (filedata));
607611 }
608612
609613
610614 void ifdefwithfile () {
615+ ScopedFile header (" abc.h" , " class A{};/*\n\n\n\n\n\n\n */\n " );
616+
611617 // Handling include guards..
612618 const char filedata[] = " #ifdef ABC\n "
613- " #file \" abc.h\"\n "
614- " class A{};/*\n\n\n\n\n\n\n */\n "
615- " #endfile\n "
619+ " #include \" abc.h\"\n "
616620 " #endif\n "
617621 " int main() {}\n " ;
618622
@@ -1576,22 +1580,9 @@ class TestPreprocessor : public TestFixture {
15761580 }
15771581
15781582 {
1579- const char filedata[] = " #file \" abc.h\"\n "
1580- " #define a\n "
1581- " \"\n "
1582- " #endfile\n " ;
1583-
1584- // expand macros..
1585- const std::string actual (expandMacros (filedata, *this ));
1586-
1587- ASSERT_EQUALS (" " , actual);
1588- 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 ());
1589- }
1590-
1591- {
1592- const char filedata[] = " #file \" abc.h\"\n "
1593- " #define a\n "
1594- " #endfile\n "
1583+ ScopedFile header (" abc.h" ,
1584+ " #define a\n " );
1585+ const char filedata[] = " #include \" abc.h\"\n "
15951586 " \"\n " ;
15961587
15971588 // expand macros..
@@ -2286,14 +2277,14 @@ class TestPreprocessor : public TestFixture {
22862277 }
22872278
22882279 void getConfigs7e () {
2280+ ScopedFile header (" test.h" ,
2281+ " #ifndef test_h\n "
2282+ " #define test_h\n "
2283+ " #ifdef ABC\n "
2284+ " #endif\n "
2285+ " #endif\n " );
22892286 const char filedata[] = " #ifdef ABC\n "
2290- " #file \" test.h\"\n "
2291- " #ifndef test_h\n "
2292- " #define test_h\n "
2293- " #ifdef ABC\n "
2294- " #endif\n "
2295- " #endif\n "
2296- " #endfile\n "
2287+ " #include \" test.h\"\n "
22972288 " #endif\n " ;
22982289 ASSERT_EQUALS (" \n ABC=ABC\n " , getConfigsStr (filedata));
22992290 }
@@ -2315,12 +2306,12 @@ class TestPreprocessor : public TestFixture {
23152306 }
23162307
23172308 void getConfigs11 () { // #9832 - include guards
2318- const char filedata[] = " #file \" test.h\"\n "
2319- " #if !defined(test_h)\n "
2320- " #define test_h\n "
2321- " 123\n "
2322- " #endif\n "
2323- " #endfile \n " ;
2309+ ScopedFile header ( " test.h" ,
2310+ " #if !defined(test_h)\n "
2311+ " #define test_h\n "
2312+ " 123\n "
2313+ " #endif\n " );
2314+ const char filedata[] = " #include \" test.h \" \n" ;
23242315 ASSERT_EQUALS (" \n " , getConfigsStr (filedata));
23252316 }
23262317
0 commit comments