Bug 23870 – ImportC doesn't accept '\' followed by newline, whereas VC does

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2023-04-30T09:10:35Z
Last change time
2023-05-01T22:10:39Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
Max Samukha

Comments

Comment #0 by maxsamukha — 2023-04-30T09:10:35Z
void HRESULT_FROM_SETUPAPI(void) { \ } windows.c(3): Error: character '\' is not a valid token
Comment #1 by bugzilla — 2023-05-01T02:01:13Z
This is invalid C code. The problem is the \ is followed by a space. The backslash line splicing done by the preprocessor happens only when the \ is followed by a newline. ********************************************* This file is rejected by the gnu preprocessor with a warning: --- warning: backslash and newline separated by space [enabled by default] void test() { \ ^ --- and the \ is removed before the C compiler sees it. ********************************************** The Microsoft preprocessor passes it unchanged to the output. VC then rejects it with "error C2017: illegal escape sequence". ********************************************** The ImportC error diagnostic appears to be correct. That leaves the question, what is the context of this on your system? Is VC accepting it?
Comment #2 by maxsamukha — 2023-05-01T03:09:28Z
(In reply to Walter Bright from comment #1) > This is invalid C code. The problem is the \ is followed by a space. The > backslash line splicing done by the preprocessor happens only when the \ is > followed by a newline. In the original test code, \ is immediately followed by a newline. You or your text editor must have added the space. > This file is rejected by the gnu preprocessor with a warning: I didn't test it with anything other than VC. VC happens to accept backslash line continuations outside macros. See https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-170: "You can also use the backslash (\) as a continuation character. When a newline character (equivalent to pressing the RETURN key) immediately follows the backslash, the compiler ignores the backslash and the newline character and treats the next line as part of the previous line. This is useful primarily for preprocessor definitions longer than a single line.". > That leaves the question, what is the context of this on your system? Is VC accepting it? Yes, VC is mentioned in the bug report's title. The test code was extracted from "shared\winerror.h", line 39382, of Windows SDK. How do you test ImportC on Windows? I suggest trying it on "um/Windows.h". If ImportC can handle that, most of headers on Windows will be covered, I believe.
Comment #3 by bugzilla — 2023-05-01T08:16:46Z
(In reply to Max Samukha from comment #2) > In the original test code, \ is immediately followed by a newline. You or > your text editor must have added the space. I see you are right. I don't know how that space got in there! Running VC preprocessor without the trailing space still emits the \ into its output. (It should not do that.) This explains the behavior you are seeing. I'll see what I can do about it. Thanks for the help!
Comment #4 by maxsamukha — 2023-05-01T11:52:37Z
(In reply to Walter Bright from comment #3) > (It should not do that.) Note that VC compiler still accepts the code with the \. > I'll see what I can do about it. Nice, thanks!
Comment #5 by bugzilla — 2023-05-01T19:50:03Z
(In reply to Max Samukha from comment #4) > Note that VC compiler still accepts the code with the \. I know. It's still a bug in that compiler. I've noticed many times over the years that when there's a bug in VC, somebody writes code that relies on it. Sigh :-)
Comment #6 by dlang-bot — 2023-05-01T20:21:07Z
@WalterBright created dlang/dmd pull request #15157 "fix Issue 23870 - ImportC doesn't accept '\' followed by newline, whe…" fixing this issue: - fix Issue 23870 - ImportC doesn't accept '\' followed by newline, whereas VC does https://github.com/dlang/dmd/pull/15157
Comment #7 by dlang-bot — 2023-05-01T22:10:39Z
dlang/dmd pull request #15157 "fix Issue 23870 - ImportC doesn't accept '\' followed by newline, whe…" was merged into master: - d4e10579275a360daf23b5853b0f8472df571b14 by Walter Bright: fix Issue 23870 - ImportC doesn't accept '\' followed by newline, whereas VC does https://github.com/dlang/dmd/pull/15157