Skip to content

Fix comment to be after extern (C):#312

Open
davispuh wants to merge 1 commit into
jacob-carlborg:masterfrom
davispuh:comment
Open

Fix comment to be after extern (C):#312
davispuh wants to merge 1 commit into
jacob-carlborg:masterfrom
davispuh:comment

Conversation

@davispuh

@davispuh davispuh commented Jul 4, 2026

Copy link
Copy Markdown

Currently code like this

#define G1

#ifdef  G1  // Guard1
#define A 1
#else
#define A 2
#endif

#ifdef  G2  // Guard2
#define B 3
#else
#define B 4
#endif

Produces:

extern (C): // Guard1
enum A = 1;

// Guard2

enum B = 4;

Notice how Guard1 comment is in same line as extern (C): which looks wrong and differs from Guard2 case.
This PR fixes this issue so that it's on a new line like Guard2 so result will be:

extern (C):

// Guard1

enum A = 1;

// Guard2

enum B = 4;

Currently code like this
```c
 #define G1

 #ifdef  G1  // Guard1
 #define A 1
 #else
 #define A 2
 #endif

 #ifdef  G2  // Guard2
 #define B 3
 #else
 #define B 4
 #endif
```

Produces:
```d
extern (C): // Guard1
enum A = 1;

// Guard2

enum B = 4;
```

Notice how `Guard1` comment is in same line as `extern (C):` which looks wrong.
This commit fixes this issue so that it's on a new line like `Guard2`.
Copilot AI review requested due to automatic review settings July 4, 2026 04:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes comment placement during translation so that comments from taken preprocessor branches (e.g., #ifdef G1 // Guard1) are emitted on their own line(s) instead of being appended inline to the generated extern (C): line.

Changes:

  • Add a new lastContentLine tracker to distinguish “latest visited source line” from “latest line that actually emitted output content”.
  • Update comment emission logic to use lastContentLine for determining when a comment should be inline vs. standalone, preventing extern (C): // Guard1.
  • Add a regression unit test covering the #ifdef-branch comment placement case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/unit/CommentUnitTests.d Adds a regression test ensuring taken-branch comments don’t join the extern (C): line.
dstep/translator/Output.d Tracks last emitted-content line and uses it to decide inline vs. standalone comment formatting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants