Bug Summary

File:clang/lib/Driver/ToolChains/MinGW.cpp
Warning:line 175, column 5
Value stored to 'OutputFile' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MinGW.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D CLANG_ROUND_TRIP_CC1_ARGS=ON -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/Driver -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/lib/Driver -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/build-llvm -ferror-limit 19 -fvisibility-inlines-hidden -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-11-10-160236-22541-1 -x c++ /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/lib/Driver/ToolChains/MinGW.cpp
1//===--- MinGW.cpp - MinGWToolChain Implementation ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MinGW.h"
10#include "CommonArgs.h"
11#include "clang/Config/config.h"
12#include "clang/Driver/Compilation.h"
13#include "clang/Driver/Driver.h"
14#include "clang/Driver/DriverDiagnostic.h"
15#include "clang/Driver/InputInfo.h"
16#include "clang/Driver/Options.h"
17#include "clang/Driver/SanitizerArgs.h"
18#include "llvm/Option/ArgList.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/Path.h"
21#include "llvm/Support/VirtualFileSystem.h"
22#include <system_error>
23
24using namespace clang::diag;
25using namespace clang::driver;
26using namespace clang;
27using namespace llvm::opt;
28
29/// MinGW Tools
30void tools::MinGW::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
31 const InputInfo &Output,
32 const InputInfoList &Inputs,
33 const ArgList &Args,
34 const char *LinkingOutput) const {
35 claimNoWarnArgs(Args);
36 ArgStringList CmdArgs;
37
38 if (getToolChain().getArch() == llvm::Triple::x86) {
39 CmdArgs.push_back("--32");
40 } else if (getToolChain().getArch() == llvm::Triple::x86_64) {
41 CmdArgs.push_back("--64");
42 }
43
44 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
45
46 CmdArgs.push_back("-o");
47 CmdArgs.push_back(Output.getFilename());
48
49 for (const auto &II : Inputs)
50 CmdArgs.push_back(II.getFilename());
51
52 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
53 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
54 Exec, CmdArgs, Inputs, Output));
55
56 if (Args.hasArg(options::OPT_gsplit_dwarf))
57 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output,
58 SplitDebugName(JA, Args, Inputs[0], Output));
59}
60
61void tools::MinGW::Linker::AddLibGCC(const ArgList &Args,
62 ArgStringList &CmdArgs) const {
63 if (Args.hasArg(options::OPT_mthreads))
64 CmdArgs.push_back("-lmingwthrd");
65 CmdArgs.push_back("-lmingw32");
66
67 // Make use of compiler-rt if --rtlib option is used
68 ToolChain::RuntimeLibType RLT = getToolChain().GetRuntimeLibType(Args);
69 if (RLT == ToolChain::RLT_Libgcc) {
70 bool Static = Args.hasArg(options::OPT_static_libgcc) ||
71 Args.hasArg(options::OPT_static);
72 bool Shared = Args.hasArg(options::OPT_shared);
73 bool CXX = getToolChain().getDriver().CCCIsCXX();
74
75 if (Static || (!CXX && !Shared)) {
76 CmdArgs.push_back("-lgcc");
77 CmdArgs.push_back("-lgcc_eh");
78 } else {
79 CmdArgs.push_back("-lgcc_s");
80 CmdArgs.push_back("-lgcc");
81 }
82 } else {
83 AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args);
84 }
85
86 CmdArgs.push_back("-lmoldname");
87 CmdArgs.push_back("-lmingwex");
88 for (auto Lib : Args.getAllArgValues(options::OPT_l))
89 if (StringRef(Lib).startswith("msvcr") || StringRef(Lib).startswith("ucrt"))
90 return;
91 CmdArgs.push_back("-lmsvcrt");
92}
93
94void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
95 const InputInfo &Output,
96 const InputInfoList &Inputs,
97 const ArgList &Args,
98 const char *LinkingOutput) const {
99 const ToolChain &TC = getToolChain();
100 const Driver &D = TC.getDriver();
101 const SanitizerArgs &Sanitize = TC.getSanitizerArgs();
102
103 ArgStringList CmdArgs;
104
105 // Silence warning for "clang -g foo.o -o foo"
106 Args.ClaimAllArgs(options::OPT_g_Group);
107 // and "clang -emit-llvm foo.o -o foo"
108 Args.ClaimAllArgs(options::OPT_emit_llvm);
109 // and for "clang -w foo.o -o foo". Other warning options are already
110 // handled somewhere else.
111 Args.ClaimAllArgs(options::OPT_w);
112
113 if (!D.SysRoot.empty())
114 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
115
116 if (Args.hasArg(options::OPT_s))
117 CmdArgs.push_back("-s");
118
119 CmdArgs.push_back("-m");
120 switch (TC.getArch()) {
121 case llvm::Triple::x86:
122 CmdArgs.push_back("i386pe");
123 break;
124 case llvm::Triple::x86_64:
125 CmdArgs.push_back("i386pep");
126 break;
127 case llvm::Triple::arm:
128 case llvm::Triple::thumb:
129 // FIXME: this is incorrect for WinCE
130 CmdArgs.push_back("thumb2pe");
131 break;
132 case llvm::Triple::aarch64:
133 CmdArgs.push_back("arm64pe");
134 break;
135 default:
136 llvm_unreachable("Unsupported target architecture.")::llvm::llvm_unreachable_internal("Unsupported target architecture."
, "/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/lib/Driver/ToolChains/MinGW.cpp"
, 136)
;
137 }
138
139 Arg *SubsysArg =
140 Args.getLastArg(options::OPT_mwindows, options::OPT_mconsole);
141 if (SubsysArg && SubsysArg->getOption().matches(options::OPT_mwindows)) {
142 CmdArgs.push_back("--subsystem");
143 CmdArgs.push_back("windows");
144 } else if (SubsysArg &&
145 SubsysArg->getOption().matches(options::OPT_mconsole)) {
146 CmdArgs.push_back("--subsystem");
147 CmdArgs.push_back("console");
148 }
149
150 if (Args.hasArg(options::OPT_mdll))
151 CmdArgs.push_back("--dll");
152 else if (Args.hasArg(options::OPT_shared))
153 CmdArgs.push_back("--shared");
154 if (Args.hasArg(options::OPT_static))
155 CmdArgs.push_back("-Bstatic");
156 else
157 CmdArgs.push_back("-Bdynamic");
158 if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) {
159 CmdArgs.push_back("-e");
160 if (TC.getArch() == llvm::Triple::x86)
161 CmdArgs.push_back("_DllMainCRTStartup@12");
162 else
163 CmdArgs.push_back("DllMainCRTStartup");
164 CmdArgs.push_back("--enable-auto-image-base");
165 }
166
167 CmdArgs.push_back("-o");
168 const char *OutputFile = Output.getFilename();
169 // GCC implicitly adds an .exe extension if it is given an output file name
170 // that lacks an extension.
171 // GCC used to do this only when the compiler itself runs on windows, but
172 // since GCC 8 it does the same when cross compiling as well.
173 if (!llvm::sys::path::has_extension(OutputFile)) {
174 CmdArgs.push_back(Args.MakeArgString(Twine(OutputFile) + ".exe"));
175 OutputFile = CmdArgs.back();
Value stored to 'OutputFile' is never read
176 } else
177 CmdArgs.push_back(OutputFile);
178
179 Args.AddAllArgs(CmdArgs, options::OPT_e);
180 // FIXME: add -N, -n flags
181 Args.AddLastArg(CmdArgs, options::OPT_r);
182 Args.AddLastArg(CmdArgs, options::OPT_s);
183 Args.AddLastArg(CmdArgs, options::OPT_t);
184 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
185 Args.AddLastArg(CmdArgs, options::OPT_Z_Flag);
186
187 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
188 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
189 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("dllcrt2.o")));
190 } else {
191 if (Args.hasArg(options::OPT_municode))
192 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2u.o")));
193 else
194 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2.o")));
195 }
196 if (Args.hasArg(options::OPT_pg))
197 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("gcrt2.o")));
198 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
199 }
200
201 Args.AddAllArgs(CmdArgs, options::OPT_L);
202 TC.AddFilePathLibArgs(Args, CmdArgs);
203
204 // Add the compiler-rt library directories if they exist to help
205 // the linker find the various sanitizer, builtin, and profiling runtimes.
206 for (const auto &LibPath : TC.getLibraryPaths()) {
207 if (TC.getVFS().exists(LibPath))
208 CmdArgs.push_back(Args.MakeArgString("-L" + LibPath));
209 }
210 auto CRTPath = TC.getCompilerRTPath();
211 if (TC.getVFS().exists(CRTPath))
212 CmdArgs.push_back(Args.MakeArgString("-L" + CRTPath));
213
214 AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
215
216 // TODO: Add profile stuff here
217
218 if (TC.ShouldLinkCXXStdlib(Args)) {
219 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
220 !Args.hasArg(options::OPT_static);
221 if (OnlyLibstdcxxStatic)
222 CmdArgs.push_back("-Bstatic");
223 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
224 if (OnlyLibstdcxxStatic)
225 CmdArgs.push_back("-Bdynamic");
226 }
227
228 bool HasWindowsApp = false;
229 for (auto Lib : Args.getAllArgValues(options::OPT_l)) {
230 if (Lib == "windowsapp") {
231 HasWindowsApp = true;
232 break;
233 }
234 }
235
236 if (!Args.hasArg(options::OPT_nostdlib)) {
237 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
238 if (Args.hasArg(options::OPT_static))
239 CmdArgs.push_back("--start-group");
240
241 if (Args.hasArg(options::OPT_fstack_protector) ||
242 Args.hasArg(options::OPT_fstack_protector_strong) ||
243 Args.hasArg(options::OPT_fstack_protector_all)) {
244 CmdArgs.push_back("-lssp_nonshared");
245 CmdArgs.push_back("-lssp");
246 }
247
248 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
249 options::OPT_fno_openmp, false)) {
250 switch (TC.getDriver().getOpenMPRuntime(Args)) {
251 case Driver::OMPRT_OMP:
252 CmdArgs.push_back("-lomp");
253 break;
254 case Driver::OMPRT_IOMP5:
255 CmdArgs.push_back("-liomp5md");
256 break;
257 case Driver::OMPRT_GOMP:
258 CmdArgs.push_back("-lgomp");
259 break;
260 case Driver::OMPRT_Unknown:
261 // Already diagnosed.
262 break;
263 }
264 }
265
266 AddLibGCC(Args, CmdArgs);
267
268 if (Args.hasArg(options::OPT_pg))
269 CmdArgs.push_back("-lgmon");
270
271 if (Args.hasArg(options::OPT_pthread))
272 CmdArgs.push_back("-lpthread");
273
274 if (Sanitize.needsAsanRt()) {
275 // MinGW always links against a shared MSVCRT.
276 CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic",
277 ToolChain::FT_Shared));
278 CmdArgs.push_back(
279 TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk"));
280 CmdArgs.push_back("--require-defined");
281 CmdArgs.push_back(TC.getArch() == llvm::Triple::x86
282 ? "___asan_seh_interceptor"
283 : "__asan_seh_interceptor");
284 // Make sure the linker consider all object files from the dynamic
285 // runtime thunk.
286 CmdArgs.push_back("--whole-archive");
287 CmdArgs.push_back(
288 TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk"));
289 CmdArgs.push_back("--no-whole-archive");
290 }
291
292 TC.addProfileRTLibs(Args, CmdArgs);
293
294 if (!HasWindowsApp) {
295 // Add system libraries. If linking to libwindowsapp.a, that import
296 // library replaces all these and we shouldn't accidentally try to
297 // link to the normal desktop mode dlls.
298 if (Args.hasArg(options::OPT_mwindows)) {
299 CmdArgs.push_back("-lgdi32");
300 CmdArgs.push_back("-lcomdlg32");
301 }
302 CmdArgs.push_back("-ladvapi32");
303 CmdArgs.push_back("-lshell32");
304 CmdArgs.push_back("-luser32");
305 CmdArgs.push_back("-lkernel32");
306 }
307
308 if (Args.hasArg(options::OPT_static)) {
309 CmdArgs.push_back("--end-group");
310 } else {
311 AddLibGCC(Args, CmdArgs);
312 if (!HasWindowsApp)
313 CmdArgs.push_back("-lkernel32");
314 }
315 }
316
317 if (!Args.hasArg(options::OPT_nostartfiles)) {
318 // Add crtfastmath.o if available and fast math is enabled.
319 TC.addFastMathRuntimeIfAvailable(Args, CmdArgs);
320
321 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
322 }
323 }
324 const char *Exec = Args.MakeArgString(TC.GetLinkerPath());
325 C.addCommand(std::make_unique<Command>(JA, *this,
326 ResponseFileSupport::AtFileUTF8(),
327 Exec, CmdArgs, Inputs, Output));
328}
329
330// Simplified from Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple.
331static bool findGccVersion(StringRef LibDir, std::string &GccLibDir,
332 std::string &Ver) {
333 auto Version = toolchains::Generic_GCC::GCCVersion::Parse("0.0.0");
334 std::error_code EC;
335 for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
336 LI = LI.increment(EC)) {
337 StringRef VersionText = llvm::sys::path::filename(LI->path());
338 auto CandidateVersion =
339 toolchains::Generic_GCC::GCCVersion::Parse(VersionText);
340 if (CandidateVersion.Major == -1)
341 continue;
342 if (CandidateVersion <= Version)
343 continue;
344 Version = CandidateVersion;
345 Ver = std::string(VersionText);
346 GccLibDir = LI->path();
347 }
348 return Ver.size();
349}
350
351void toolchains::MinGW::findGccLibDir() {
352 llvm::SmallVector<llvm::SmallString<32>, 2> SubdirNames;
353 SubdirNames.emplace_back(getTriple().getArchName());
354 SubdirNames[0] += "-w64-mingw32";
355 SubdirNames.emplace_back("mingw32");
356 if (SubdirName.empty())
357 SubdirName = std::string(SubdirNames[0].str());
358 // lib: Arch Linux, Ubuntu, Windows
359 // lib64: openSUSE Linux
360 for (StringRef CandidateLib : {"lib", "lib64"}) {
361 for (StringRef CandidateSysroot : SubdirNames) {
362 llvm::SmallString<1024> LibDir(Base);
363 llvm::sys::path::append(LibDir, CandidateLib, "gcc", CandidateSysroot);
364 if (findGccVersion(LibDir, GccLibDir, Ver)) {
365 SubdirName = std::string(CandidateSysroot);
366 return;
367 }
368 }
369 }
370}
371
372static llvm::ErrorOr<std::string> findGcc(const llvm::Triple &T) {
373 llvm::SmallVector<llvm::SmallString<32>, 2> Gccs;
374 Gccs.emplace_back(T.getArchName());
375 Gccs[0] += "-w64-mingw32-gcc";
376 Gccs.emplace_back("mingw32-gcc");
377 // Please do not add "gcc" here
378 for (StringRef CandidateGcc : Gccs)
379 if (llvm::ErrorOr<std::string> GPPName = llvm::sys::findProgramByName(CandidateGcc))
380 return GPPName;
381 return make_error_code(std::errc::no_such_file_or_directory);
382}
383
384static llvm::ErrorOr<std::string>
385findClangRelativeSysroot(const Driver &D, const llvm::Triple &T,
386 std::string &SubdirName) {
387 llvm::SmallVector<llvm::SmallString<32>, 2> Subdirs;
388 Subdirs.emplace_back(T.str());
389 Subdirs.emplace_back(T.getArchName());
390 Subdirs[1] += "-w64-mingw32";
391 StringRef ClangRoot = llvm::sys::path::parent_path(D.getInstalledDir());
392 StringRef Sep = llvm::sys::path::get_separator();
393 for (StringRef CandidateSubdir : Subdirs) {
394 if (llvm::sys::fs::is_directory(ClangRoot + Sep + CandidateSubdir)) {
395 SubdirName = std::string(CandidateSubdir);
396 return (ClangRoot + Sep + CandidateSubdir).str();
397 }
398 }
399 return make_error_code(std::errc::no_such_file_or_directory);
400}
401
402toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple,
403 const ArgList &Args)
404 : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args),
405 RocmInstallation(D, Triple, Args) {
406 getProgramPaths().push_back(getDriver().getInstalledDir());
407
408 // The sequence for detecting a sysroot here should be kept in sync with
409 // the testTriple function below.
410 if (getDriver().SysRoot.size())
411 Base = getDriver().SysRoot;
412 // Look for <clang-bin>/../<triplet>; if found, use <clang-bin>/.. as the
413 // base as it could still be a base for a gcc setup with libgcc.
414 else if (llvm::ErrorOr<std::string> TargetSubdir =
415 findClangRelativeSysroot(getDriver(), getTriple(), SubdirName))
416 Base = std::string(llvm::sys::path::parent_path(TargetSubdir.get()));
417 else if (llvm::ErrorOr<std::string> GPPName = findGcc(getTriple()))
418 Base = std::string(llvm::sys::path::parent_path(
419 llvm::sys::path::parent_path(GPPName.get())));
420 else
421 Base = std::string(
422 llvm::sys::path::parent_path(getDriver().getInstalledDir()));
423
424 Base += llvm::sys::path::get_separator();
425 findGccLibDir();
426 // GccLibDir must precede Base/lib so that the
427 // correct crtbegin.o ,cetend.o would be found.
428 getFilePaths().push_back(GccLibDir);
429 getFilePaths().push_back(
430 (Base + SubdirName + llvm::sys::path::get_separator() + "lib").str());
431 getFilePaths().push_back(Base + "lib");
432 // openSUSE
433 getFilePaths().push_back(Base + SubdirName + "/sys-root/mingw/lib");
434
435 NativeLLVMSupport =
436 Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER"")
437 .equals_insensitive("lld");
438}
439
440bool toolchains::MinGW::IsIntegratedAssemblerDefault() const { return true; }
441
442Tool *toolchains::MinGW::getTool(Action::ActionClass AC) const {
443 switch (AC) {
444 case Action::PreprocessJobClass:
445 if (!Preprocessor)
446 Preprocessor.reset(new tools::gcc::Preprocessor(*this));
447 return Preprocessor.get();
448 case Action::CompileJobClass:
449 if (!Compiler)
450 Compiler.reset(new tools::gcc::Compiler(*this));
451 return Compiler.get();
452 default:
453 return ToolChain::getTool(AC);
454 }
455}
456
457Tool *toolchains::MinGW::buildAssembler() const {
458 return new tools::MinGW::Assembler(*this);
459}
460
461Tool *toolchains::MinGW::buildLinker() const {
462 return new tools::MinGW::Linker(*this);
463}
464
465bool toolchains::MinGW::HasNativeLLVMSupport() const {
466 return NativeLLVMSupport;
467}
468
469bool toolchains::MinGW::IsUnwindTablesDefault(const ArgList &Args) const {
470 Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions,
471 options::OPT_fseh_exceptions,
472 options::OPT_fdwarf_exceptions);
473 if (ExceptionArg &&
474 ExceptionArg->getOption().matches(options::OPT_fseh_exceptions))
475 return true;
476 return getArch() == llvm::Triple::x86_64 ||
477 getArch() == llvm::Triple::aarch64;
478}
479
480bool toolchains::MinGW::isPICDefault() const {
481 return getArch() == llvm::Triple::x86_64 ||
482 getArch() == llvm::Triple::aarch64;
483}
484
485bool toolchains::MinGW::isPIEDefault() const { return false; }
486
487bool toolchains::MinGW::isPICDefaultForced() const {
488 return getArch() == llvm::Triple::x86_64 ||
489 getArch() == llvm::Triple::aarch64;
490}
491
492llvm::ExceptionHandling
493toolchains::MinGW::GetExceptionModel(const ArgList &Args) const {
494 if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64)
495 return llvm::ExceptionHandling::WinEH;
496 return llvm::ExceptionHandling::DwarfCFI;
497}
498
499SanitizerMask toolchains::MinGW::getSupportedSanitizers() const {
500 SanitizerMask Res = ToolChain::getSupportedSanitizers();
501 Res |= SanitizerKind::Address;
502 Res |= SanitizerKind::PointerCompare;
503 Res |= SanitizerKind::PointerSubtract;
504 Res |= SanitizerKind::Vptr;
505 return Res;
506}
507
508void toolchains::MinGW::AddCudaIncludeArgs(const ArgList &DriverArgs,
509 ArgStringList &CC1Args) const {
510 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
511}
512
513void toolchains::MinGW::AddHIPIncludeArgs(const ArgList &DriverArgs,
514 ArgStringList &CC1Args) const {
515 RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
516}
517
518void toolchains::MinGW::printVerboseInfo(raw_ostream &OS) const {
519 CudaInstallation.print(OS);
520 RocmInstallation.print(OS);
521}
522
523// Include directories for various hosts:
524
525// Windows, mingw.org
526// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++
527// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32
528// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward
529// c:\mingw\include
530// c:\mingw\mingw32\include
531
532// Windows, mingw-w64 mingw-builds
533// c:\mingw32\i686-w64-mingw32\include
534// c:\mingw32\i686-w64-mingw32\include\c++
535// c:\mingw32\i686-w64-mingw32\include\c++\i686-w64-mingw32
536// c:\mingw32\i686-w64-mingw32\include\c++\backward
537
538// Windows, mingw-w64 msys2
539// c:\msys64\mingw32\include
540// c:\msys64\mingw32\i686-w64-mingw32\include
541// c:\msys64\mingw32\include\c++\4.9.2
542// c:\msys64\mingw32\include\c++\4.9.2\i686-w64-mingw32
543// c:\msys64\mingw32\include\c++\4.9.2\backward
544
545// openSUSE
546// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++
547// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32
548// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward
549// /usr/x86_64-w64-mingw32/sys-root/mingw/include
550
551// Arch Linux
552// /usr/i686-w64-mingw32/include/c++/5.1.0
553// /usr/i686-w64-mingw32/include/c++/5.1.0/i686-w64-mingw32
554// /usr/i686-w64-mingw32/include/c++/5.1.0/backward
555// /usr/i686-w64-mingw32/include
556
557// Ubuntu
558// /usr/include/c++/4.8
559// /usr/include/c++/4.8/x86_64-w64-mingw32
560// /usr/include/c++/4.8/backward
561// /usr/x86_64-w64-mingw32/include
562
563void toolchains::MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
564 ArgStringList &CC1Args) const {
565 if (DriverArgs.hasArg(options::OPT_nostdinc))
566 return;
567
568 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
569 SmallString<1024> P(getDriver().ResourceDir);
570 llvm::sys::path::append(P, "include");
571 addSystemInclude(DriverArgs, CC1Args, P.str());
572 }
573
574 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
575 return;
576
577 if (GetRuntimeLibType(DriverArgs) == ToolChain::RLT_Libgcc) {
578 // openSUSE
579 addSystemInclude(DriverArgs, CC1Args,
580 Base + SubdirName + "/sys-root/mingw/include");
581 }
582
583 addSystemInclude(DriverArgs, CC1Args,
584 Base + SubdirName + llvm::sys::path::get_separator() +
585 "include");
586 addSystemInclude(DriverArgs, CC1Args, Base + "include");
587}
588
589void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(
590 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
591 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
592 DriverArgs.hasArg(options::OPT_nostdincxx))
593 return;
594
595 StringRef Slash = llvm::sys::path::get_separator();
596
597 switch (GetCXXStdlibType(DriverArgs)) {
598 case ToolChain::CST_Libcxx: {
599 std::string TargetDir = (Base + "include" + Slash + getTripleString() +
600 Slash + "c++" + Slash + "v1")
601 .str();
602 if (getDriver().getVFS().exists(TargetDir))
603 addSystemInclude(DriverArgs, CC1Args, TargetDir);
604 addSystemInclude(DriverArgs, CC1Args,
605 Base + SubdirName + Slash + "include" + Slash + "c++" +
606 Slash + "v1");
607 addSystemInclude(DriverArgs, CC1Args,
608 Base + "include" + Slash + "c++" + Slash + "v1");
609 break;
610 }
611
612 case ToolChain::CST_Libstdcxx:
613 llvm::SmallVector<llvm::SmallString<1024>, 4> CppIncludeBases;
614 CppIncludeBases.emplace_back(Base);
615 llvm::sys::path::append(CppIncludeBases[0], SubdirName, "include", "c++");
616 CppIncludeBases.emplace_back(Base);
617 llvm::sys::path::append(CppIncludeBases[1], SubdirName, "include", "c++",
618 Ver);
619 CppIncludeBases.emplace_back(Base);
620 llvm::sys::path::append(CppIncludeBases[2], "include", "c++", Ver);
621 CppIncludeBases.emplace_back(GccLibDir);
622 llvm::sys::path::append(CppIncludeBases[3], "include", "c++");
623 for (auto &CppIncludeBase : CppIncludeBases) {
624 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
625 CppIncludeBase += Slash;
626 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + SubdirName);
627 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward");
628 }
629 break;
630 }
631}
632
633static bool testTriple(const Driver &D, const llvm::Triple &Triple,
634 const ArgList &Args) {
635 // If an explicit sysroot is set, that will be used and we shouldn't try to
636 // detect anything else.
637 std::string SubdirName;
638 if (D.SysRoot.size())
639 return true;
640 if (llvm::ErrorOr<std::string> TargetSubdir =
641 findClangRelativeSysroot(D, Triple, SubdirName))
642 return true;
643 if (llvm::ErrorOr<std::string> GPPName = findGcc(Triple))
644 return true;
645 // If we neither found a colocated sysroot or a matching gcc executable,
646 // conclude that we can't know if this is the correct spelling of the triple.
647 return false;
648}
649
650static llvm::Triple adjustTriple(const Driver &D, const llvm::Triple &Triple,
651 const ArgList &Args) {
652 // First test if the original triple can find a sysroot with the triple
653 // name.
654 if (testTriple(D, Triple, Args))
655 return Triple;
656 llvm::SmallVector<llvm::StringRef, 3> Archs;
657 // If not, test a couple other possible arch names that might be what was
658 // intended.
659 if (Triple.getArch() == llvm::Triple::x86) {
660 Archs.emplace_back("i386");
661 Archs.emplace_back("i586");
662 Archs.emplace_back("i686");
663 } else if (Triple.getArch() == llvm::Triple::arm ||
664 Triple.getArch() == llvm::Triple::thumb) {
665 Archs.emplace_back("armv7");
666 }
667 for (auto A : Archs) {
668 llvm::Triple TestTriple(Triple);
669 TestTriple.setArchName(A);
670 if (testTriple(D, TestTriple, Args))
671 return TestTriple;
672 }
673 // If none was found, just proceed with the original value.
674 return Triple;
675}
676
677void toolchains::MinGW::fixTripleArch(const Driver &D, llvm::Triple &Triple,
678 const ArgList &Args) {
679 if (Triple.getArch() == llvm::Triple::x86 ||
680 Triple.getArch() == llvm::Triple::arm ||
681 Triple.getArch() == llvm::Triple::thumb)
682 Triple = adjustTriple(D, Triple, Args);
683}