[PATCH] D159236: [TableGen] Fix incorrect handling of nested `#ifndef` directives

Min-Yih Hsu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 30 16:17:20 PDT 2023


myhsu created this revision.
myhsu added reviewers: Paul-C-Anagnostopoulos, rriddle, silvas.
Herald added a subscriber: hiraditya.
Herald added a project: All.
myhsu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 <https://github.com/llvm/llvm-project/issues/65100> .


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159236

Files:
  llvm/lib/TableGen/TGLexer.cpp
  llvm/test/TableGen/nested_ifdef.inc
  llvm/test/TableGen/nested_ifdef2.inc
  llvm/test/TableGen/nested_ifdef_main.td


Index: llvm/test/TableGen/nested_ifdef_main.td
===================================================================
--- /dev/null
+++ 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;
Index: llvm/test/TableGen/nested_ifdef2.inc
===================================================================
--- /dev/null
+++ llvm/test/TableGen/nested_ifdef2.inc
@@ -0,0 +1,8 @@
+#ifndef NESTED_IFDEF2
+#define NESTED_IFDEF2
+
+include "nested_ifdef.inc"
+
+def bar;
+
+#endif
Index: llvm/test/TableGen/nested_ifdef.inc
===================================================================
--- /dev/null
+++ llvm/test/TableGen/nested_ifdef.inc
@@ -0,0 +1,10 @@
+#ifndef NESTED_IFDEF
+#define NESTED_IFDEF
+
+def foo;
+
+#ifndef HAHA
+def haha;
+#endif
+
+#endif
Index: llvm/lib/TableGen/TGLexer.cpp
===================================================================
--- llvm/lib/TableGen/TGLexer.cpp
+++ llvm/lib/TableGen/TGLexer.cpp
@@ -717,16 +717,15 @@
 
     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 " +


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159236.554853.patch
Type: text/x-patch
Size: 1944 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230830/551b4a67/attachment.bin>


More information about the llvm-commits mailing list