@@ -103,15 +103,15 @@ static std::string readfile(const char code[], std::size_t size, simplecpp::Outp
103103 return makeTokenList (code,size,files,std::string (),outputList).stringify ();
104104}
105105
106- static std::string preprocess (const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, const std::string &file = std::string())
106+ static std::string preprocess (const char code[], const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage = nullptr , std::list<simplecpp::IfCond> *ifCond = nullptr , const std::string &file = std::string())
107107{
108108 std::vector<std::string> files;
109109 simplecpp::FileDataCache cache;
110110 simplecpp::TokenList tokens = makeTokenList (code,files, file);
111111 if (dui.removeComments )
112112 tokens.removeComments ();
113113 simplecpp::TokenList tokens2 (files);
114- simplecpp::preprocess (tokens2, tokens, files, cache, dui, outputList);
114+ simplecpp::preprocess (tokens2, tokens, files, cache, dui, outputList, macroUsage, ifCond );
115115 simplecpp::cleanup (cache);
116116 return tokens2.stringify ();
117117}
@@ -123,7 +123,7 @@ static std::string preprocess(const char code[])
123123
124124static std::string preprocess (const char code[], const std::string &file)
125125{
126- return preprocess (code, simplecpp::DUI (), nullptr , file);
126+ return preprocess (code, simplecpp::DUI (), nullptr , nullptr , nullptr , file);
127127}
128128
129129static std::string preprocess (const char code[], const simplecpp::DUI &dui)
@@ -136,6 +136,16 @@ static std::string preprocess(const char code[], simplecpp::OutputList *outputLi
136136 return preprocess (code, simplecpp::DUI (), outputList);
137137}
138138
139+ static std::string preprocess (const char code[], std::list<simplecpp::IfCond> *ifCond)
140+ {
141+ return preprocess (code, simplecpp::DUI (), nullptr , nullptr , ifCond);
142+ }
143+
144+ static std::string preprocess (const char code[], std::list<simplecpp::MacroUsage> *macroUsage)
145+ {
146+ return preprocess (code, simplecpp::DUI (), nullptr , macroUsage);
147+ }
148+
139149static std::string toString (const simplecpp::OutputList &outputList)
140150{
141151 std::ostringstream ostr;
@@ -3461,6 +3471,70 @@ static void bad_macro_syntax() // #616
34613471 ASSERT_EQUALS (" bad macro syntax. macroname=\" value=1" , outputList.cbegin ()->msg );
34623472}
34633473
3474+ static void ifCond ()
3475+ {
3476+ {
3477+ const char code[] = " int i;" ;
3478+ std::list<simplecpp::IfCond> ifCond;
3479+ ASSERT_EQUALS (" int i ;" , preprocess (code, &ifCond));
3480+ ASSERT_EQUALS (0 , ifCond.size ());
3481+ }
3482+ {
3483+ const char code[] = " #if 0\n "
3484+ " # elif __GNUC__ == 1\n "
3485+ " # elif defined(__APPLE__)\n "
3486+ " #endif\n " ;
3487+ std::list<simplecpp::IfCond> ifCond;
3488+ ASSERT_EQUALS (" " , preprocess (code, &ifCond));
3489+ ASSERT_EQUALS (3 , ifCond.size ());
3490+ auto it = ifCond.cbegin ();
3491+ ASSERT_EQUALS (0 , it->location .fileIndex );
3492+ ASSERT_EQUALS (1 , it->location .line );
3493+ ASSERT_EQUALS (2 , it->location .col );
3494+ ASSERT_EQUALS (" 0" , it->E );
3495+ ASSERT_EQUALS (0 , it->result );
3496+ ++it;
3497+ ASSERT_EQUALS (0 , it->location .fileIndex );
3498+ ASSERT_EQUALS (2 , it->location .line );
3499+ ASSERT_EQUALS (3 , it->location .col );
3500+ ASSERT_EQUALS (" __GNUC__ == 1" , it->E );
3501+ ASSERT_EQUALS (0 , it->result );
3502+ ++it;
3503+ ASSERT_EQUALS (0 , it->location .fileIndex );
3504+ ASSERT_EQUALS (3 , it->location .line );
3505+ ASSERT_EQUALS (4 , it->location .col );
3506+ ASSERT_EQUALS (" 0" , it->E );
3507+ ASSERT_EQUALS (0 , it->result );
3508+ }
3509+ }
3510+
3511+ static void macroUsage ()
3512+ {
3513+ {
3514+ const char code[] = " int i;" ;
3515+ std::list<simplecpp::MacroUsage> macroUsage;
3516+ ASSERT_EQUALS (" int i ;" , preprocess (code, ¯oUsage));
3517+ ASSERT_EQUALS (0 , macroUsage.size ());
3518+ }
3519+ {
3520+ const char code[] = " #define DEF_1\n "
3521+ " #ifdef DEF_1\n "
3522+ " #endif\n " ;
3523+ std::list<simplecpp::MacroUsage> macroUsage;
3524+ ASSERT_EQUALS (" " , preprocess (code, ¯oUsage));
3525+ ASSERT_EQUALS (1 , macroUsage.size ());
3526+ auto it = macroUsage.cbegin ();
3527+ ASSERT_EQUALS (" DEF_1" , it->macroName );
3528+ ASSERT_EQUALS (0 , it->macroLocation .fileIndex );
3529+ ASSERT_EQUALS (1 , it->macroLocation .line );
3530+ ASSERT_EQUALS (9 , it->macroLocation .col );
3531+ ASSERT_EQUALS (true , it->macroValueKnown );
3532+ ASSERT_EQUALS (0 , it->useLocation .fileIndex );
3533+ ASSERT_EQUALS (2 , it->useLocation .line );
3534+ ASSERT_EQUALS (8 , it->useLocation .col );
3535+ }
3536+ }
3537+
34643538static void isAbsolutePath () {
34653539#ifdef _WIN32
34663540 ASSERT_EQUALS (true , simplecpp::isAbsolutePath (" C:\\ foo\\ bar" ));
@@ -3790,6 +3864,9 @@ int main(int argc, char **argv)
37903864
37913865 TEST_CASE (bad_macro_syntax);
37923866
3867+ TEST_CASE (ifCond);
3868+ TEST_CASE (macroUsage);
3869+
37933870 TEST_CASE (fuzz_crash);
37943871
37953872 TEST_CASE (leak);
0 commit comments