Skip to content

Commit

Permalink
Run C preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Jun 7, 2023
1 parent f21241b commit 5b13776
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 24 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ set(DRV_SRC
driver/cl_options-llvm.cpp
driver/codegenerator.cpp
driver/configfile.cpp
driver/cpreprocessor.cpp
driver/dcomputecodegenerator.cpp
driver/exe_path.cpp
driver/targetmachine.cpp
Expand Down
2 changes: 1 addition & 1 deletion driver/archiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ int createStaticLibrary() {
}

// invoke external archiver
return executeToolAndWait(tool, args, global.params.verbose);
return executeToolAndWait(Loc(), tool, args, global.params.verbose);
}

const char *getPathToProducedStaticLibrary() {
Expand Down
84 changes: 84 additions & 0 deletions driver/cpreprocessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "driver/cpreprocessor.h"

#include "dmd/errors.h"
#include "driver/cl_options.h"
#include "driver/tool.h"

namespace {
const char *getPathToImportc_h(const Loc &loc) {
// importc.h should be next to object.d
static const char *cached = nullptr;
if (!cached) {
cached = FileName::searchPath(global.path, "importc.h", false);
if (!cached) {
error(loc, "cannot find \"importc.h\" along import path");
fatal();
}
}
return cached;
}
} // anonymous namespace

FileName runCPreprocessor(FileName csrcfile, const Loc &loc, bool &ifile,
OutBuffer &defines) {
const char *importc_h = getPathToImportc_h(loc);
const char *ifilename = FileName::forceExt(csrcfile.toChars(), i_ext.ptr);

const auto &triple = *global.params.targetTriple;
const bool isMSVC = triple.isWindowsMSVCEnvironment();

#if 0 //ifdef _WIN32
// TODO: INCLUDE env var etc.?
windows::MsvcEnvironmentScope msvcEnv;
if (isMSVC)
msvcEnv.setup();
#endif

const std::string cc = getGcc(isMSVC ? "cl.exe" : "cc");
std::vector<std::string> args;

if (!isMSVC)
appendTargetArgsForGcc(args);

if (triple.isOSDarwin())
args.push_back("-fno-blocks"); // disable blocks extension

for (const auto &ccSwitch : opts::ccSwitches) {
args.push_back(ccSwitch);
}
// TODO: -Xcpp switches?

if (isMSVC) {
args.push_back("/P"); // run preprocessor
args.push_back("/Zc:preprocessor"); // use the new conforming preprocessor
args.push_back("/PD"); // undocumented: print all macro definitions
args.push_back("/nologo");
args.push_back(csrcfile.toChars());
args.push_back((llvm::Twine("/FI") + importc_h).str());
// preprocessed output file
args.push_back((llvm::Twine("/Fi") + ifilename).str());
} else { // Posix
// merge #define's with output:
// https://gcc.gnu.org/onlinedocs/cpp/Invocation.html#index-dD
args.push_back("-dD");

// need to redefine some macros in importc.h
args.push_back("-Wno-builtin-macro-redefined");

args.push_back("-E"); // run preprocessor only
args.push_back("-include");
args.push_back(importc_h);
args.push_back(csrcfile.toChars());
args.push_back("-o");
args.push_back(ifilename);
}

const int status = executeToolAndWait(loc, cc, args, global.params.verbose);
if (status) {
errorSupplemental(loc, "C preprocessor failed for file '%s'", csrcfile.toChars());
fatal();
}

ifile = true;
return FileName::create(ifilename);
}
8 changes: 8 additions & 0 deletions driver/cpreprocessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "dmd/common/outbuffer.h"
#include "dmd/globals.h"
#include "dmd/root/filename.h"

FileName runCPreprocessor(FileName csrcfile, const Loc &loc, bool &ifile,
OutBuffer &defines);
3 changes: 2 additions & 1 deletion driver/linker-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,5 +858,6 @@ int linkObjToBinaryGcc(llvm::StringRef outputPath,
logstr << "\n"; // FIXME where's flush ?

// try to call linker
return executeToolAndWait(tool, argsBuilder->args, global.params.verbose);
return executeToolAndWait(Loc(), tool, argsBuilder->args,
global.params.verbose);
}
7 changes: 1 addition & 6 deletions driver/linker-msvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ void addSanitizerLibs(std::vector<std::string> &args) {

int linkObjToBinaryMSVC(llvm::StringRef outputPath,
const std::vector<std::string> &defaultLibNames) {
if (!opts::ccSwitches.empty()) {
error(Loc(), "-Xcc is not supported for MSVC");
fatal();
}

#ifdef _WIN32
windows::MsvcEnvironmentScope msvcEnv;

Expand Down Expand Up @@ -309,5 +304,5 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath,
#endif
}

return executeToolAndWait(linker, args, global.params.verbose);
return executeToolAndWait(Loc(), linker, args, global.params.verbose);
}
2 changes: 1 addition & 1 deletion driver/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ int runProgram() {

// Run executable
int status =
executeToolAndWait(gExePath, opts::runargs, global.params.verbose);
executeToolAndWait(Loc(), gExePath, opts::runargs, global.params.verbose);
if (status < 0) {
#if defined(_MSC_VER) || defined(__MINGW32__)
error(Loc(), "program received signal %d", -status);
Expand Down
3 changes: 3 additions & 0 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "driver/cl_options_sanitizers.h"
#include "driver/codegenerator.h"
#include "driver/configfile.h"
#include "driver/cpreprocessor.h"
#include "driver/dcomputecodegenerator.h"
#include "driver/exe_path.h"
#include "driver/ldc-version.h"
Expand Down Expand Up @@ -1183,6 +1184,8 @@ int cppmain() {
global.params.dllimport = DLLImport::none;
}

global.preprocess = &runCPreprocessor;

// allocate the target abi
gABI = TargetABI::getTarget();

Expand Down
2 changes: 1 addition & 1 deletion driver/toobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static void assemble(const std::string &asmpath, const std::string &objpath) {
appendTargetArgsForGcc(args);

// Run the compiler to assembly the program.
int R = executeToolAndWait(getGcc(), args, global.params.verbose);
int R = executeToolAndWait(Loc(), getGcc(), args, global.params.verbose);
if (R) {
error(Loc(), "Error while invoking external assembler.");
fatal();
Expand Down
10 changes: 5 additions & 5 deletions driver/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ std::string getProgram(const char *fallbackName,

////////////////////////////////////////////////////////////////////////////////

std::string getGcc() { return getProgram("cc", &gcc, "CC"); }
std::string getGcc(const char *fallback) { return getProgram(fallback, &gcc, "CC"); }

////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -223,11 +223,11 @@ std::vector<const char *> getFullArgs(const char *tool,

////////////////////////////////////////////////////////////////////////////////

int executeToolAndWait(const std::string &tool_,
int executeToolAndWait(const Loc &loc, const std::string &tool_,
const std::vector<std::string> &args, bool verbose) {
const auto tool = findProgramByName(tool_);
if (tool.empty()) {
error(Loc(), "cannot find program `%s`", tool_.c_str());
error(loc, "cannot find program `%s`", tool_.c_str());
return -1;
}

Expand All @@ -249,9 +249,9 @@ int executeToolAndWait(const std::string &tool_,
args::executeAndWait(std::move(fullArgs), rspEncoding, &errorMsg);

if (status) {
error(Loc(), "%s failed with status: %d", tool.c_str(), status);
error(loc, "%s failed with status: %d", tool.c_str(), status);
if (!errorMsg.empty()) {
errorSupplemental(Loc(), "message: %s", errorMsg.c_str());
errorSupplemental(loc, "message: %s", errorMsg.c_str());
}
}

Expand Down
6 changes: 4 additions & 2 deletions driver/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

#include "llvm/Support/CommandLine.h"

struct Loc;

namespace opts {
extern llvm::cl::opt<std::string> linker;
}

std::string getGcc();
std::string getGcc(const char *fallback = "cc");
void appendTargetArgsForGcc(std::vector<std::string> &args);

std::string getProgram(const char *fallbackName,
Expand All @@ -37,7 +39,7 @@ std::vector<const char *> getFullArgs(const char *tool,
const std::vector<std::string> &args,
bool printVerbose);

int executeToolAndWait(const std::string &tool,
int executeToolAndWait(const Loc &loc, const std::string &tool,
const std::vector<std::string> &args,
bool verbose = false);

Expand Down
1 change: 0 additions & 1 deletion tests/dmd/compilable/stdcheaders.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* Do a smoke test of the C Standard headers.
* Many platforms do not support all the C Standard headers.
* DISABLED: LDC // FIXME: needs preprocessor
*/

#include <assert.h>
Expand Down
2 changes: 0 additions & 2 deletions tests/dmd/compilable/test22809.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ int y = ((size_t)((char *)&((struct Foo *)1)->x - (char *)1));
_Static_assert(((size_t)((char *)&((struct Foo *)0)->y - (char *)0))==4, "");


/* LDC FIXME: needs preprocessor (for including importc.h)
// https://issues.dlang.org/show_bug.cgi?id=23584

int foo(float bar)
Expand All @@ -28,4 +27,3 @@ void test23584()
int i = foo(3.5);
_Static_assert(foo(3.5) == 0x40600000, "1");
}
*/
1 change: 0 additions & 1 deletion tests/dmd/compilable/test23214.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// https://issues.dlang.org/show_bug.cgi?id=23214
// DISABLED: LDC // FIXME - need to invoke C pre-processor

typedef unsigned __int64 uintptr_t;
1 change: 0 additions & 1 deletion tests/dmd/compilable/test23583.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// DISABLED: LDC // FIXME: needs preprocessor
// https://issues.dlang.org/show_bug.cgi?id=23580
// https://issues.dlang.org/show_bug.cgi?id=23581
// https://issues.dlang.org/show_bug.cgi?id=23582
Expand Down
1 change: 0 additions & 1 deletion tests/dmd/compilable/test23616.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// DISABLED: LDC // FIXME: needs preprocessor
// https://issues.dlang.org/show_bug.cgi?id=23616

#if __has_extension(gnu_asm)
Expand Down
2 changes: 1 addition & 1 deletion tests/dmd/runnable/initializer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// DISABLED: LDC // FIXME: needs preprocessor for __LINE__
// DISABLED: LDC // FIXME: needs support for importC special cases

/* Test initializers */

Expand Down

0 comments on commit 5b13776

Please sign in to comment.