Skip to content

Conversation

@ljluestc
Copy link

@ljluestc ljluestc commented Apr 5, 2025

Title: Add Support for Converting #define Constants to Go Constants (#469)
Description:
This pull request addresses issue #469 by adding support for converting C #define constants (specifically T_EOF, T_SPACE, and T_TAB) into Go constants during the preprocessing stage of c4go. The change ensures these constants are preserved in the transpiled Go code, improving compatibility with C code that relies on such definitions.
Changes Made
Modified preprocessor/preprocessor.go:
Added a defines field (map[string]string) to the FilePP struct to store #define constants.

Updated NewFilePP to initialize the defines map and append Go-style const declarations to the preprocessed output (f.pp) when constants are present.

Modified analyzeFiles to take a *FilePP parameter, parse #define directives using a regex (#define\s+(\w+)\s+([^\s]+)), and store specific constants (T_EOF, T_SPACE, T_TAB) in f.defines.

Preserved the original include hierarchy logic to maintain existing functionality.

Added preprocessor/preprocessor_test.go:
Created unit tests to verify that #define constants are correctly converted to Go constants in the preprocessed output.

Tests cover both cases: files with #define constants and files without.

Why This Approach?
The constants are added at the top of the preprocessed output as a Go const block, making them available for later transpilation stages.

Limiting the initial implementation to T_EOF, T_SPACE, and T_TAB matches the issue’s example, but the framework is extensible for other constants.

Retaining the include hierarchy logic ensures no regression in existing preprocessor behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant