Skip to content

Commit

Permalink
ImportC add Windows cl preprocessor #define support
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and RazvanN7 committed May 24, 2022
1 parent e311b9a commit 4e0bf1b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
9 changes: 8 additions & 1 deletion src/dmd/cparse.d
Expand Up @@ -5156,6 +5156,11 @@ final class CParser(AST) : Parser!AST
{
auto id = n.ident;
scan(&n);
if (n.value == TOK.endOfLine) // #define identifier
{
nextDefineLine();
continue;
}
if (n.value == TOK.int32Literal)
{
const value = n.intvalue;
Expand All @@ -5168,12 +5173,14 @@ final class CParser(AST) : Parser!AST
AST.Expression e = new AST.IntegerExp(scanloc, value, AST.Type.tint32);
auto v = new AST.VarDeclaration(scanloc, AST.Type.tint32, id, new AST.ExpInitializer(scanloc, e), STC.manifest);
symbols.push(v);
nextDefineLine();
continue;
}
}
}
skipToNextLine();
}
else
else if (n.value != TOK.endOfLine)
{
skipToNextLine();
}
Expand Down
51 changes: 38 additions & 13 deletions src/dmd/link.d
Expand Up @@ -1058,7 +1058,7 @@ public int runPreprocessor(const(char)[] cpp, const(char)[] filename, const(char
*/
OutBuffer buf;
buf.writestring(cpp);
buf.printf(" /P /nologo %.*s /FI%s /Fi%.*s",
buf.printf(" /P /Zc:preprocessor /PD /nologo %.*s /FI%s /Fi%.*s",
cast(int)filename.length, filename.ptr, importc_h, cast(int)output.length, output.ptr);

/* Append preprocessor switches to command line
Expand All @@ -1077,24 +1077,46 @@ public int runPreprocessor(const(char)[] cpp, const(char)[] filename, const(char

ubyte[2048] buffer = void;

bool firstLine = true;
OutBuffer linebuf; // each line from stdout
bool print = false; // print line collected from stdout

/* Collect text captured from stdout to linebuf[].
* Then decide to print or discard the contents.
* Discarding lines that consist only of a filename is necessary to pass
* the D test suite which diffs the output. CL's emission of filenames cannot
* be turned off.
*/
void sink(ubyte[] data)
{
if (firstLine)
foreach (c; data)
{
for (size_t i = 0; 1; ++i)
switch (c)
{
if (i == data.length)
return;
if (data[i] == '\n') // reached end of first line
{
data = data[i + 1 .. data.length];
firstLine = false;
case '\r':
break;

case '\n':
if (print)
printf("%s\n", linebuf.peekChars());

// set up for next line
linebuf.setsize(0);
print = false;
break;

case '\t':
case ';':
case '(':
case '\'':
case '"': // various non-filename characters
print = true; // mean it's not a filename
goto default;

default:
linebuf.writeByte(c);
break;
}
}
}
printf("%.*s", cast(int)data.length, data.ptr);
}

// Convert command to wchar
Expand All @@ -1103,7 +1125,10 @@ public int runPreprocessor(const(char)[] cpp, const(char)[] filename, const(char
auto szCommand = toWStringz(buf.peekChars()[0 .. buf.length], smbuf);

int exitCode = runProcessCollectStdout(szCommand.ptr, buffer[], &sink);
printf("\n");

if (linebuf.length && print) // anything leftover from stdout collection
printf("%s\n", defines.peekChars());

return exitCode;
}
else
Expand Down
@@ -1,3 +1,3 @@
// DISABLED: win64 win32mscoff
/* */
#define GHI 3
_Static_assert(GHI == 3, "1");
4 changes: 4 additions & 0 deletions test/compilable/testdefines.d
@@ -0,0 +1,4 @@
// EXTRA_FILES: imports/defines.c
import imports.defines;

static assert(GHI == 3);

0 comments on commit 4e0bf1b

Please sign in to comment.