@@ -3978,6 +3978,26 @@ static std::string getTimeDefine(const struct tm *timep)
39783978 return std::string (" \" " ).append (buf).append (" \" " );
39793979}
39803980
3981+ /* * Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
3982+ static void addMacroUsage (const simplecpp::Macro ¯o, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
3983+ {
3984+ if (!macroUsage)
3985+ return ;
3986+ std::list<simplecpp::Location> usage = macro.usage ();
3987+ const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find (macro.name ());
3988+ if (maybeUsed != maybeUsedMacros.end ()) {
3989+ usage.splice (usage.end (), maybeUsed->second );
3990+ maybeUsedMacros.erase (maybeUsed);
3991+ }
3992+ for (std::list<simplecpp::Location>::const_iterator usageIt = usage.begin (); usageIt != usage.end (); ++usageIt) {
3993+ simplecpp::MacroUsage mu (macro.valueDefinedInCode ());
3994+ mu.macroName = macro.name ();
3995+ mu.macroLocation = macro.defineLocation ();
3996+ mu.useLocation = *usageIt;
3997+ macroUsage->emplace_back (std::move (mu));
3998+ }
3999+ }
4000+
39814001void simplecpp::preprocess (simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &cache, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
39824002{
39834003#ifdef SIMPLECPP_WINDOWS
@@ -4187,8 +4207,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
41874207 const MacroMap::iterator it = macros.find (macro.name ());
41884208 if (it == macros.end ())
41894209 macros.insert (std::pair<TokenString, Macro>(macro.name (), macro));
4190- else
4210+ else {
4211+ addMacroUsage (it->second , maybeUsedMacros, macroUsage);
41914212 it->second = macro;
4213+ }
41924214 }
41934215 } catch (const std::runtime_error &err) {
41944216 if (outputList) {
@@ -4477,8 +4499,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
44774499 const Token *tok = rawtok->next ;
44784500 while (sameline (rawtok,tok) && tok->comment )
44794501 tok = tok->next ;
4480- if (sameline (rawtok, tok))
4481- macros.erase (tok->str ());
4502+ if (sameline (rawtok, tok)) {
4503+ const MacroMap::iterator it = macros.find (tok->str ());
4504+ if (it != macros.end ()) {
4505+ addMacroUsage (it->second , maybeUsedMacros, macroUsage);
4506+ macros.erase (it);
4507+ }
4508+ }
44824509 }
44834510 } else if (ifstates.top () == True && rawtok->str () == PRAGMA && rawtok->next && rawtok->next ->str () == ONCE && sameline (rawtok,rawtok->next )) {
44844511 pragmaOnce.insert (rawtokens.file (rawtok->location ));
@@ -4530,21 +4557,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
45304557 }
45314558 }
45324559
4533- if (macroUsage) {
4534- for (simplecpp::MacroMap::const_iterator macroIt = macros.begin (); macroIt != macros.end (); ++macroIt) {
4535- const Macro ¯o = macroIt->second ;
4536- std::list<Location> usage = macro.usage ();
4537- const std::list<Location>& temp = maybeUsedMacros[macro.name ()];
4538- usage.insert (usage.end (), temp.begin (), temp.end ());
4539- for (std::list<Location>::const_iterator usageIt = usage.begin (); usageIt != usage.end (); ++usageIt) {
4540- MacroUsage mu (macro.valueDefinedInCode ());
4541- mu.macroName = macro.name ();
4542- mu.macroLocation = macro.defineLocation ();
4543- mu.useLocation = *usageIt;
4544- macroUsage->emplace_back (std::move (mu));
4545- }
4546- }
4547- }
4560+ for (simplecpp::MacroMap::const_iterator macroIt = macros.begin (); macroIt != macros.end (); ++macroIt)
4561+ addMacroUsage (macroIt->second , maybeUsedMacros, macroUsage);
45484562}
45494563
45504564void simplecpp::cleanup (FileDataCache &cache)
0 commit comments