[llvm] Add include guards to LLVMOption TableGen
authorBrian Gesiak <brian@modocache.io>
Tue, 27 Jun 2023 20:25:39 +0000 (16:25 -0400)
committerBrian Gesiak <brian@modocache.io>
Wed, 28 Jun 2023 00:21:31 +0000 (20:21 -0400)
Add include guards that allow multiple includes of `OptParser.td`. This
is helpful when defining multiple sets of options in multiple files.
For example, a user could define a `HelpOptions.td` file that defines
only `-h` and `--help`:

```
// HelpOptions.td
include "llvm/Option/OptParser.td"

def HelpOptionGroup : OptionGroup<"Help Options">;
def help : Flag<["--", "-"], "help">, Group<HelpOptionGroup>,
           Flags<[]>;
def : Flag<["-"], "h">, Group<HelpOptionGroup>, Flags<[]>, Alias<help>;
```

This file could then be included into any TableGen file that wishes to
define these options:

```
// MyOptions.td
include "llvm/Option/OptParser.td"

def MyOptionGroup : OptionGroup<"My Options">;
// ...define my options.

// And also define `-h` and `--help`:
include "HelpOptions.td"
```

This currently isn't possible, because this would result in
`OptParser.td` being included twice. Alternatively, the include of
`OptParser.td` in the `HelpOptions.td` example above could be removed,
but then `llvm-tblgen --gen-opt-parser-defs HelpOptions.td` would fail
(because the `OptionGroup` and `Option` records are defined in
`OptParser.td`).

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D153915

llvm/include/llvm/Option/OptParser.td

index 9c73f47..94b945d 100644 (file)
@@ -11,6 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 
+#ifndef LLVM_OPTION_OPTPARSER_TD
+#define LLVM_OPTION_OPTPARSER_TD
+
 // Define the kinds of options.
 
 class OptionKind<string name, int precedence = 0, bit sentinel = false> {
@@ -246,3 +249,5 @@ class ValueExtractor<code extractor> { code ValueExtractor = extractor; }
 // aren't duplicated).
 def INPUT : Option<[], "<input>", KIND_INPUT>;
 def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>;
+
+#endif // LLVM_OPTION_OPTPARSER_TD