@@ -286,78 +286,59 @@ class MacroCallback : public PPCallbacks {
286
286
MacroCallback (const SourceManager &SM, APISet &API, Preprocessor &PP)
287
287
: SM(SM), API(API), PP(PP) {}
288
288
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 ();
292
294
293
- if (MacroInfo-> isBuiltinMacro () )
294
- return ;
295
+ if (!MI )
296
+ continue ;
295
297
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 ;
300
301
301
- PendingMacros.emplace_back (MacroNameToken, MD);
302
- }
302
+ // Ignore builtin macros and ones defined via the command line.
303
+ if (MI->isBuiltinMacro ())
304
+ continue ;
303
305
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 ();
319
307
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))
325
310
continue ;
326
311
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))
328
320
continue ;
329
321
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 );
332
324
SmallString<128 > USR;
333
- index::generateUSRForMacro (Name, PM.MacroNameToken .getLocation (), SM,
334
- USR);
335
-
325
+ index::generateUSRForMacro (Name, DefLoc, SM, USR);
336
326
API.createRecord <extractapi::MacroDefinitionRecord>(
337
327
USR, Name, SymbolReference (), Loc,
338
- DeclarationFragmentsBuilder::getFragmentsForMacro (Name, PM. MD ),
328
+ DeclarationFragmentsBuilder::getFragmentsForMacro (Name, MI ),
339
329
DeclarationFragmentsBuilder::getSubHeadingForMacro (Name),
340
- SM.isInSystemHeader (PM. MacroNameToken . getLocation () ));
330
+ SM.isInSystemHeader (DefLoc ));
341
331
}
342
-
343
- PendingMacros.clear ();
344
332
}
345
333
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
+ }
356
338
357
339
const SourceManager &SM;
358
340
APISet &API;
359
341
Preprocessor &PP;
360
- llvm::SmallVector<PendingMacro> PendingMacros;
361
342
};
362
343
363
344
class APIMacroCallback : public MacroCallback {
@@ -366,9 +347,10 @@ class APIMacroCallback : public MacroCallback {
366
347
LocationFileChecker &LCF)
367
348
: MacroCallback(SM, API, PP), LCF(LCF) {}
368
349
369
- bool shouldMacroBeIncluded (const PendingMacro &PM) override {
350
+ bool shouldMacroBeIncluded (const SourceLocation &MacroLoc,
351
+ StringRef ModuleName) override {
370
352
// Do not include macros from external files
371
- return LCF (PM. MacroNameToken . getLocation ()) ;
353
+ return LCF (MacroLoc) || API. ProductName == ModuleName ;
372
354
}
373
355
374
356
private:
0 commit comments