Skip to main content

A minimalistic C preprocessor preserving the original C code formatting

Project description

Introduction

Neatcpp is a minimalistic C code preprocessor written in Python. The preprocessor is not intended to create source files for the compiler. It is aimed at the creation of C source files with correctly expanded macros for the purposes of static analysis or code coverage analysis.

The neatcpp produces a code with formatting very close to the original code, i.e., the line endings, intendantion and comments are preserved, even in the expanded macro bodies. It is also possible to keep the original preprocessor directives.

Usage

Neatcpp as a Python module

Neatcpp preprocessor object should be created as an instance of the NeatCpp class and controlled through its public object attributes and methods.

The code below shows the basic usage of neatcpp as a Python module. See the samples\module_usage directory for the executable example listed below together with another slightly more advanced sample script.

# Import preprocessor class NeatCpp
from neatcpp import NeatCpp


SAMPLE_CODE = """
#define CUBE(X)     (X) * (X) * (X)
#define A           5
#define B           3

a = CUBE(A);
"""

# Create the preprocessor object.
neatcpp = NeatCpp()

# Process C code defined in a SAMPLE_CODE string.
neatcpp.process_code(SAMPLE_CODE)

# Print full global output. The full output means that everything from the original code
# (including preprocessor directives) is included in the output.
print(neatcpp.output_full)
# Prints:
# #define CUBE(X)     (X) * (X) * (X)
# #define A           5
# #define B           3
#
# a = (5) * (5) * (5);

# Print standard preprocessed output without the processed directives and comments
# not related to the remaining code.
print(neatcpp.output)
# Prints:
# a = (5) * (5) * (5);

print(neatcpp.expand_macros("b = CUBE(B + 1);"))
# Prints:
# b = (3 + 1) * (3 + 1) * (3 + 1);

print(neatcpp.evaluate("CUBE(2 + 2)"))
# Prints:
# 64

print(neatcpp.is_true("CUBE(A - B) < CUBE(A + B)"))
# Prints:
# True

# Reset internal preprocessor output.
neatcpp.reset_output()

# Add include directory to search for included files.
neatcpp.add_include_dirs("path/to/incl/dir")

# Process C code defined in file.
neatcpp.process_files("path/to/src.c")

# Save preprocessor output to the output file.
neatcpp.save_output_to_file("path/to/src_processed.c")

Neatcpp as a standalone script

C source files can be processed from a commmand line with the arguments in a following format:

python neatcpp.py in1.c [in2.c ...] out.c [-s sin1.c [sin2.c ...]] [-i incl1 [incl2 ...]] [-x excl1 [excl2 ...]] [-f] [-v 0-2] [-V] [-h]

Positional arguments:

  • in1.c [in2.c ...] - Input source files to be processed.
  • out.c - Output source file with processed code from all specified input files.

Optional arguments:

  • -s sin1.c [sin2.c ...] - Input source files processed silently , i.e., without generated output, before processing the main input files.
  • -i incl1 [incl2 ...] - Included directories to search for the input files or for the included files defined by the #include statements in the input sources.
  • -x - Excluded macros or files. #define and #include statements for these identifiers will not be processed.
  • -f - Option to enable full output, i.e., to include directives, all comments and whitespaces in the preprocessor output
  • -v 0-2 - Set console log verbosity level (0 = logging OFF with errors still shown).
  • -V - Show program name and version.
  • -h - Show help message and exit.

See the example in the samples\script_usage directory illustrating the command line usage.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neatcpp-1.2.0.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neatcpp-1.2.0-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file neatcpp-1.2.0.tar.gz.

File metadata

  • Download URL: neatcpp-1.2.0.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for neatcpp-1.2.0.tar.gz
Algorithm Hash digest
SHA256 9a6c6533aef3ae65968297cefb954edecf256680f0d23c168104f0432b648d18
MD5 db8feaa5f7268e80be03b30f1ca26dc2
BLAKE2b-256 739f0a85077e260fe1bfaee7cbf15cd223ca338d369ad74dde2509385f3a73ac

See more details on using hashes here.

File details

Details for the file neatcpp-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: neatcpp-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for neatcpp-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1c0113212c3b5b7822f894dee68830e7aaae7bc1993c6e6fe8bc5398d4e55ce
MD5 89873c95e08351c21b6502fd26108d8b
BLAKE2b-256 7eab0388af3579803c1be44f4661d7c662769c99789f15d97b30f28f70e3be69

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page