Skip to content

Commit

Permalink
[TableGen] Preprocessing support
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D54926

llvm-svn: 347686
  • Loading branch information
Vyacheslav Zakharin committed Nov 27, 2018
1 parent 4a3d758 commit f7d079e
Show file tree
Hide file tree
Showing 27 changed files with 1,136 additions and 31 deletions.
49 changes: 48 additions & 1 deletion llvm/docs/TableGen/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Lexical Analysis
================

TableGen supports BCPL (``// ...``) and nestable C-style (``/* ... */``)
comments.
comments. TableGen also provides simple `Preprocessing Support`_.

The following is a listing of the basic punctuation tokens::

Expand Down Expand Up @@ -448,3 +448,50 @@ applied at the end of parsing the base classes of a record.
BaseMultiClassList: `MultiClassID` ("," `MultiClassID`)*
MultiClassID: `TokIdentifier`
MultiClassObject: `Def` | `Defm` | `Let` | `Foreach`

Preprocessing Support
=====================

TableGen's embedded preprocessor is only intended for conditional compilation.
It supports the following directives:

.. productionlist::
LineBegin: ^
LineEnd: "\n" | "\r" | EOF
WhiteSpace: " " | "\t"
CStyleComment: "/*" (.* - "*/") "*/"
BCPLComment: "//" (.* - `LineEnd`) `LineEnd`
WhiteSpaceOrCStyleComment: `WhiteSpace` | `CStyleComment`
WhiteSpaceOrAnyComment: `WhiteSpace` | `CStyleComment` | `BCPLComment`
MacroName: `ualpha` (`ualpha` | "0"..."9")*
PrepDefine: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
: "#define" (`WhiteSpace`)+ `MacroName`
: (`WhiteSpaceOrAnyComment`)* `LineEnd`
PrepIfdef: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
: "#ifdef" (`WhiteSpace`)+ `MacroName`
: (`WhiteSpaceOrAnyComment`)* `LineEnd`
PrepElse: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
: "#else" (`WhiteSpaceOrAnyComment`)* `LineEnd`
PrepEndif: `LineBegin` (`WhiteSpaceOrCStyleComment`)*
: "#endif" (`WhiteSpaceOrAnyComment`)* `LineEnd`
PrepRegContentException: `PredIfdef` | `PredElse` | `PredEndif` | EOF
PrepRegion: .* - `PrepRegContentException`
:| `PrepIfDef`
: (`PrepRegion`)*
: [`PrepElse`]
: (`PrepRegion`)*
: `PrepEndif`

:token:`PrepRegion` may occur anywhere in a TD file, as long as it matches
the grammar specification.

:token:`PrepDefine` allows defining a :token:`MacroName` so that any following
:token:`PrepIfdef` - :token:`PrepElse` preprocessing region part and
:token:`PrepIfdef` - :token:`PrepEndif` preprocessing region
are enabled for TableGen tokens parsing.

A preprocessing region, starting (i.e. having its :token:`PrepIfdef`) in a file,
must end (i.e. have its :token:`PrepEndif`) in the same file.

A :token:`MacroName` may be defined externally by using ``{ -D<NAME> }``
option of TableGen.
6 changes: 5 additions & 1 deletion llvm/lib/TableGen/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ static cl::list<std::string>
IncludeDirs("I", cl::desc("Directory of include files"),
cl::value_desc("directory"), cl::Prefix);

static cl::list<std::string>
MacroNames("D", cl::desc("Name of the macro to be defined"),
cl::value_desc("macro name"), cl::Prefix);

static int reportError(const char *ProgName, Twine Msg) {
errs() << ProgName << ": " << Msg;
errs().flush();
Expand Down Expand Up @@ -91,7 +95,7 @@ int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);

TGParser Parser(SrcMgr, Records);
TGParser Parser(SrcMgr, MacroNames, Records);

if (Parser.ParseFile())
return 1;
Expand Down
Loading

0 comments on commit f7d079e

Please sign in to comment.