Skip to content

Commit

Permalink
add support for printing help for pragmas (#1905)
Browse files Browse the repository at this point in the history
add support for printing help for pragmas

Printing help is a backend specific option. It requires a bit more
flexible parsing of annotations (i.e., registering handlers outside
the constructor) and a bit of driver tweaking.
  • Loading branch information
cc10512 authored Apr 27, 2019
1 parent 6b230f4 commit d8a04d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions frontends/p4/parseAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class ParseAnnotations : public Modifier {
static bool parseExpressionList(IR::Annotation* annotation);
static bool parseKvList(IR::Annotation* annotation);

void addHandler(cstring name, Handler h) { handlers.insert({name, h}); }

private:
/// Whether to warn about unknown annotations.
const bool warnUnknown;
Expand Down
29 changes: 24 additions & 5 deletions tools/driver/p4c_src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def main():
parser.add_argument("-I", dest="search_path",
help="Add directory to include search path",
action="append", default=[])
parser.add_argument("-o", dest="output_directory",
parser.add_argument("-o", "--output", dest="output_directory",
help="Write output to the provided path",
action="store", metavar="PATH", default=".")
parser.add_argument("--p4runtime-file",
Expand All @@ -136,14 +136,19 @@ def main():
"description (default is binary). "
"[Deprecated; use '--p4runtime-files' instead]",
action="store", default="binary")
parser.add_argument("--target-help", dest="show_target_help",
parser.add_argument("--help-pragmas", "--pragma-help", "--pragmas-help",
"--help-annotations", "--annotation-help", "--annotations-help",
dest="help_pragmas", action="store_true", default=False,
help = "Print the documentation about supported annotations/pragmas and exit.")
parser.add_argument("--help-targets", "--target-help", "--targets-help",
dest="show_target_help",
help="Display target specific command line options.",
action="store_true", default=False)
parser.add_argument("-S", dest="run_till_assembler",
help="Only run the preprocess and compilation steps",
action="store_true", default=False)
parser.add_argument("--std", "-x", dest="language",
choices = ["p4-14", "p4-16"],
choices = ["p4-14", "p4_14", "p4-16", "p4_16"],
help="Treat subsequent input files as having type language.",
action="store", default="p4-16")

Expand All @@ -166,6 +171,10 @@ def main():
user_defined_version = os.environ.get('P4C_DEFAULT_VERSION')
if user_defined_version != None:
opts.language = user_defined_version
# accept multiple ways of specifying which language, and ensure that it is a consistent
# string from now on.
if opts.language == "p4_14": opts.language = "p4-14"
if opts.language == "p4_16": opts.language = "p4-16"

user_defined_target = os.environ.get('P4C_DEFAULT_TARGET')
if user_defined_target != None:
Expand All @@ -184,20 +193,30 @@ def main():
print display_supported_targets(cfg)
sys.exit(0)

# When using --help-* options, we don't necessarily need to pass an input file
# However, by default the driver checks the input and fails if it does not exist.
# Also, loading the backend configuration files requires a source file to be set,
# so in that case, we set the input to dummy.p4 and expect that the backend itself
# will do its printing and ignore the input file.
# If not, the compiler will error out anyway.
checkInput = not opts.help_pragmas

input_specified = False
if opts.source_file:
input_specified = True
if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
if opts.json_source:
input_specified = True
if not input_specified:
if checkInput and not input_specified:
parser.error('No input specified.')
elif not checkInput:
opts.source_file = "dummy.p4"

if (os.environ['P4C_BUILD_TYPE'] == "DEVELOPER"):
if opts.json_source:
opts.source_file = opts.json_source

if not os.path.isfile(opts.source_file):
if checkInput and not os.path.isfile(opts.source_file):
print >> sys.stderr, 'Input file ' + opts.source_file + ' does not exist'
sys.exit(1)

Expand Down

0 comments on commit d8a04d0

Please sign in to comment.