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 committed May 23, 2022
1 parent 5807f1e commit a1bd9f8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
24 changes: 24 additions & 0 deletions src/dmd/cparse.d
Expand Up @@ -5149,6 +5149,13 @@ final class CParser(AST) : Parser!AST
{
if (p[0 .. 7] == "#define")
{
version (Windows)
{
if (global.params.verbose)
{
message("saw #define");
}
}
p += 7;
scan(&n);
//printf("%s\n", n.toChars());
Expand All @@ -5168,8 +5175,25 @@ 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);
version (Windows)
{
if (global.params.verbose)
{
message("declare manifest constant %s", v.toChars());
}
}
}
}
else
{
version (Windows)
{
if (global.params.verbose)
{
message("token %s", n.toChars());
}
}
}
}
skipToNextLine();
}
Expand Down
9 changes: 9 additions & 0 deletions src/dmd/dmodule.d
Expand Up @@ -683,8 +683,17 @@ extern (C++) final class Module : Package
filename = global.preprocess(srcfile, loc, global.params.cppswitches, ifile, &defines); // run C preprocessor
}

version (Windows)
{
if (global.params.verbose) { message("lookup: %s", filename.toChars()); }
}
if (auto result = global.fileManager.lookup(filename))
{
version (Windows)
{
if (0 && global.params.verbose && strcmp(filename.toChars(), "defines.i".ptr) == 0)
{ OutBuffer bufm; bufm.writestring(cast(string)result); message("result: %s", bufm.peekChars()); }
}
this.src = result;
if (global.params.makeDeps.doOutput)
global.params.makeDeps.files.push(srcfile.toChars());
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");
5 changes: 5 additions & 0 deletions test/compilable/testdefines.d
@@ -0,0 +1,5 @@
// EXTRA_FILES: imports/defines.c
// REQUIRED_ARGS: -v
import imports.defines;

static assert(GHI == 3);

0 comments on commit a1bd9f8

Please sign in to comment.