@@ -322,15 +322,17 @@ static Optional<std::pair<llvm::APSInt, Type>>
322322 return None;
323323}
324324
325-
326325static 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,
331329 clang::QualType castType) {
332330 if (name.empty ()) return nullptr ;
333331
332+ assert (visitedMacros.count (name.str ()) &&
333+ " Add the name of the macro to visitedMacros before calling this "
334+ " function." );
335+
334336 auto numTokens = macro->getNumTokens ();
335337 auto tokenI = macro->tokens_begin (), tokenE = macro->tokens_end ();
336338
@@ -427,9 +429,15 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
427429
428430 auto macroID = impl.getClangPreprocessor ().getMacroInfo (clangID);
429431 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+
430437 // FIXME: This was clearly intended to pass the cast type down, but
431438 // 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*/ {});
433441 }
434442 }
435443
@@ -686,8 +694,11 @@ ValueDecl *ClangImporter::Implementation::importMacro(Identifier name,
686694 DC = ImportedHeaderUnit;
687695 }
688696
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*/ {});
691702
692703 // Update the entry for the value we just imported.
693704 // It's /probably/ the last entry in ImportedMacros[name], but there's an
0 commit comments