Skip to content

Commit c7c7f36

Browse files
committed
CMD: add cmd to show cil-based source code for project file
1 parent 728c20d commit c7c7f36

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

chc/cmdline/c_project/cprojectutil.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,55 @@ def is_included(path: Optional[str]) -> bool:
420420
exit(0)
421421

422422

423+
def cproject_cil_source(args: argparse.Namespace) -> NoReturn:
424+
"""Outputs a textual representation of the CIL AST."""
425+
426+
#arguments
427+
tgtpath: str = args.tgtpath
428+
projectname: str = args.projectname
429+
filename: str = args.filename
430+
loglevel: str = args.loglevel
431+
logfilename: Optional[str] = args.logfilename
432+
logfilemode: str = args.logfilemode
433+
434+
targetpath = os.path.abspath(tgtpath)
435+
projectpath = targetpath
436+
cfilename_c = os.path.basename(filename)
437+
cfilename = cfilename_c[:-2]
438+
cfilepath = os.path.dirname(filename)
439+
440+
set_logging(
441+
loglevel,
442+
targetpath,
443+
logfilename=logfilename,
444+
mode=logfilemode,
445+
msg="Command cfile mk-headerfile was invoked")
446+
447+
if not UF.has_analysisresults_path(targetpath, projectname):
448+
print_error(
449+
f"No analysis results found for {projectname} in {targetpath}")
450+
exit(1)
451+
452+
contractpath = os.path.join(targetpath, "chc_contracts")
453+
capp = CApplication(
454+
projectpath, projectname, targetpath, contractpath)
455+
456+
if capp.has_file(filename[:-2]):
457+
cfile = capp.get_file(filename[:-2])
458+
else:
459+
print_error(f"File {filename} not found")
460+
exit(1)
461+
462+
fcilsource = cfile.cil_source()
463+
464+
outputfilename = cfile.cfilename + ".cil.c"
465+
with open(outputfilename, "w") as fp:
466+
fp.write(fcilsource)
467+
print("Cil source code written to " + outputfilename)
468+
469+
exit(0)
470+
471+
423472
def cproject_analyze_project(args: argparse.Namespace) -> NoReturn:
424473

425474
# arguments

chc/cmdline/chkc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,29 @@ def parse() -> argparse.Namespace:
15271527
help="file mode for log file: append (a, default), or write (w)")
15281528
cprojectmkheader.set_defaults(func=P.cproject_mk_headerfile)
15291529

1530+
# --- cil-source
1531+
cprojectcilsource = cprojectparsers.add_parser("cil-source")
1532+
cprojectcilsource.add_argument(
1533+
"tgtpath", help="directory that contains the analysis results")
1534+
cprojectcilsource.add_argument(
1535+
"projectname", help="name of the project")
1536+
cprojectcilsource.add_argument(
1537+
"filename", help="filename with relative path wrt the project")
1538+
cprojectcilsource.add_argument(
1539+
"--loglevel", "-log",
1540+
choices=UL.LogLevel.options(),
1541+
default="NONE",
1542+
help="activate logging with given level (default to stderr)")
1543+
cprojectcilsource.add_argument(
1544+
"--logfilename",
1545+
help="name of file to write log messages")
1546+
cprojectcilsource.add_argument(
1547+
"--logfilemode",
1548+
choices=["a", "w"],
1549+
default="a",
1550+
help="file mode for log file: append (a, default), or write (w)")
1551+
cprojectcilsource.set_defaults(func=P.cproject_cil_source)
1552+
15301553
# --- analyze
15311554
cprojectanalyze = cprojectparsers.add_parser("analyze")
15321555
cprojectanalyze.add_argument(

0 commit comments

Comments
 (0)