# loki.frontend.preprocessing module

Preprocessing utilities for frontends.

preprocess_cpp(source, filepath=None, includes=None, defines=None)

  Invoke an external C-preprocessor to sanitize input files.

  Note that the global option `LOKI_CPP_DUMP_FILES` will cause the intermediate preprocessed source to be written to a temporary file in `LOKI_TMP_DIR`.

  Parameters:

    - source (str) – Source string to preprocess via `pcpp`

    - filepath (str or pathlib.Path) – Optional filepath name, which will be used to derive the filename should intermediate file dumping be enabled via the global config.

    - includes ((list of) str) – Include paths for the C-preprocessor.

    - defines ((list of) str) – Symbol definitions to add to the C-preprocessor.

sanitize_input(source, frontend)

  Apply internal regex-based sanitisation rules to filter out known frontend incompatibilities.

  Note that this will create a record of all things stripped (`pp_info`), which will be used to re-insert the dropped source info when converting the parsed AST to our IR.

  The `sanitize_registry` (see below) holds pre-defined rules for each frontend.

sanitize_registry = {Frontend.OMNI: {}, Frontend.FP: {'CONVERT_ENDIAN': <loki.frontend.preprocessing.PPRule object>, 'FYPP ANNOTATIONS': <loki.frontend.preprocessing.PPRule object>, 'IBM_DIRECTIVES': <loki.frontend.preprocessing.PPRule object>, 'INTEGER_PP_DIRECTIVES': <loki.frontend.preprocessing.PPRule object>, 'OPEN_NEWUNIT': <loki.frontend.preprocessing.PPRule object>, 'STRING_PP_DIRECTIVES': <loki.frontend.preprocessing.PPRule object>}, Frontend.REGEX: {'FYPP ANNOTATIONS': <loki.frontend.preprocessing.PPRule object>}}

  The frontend sanitization registry dict holds workaround rules for Fortran features that cause bugs and failures in frontends. It’s mostly a regex expression that removes certains strings and stores them, so that they can be re-inserted into the IR by a callback.

class PPRule(match, replace, postprocess=None)

  Bases: `object`

  A preprocessing rule that defines and applies a source replacement and collects associated meta-data.

  reset()
  filter(line, lineno)

    Filter a source line by matching the given rule and storing meta-content.

  property info

    Meta-information that will be dumped alongside preprocessed source files to re-insert information into a fully parsed IR tree.

  postprocess(ir, info)