@@ -259,6 +259,11 @@ class TestPreprocessor : public TestFixture {
259259 TEST_CASE (testDirectiveIncludeComments);
260260
261261 TEST_CASE (testMissingInclude);
262+ TEST_CASE (testMissingInclude2);
263+ TEST_CASE (testMissingSystemInclude);
264+ TEST_CASE (testMissingSystemInclude2);
265+ TEST_CASE (testMissingSystemInclude3);
266+ TEST_CASE (testMissingIncludeMixed);
262267 TEST_CASE (testMissingIncludeCheckConfig);
263268 }
264269
@@ -2432,11 +2437,143 @@ class TestPreprocessor : public TestFixture {
24322437 ASSERT_EQUALS (dumpdata, ostr.str ());
24332438 }
24342439
2440+ // TODO: test with absolute path
2441+ // TODO: test with include path
2442+
2443+ // test for existing local include
24352444 void testMissingInclude () {
24362445 Preprocessor::missingIncludeFlag = false ;
24372446 Preprocessor::missingSystemIncludeFlag = false ;
24382447
24392448 Settings settings;
2449+ settings.clearIncludeCache = true ;
2450+ settings.severity .clear ();
2451+ settings.checks .enable (Checks::missingInclude);
2452+ Preprocessor preprocessor (settings, this );
2453+
2454+ ScopedFile header (" header.h" , " " );
2455+
2456+ std::string code (" #include \" header.h\" " );
2457+ errout.str (" " );
2458+ preprocessor.getcode (code, " " , " test.c" );
2459+ ASSERT_EQUALS (false , Preprocessor::missingIncludeFlag);
2460+ ASSERT_EQUALS (false , Preprocessor::missingSystemIncludeFlag);
2461+
2462+ // the expected messages are emitted outside of the Preprocessor
2463+ ASSERT_EQUALS (" " , errout.str ());
2464+
2465+ Preprocessor::missingIncludeFlag = false ;
2466+ Preprocessor::missingSystemIncludeFlag = false ;
2467+ }
2468+
2469+ // test for missing local include
2470+ void testMissingInclude2 () {
2471+ Preprocessor::missingIncludeFlag = false ;
2472+ Preprocessor::missingSystemIncludeFlag = false ;
2473+
2474+ Settings settings;
2475+ settings.clearIncludeCache = true ;
2476+ settings.severity .clear ();
2477+ settings.checks .enable (Checks::missingInclude);
2478+ Preprocessor preprocessor (settings, this );
2479+
2480+ std::string code (" #include \" header.h\" " );
2481+ errout.str (" " );
2482+ preprocessor.getcode (code, " " , " test.c" );
2483+ ASSERT_EQUALS (true , Preprocessor::missingIncludeFlag);
2484+ ASSERT_EQUALS (false , Preprocessor::missingSystemIncludeFlag);
2485+
2486+ // the expected messages are emitted outside of the Preprocessor
2487+ ASSERT_EQUALS (" " , errout.str ());
2488+
2489+ Preprocessor::missingIncludeFlag = false ;
2490+ Preprocessor::missingSystemIncludeFlag = false ;
2491+ }
2492+
2493+ // test for missing system include - system includes are not searched for in relative path
2494+ void testMissingSystemInclude () {
2495+ Preprocessor::missingIncludeFlag = false ;
2496+ Preprocessor::missingSystemIncludeFlag = false ;
2497+
2498+ Settings settings;
2499+ settings.clearIncludeCache = true ;
2500+ settings.severity .clear ();
2501+ settings.checks .enable (Checks::missingInclude);
2502+ Preprocessor preprocessor (settings, this );
2503+
2504+ ScopedFile header (" header.h" , " " );
2505+
2506+ std::string code (" #include <header.h>" );
2507+ errout.str (" " );
2508+ preprocessor.getcode (code, " " , " test.c" );
2509+ ASSERT_EQUALS (false , Preprocessor::missingIncludeFlag);
2510+ ASSERT_EQUALS (true , Preprocessor::missingSystemIncludeFlag);
2511+
2512+ // the expected messages are emitted outside of the Preprocessor
2513+ ASSERT_EQUALS (" " , errout.str ());
2514+
2515+ Preprocessor::missingIncludeFlag = false ;
2516+ Preprocessor::missingSystemIncludeFlag = false ;
2517+ }
2518+
2519+ // test for missing system include
2520+ void testMissingSystemInclude2 () {
2521+ Preprocessor::missingIncludeFlag = false ;
2522+ Preprocessor::missingSystemIncludeFlag = false ;
2523+
2524+ Settings settings;
2525+ settings.clearIncludeCache = true ;
2526+ settings.severity .clear ();
2527+ settings.checks .enable (Checks::missingInclude);
2528+ Preprocessor preprocessor (settings, this );
2529+
2530+ std::string code (" #include <header.h>" );
2531+ errout.str (" " );
2532+ preprocessor.getcode (code, " " , " test.c" );
2533+ ASSERT_EQUALS (false , Preprocessor::missingIncludeFlag);
2534+ ASSERT_EQUALS (true , Preprocessor::missingSystemIncludeFlag);
2535+
2536+ // the expected messages are emitted outside of the Preprocessor
2537+ ASSERT_EQUALS (" " , errout.str ());
2538+
2539+ Preprocessor::missingIncludeFlag = false ;
2540+ Preprocessor::missingSystemIncludeFlag = false ;
2541+ }
2542+
2543+ // test for existing system include in system include path
2544+ void testMissingSystemInclude3 () {
2545+ Preprocessor::missingIncludeFlag = false ;
2546+ Preprocessor::missingSystemIncludeFlag = false ;
2547+
2548+ Settings settings;
2549+ settings.clearIncludeCache = true ;
2550+ settings.severity .clear ();
2551+ settings.checks .enable (Checks::missingInclude);
2552+ settings.includePaths .emplace_back (" system" );
2553+ Preprocessor preprocessor (settings, this );
2554+
2555+ ScopedFile header (" header.h" , " " , " system" );
2556+
2557+ std::string code (" #include <header.h>" );
2558+ errout.str (" " );
2559+ preprocessor.getcode (code, " " , " test.c" );
2560+ ASSERT_EQUALS (false , Preprocessor::missingIncludeFlag);
2561+ ASSERT_EQUALS (false , Preprocessor::missingSystemIncludeFlag);
2562+
2563+ // the expected messages are emitted outside of the Preprocessor
2564+ ASSERT_EQUALS (" " , errout.str ());
2565+
2566+ Preprocessor::missingIncludeFlag = false ;
2567+ Preprocessor::missingSystemIncludeFlag = false ;
2568+ }
2569+
2570+ // test for missing local and system include
2571+ void testMissingIncludeMixed () {
2572+ Preprocessor::missingIncludeFlag = false ;
2573+ Preprocessor::missingSystemIncludeFlag = false ;
2574+
2575+ Settings settings;
2576+ settings.clearIncludeCache = true ;
24402577 settings.severity .clear ();
24412578 settings.checks .enable (Checks::missingInclude);
24422579 Preprocessor preprocessor (settings, this );
@@ -2465,18 +2602,22 @@ class TestPreprocessor : public TestFixture {
24652602 Preprocessor::missingSystemIncludeFlag = false ;
24662603
24672604 Settings settings;
2605+ settings.clearIncludeCache = true ;
24682606 settings.checkConfiguration = true ;
24692607 settings.severity .clear ();
2608+ settings.includePaths .emplace_back (" system" );
24702609 // needs to be reported regardless of enabled checks
24712610 Preprocessor preprocessor (settings, this );
24722611
24732612 ScopedFile header (" header.h" , " " );
24742613 ScopedFile header2 (" header2.h" , " " );
2614+ ScopedFile header3 (" header3.h" , " " , " system" );
24752615
24762616 std::string code (" #include \" missing.h\"\n "
24772617 " #include <header.h>\n "
24782618 " #include <missing2.h>\n "
2479- " #include \" header2.h\" " );
2619+ " #include \" header2.h\" "
2620+ " #include <header3.h>\n " );
24802621 errout.str (" " );
24812622 preprocessor.getcode (code, " " , " test.c" );
24822623 ASSERT_EQUALS (true , Preprocessor::missingIncludeFlag);
0 commit comments