Repository navigation
Restore the -k option (keep horizontal whitespace) - #49
Conversation
With -k, spaces and tabs are written out as they are in the input, and each comment becomes spaces of the same length, so a token keeps the column it has in the source file.
There was a problem hiding this comment.
🟡 Changes recommended
The -k comment-padding path can write past its allocation, while multiline comments and non-invoked function-like macros still shift token columns.
3 open issues
What changed in this PR
Adds the upstream mcpp -k option to preserve horizontal whitespace, supporting accurate source-column spans in the Slice compiler.
Changes:
- Registers and documents
-k. - Adds whitespace-preservation state and updates it during directive processing.
- Preserves spaces and tabs and pads block comments based on their length.
| File | Description |
|---|---|
system.c |
Parses -k and adds usage text. |
support.c |
Preserves horizontal whitespace and calculates comment padding. |
mcpp_main.c |
Initializes and enables whitespace-preservation mode. |
internal.H |
Declares the new option and shared state. |
directive.c |
Updates preservation state after directives. |
Review effort: Balanced
With -k, a comment that follows a truncated comment and a token on the same line had its padding length computed as a negative number, which became a huge count.
pepone
left a comment
There was a problem hiding this comment.
🤖 Claude (Opus 5.5):
-
With
-k -M, a function-like macro call that spans lines and has tokens after it setssrc_col, but only the output path inmcpp_mainclears it, so every later line skipsdirective(). With the input below,mcpp -k -Mprintsdep2.o: dep2.icewithoutdep.h, while-Malone lists it. Please clearsrc_colnext towrong_line = FALSEin theno_outputbranch (mcpp_main.c:511). I checked that this fixes-k -Mand leaves the-kand-C -koutput unchanged.#define F(x) x F( 1) z #include "dep.h"
With -k and -M, a function-like macro call that spans lines and has tokens after it sets 'src_col', and every later directive is then skipped, so the dependency line misses the files included after it.
|
Fixed in 0133d03: 🤖 Addressed by Claude Code |
This reverts commit 0133d03.
|
Reverted in ef1e5eb; my earlier comment said this was fixed. |
There was a problem hiding this comment.
I approve of this PR because it brings our fork closer to what the other MCPPs do.
And I would rather be faithful to the original, than go around fixing minor bugs.
But, -k is not sufficient to the goal of preserving column counts, at least not with the logic of this fork. I didn't check everything, but at the very least, MCPP expands/moves macros and comments in ways that -k can't handle.
So even with this PR, we won't always have accurate column counts in our Slice files.
|
🤖 Claude (Opus 5.5): Agreed: Ice links the Homebrew mcpp on macOS and the distribution's |
|
Agreed, |
bernardnormier
left a comment
There was a problem hiding this comment.
The restore is correct and faithful to 2.7.2, with one exception: the change to the comment padding in parse_line. Please keep this PR a pure restore and move that fix to a follow-up PR (see the inline comment).
Title and description. The description doesn't give a reader the background needed to judge the change, and the title says "Add" for code that is restored. Suggested title: Restore the -k option (keep horizontal whitespace). Suggested opening for the description, ahead of the existing bullet list:
Background
This repository is a fork of mcpp 2.7.2. The upstream project is no longer maintained. In April 2015 (42be792) we removed from this fork the code the Slice compilers didn't use, and the
-koption was part of what was removed.The Slice compilers use this fork only on the platforms where we build mcpp ourselves, such as Windows and RHEL. On the other platforms, such as macOS, they use the mcpp 2.7.2 that the platform provides, which is complete and accepts
-k.zeroc-ice/ice#7007 needs
-kon every platform, so this PR restores it here. None of this code is new: it is the 2.7.2 code from the import in e6e749e.
The bullet about a comment that starts when the line buffer is already full then moves to the follow-up PR.
| /* | ||
| * Truncate a comment too long for the rest of the buffer. | ||
| * The buffer may already be full: an earlier comment of | ||
| * this line was truncated to end at 'limit - 1', and a | ||
| * token after it moved 'tp' to 'limit'. 'limit - tp - 1' | ||
| * is then negative, and would become a huge 'com_size', | ||
| * so the comment gets no padding instead. | ||
| */ | ||
| if (tp + com_size >= limit - 1) /* Too long comment */ | ||
| com_size = (tp < limit - 1) ? limit - tp - 1 : 0; |
There was a problem hiding this comment.
This is a fix to the 2.7.2 code, and the mcpp that Homebrew and the Linux distributions package doesn't have it. Please restore the 2.7.2 lines here and propose the fix in a follow-up PR:
| /* | |
| * Truncate a comment too long for the rest of the buffer. | |
| * The buffer may already be full: an earlier comment of | |
| * this line was truncated to end at 'limit - 1', and a | |
| * token after it moved 'tp' to 'limit'. 'limit - tp - 1' | |
| * is then negative, and would become a huge 'com_size', | |
| * so the comment gets no padding instead. | |
| */ | |
| if (tp + com_size >= limit - 1) /* Too long comment */ | |
| com_size = (tp < limit - 1) ? limit - tp - 1 : 0; | |
| if (tp + com_size >= limit - 1) /* Too long comment */ | |
| com_size = limit - tp - 1; /* Truncate */ |
There was a problem hiding this comment.
Restored the 2.7.2 lines in 5f6aa94, and took the suggested title. The description now opens with the background on the fork and on which platforms use it.
This reverts commit c5996f4.


Restore the
-koption of mcpp 2.7.2, so that a token in the preprocessed output keeps the column it has in the source file. The source spans in zeroc-ice/ice#7007 need this to be exact.This repository is a fork of mcpp 2.7.2, which is no longer maintained. In April 2015 (42be792), the fork dropped the code the Slice compilers didn't use, and
-kwas part of it.The Slice compilers use this fork only where we build mcpp ourselves, such as Windows and RHEL. Elsewhere, such as macOS and Debian, they use the platform's mcpp 2.7.2, which accepts
-k. None of the restored code is new: it is the 2.7.2 code from the import in e6e749e, apart from a check for a preprocessing mode this fork no longer has.-kagain, and list it in the usage text.-kis given, and replace each comment with spaces of the same length (parse_lineandread_a_commentinsupport.c).-kisn't given.