Skip to content

Commit a09f09c

Browse files
committed
[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
1 parent f9fe603 commit a09f09c

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

llvm/lib/TableGen/TGLexer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,15 @@ tgtok::TokKind TGLexer::lexPreprocessor(
723723

724724
bool MacroIsDefined = DefinedMacros.count(MacroName) != 0;
725725

726-
// Canonicalize ifndef to ifdef equivalent
727-
if (Kind == tgtok::Ifndef) {
726+
// Canonicalize ifndef's MacroIsDefined to its ifdef equivalent.
727+
if (Kind == tgtok::Ifndef)
728728
MacroIsDefined = !MacroIsDefined;
729-
Kind = tgtok::Ifdef;
730-
}
731729

732730
// Regardless of whether we are processing tokens or not,
733731
// we put the #ifdef control on stack.
732+
// Note that MacroIsDefined has been canonicalized against ifdef.
734733
PrepIncludeStack.back()->push_back(
735-
{Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
734+
{tgtok::Ifdef, MacroIsDefined, SMLoc::getFromPointer(TokStart)});
736735

737736
if (!prepSkipDirectiveEnd())
738737
return ReturnError(CurPtr, "Only comments are supported after " +

llvm/test/TableGen/nested_ifdef.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef NESTED_IFDEF
2+
#define NESTED_IFDEF
3+
4+
def foo;
5+
6+
#ifndef HAHA
7+
def haha;
8+
#endif
9+
10+
#endif

llvm/test/TableGen/nested_ifdef2.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef NESTED_IFDEF2
2+
#define NESTED_IFDEF2
3+
4+
include "nested_ifdef.inc"
5+
6+
def bar;
7+
8+
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: llvm-tblgen -I %p %s | FileCheck %s
2+
3+
include "nested_ifdef.inc"
4+
include "nested_ifdef2.inc"
5+
6+
// CHECK: def bar
7+
// CHECK: def foo
8+
// CHECK: def haha
9+
// CHECK: def zoo
10+
11+
def zoo;

0 commit comments

Comments
 (0)