Skip to content

Commit 9f3980a

Browse files
authored
Merge pull request swiftlang#34587 from zoecarver/cxx/fix/redefined-macros
[cxx-interop] Bail on redefined macros.
2 parents 77201c0 + e639cae commit 9f3980a

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

lib/ClangImporter/ImportMacro.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,17 @@ static Optional<std::pair<llvm::APSInt, Type>>
322322
return None;
323323
}
324324

325-
326325
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,
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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module RedefinedMacro {
2+
header "redefined-macro.h"
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#define FOO 0
2+
#define TMP_FOO FOO
3+
#undef FOO
4+
#define FOO TMP_FOO
5+
6+
#define ONE TWO
7+
#define TWO THREE
8+
#define THREE ONE
9+
10+
#define THE_MAX THREE
11+
#undef THREE
12+
#define THREE THE_MAX
13+
14+
#define BAR 0
15+
#define BAZ BAR
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=RedefinedMacro -I %S/Inputs -source-filename=x | %FileCheck %s
2+
3+
// We cannot import redefined macros.
4+
// CHECK-NOT: FOO
5+
// CHECK-NOT: TMP_FOO
6+
// CHECK-NOT: THREE
7+
// CHECK-NOT: THE_MAX
8+
9+
// CHECK: var BAR
10+
// CHECK: var BAZ

0 commit comments

Comments
 (0)