Skip to content

Commit 9cfc6a0

Browse files
committed
CLI: add command to print header file
1 parent 377c2da commit 9cfc6a0

File tree

10 files changed

+821
-8
lines changed

10 files changed

+821
-8
lines changed

chc/app/CAttributes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2017-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2022 Henny B. Sipma
9-
# Copyright (c) 2023-2024 Aarno Labs LLC
9+
# Copyright (c) 2023-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -40,6 +40,7 @@
4040
from chc.app.CDictionary import CDictionary
4141
from chc.app.CTyp import CTyp
4242
from chc.app.CTypsig import CTypsig
43+
from chc.app.CVisitor import CVisitor
4344

4445

4546
class CAttr(CDictionaryRecord):
@@ -557,5 +558,8 @@ def attributes(self) -> List[CAttribute]:
557558
def length(self) -> int:
558559
return len(self.attributes)
559560

561+
def accept(self, visitor: "CVisitor") -> None:
562+
visitor.visit_attributes(self)
563+
560564
def __str__(self) -> str:
561565
return ",".join([str(p) for p in self.attributes])

chc/app/CExp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2017-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2022 Henny B. Sipma
9-
# Copyright (c) 2023-2024 Aarno Labs LLC
9+
# Copyright (c) 2023-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -39,6 +39,7 @@
3939
from chc.app.CConst import CConst
4040
from chc.app.CTyp import CTyp
4141
from chc.app.CLval import CLval
42+
from chc.app.CVisitor import CVisitor
4243

4344

4445
binoperatorstrings = {
@@ -147,6 +148,9 @@ def get_strings(self) -> List[str]:
147148
def get_variable_uses(self, vid: int) -> int:
148149
return 0
149150

151+
def accept(self, visitor: "CVisitor") -> None:
152+
raise Exception("CExp.accept: " + str(self))
153+
150154
def to_dict(self) -> Dict[str, Any]:
151155
return {"base": "exp"}
152156

@@ -179,6 +183,9 @@ def constant(self) -> "CConst":
179183
def get_strings(self) -> List[str]:
180184
return self.constant.get_strings()
181185

186+
def accept(self, visitor: "CVisitor") -> None:
187+
visitor.visit_constexp(self)
188+
182189
def to_dict(self) -> Dict[str, Any]:
183190
return {"base": "const", "value": str(self.constant)}
184191

chc/app/CFile.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2017-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2022 Henny B. Sipma
9-
# Copyright (c) 2023-2024 Aarno Labs LLC
9+
# Copyright (c) 2023-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -51,6 +51,7 @@
5151
from chc.app.CFileGlobals import CGVarDecl
5252
from chc.app.CFileGlobals import CGVarDef
5353
from chc.app.CGXrefs import CGXrefs
54+
from chc.app.CPrettyPrinter import CPrettyPrinter
5455

5556
from chc.proof.CFilePredicateDictionary import CFilePredicateDictionary
5657
from chc.proof.CFunctionPO import CFunctionPO
@@ -183,6 +184,36 @@ def cfileglobals(self) -> CFileGlobals:
183184
def gfunctions(self) -> Dict[int, "CGFunction"]:
184185
return self.cfileglobals.gfunctions
185186

187+
def header_declarations(
188+
self,
189+
gvarnames: Dict[str, str] = {},
190+
fnnames: Dict[str, str] = {}) -> str:
191+
lines: List[str] = []
192+
srcfilename = self.name + ".c"
193+
lines.append("// " + srcfilename + "\n")
194+
gvars = self.gvardefs.values()
195+
if len(gvars) > 0:
196+
lines.append("// static and global variable definitions\n")
197+
for gvar in gvars:
198+
pp = CPrettyPrinter()
199+
binloc: Optional[str] = gvarnames.get(gvar.varinfo.vname)
200+
gvardef = pp.gvar_definition_str(
201+
gvar.varinfo, srcloc=srcfilename, binloc=binloc)
202+
lines.append(gvardef)
203+
204+
fns = self.gfunctions.values()
205+
if len(gvars) > 0 and len(fns) > 0:
206+
lines.append("// function signatures\n")
207+
for fn in fns:
208+
if fn.is_system_function:
209+
continue
210+
pp = CPrettyPrinter()
211+
binloc = fnnames.get(fn.varinfo.vname)
212+
fndecl = pp.function_declaration_str(
213+
fn.varinfo, srcloc=srcfilename, binloc=binloc)
214+
lines.append(fndecl)
215+
return "\n".join(lines)
216+
186217
@property
187218
def functioncount(self) -> int:
188219
return self.cfileglobals.functioncount

chc/app/CFileGlobals.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# Copyright (c) 2017-2020 Kestrel Technology LLC
88
# Copyright (c) 2020-2023 Henny B. Sipma
9-
# Copyright (c) 2024 Aarno Labs LLC
9+
# Copyright (c) 2024-2025 Aarno Labs LLC
1010
#
1111
# Permission is hereby granted, free of charge, to any person obtaining a copy
1212
# of this software and associated documentation files (the "Software"), to deal
@@ -105,6 +105,11 @@ def vtype(self) -> "CTyp":
105105
def line(self) -> int:
106106
return self.varinfo.line
107107

108+
@property
109+
def is_system_function(self) -> bool:
110+
vdecl = self.varinfo.vdecl
111+
return vdecl is None or vdecl.file.startswith("/")
112+
108113
def __str__(self) -> str:
109114
return f"{self.vname}: {self.vtype}"
110115

0 commit comments

Comments
 (0)