@@ -322,15 +322,17 @@ static Optional<std::pair<llvm::APSInt, Type>>
322
322
return None;
323
323
}
324
324
325
-
326
325
static ValueDecl *importMacro (ClangImporter::Implementation &impl,
327
- DeclContext *DC,
328
- Identifier name,
329
- const clang::MacroInfo *macro,
330
- ClangNode ClangN,
326
+ llvm::SmallSet<StringRef, 4 > &visitedMacros,
327
+ DeclContext *DC, Identifier name,
328
+ const clang::MacroInfo *macro, ClangNode ClangN,
331
329
clang::QualType castType) {
332
330
if (name.empty ()) return nullptr ;
333
331
332
+ assert (visitedMacros.count (name.str ()) &&
333
+ " Add the name of the macro to visitedMacros before calling this "
334
+ " function." );
335
+
334
336
auto numTokens = macro->getNumTokens ();
335
337
auto tokenI = macro->tokens_begin (), tokenE = macro->tokens_end ();
336
338
@@ -427,9 +429,15 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
427
429
428
430
auto macroID = impl.getClangPreprocessor ().getMacroInfo (clangID);
429
431
if (macroID && macroID != macro) {
432
+ // If we've already visited this macro, then bail to prevent an
433
+ // infinite loop. Otherwise, record that we're going to visit it.
434
+ if (!visitedMacros.insert (clangID->getName ()).second )
435
+ return nullptr ;
436
+
430
437
// FIXME: This was clearly intended to pass the cast type down, but
431
438
// doing so would be a behavior change.
432
- return importMacro (impl, DC, name, macroID, ClangN, /* castType*/ {});
439
+ return importMacro (impl, visitedMacros, DC, name, macroID, ClangN,
440
+ /* castType*/ {});
433
441
}
434
442
}
435
443
@@ -686,8 +694,11 @@ ValueDecl *ClangImporter::Implementation::importMacro(Identifier name,
686
694
DC = ImportedHeaderUnit;
687
695
}
688
696
689
- auto valueDecl = ::importMacro (*this , DC, name, macro, macroNode,
690
- /* castType*/ {});
697
+ llvm::SmallSet<StringRef, 4 > visitedMacros;
698
+ visitedMacros.insert (name.str ());
699
+ auto valueDecl =
700
+ ::importMacro (*this , visitedMacros, DC, name, macro, macroNode,
701
+ /* castType*/ {});
691
702
692
703
// Update the entry for the value we just imported.
693
704
// It's /probably/ the last entry in ImportedMacros[name], but there's an
0 commit comments