@@ -286,78 +286,59 @@ class MacroCallback : public PPCallbacks {
286286 MacroCallback (const SourceManager &SM, APISet &API, Preprocessor &PP)
287287 : SM(SM), API(API), PP(PP) {}
288288
289- void MacroDefined (const Token &MacroNameToken,
290- const MacroDirective *MD) override {
291- auto *MacroInfo = MD->getMacroInfo ();
289+ void EndOfMainFile () override {
290+ for (const auto &M : PP.macros ()) {
291+ auto *II = M.getFirst ();
292+ auto MD = PP.getMacroDefinition (II);
293+ auto *MI = MD.getMacroInfo ();
292294
293- if (MacroInfo-> isBuiltinMacro () )
294- return ;
295+ if (!MI )
296+ continue ;
295297
296- auto SourceLoc = MacroNameToken.getLocation ();
297- if (SM.isWrittenInBuiltinFile (SourceLoc) ||
298- SM.isWrittenInCommandLineFile (SourceLoc))
299- return ;
298+ // Ignore header guard macros
299+ if (MI->isUsedForHeaderGuard ())
300+ continue ;
300301
301- PendingMacros.emplace_back (MacroNameToken, MD);
302- }
302+ // Ignore builtin macros and ones defined via the command line.
303+ if (MI->isBuiltinMacro ())
304+ continue ;
303305
304- // If a macro gets undefined at some point during preprocessing of the inputs
305- // it means that it isn't an exposed API and we should therefore not add a
306- // macro definition for it.
307- void MacroUndefined (const Token &MacroNameToken, const MacroDefinition &MD,
308- const MacroDirective *Undef) override {
309- // If this macro wasn't previously defined we don't need to do anything
310- // here.
311- if (!Undef)
312- return ;
313-
314- llvm::erase_if (PendingMacros, [&MD, this ](const PendingMacro &PM) {
315- return MD.getMacroInfo ()->isIdenticalTo (*PM.MD ->getMacroInfo (), PP,
316- /* Syntactically*/ false );
317- });
318- }
306+ auto DefLoc = MI->getDefinitionLoc ();
319307
320- void EndOfMainFile () override {
321- for (auto &PM : PendingMacros) {
322- // `isUsedForHeaderGuard` is only set when the preprocessor leaves the
323- // file so check for it here.
324- if (PM.MD ->getMacroInfo ()->isUsedForHeaderGuard ())
308+ if (SM.isWrittenInBuiltinFile (DefLoc) ||
309+ SM.isWrittenInCommandLineFile (DefLoc))
325310 continue ;
326311
327- if (!shouldMacroBeIncluded (PM))
312+ auto AssociatedModuleMacros = MD.getModuleMacros ();
313+ StringRef OwningModuleName;
314+ if (!AssociatedModuleMacros.empty ())
315+ OwningModuleName = AssociatedModuleMacros.back ()
316+ ->getOwningModule ()
317+ ->getTopLevelModuleName ();
318+
319+ if (!shouldMacroBeIncluded (DefLoc, OwningModuleName))
328320 continue ;
329321
330- StringRef Name = PM. MacroNameToken . getIdentifierInfo () ->getName ();
331- PresumedLoc Loc = SM.getPresumedLoc (PM. MacroNameToken . getLocation () );
322+ StringRef Name = II ->getName ();
323+ PresumedLoc Loc = SM.getPresumedLoc (DefLoc );
332324 SmallString<128 > USR;
333- index::generateUSRForMacro (Name, PM.MacroNameToken .getLocation (), SM,
334- USR);
335-
325+ index::generateUSRForMacro (Name, DefLoc, SM, USR);
336326 API.createRecord <extractapi::MacroDefinitionRecord>(
337327 USR, Name, SymbolReference (), Loc,
338- DeclarationFragmentsBuilder::getFragmentsForMacro (Name, PM. MD ),
328+ DeclarationFragmentsBuilder::getFragmentsForMacro (Name, MI ),
339329 DeclarationFragmentsBuilder::getSubHeadingForMacro (Name),
340- SM.isInSystemHeader (PM. MacroNameToken . getLocation () ));
330+ SM.isInSystemHeader (DefLoc ));
341331 }
342-
343- PendingMacros.clear ();
344332 }
345333
346- protected:
347- struct PendingMacro {
348- Token MacroNameToken;
349- const MacroDirective *MD;
350-
351- PendingMacro (const Token &MacroNameToken, const MacroDirective *MD)
352- : MacroNameToken(MacroNameToken), MD(MD) {}
353- };
354-
355- virtual bool shouldMacroBeIncluded (const PendingMacro &PM) { return true ; }
334+ virtual bool shouldMacroBeIncluded (const SourceLocation &MacroLoc,
335+ StringRef ModuleName) {
336+ return true ;
337+ }
356338
357339 const SourceManager &SM;
358340 APISet &API;
359341 Preprocessor &PP;
360- llvm::SmallVector<PendingMacro> PendingMacros;
361342};
362343
363344class APIMacroCallback : public MacroCallback {
@@ -366,9 +347,10 @@ class APIMacroCallback : public MacroCallback {
366347 LocationFileChecker &LCF)
367348 : MacroCallback(SM, API, PP), LCF(LCF) {}
368349
369- bool shouldMacroBeIncluded (const PendingMacro &PM) override {
350+ bool shouldMacroBeIncluded (const SourceLocation &MacroLoc,
351+ StringRef ModuleName) override {
370352 // Do not include macros from external files
371- return LCF (PM. MacroNameToken . getLocation ()) ;
353+ return LCF (MacroLoc) || API. ProductName == ModuleName ;
372354 }
373355
374356private:
0 commit comments