From: Tim Northover Date: Tue, 14 May 2019 13:04:25 +0000 (+0000) Subject: TableGen: support #ifndef in addition to #ifdef. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f19a9097c6a6b5b1279bfbe433742cae9c8997c4;p=llvm TableGen: support #ifndef in addition to #ifdef. TableGen has a limited preprocessor, which only really supports easier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360670 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/TableGen/TGLexer.cpp b/lib/TableGen/TGLexer.cpp index b9420253b2b..d28c62b3133 100644 --- a/lib/TableGen/TGLexer.cpp +++ b/lib/TableGen/TGLexer.cpp @@ -36,6 +36,7 @@ struct { const char *Word; } PreprocessorDirs[] = { { tgtok::Ifdef, "ifdef" }, + { tgtok::Ifndef, "ifndef" }, { tgtok::Else, "else" }, { tgtok::Endif, "endif" }, { tgtok::Define, "define" } @@ -676,21 +677,28 @@ tgtok::TokKind TGLexer::lexPreprocessor( PrintFatalError("lexPreprocessor() called for unknown " "preprocessor directive"); - if (Kind == tgtok::Ifdef) { + if (Kind == tgtok::Ifdef || Kind == tgtok::Ifndef) { StringRef MacroName = prepLexMacroName(); + StringRef IfTokName = Kind == tgtok::Ifdef ? "#ifdef" : "#ifndef"; if (MacroName.empty()) - return ReturnError(TokStart, "Expected macro name after #ifdef"); + return ReturnError(TokStart, "Expected macro name after " + IfTokName); bool MacroIsDefined = DefinedMacros.count(MacroName) != 0; + // Canonicalize ifndef to 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. PrepIncludeStack.back()->push_back( {Kind, MacroIsDefined, SMLoc::getFromPointer(TokStart)}); if (!prepSkipDirectiveEnd()) - return ReturnError(CurPtr, - "Only comments are supported after #ifdef NAME"); + return ReturnError(CurPtr, "Only comments are supported after " + + IfTokName + " NAME"); // If we were not processing tokens before this #ifdef, // then just return back to the lines skipping code. @@ -714,7 +722,7 @@ tgtok::TokKind TGLexer::lexPreprocessor( // Check if this #else is correct before calling prepSkipDirectiveEnd(), // which will move CurPtr away from the beginning of #else. if (PrepIncludeStack.back()->empty()) - return ReturnError(TokStart, "#else without #ifdef"); + return ReturnError(TokStart, "#else without #ifdef or #ifndef"); PreprocessorControlDesc IfdefEntry = PrepIncludeStack.back()->back(); diff --git a/lib/TableGen/TGLexer.h b/lib/TableGen/TGLexer.h index 4b42d31de53..3085ab2c047 100644 --- a/lib/TableGen/TGLexer.h +++ b/lib/TableGen/TGLexer.h @@ -65,7 +65,7 @@ namespace tgtok { // Preprocessing tokens for internal usage by the lexer. // They are never returned as a result of Lex(). - Ifdef, Else, Endif, Define + Ifdef, Ifndef, Else, Endif, Define }; } diff --git a/test/TableGen/prep-diag5.td b/test/TableGen/prep-diag5.td index 7dc178bef22..1b9df59acd9 100644 --- a/test/TableGen/prep-diag5.td +++ b/test/TableGen/prep-diag5.td @@ -1,6 +1,6 @@ // RUN: not llvm-tblgen -I %p %s 2>&1 | FileCheck %s -// CHECK: error: #else without #ifdef +// CHECK: error: #else without #ifdef or #ifndef #else #else #endif diff --git a/test/TableGen/prep-ifndef-diag-1.td b/test/TableGen/prep-ifndef-diag-1.td new file mode 100644 index 00000000000..941f2d377a9 --- /dev/null +++ b/test/TableGen/prep-ifndef-diag-1.td @@ -0,0 +1,4 @@ +// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s + +// CHECK: error: Expected macro name after #ifndef +#ifndef 1 diff --git a/test/TableGen/prep-ifndef-diag-2.td b/test/TableGen/prep-ifndef-diag-2.td new file mode 100644 index 00000000000..7b5f9dfd24b --- /dev/null +++ b/test/TableGen/prep-ifndef-diag-2.td @@ -0,0 +1,4 @@ +// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s + +// CHECK: error: Only comments are supported after #ifndef NAME +#ifndef MACRO 42 diff --git a/test/TableGen/prep-ifndef.td b/test/TableGen/prep-ifndef.td new file mode 100644 index 00000000000..6e5254f2856 --- /dev/null +++ b/test/TableGen/prep-ifndef.td @@ -0,0 +1,10 @@ +// RUN: llvm-tblgen %s -DMACRO | FileCheck %s +// RUN: llvm-tblgen %s | FileCheck %s --check-prefix=CHECK-NOMACRO + +#ifndef MACRO +// CHECK-NOMACRO: def nomacro +def nomacro; +#else +// CHECK: def macro +def macro; +#endif