Add Support for Converting #define Constants to Go Constants (#469)#518
Draft
ljluestc wants to merge 3 commits intoKonstantin8105:masterfrom
Draft
Add Support for Converting #define Constants to Go Constants (#469)#518ljluestc wants to merge 3 commits intoKonstantin8105:masterfrom
ljluestc wants to merge 3 commits intoKonstantin8105:masterfrom
Conversation
added 3 commits
April 5, 2025 10:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.