forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpreprocessor.cpp
More file actions
171 lines (139 loc) · 5.16 KB
/
Copy pathcpreprocessor.cpp
File metadata and controls
171 lines (139 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "driver/cpreprocessor.h"
#include "dmd/errors.h"
#include "dmd/timetrace.h"
#include "driver/cl_options.h"
#include "driver/tool.h"
#include "gen/irstate.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Target/TargetMachine.h"
namespace {
const char *getPathToImportc_h(Loc loc) {
// importc.h should be next to object.d
static const char *cached = nullptr;
if (!cached) {
cached = FileName::searchPath(global.importPaths, "importc.h", false);
if (!cached) {
error(loc, "cannot find \"importc.h\" along import path");
fatal();
}
#ifdef _WIN32
// if the path to importc.h is relative, cl.exe (but not clang-cl) treats it as relative to the .c file!
cached = FileName::toAbsolute(cached);
#endif
}
return cached;
}
FileName getOutputPath(Loc loc, const char *csrcfile) {
llvm::SmallString<64> buffer;
// 1) create a new temporary directory (e.g., `/tmp/itmp-ldc-10ecec`)
auto ec = llvm::sys::fs::createUniqueDirectory("itmp-ldc", buffer);
if (ec) {
error(loc,
"failed to create temporary directory for preprocessed .i file: %s\n%s",
buffer.c_str(), ec.message().c_str());
fatal();
}
// 2) append the .c file name, replacing the extension with .i
llvm::sys::path::append(buffer, FileName::name(csrcfile));
llvm::sys::path::replace_extension(buffer, i_ext.ptr);
// the directory is removed (after the file) in Module.read()
return FileName::create(buffer.c_str()); // allocates a copy
}
} // anonymous namespace
FileName runCPreprocessor(FileName csrcfile, Loc loc, OutBuffer &defines) {
dmd::TimeTraceScope timeScope("Preprocess C file", csrcfile.toChars(), loc);
const char *importc_h = getPathToImportc_h(loc);
const auto &triple = *global.params.targetTriple;
const bool isMSVC = triple.isWindowsMSVCEnvironment();
#ifdef _WIN32
windows::MsvcEnvironmentScope msvcEnv;
if (isMSVC)
msvcEnv.setup(/*forPreprocessingOnly=*/true);
#endif
FileName ipath = getOutputPath(loc, csrcfile.toChars());
std::vector<std::string> args;
const std::string &cc = getCC(args);
args.push_back(isMSVC ? "/std:c11" : "-std=c11");
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);
}
for (const auto &cppSwitch : opts::cppSwitches) {
args.push_back(cppSwitch);
}
if (isMSVC) {
args.push_back("/nologo");
args.push_back("/P"); // preprocess only
const bool isClangCl = llvm::StringRef(cc).contains_insensitive("clang-cl");
if (!isClangCl) {
args.push_back("/PD"); // print all macro definitions
args.push_back("/Zc:preprocessor"); // use the new conforming preprocessor
} else {
// propagate the target to the preprocessor
args.push_back("--target=" + triple.getTriple());
// propagate all enabled/disabled features to the preprocessor
const auto &subTarget = gTargetMachine->getMCSubtargetInfo();
#if LLVM_VERSION_MAJOR >= 23
const auto &featureBits = subTarget.getFeatureBits();
#else
const auto &featureBits = subTarget->getFeatureBits();
#endif
llvm::SmallString<64> featureString;
for (const auto &feature :
#if LLVM_VERSION_MAJOR >= 23
subTarget.getAllProcessorFeatures()
#else
subTarget->getAllProcessorFeatures()
#endif
) {
args.push_back("-Xclang");
args.push_back("-target-feature");
args.push_back("-Xclang");
featureString += featureBits.test(feature.Value) ? '+' : '-';
#if LLVM_VERSION_MAJOR >= 23
featureString += feature.key();
#else
featureString += feature.Key;
#endif
args.push_back(featureString.str().str());
featureString.clear();
}
// print macro definitions (clang-cl doesn't support /PD - use clang's
// -dD)
args.push_back("/clang:-dD");
// need to redefine some macros in importc.h
args.push_back("-Wno-builtin-macro-redefined");
// disable the clang resource headers (immintrin.h etc.), using
// unsupported types like __int128, __bf16 etc. - stick to the MS headers
args.push_back("-nobuiltininc");
}
args.push_back(csrcfile.toChars());
args.push_back((llvm::Twine("/FI") + importc_h).str());
// preprocessed output file
args.push_back((llvm::Twine("/Fi") + ipath.toChars()).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(ipath.toChars());
}
const int status = executeToolAndWait(loc, cc, args, global.params.v.verbose);
if (status) {
errorSupplemental(loc, "C preprocessor failed for file '%s'", csrcfile.toChars());
fatal();
}
return ipath;
}