mirror of
https://github.com/github/codeql.git
synced 2025-12-17 09:13:20 +01:00
This checks in the trapgen script generating trap entries in C++. The codegen suite has been slightly reorganized, moving the templates directory up one level and chopping everything into smaller bazel packages. Running tests is now done via ``` bazel run //swift/codegen/test ``` With respect to the PoC, the nested `codeql::trap` namespace has been dropped in favour of a `Trap` prefix (or suffix in case of entries) within the `codeql` namespace. Also, generated C++ code is not checked in in git any more, and generated during build. Finally, labels get printed in hex in the trap file. `TrapLabel` is for the moment only default-constructible, so only one single label is possible. `TrapArena`, that is responsible for creating disjoint labels will come in a later commit.
51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
""" template renderer module, wrapping around `pystache.Renderer`
|
|
|
|
`pystache` is a python mustache engine, and mustache is a template language. More information on
|
|
|
|
https://mustache.github.io/
|
|
"""
|
|
|
|
import logging
|
|
import pathlib
|
|
|
|
import pystache
|
|
|
|
from . import paths
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class Renderer:
|
|
""" Template renderer using mustache templates in the `templates` directory """
|
|
|
|
def __init__(self):
|
|
self._r = pystache.Renderer(search_dirs=str(paths.templates_dir), escape=lambda u: u)
|
|
self.written = set()
|
|
|
|
def render(self, data, output: pathlib.Path, guard_base: pathlib.Path = None):
|
|
""" Render `data` to `output`.
|
|
|
|
`data` must have a `template` attribute denoting which template to use from the template directory.
|
|
|
|
If the file is unchanged, then no write is performed (and `done_something` remains unchanged)
|
|
|
|
If `guard_base` is provided, it must be a path at the root of `output` and a header guard will be injected in
|
|
the template based off of the relative path of `output` in `guard_base`
|
|
"""
|
|
mnemonic = type(data).__name__
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
guard = None
|
|
if guard_base is not None:
|
|
guard = str(output.relative_to(guard_base)).replace("/", "_").replace(".", "_").upper()
|
|
data = self._r.render_name(data.template, data, generator=paths.exe_file, guard=guard)
|
|
with open(output, "w") as out:
|
|
out.write(data)
|
|
log.debug(f"generated {mnemonic} {output.name}")
|
|
self.written.add(output)
|
|
|
|
def cleanup(self, existing):
|
|
""" Remove files in `existing` for which no `render` has been called """
|
|
for f in existing - self.written:
|
|
f.unlink(missing_ok=True)
|
|
log.info(f"removed {f.name}")
|