]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/MSVCToolChain.cpp
Update clang to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / MSVCToolChain.cpp
1 //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ToolChains.h"
11 #include "Tools.h"
12 #include "clang/Basic/CharInfo.h"
13 #include "clang/Basic/Version.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/DriverDiagnostic.h"
17 #include "clang/Driver/Options.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/Option/Arg.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Support/ConvertUTF.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/Process.h"
28 #include <cstdio>
29
30 // Include the necessary headers to interface with the Windows registry and
31 // environment.
32 #if defined(LLVM_ON_WIN32)
33 #define USE_WIN32
34 #endif
35
36 #ifdef USE_WIN32
37   #define WIN32_LEAN_AND_MEAN
38   #define NOGDI
39   #ifndef NOMINMAX
40     #define NOMINMAX
41   #endif
42   #include <windows.h>
43 #endif
44
45 using namespace clang::driver;
46 using namespace clang::driver::toolchains;
47 using namespace clang;
48 using namespace llvm::opt;
49
50 MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple,
51                              const ArgList &Args)
52   : ToolChain(D, Triple, Args) {
53   getProgramPaths().push_back(getDriver().getInstalledDir());
54   if (getDriver().getInstalledDir() != getDriver().Dir)
55     getProgramPaths().push_back(getDriver().Dir);
56 }
57
58 Tool *MSVCToolChain::buildLinker() const {
59   return new tools::visualstudio::Linker(*this);
60 }
61
62 Tool *MSVCToolChain::buildAssembler() const {
63   if (getTriple().isOSBinFormatMachO())
64     return new tools::darwin::Assembler(*this);
65   getDriver().Diag(clang::diag::err_no_external_assembler);
66   return nullptr;
67 }
68
69 bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
70   return true;
71 }
72
73 bool MSVCToolChain::IsUnwindTablesDefault() const {
74   // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
75   // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
76   // how to generate them yet.
77
78   // Don't emit unwind tables by default for MachO targets.
79   if (getTriple().isOSBinFormatMachO())
80     return false;
81
82   return getArch() == llvm::Triple::x86_64;
83 }
84
85 bool MSVCToolChain::isPICDefault() const {
86   return getArch() == llvm::Triple::x86_64;
87 }
88
89 bool MSVCToolChain::isPIEDefault() const {
90   return false;
91 }
92
93 bool MSVCToolChain::isPICDefaultForced() const {
94   return getArch() == llvm::Triple::x86_64;
95 }
96
97 #ifdef USE_WIN32
98 static bool readFullStringValue(HKEY hkey, const char *valueName,
99                                 std::string &value) {
100   std::wstring WideValueName;
101   if (!llvm::ConvertUTF8toWide(valueName, WideValueName))
102     return false;
103
104   DWORD result = 0;
105   DWORD valueSize = 0;
106   DWORD type = 0;
107   // First just query for the required size.
108   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
109                             &valueSize);
110   if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
111     return false;
112   std::vector<BYTE> buffer(valueSize);
113   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
114                             &valueSize);
115   if (result == ERROR_SUCCESS) {
116     std::wstring WideValue(reinterpret_cast<const wchar_t *>(buffer.data()),
117                            valueSize / sizeof(wchar_t));
118     if (valueSize && WideValue.back() == L'\0') {
119       WideValue.pop_back();
120     }
121     // The destination buffer must be empty as an invariant of the conversion
122     // function; but this function is sometimes called in a loop that passes in
123     // the same buffer, however. Simply clear it out so we can overwrite it.
124     value.clear();
125     return llvm::convertWideToUTF8(WideValue, value);
126   }
127   return false;
128 }
129 #endif
130
131 /// \brief Read registry string.
132 /// This also supports a means to look for high-versioned keys by use
133 /// of a $VERSION placeholder in the key path.
134 /// $VERSION in the key path is a placeholder for the version number,
135 /// causing the highest value path to be searched for and used.
136 /// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
137 /// There can be additional characters in the component.  Only the numeric
138 /// characters are compared.  This function only searches HKLM.
139 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
140                                     std::string &value, std::string *phValue) {
141 #ifndef USE_WIN32
142   return false;
143 #else
144   HKEY hRootKey = HKEY_LOCAL_MACHINE;
145   HKEY hKey = NULL;
146   long lResult;
147   bool returnValue = false;
148
149   const char *placeHolder = strstr(keyPath, "$VERSION");
150   std::string bestName;
151   // If we have a $VERSION placeholder, do the highest-version search.
152   if (placeHolder) {
153     const char *keyEnd = placeHolder - 1;
154     const char *nextKey = placeHolder;
155     // Find end of previous key.
156     while ((keyEnd > keyPath) && (*keyEnd != '\\'))
157       keyEnd--;
158     // Find end of key containing $VERSION.
159     while (*nextKey && (*nextKey != '\\'))
160       nextKey++;
161     size_t partialKeyLength = keyEnd - keyPath;
162     char partialKey[256];
163     if (partialKeyLength >= sizeof(partialKey))
164       partialKeyLength = sizeof(partialKey) - 1;
165     strncpy(partialKey, keyPath, partialKeyLength);
166     partialKey[partialKeyLength] = '\0';
167     HKEY hTopKey = NULL;
168     lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
169                             &hTopKey);
170     if (lResult == ERROR_SUCCESS) {
171       char keyName[256];
172       double bestValue = 0.0;
173       DWORD index, size = sizeof(keyName) - 1;
174       for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL,
175                                     NULL, NULL) == ERROR_SUCCESS;
176            index++) {
177         const char *sp = keyName;
178         while (*sp && !isDigit(*sp))
179           sp++;
180         if (!*sp)
181           continue;
182         const char *ep = sp + 1;
183         while (*ep && (isDigit(*ep) || (*ep == '.')))
184           ep++;
185         char numBuf[32];
186         strncpy(numBuf, sp, sizeof(numBuf) - 1);
187         numBuf[sizeof(numBuf) - 1] = '\0';
188         double dvalue = strtod(numBuf, NULL);
189         if (dvalue > bestValue) {
190           // Test that InstallDir is indeed there before keeping this index.
191           // Open the chosen key path remainder.
192           bestName = keyName;
193           // Append rest of key.
194           bestName.append(nextKey);
195           lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
196                                   KEY_READ | KEY_WOW64_32KEY, &hKey);
197           if (lResult == ERROR_SUCCESS) {
198             if (readFullStringValue(hKey, valueName, value)) {
199               bestValue = dvalue;
200               if (phValue)
201                 *phValue = bestName;
202               returnValue = true;
203             }
204             RegCloseKey(hKey);
205           }
206         }
207         size = sizeof(keyName) - 1;
208       }
209       RegCloseKey(hTopKey);
210     }
211   } else {
212     lResult =
213         RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
214     if (lResult == ERROR_SUCCESS) {
215       if (readFullStringValue(hKey, valueName, value))
216         returnValue = true;
217       if (phValue)
218         phValue->clear();
219       RegCloseKey(hKey);
220     }
221   }
222   return returnValue;
223 #endif // USE_WIN32
224 }
225
226 // Convert LLVM's ArchType
227 // to the corresponding name of Windows SDK libraries subfolder
228 static StringRef getWindowsSDKArch(llvm::Triple::ArchType Arch) {
229   switch (Arch) {
230   case llvm::Triple::x86:
231     return "x86";
232   case llvm::Triple::x86_64:
233     return "x64";
234   case llvm::Triple::arm:
235     return "arm";
236   default:
237     return "";
238   }
239 }
240
241 // Find the most recent version of Universal CRT or Windows 10 SDK.
242 // vcvarsqueryregistry.bat from Visual Studio 2015 sorts entries in the include
243 // directory by name and uses the last one of the list.
244 // So we compare entry names lexicographically to find the greatest one.
245 static bool getWindows10SDKVersion(const std::string &SDKPath,
246                                    std::string &SDKVersion) {
247   SDKVersion.clear();
248
249   std::error_code EC;
250   llvm::SmallString<128> IncludePath(SDKPath);
251   llvm::sys::path::append(IncludePath, "Include");
252   for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd;
253        DirIt != DirEnd && !EC; DirIt.increment(EC)) {
254     if (!llvm::sys::fs::is_directory(DirIt->path()))
255       continue;
256     StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
257     // If WDK is installed, there could be subfolders like "wdf" in the
258     // "Include" directory.
259     // Allow only directories which names start with "10.".
260     if (!CandidateName.startswith("10."))
261       continue;
262     if (CandidateName > SDKVersion)
263       SDKVersion = CandidateName;
264   }
265
266   return !SDKVersion.empty();
267 }
268
269 /// \brief Get Windows SDK installation directory.
270 bool MSVCToolChain::getWindowsSDKDir(std::string &Path, int &Major,
271                                      std::string &WindowsSDKIncludeVersion,
272                                      std::string &WindowsSDKLibVersion) const {
273   std::string RegistrySDKVersion;
274   // Try the Windows registry.
275   if (!getSystemRegistryString(
276           "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
277           "InstallationFolder", Path, &RegistrySDKVersion))
278     return false;
279   if (Path.empty() || RegistrySDKVersion.empty())
280     return false;
281
282   WindowsSDKIncludeVersion.clear();
283   WindowsSDKLibVersion.clear();
284   Major = 0;
285   std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major);
286   if (Major <= 7)
287     return true;
288   if (Major == 8) {
289     // Windows SDK 8.x installs libraries in a folder whose names depend on the
290     // version of the OS you're targeting.  By default choose the newest, which
291     // usually corresponds to the version of the OS you've installed the SDK on.
292     const char *Tests[] = {"winv6.3", "win8", "win7"};
293     for (const char *Test : Tests) {
294       llvm::SmallString<128> TestPath(Path);
295       llvm::sys::path::append(TestPath, "Lib", Test);
296       if (llvm::sys::fs::exists(TestPath.c_str())) {
297         WindowsSDKLibVersion = Test;
298         break;
299       }
300     }
301     return !WindowsSDKLibVersion.empty();
302   }
303   if (Major == 10) {
304     if (!getWindows10SDKVersion(Path, WindowsSDKIncludeVersion))
305       return false;
306     WindowsSDKLibVersion = WindowsSDKIncludeVersion;
307     return true;
308   }
309   // Unsupported SDK version
310   return false;
311 }
312
313 // Gets the library path required to link against the Windows SDK.
314 bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const {
315   std::string sdkPath;
316   int sdkMajor = 0;
317   std::string windowsSDKIncludeVersion;
318   std::string windowsSDKLibVersion;
319
320   path.clear();
321   if (!getWindowsSDKDir(sdkPath, sdkMajor, windowsSDKIncludeVersion,
322                         windowsSDKLibVersion))
323     return false;
324
325   llvm::SmallString<128> libPath(sdkPath);
326   llvm::sys::path::append(libPath, "Lib");
327   if (sdkMajor <= 7) {
328     switch (getArch()) {
329     // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
330     case llvm::Triple::x86:
331       break;
332     case llvm::Triple::x86_64:
333       llvm::sys::path::append(libPath, "x64");
334       break;
335     case llvm::Triple::arm:
336       // It is not necessary to link against Windows SDK 7.x when targeting ARM.
337       return false;
338     default:
339       return false;
340     }
341   } else {
342     const StringRef archName = getWindowsSDKArch(getArch());
343     if (archName.empty())
344       return false;
345     llvm::sys::path::append(libPath, windowsSDKLibVersion, "um", archName);
346   }
347
348   path = libPath.str();
349   return true;
350 }
351
352 // Check if the Include path of a specified version of Visual Studio contains
353 // specific header files. If not, they are probably shipped with Universal CRT.
354 bool clang::driver::toolchains::MSVCToolChain::useUniversalCRT(
355     std::string &VisualStudioDir) const {
356   llvm::SmallString<128> TestPath(VisualStudioDir);
357   llvm::sys::path::append(TestPath, "VC\\include\\stdlib.h");
358
359   return !llvm::sys::fs::exists(TestPath);
360 }
361
362 bool MSVCToolChain::getUniversalCRTSdkDir(std::string &Path,
363                                           std::string &UCRTVersion) const {
364   // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry
365   // for the specific key "KitsRoot10". So do we.
366   if (!getSystemRegistryString(
367           "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10",
368           Path, nullptr))
369     return false;
370
371   return getWindows10SDKVersion(Path, UCRTVersion);
372 }
373
374 bool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const {
375   std::string UniversalCRTSdkPath;
376   std::string UCRTVersion;
377
378   Path.clear();
379   if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion))
380     return false;
381
382   StringRef ArchName = getWindowsSDKArch(getArch());
383   if (ArchName.empty())
384     return false;
385
386   llvm::SmallString<128> LibPath(UniversalCRTSdkPath);
387   llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName);
388
389   Path = LibPath.str();
390   return true;
391 }
392
393 // Get the location to use for Visual Studio binaries.  The location priority
394 // is: %VCINSTALLDIR% > %PATH% > newest copy of Visual Studio installed on
395 // system (as reported by the registry).
396 bool MSVCToolChain::getVisualStudioBinariesFolder(const char *clangProgramPath,
397                                                   std::string &path) const {
398   path.clear();
399
400   SmallString<128> BinDir;
401
402   // First check the environment variables that vsvars32.bat sets.
403   llvm::Optional<std::string> VcInstallDir =
404       llvm::sys::Process::GetEnv("VCINSTALLDIR");
405   if (VcInstallDir.hasValue()) {
406     BinDir = VcInstallDir.getValue();
407     llvm::sys::path::append(BinDir, "bin");
408   } else {
409     // Next walk the PATH, trying to find a cl.exe in the path.  If we find one,
410     // use that.  However, make sure it's not clang's cl.exe.
411     llvm::Optional<std::string> OptPath = llvm::sys::Process::GetEnv("PATH");
412     if (OptPath.hasValue()) {
413       const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
414       SmallVector<StringRef, 8> PathSegments;
415       llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr);
416
417       for (StringRef PathSegment : PathSegments) {
418         if (PathSegment.empty())
419           continue;
420
421         SmallString<128> FilePath(PathSegment);
422         llvm::sys::path::append(FilePath, "cl.exe");
423         // Checking if cl.exe exists is a small optimization over calling
424         // can_execute, which really only checks for existence but will also do
425         // extra checks for cl.exe.exe.  These add up when walking a long path.
426         if (llvm::sys::fs::exists(FilePath.c_str()) &&
427             !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) {
428           // If we found it on the PATH, use it exactly as is with no
429           // modifications.
430           path = PathSegment;
431           return true;
432         }
433       }
434     }
435
436     std::string installDir;
437     // With no VCINSTALLDIR and nothing on the PATH, if we can't find it in the
438     // registry then we have no choice but to fail.
439     if (!getVisualStudioInstallDir(installDir))
440       return false;
441
442     // Regardless of what binary we're ultimately trying to find, we make sure
443     // that this is a Visual Studio directory by checking for cl.exe.  We use
444     // cl.exe instead of other binaries like link.exe because programs such as
445     // GnuWin32 also have a utility called link.exe, so cl.exe is the least
446     // ambiguous.
447     BinDir = installDir;
448     llvm::sys::path::append(BinDir, "VC", "bin");
449     SmallString<128> ClPath(BinDir);
450     llvm::sys::path::append(ClPath, "cl.exe");
451
452     if (!llvm::sys::fs::can_execute(ClPath.c_str()))
453       return false;
454   }
455
456   if (BinDir.empty())
457     return false;
458
459   switch (getArch()) {
460   case llvm::Triple::x86:
461     break;
462   case llvm::Triple::x86_64:
463     llvm::sys::path::append(BinDir, "amd64");
464     break;
465   case llvm::Triple::arm:
466     llvm::sys::path::append(BinDir, "arm");
467     break;
468   default:
469     // Whatever this is, Visual Studio doesn't have a toolchain for it.
470     return false;
471   }
472   path = BinDir.str();
473   return true;
474 }
475
476 VersionTuple MSVCToolChain::getMSVCVersionFromTriple() const {
477   unsigned Major, Minor, Micro;
478   getTriple().getEnvironmentVersion(Major, Minor, Micro);
479   if (Major || Minor || Micro)
480     return VersionTuple(Major, Minor, Micro);
481   return VersionTuple();
482 }
483
484 VersionTuple MSVCToolChain::getMSVCVersionFromExe() const {
485   VersionTuple Version;
486 #ifdef USE_WIN32
487   std::string BinPath;
488   if (!getVisualStudioBinariesFolder("", BinPath))
489     return Version;
490   SmallString<128> ClExe(BinPath);
491   llvm::sys::path::append(ClExe, "cl.exe");
492
493   std::wstring ClExeWide;
494   if (!llvm::ConvertUTF8toWide(ClExe.c_str(), ClExeWide))
495     return Version;
496
497   const DWORD VersionSize = ::GetFileVersionInfoSizeW(ClExeWide.c_str(),
498                                                       nullptr);
499   if (VersionSize == 0)
500     return Version;
501
502   SmallVector<uint8_t, 4 * 1024> VersionBlock(VersionSize);
503   if (!::GetFileVersionInfoW(ClExeWide.c_str(), 0, VersionSize,
504                              VersionBlock.data()))
505     return Version;
506
507   VS_FIXEDFILEINFO *FileInfo = nullptr;
508   UINT FileInfoSize = 0;
509   if (!::VerQueryValueW(VersionBlock.data(), L"\\",
510                         reinterpret_cast<LPVOID *>(&FileInfo), &FileInfoSize) ||
511       FileInfoSize < sizeof(*FileInfo))
512     return Version;
513
514   const unsigned Major = (FileInfo->dwFileVersionMS >> 16) & 0xFFFF;
515   const unsigned Minor = (FileInfo->dwFileVersionMS      ) & 0xFFFF;
516   const unsigned Micro = (FileInfo->dwFileVersionLS >> 16) & 0xFFFF;
517
518   Version = VersionTuple(Major, Minor, Micro);
519 #endif
520   return Version;
521 }
522
523 // Get Visual Studio installation directory.
524 bool MSVCToolChain::getVisualStudioInstallDir(std::string &path) const {
525   // First check the environment variables that vsvars32.bat sets.
526   if (llvm::Optional<std::string> VcInstallDir =
527           llvm::sys::Process::GetEnv("VCINSTALLDIR")) {
528     path = std::move(*VcInstallDir);
529     path = path.substr(0, path.find("\\VC"));
530     return true;
531   }
532
533   std::string vsIDEInstallDir;
534   std::string vsExpressIDEInstallDir;
535   // Then try the windows registry.
536   bool hasVCDir =
537       getSystemRegistryString("SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
538                               "InstallDir", vsIDEInstallDir, nullptr);
539   if (hasVCDir && !vsIDEInstallDir.empty()) {
540     path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find("\\Common7\\IDE"));
541     return true;
542   }
543
544   bool hasVCExpressDir =
545       getSystemRegistryString("SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
546                               "InstallDir", vsExpressIDEInstallDir, nullptr);
547   if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) {
548     path = vsExpressIDEInstallDir.substr(
549         0, vsIDEInstallDir.find("\\Common7\\IDE"));
550     return true;
551   }
552
553   // Try the environment.
554   std::string vcomntools;
555   if (llvm::Optional<std::string> vs120comntools =
556           llvm::sys::Process::GetEnv("VS120COMNTOOLS"))
557     vcomntools = std::move(*vs120comntools);
558   else if (llvm::Optional<std::string> vs100comntools =
559                llvm::sys::Process::GetEnv("VS100COMNTOOLS"))
560     vcomntools = std::move(*vs100comntools);
561   else if (llvm::Optional<std::string> vs90comntools =
562                llvm::sys::Process::GetEnv("VS90COMNTOOLS"))
563     vcomntools = std::move(*vs90comntools);
564   else if (llvm::Optional<std::string> vs80comntools =
565                llvm::sys::Process::GetEnv("VS80COMNTOOLS"))
566     vcomntools = std::move(*vs80comntools);
567
568   // Find any version we can.
569   if (!vcomntools.empty()) {
570     size_t p = vcomntools.find("\\Common7\\Tools");
571     if (p != std::string::npos)
572       vcomntools.resize(p);
573     path = std::move(vcomntools);
574     return true;
575   }
576   return false;
577 }
578
579 void MSVCToolChain::AddSystemIncludeWithSubfolder(
580     const ArgList &DriverArgs, ArgStringList &CC1Args,
581     const std::string &folder, const Twine &subfolder1, const Twine &subfolder2,
582     const Twine &subfolder3) const {
583   llvm::SmallString<128> path(folder);
584   llvm::sys::path::append(path, subfolder1, subfolder2, subfolder3);
585   addSystemInclude(DriverArgs, CC1Args, path);
586 }
587
588 void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
589                                               ArgStringList &CC1Args) const {
590   if (DriverArgs.hasArg(options::OPT_nostdinc))
591     return;
592
593   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
594     AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, getDriver().ResourceDir,
595                                   "include");
596   }
597
598   // Add %INCLUDE%-like directories from the -imsvc flag.
599   for (const auto &Path : DriverArgs.getAllArgValues(options::OPT__SLASH_imsvc))
600     addSystemInclude(DriverArgs, CC1Args, Path);
601
602   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
603     return;
604
605   // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
606   if (llvm::Optional<std::string> cl_include_dir =
607           llvm::sys::Process::GetEnv("INCLUDE")) {
608     SmallVector<StringRef, 8> Dirs;
609     StringRef(*cl_include_dir)
610         .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
611     for (StringRef Dir : Dirs)
612       addSystemInclude(DriverArgs, CC1Args, Dir);
613     if (!Dirs.empty())
614       return;
615   }
616
617   std::string VSDir;
618
619   // When built with access to the proper Windows APIs, try to actually find
620   // the correct include paths first.
621   if (getVisualStudioInstallDir(VSDir)) {
622     AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, VSDir, "VC\\include");
623
624     if (useUniversalCRT(VSDir)) {
625       std::string UniversalCRTSdkPath;
626       std::string UCRTVersion;
627       if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) {
628         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, UniversalCRTSdkPath,
629                                       "Include", UCRTVersion, "ucrt");
630       }
631     }
632
633     std::string WindowsSDKDir;
634     int major;
635     std::string windowsSDKIncludeVersion;
636     std::string windowsSDKLibVersion;
637     if (getWindowsSDKDir(WindowsSDKDir, major, windowsSDKIncludeVersion,
638                          windowsSDKLibVersion)) {
639       if (major >= 8) {
640         // Note: windowsSDKIncludeVersion is empty for SDKs prior to v10.
641         // Anyway, llvm::sys::path::append is able to manage it.
642         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
643                                       "include", windowsSDKIncludeVersion,
644                                       "shared");
645         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
646                                       "include", windowsSDKIncludeVersion,
647                                       "um");
648         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
649                                       "include", windowsSDKIncludeVersion,
650                                       "winrt");
651       } else {
652         AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir,
653                                       "include");
654       }
655     } else {
656       addSystemInclude(DriverArgs, CC1Args, VSDir);
657     }
658     return;
659   }
660
661 #if defined(LLVM_ON_WIN32)
662   // As a fallback, select default install paths.
663   // FIXME: Don't guess drives and paths like this on Windows.
664   const StringRef Paths[] = {
665     "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
666     "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
667     "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
668     "C:/Program Files/Microsoft Visual Studio 8/VC/include",
669     "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
670   };
671   addSystemIncludes(DriverArgs, CC1Args, Paths);
672 #endif
673 }
674
675 void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
676                                                  ArgStringList &CC1Args) const {
677   // FIXME: There should probably be logic here to find libc++ on Windows.
678 }
679
680 VersionTuple MSVCToolChain::computeMSVCVersion(const Driver *D,
681                                                const ArgList &Args) const {
682   bool IsWindowsMSVC = getTriple().isWindowsMSVCEnvironment();
683   VersionTuple MSVT = ToolChain::computeMSVCVersion(D, Args);
684   if (MSVT.empty()) MSVT = getMSVCVersionFromTriple();
685   if (MSVT.empty() && IsWindowsMSVC) MSVT = getMSVCVersionFromExe();
686   if (MSVT.empty() &&
687       Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
688                    IsWindowsMSVC)) {
689     // -fms-compatibility-version=18.00 is default.
690     // FIXME: Consider bumping this to 19 (MSVC2015) soon.
691     MSVT = VersionTuple(18);
692   }
693   return MSVT;
694 }
695
696 std::string
697 MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
698                                            types::ID InputType) const {
699   // The MSVC version doesn't care about the architecture, even though it
700   // may look at the triple internally.
701   VersionTuple MSVT = computeMSVCVersion(/*D=*/nullptr, Args);
702   MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0),
703                       MSVT.getSubminor().getValueOr(0));
704
705   // For the rest of the triple, however, a computed architecture name may
706   // be needed.
707   llvm::Triple Triple(ToolChain::ComputeEffectiveClangTriple(Args, InputType));
708   if (Triple.getEnvironment() == llvm::Triple::MSVC) {
709     StringRef ObjFmt = Triple.getEnvironmentName().split('-').second;
710     if (ObjFmt.empty())
711       Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str());
712     else
713       Triple.setEnvironmentName(
714           (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str());
715   }
716   return Triple.getTriple();
717 }
718
719 SanitizerMask MSVCToolChain::getSupportedSanitizers() const {
720   SanitizerMask Res = ToolChain::getSupportedSanitizers();
721   Res |= SanitizerKind::Address;
722   return Res;
723 }
724
725 static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL,
726                             bool SupportsForcingFramePointer,
727                             const char *ExpandChar, const OptTable &Opts) {
728   assert(A->getOption().matches(options::OPT__SLASH_O));
729
730   StringRef OptStr = A->getValue();
731   for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
732     const char &OptChar = *(OptStr.data() + I);
733     switch (OptChar) {
734     default:
735       break;
736     case '1':
737     case '2':
738     case 'x':
739     case 'd':
740       if (&OptChar == ExpandChar) {
741         if (OptChar == 'd') {
742           DAL.AddFlagArg(A, Opts.getOption(options::OPT_O0));
743         } else {
744           if (OptChar == '1') {
745             DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
746           } else if (OptChar == '2' || OptChar == 'x') {
747             DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
748             DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
749           }
750           if (SupportsForcingFramePointer &&
751               !DAL.hasArgNoClaim(options::OPT_fno_omit_frame_pointer))
752             DAL.AddFlagArg(A,
753                            Opts.getOption(options::OPT_fomit_frame_pointer));
754           if (OptChar == '1' || OptChar == '2')
755             DAL.AddFlagArg(A,
756                            Opts.getOption(options::OPT_ffunction_sections));
757         }
758       }
759       break;
760     case 'b':
761       if (I + 1 != E && isdigit(OptStr[I + 1])) {
762         switch (OptStr[I + 1]) {
763         case '0':
764           DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_inline));
765           break;
766         case '1':
767           DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_hint_functions));
768           break;
769         case '2':
770           DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_functions));
771           break;
772         }
773         ++I;
774       }
775       break;
776     case 'g':
777       break;
778     case 'i':
779       if (I + 1 != E && OptStr[I + 1] == '-') {
780         ++I;
781         DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin));
782       } else {
783         DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
784       }
785       break;
786     case 's':
787       DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s");
788       break;
789     case 't':
790       DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
791       break;
792     case 'y': {
793       bool OmitFramePointer = true;
794       if (I + 1 != E && OptStr[I + 1] == '-') {
795         OmitFramePointer = false;
796         ++I;
797       }
798       if (SupportsForcingFramePointer) {
799         if (OmitFramePointer)
800           DAL.AddFlagArg(A,
801                          Opts.getOption(options::OPT_fomit_frame_pointer));
802         else
803           DAL.AddFlagArg(
804               A, Opts.getOption(options::OPT_fno_omit_frame_pointer));
805       } else {
806         // Don't warn about /Oy- in 64-bit builds (where
807         // SupportsForcingFramePointer is false).  The flag having no effect
808         // there is a compiler-internal optimization, and people shouldn't have
809         // to special-case their build files for 64-bit clang-cl.
810         A->claim();
811       }
812       break;
813     }
814     }
815   }
816 }
817
818 static void TranslateDArg(Arg *A, llvm::opt::DerivedArgList &DAL,
819                           const OptTable &Opts) {
820   assert(A->getOption().matches(options::OPT_D));
821
822   StringRef Val = A->getValue();
823   size_t Hash = Val.find('#');
824   if (Hash == StringRef::npos || Hash > Val.find('=')) {
825     DAL.append(A);
826     return;
827   }
828
829   std::string NewVal = Val;
830   NewVal[Hash] = '=';
831   DAL.AddJoinedArg(A, Opts.getOption(options::OPT_D), NewVal);
832 }
833
834 llvm::opt::DerivedArgList *
835 MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
836                              StringRef BoundArch, Action::OffloadKind) const {
837   DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
838   const OptTable &Opts = getDriver().getOpts();
839
840   // /Oy and /Oy- only has an effect under X86-32.
841   bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86;
842
843   // The -O[12xd] flag actually expands to several flags.  We must desugar the
844   // flags so that options embedded can be negated.  For example, the '-O2' flag
845   // enables '-Oy'.  Expanding '-O2' into its constituent flags allows us to
846   // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single
847   // aspect of '-O2'.
848   //
849   // Note that this expansion logic only applies to the *last* of '[12xd]'.
850
851   // First step is to search for the character we'd like to expand.
852   const char *ExpandChar = nullptr;
853   for (Arg *A : Args) {
854     if (!A->getOption().matches(options::OPT__SLASH_O))
855       continue;
856     StringRef OptStr = A->getValue();
857     for (size_t I = 0, E = OptStr.size(); I != E; ++I) {
858       char OptChar = OptStr[I];
859       char PrevChar = I > 0 ? OptStr[I - 1] : '0';
860       if (PrevChar == 'b') {
861         // OptChar does not expand; it's an argument to the previous char.
862         continue;
863       }
864       if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd')
865         ExpandChar = OptStr.data() + I;
866     }
867   }
868
869   for (Arg *A : Args) {
870     if (A->getOption().matches(options::OPT__SLASH_O)) {
871       // The -O flag actually takes an amalgam of other options.  For example,
872       // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'.
873       TranslateOptArg(A, *DAL, SupportsForcingFramePointer, ExpandChar, Opts);
874     } else if (A->getOption().matches(options::OPT_D)) {
875       // Translate -Dfoo#bar into -Dfoo=bar.
876       TranslateDArg(A, *DAL, Opts);
877     } else {
878       DAL->append(A);
879     }
880   }
881
882   return DAL;
883 }