[llvm] a09f09c - [TableGen] Fix incorrect handling of nested `#ifndef` directives
Min-Yih Hsu via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 7 12:57:58 PDT 2023
Author: Min-Yih Hsu
Date: 2023-09-07T12:42:22-07:00
New Revision: a09f09c618e9ec9ba8455ffa7f9765b240f0d16d
URL: https://github.com/llvm/llvm-project/commit/a09f09c618e9ec9ba8455ffa7f9765b240f0d16d
DIFF: https://github.com/llvm/llvm-project/commit/a09f09c618e9ec9ba8455ffa7f9765b240f0d16d.diff
LOG: [TableGen] Fix incorrect handling of nested `#ifndef` directives
TableGen's lexer was unable to handle nested #ifndef when the outer
`#ifdef` / `#ifndef` scope is subject to skip. This was caused by returning
the canonicalized token when it should have returned the original one.
Fix #65100.
Differential Revision: https://reviews.llvm.org/D159236
Added:
llvm/test/TableGen/nested_ifdef.inc
llvm/test/TableGen/nested_ifdef2.inc
llvm/test/TableGen/nested_ifdef_main.td
Modified:
llvm/lib/TableGen/TGLexer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index 484f840dc4fc329..1730f5d88ac5aa7 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -723,16 +723,15 @@ tgtok::TokKind TGLexer::lexPreprocessor(
bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
- // Canonicalize ifndef to ifdef equivalent
- if (Kind == tgtok::Ifndef) {
+ // Canonicalize ifndef's MacroIsDefined to its ifdef equivalent.
+ if (Kind == tgtok::Ifndef)
MacroIsDefined = !MacroIsDefined;
- Kind = tgtok::Ifdef;
- }
// Regardless of whether we are processing tokens or not,
// we put the #ifdef control on stack.
+ // Note that MacroIsDefined has been canonicalized against ifdef.
PrepIncludeStack.back()->push_back(
- {Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
+ {tgtok::Ifdef, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
if (!prepSkipDirectiveEnd())
return ReturnError(CurPtr, "Only comments are supported after " +
diff --git a/llvm/test/TableGen/nested_ifdef.inc b/llvm/test/TableGen/nested_ifdef.inc
new file mode 100644
index 000000000000000..652ff6931489fab
--- /dev/null
+++ b/llvm/test/TableGen/nested_ifdef.inc
@@ -0,0 +1,10 @@
+#ifndef NESTED_IFDEF
+#define NESTED_IFDEF
+
+def foo;
+
+#ifndef HAHA
+def haha;
+#endif
+
+#endif
diff --git a/llvm/test/TableGen/nested_ifdef2.inc b/llvm/test/TableGen/nested_ifdef2.inc
new file mode 100644
index 000000000000000..c3aaa1b06be1285
--- /dev/null
+++ b/llvm/test/TableGen/nested_ifdef2.inc
@@ -0,0 +1,8 @@
+#ifndef NESTED_IFDEF2
+#define NESTED_IFDEF2
+
+include "nested_ifdef.inc"
+
+def bar;
+
+#endif
diff --git a/llvm/test/TableGen/nested_ifdef_main.td b/llvm/test/TableGen/nested_ifdef_main.td
new file mode 100644
index 000000000000000..b4e30dee90da25f
--- /dev/null
+++ b/llvm/test/TableGen/nested_ifdef_main.td
@@ -0,0 +1,11 @@
+// RUN: llvm-tblgen -I %p %s | FileCheck %s
+
+include "nested_ifdef.inc"
+include "nested_ifdef2.inc"
+
+// CHECK: def bar
+// CHECK: def foo
+// CHECK: def haha
+// CHECK: def zoo
+
+def zoo;
More information about the llvm-commits
mailing list