@@ -4188,6 +4188,27 @@ static std::string getTimeDefine(const struct tm *timep)
41884188 return std::string (" \" " ).append (buf).append (" \" " );
41894189}
41904190
4191+ /* * Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
4192+ static void addMacroUsage (const simplecpp::Macro ¯o, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
4193+ {
4194+ if (!macroUsage)
4195+ return ;
4196+ std::list<simplecpp::Macro::Usage> usage = macro.usage ();
4197+ const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find (macro.name ());
4198+ if (maybeUsed != maybeUsedMacros.end ()) {
4199+ usage.insert (usage.end (), maybeUsed->second .begin (), maybeUsed->second .end ());
4200+ maybeUsedMacros.erase (maybeUsed);
4201+ }
4202+ for (std::list<simplecpp::Macro::Usage>::iterator usageIt = usage.begin (); usageIt != usage.end (); ++usageIt) {
4203+ simplecpp::MacroUsage mu (macro.valueDefinedInCode ());
4204+ mu.macroName = macro.name ();
4205+ mu.macroLocation = macro.defineLocation ();
4206+ mu.useLocation = usageIt->location ;
4207+ mu.arg = std::move (usageIt->arg );
4208+ macroUsage->emplace_back (std::move (mu));
4209+ }
4210+ }
4211+
41914212void 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)
41924213{
41934214#ifdef SIMPLECPP_WINDOWS
@@ -4395,10 +4416,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43954416 const Macro ¯o = Macro (rawtok->previous , files);
43964417 if (dui.undefined .find (macro.name ()) == dui.undefined .end ()) {
43974418 const MacroMap::iterator it = macros.find (macro.name ());
4398- if (it == macros.end ())
4419+ if (it == macros.end ()) {
43994420 macros.insert (std::pair<TokenString, Macro>(macro.name (), macro));
4400- else
4421+ } else {
4422+ addMacroUsage (it->second , maybeUsedMacros, macroUsage);
44014423 it->second = macro;
4424+ }
44024425 }
44034426 } catch (const std::runtime_error &err) {
44044427 if (outputList) {
@@ -4687,8 +4710,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46874710 const Token *tok = rawtok->next ;
46884711 while (sameline (rawtok,tok) && tok->comment )
46894712 tok = tok->next ;
4690- if (sameline (rawtok, tok))
4691- macros.erase (tok->str ());
4713+ if (sameline (rawtok, tok)) {
4714+ const MacroMap::iterator it = macros.find (tok->str ());
4715+ if (it != macros.end ()) {
4716+ addMacroUsage (it->second , maybeUsedMacros, macroUsage);
4717+ macros.erase (it);
4718+ }
4719+ }
46924720 }
46934721 } else if (ifstates.top () == True && rawtok->str () == PRAGMA && rawtok->next && rawtok->next ->str () == ONCE && sameline (rawtok,rawtok->next )) {
46944722 pragmaOnce.insert (rawtokens.file (rawtok->location ));
@@ -4740,22 +4768,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47404768 }
47414769 }
47424770
4743- if (macroUsage) {
4744- for (simplecpp::MacroMap::const_iterator macroIt = macros.begin (); macroIt != macros.end (); ++macroIt) {
4745- const Macro ¯o = macroIt->second ;
4746- std::list<Macro::Usage> usage = macro.usage ();
4747- const std::list<Location>& temp = maybeUsedMacros[macro.name ()];
4748- usage.insert (usage.end (), temp.begin (), temp.end ());
4749- for (std::list<Macro::Usage>::iterator usageIt = usage.begin (); usageIt != usage.end (); ++usageIt) {
4750- MacroUsage mu (macro.valueDefinedInCode ());
4751- mu.macroName = macro.name ();
4752- mu.macroLocation = macro.defineLocation ();
4753- mu.useLocation = usageIt->location ;
4754- mu.arg = std::move (usageIt->arg );
4755- macroUsage->emplace_back (std::move (mu));
4756- }
4757- }
4758- }
4771+ for (simplecpp::MacroMap::const_iterator macroIt = macros.begin (); macroIt != macros.end (); ++macroIt)
4772+ addMacroUsage (macroIt->second , maybeUsedMacros, macroUsage);
47594773}
47604774
47614775void simplecpp::cleanup (FileDataCache &cache)
0 commit comments