199768b24SSam McCall //===--- CompileCommands.cpp ----------------------------------------------===// 299768b24SSam McCall // 399768b24SSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 499768b24SSam McCall // See https://llvm.org/LICENSE.txt for license information. 599768b24SSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 699768b24SSam McCall // 799768b24SSam McCall //===----------------------------------------------------------------------===// 899768b24SSam McCall 999768b24SSam McCall #include "CompileCommands.h" 10c5a6ee16SSam McCall #include "Config.h" 11ad97ccf6SSam McCall #include "support/Logger.h" 12ab714ba0SKadir Cetinkaya #include "support/Trace.h" 13ab714ba0SKadir Cetinkaya #include "clang/Driver/Driver.h" 148eb8c92eSSam McCall #include "clang/Driver/Options.h" 1599768b24SSam McCall #include "clang/Frontend/CompilerInvocation.h" 164d006520SSam McCall #include "clang/Tooling/CompilationDatabase.h" 17d60d3455SDmitry Polukhin #include "clang/Tooling/Tooling.h" 18ab714ba0SKadir Cetinkaya #include "llvm/ADT/ArrayRef.h" 190a3c7960SKadir Cetinkaya #include "llvm/ADT/STLExtras.h" 20ab714ba0SKadir Cetinkaya #include "llvm/ADT/SmallVector.h" 210a3c7960SKadir Cetinkaya #include "llvm/ADT/StringRef.h" 22ab714ba0SKadir Cetinkaya #include "llvm/Option/ArgList.h" 238eb8c92eSSam McCall #include "llvm/Option/Option.h" 248eb8c92eSSam McCall #include "llvm/Support/Allocator.h" 258eb8c92eSSam McCall #include "llvm/Support/Debug.h" 2699768b24SSam McCall #include "llvm/Support/FileSystem.h" 2799768b24SSam McCall #include "llvm/Support/FileUtilities.h" 2899768b24SSam McCall #include "llvm/Support/MemoryBuffer.h" 2999768b24SSam McCall #include "llvm/Support/Path.h" 3099768b24SSam McCall #include "llvm/Support/Program.h" 310a3c7960SKadir Cetinkaya #include <iterator> 32f885ae23SMatt Arsenault #include <optional> 33b6626515SKadir Cetinkaya #include <string> 34b6626515SKadir Cetinkaya #include <vector> 3599768b24SSam McCall 3699768b24SSam McCall namespace clang { 3799768b24SSam McCall namespace clangd { 3899768b24SSam McCall namespace { 3999768b24SSam McCall 4099768b24SSam McCall // Query apple's `xcrun` launcher, which is the source of truth for "how should" 4199768b24SSam McCall // clang be invoked on this system. 42f71ffd3bSKazu Hirata std::optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) { 4399768b24SSam McCall auto Xcrun = llvm::sys::findProgramByName("xcrun"); 4499768b24SSam McCall if (!Xcrun) { 4599768b24SSam McCall log("Couldn't find xcrun. Hopefully you have a non-apple toolchain..."); 46059a23c0SKazu Hirata return std::nullopt; 4799768b24SSam McCall } 4899768b24SSam McCall llvm::SmallString<64> OutFile; 4999768b24SSam McCall llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile); 5099768b24SSam McCall llvm::FileRemover OutRemover(OutFile); 51f4fb2b30SFangrui Song std::optional<llvm::StringRef> Redirects[3] = { 529878e176SAaron Ballman /*stdin=*/{""}, /*stdout=*/{OutFile.str()}, /*stderr=*/{""}}; 5399768b24SSam McCall vlog("Invoking {0} to find clang installation", *Xcrun); 5499768b24SSam McCall int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv, 55f4fb2b30SFangrui Song /*Env=*/std::nullopt, Redirects, 5699768b24SSam McCall /*SecondsToWait=*/10); 5799768b24SSam McCall if (Ret != 0) { 5899768b24SSam McCall log("xcrun exists but failed with code {0}. " 5999768b24SSam McCall "If you have a non-apple toolchain, this is OK. " 6099768b24SSam McCall "Otherwise, try xcode-select --install.", 6199768b24SSam McCall Ret); 62059a23c0SKazu Hirata return std::nullopt; 6399768b24SSam McCall } 6499768b24SSam McCall 6599768b24SSam McCall auto Buf = llvm::MemoryBuffer::getFile(OutFile); 6699768b24SSam McCall if (!Buf) { 6799768b24SSam McCall log("Can't read xcrun output: {0}", Buf.getError().message()); 68059a23c0SKazu Hirata return std::nullopt; 6999768b24SSam McCall } 7099768b24SSam McCall StringRef Path = Buf->get()->getBuffer().trim(); 7199768b24SSam McCall if (Path.empty()) { 7299768b24SSam McCall log("xcrun produced no output"); 73059a23c0SKazu Hirata return std::nullopt; 7499768b24SSam McCall } 7599768b24SSam McCall return Path.str(); 7699768b24SSam McCall } 7799768b24SSam McCall 7899768b24SSam McCall // Resolve symlinks if possible. 7999768b24SSam McCall std::string resolve(std::string Path) { 8099768b24SSam McCall llvm::SmallString<128> Resolved; 8199768b24SSam McCall if (llvm::sys::fs::real_path(Path, Resolved)) { 8299768b24SSam McCall log("Failed to resolve possible symlink {0}", Path); 8399768b24SSam McCall return Path; 8499768b24SSam McCall } 855b2772e1SKazu Hirata return std::string(Resolved); 8699768b24SSam McCall } 8799768b24SSam McCall 8899768b24SSam McCall // Get a plausible full `clang` path. 8999768b24SSam McCall // This is used in the fallback compile command, or when the CDB returns a 9099768b24SSam McCall // generic driver with no path. 9199768b24SSam McCall std::string detectClangPath() { 9299768b24SSam McCall // The driver and/or cc1 sometimes depend on the binary name to compute 9399768b24SSam McCall // useful things like the standard library location. 9499768b24SSam McCall // We need to emulate what clang on this system is likely to see. 9599768b24SSam McCall // cc1 in particular looks at the "real path" of the running process, and 9699768b24SSam McCall // so if /usr/bin/clang is a symlink, it sees the resolved path. 9799768b24SSam McCall // clangd doesn't have that luxury, so we resolve symlinks ourselves. 9899768b24SSam McCall 9999768b24SSam McCall // On Mac, `which clang` is /usr/bin/clang. It runs `xcrun clang`, which knows 10099768b24SSam McCall // where the real clang is kept. We need to do the same thing, 10199768b24SSam McCall // because cc1 (not the driver!) will find libc++ relative to argv[0]. 10299768b24SSam McCall #ifdef __APPLE__ 10399768b24SSam McCall if (auto MacClang = queryXcrun({"xcrun", "--find", "clang"})) 10499768b24SSam McCall return resolve(std::move(*MacClang)); 10599768b24SSam McCall #endif 10699768b24SSam McCall // On other platforms, just look for compilers on the PATH. 10799768b24SSam McCall for (const char *Name : {"clang", "gcc", "cc"}) 10899768b24SSam McCall if (auto PathCC = llvm::sys::findProgramByName(Name)) 10999768b24SSam McCall return resolve(std::move(*PathCC)); 11099768b24SSam McCall // Fallback: a nonexistent 'clang' binary next to clangd. 111f71404c3SKadir Cetinkaya static int StaticForMainAddr; 11299768b24SSam McCall std::string ClangdExecutable = 113f71404c3SKadir Cetinkaya llvm::sys::fs::getMainExecutable("clangd", (void *)&StaticForMainAddr); 11499768b24SSam McCall SmallString<128> ClangPath; 11599768b24SSam McCall ClangPath = llvm::sys::path::parent_path(ClangdExecutable); 11699768b24SSam McCall llvm::sys::path::append(ClangPath, "clang"); 1175b2772e1SKazu Hirata return std::string(ClangPath); 11899768b24SSam McCall } 11999768b24SSam McCall 12099768b24SSam McCall // On mac, /usr/bin/clang sets SDKROOT and then invokes the real clang. 12199768b24SSam McCall // The effect of this is to set -isysroot correctly. We do the same. 122f71ffd3bSKazu Hirata std::optional<std::string> detectSysroot() { 12399768b24SSam McCall #ifndef __APPLE__ 124059a23c0SKazu Hirata return std::nullopt; 12599768b24SSam McCall #endif 12699768b24SSam McCall 12799768b24SSam McCall // SDKROOT overridden in environment, respect it. Driver will set isysroot. 12899768b24SSam McCall if (::getenv("SDKROOT")) 129059a23c0SKazu Hirata return std::nullopt; 13099768b24SSam McCall return queryXcrun({"xcrun", "--show-sdk-path"}); 13199768b24SSam McCall } 13299768b24SSam McCall 13399768b24SSam McCall std::string detectStandardResourceDir() { 134f71404c3SKadir Cetinkaya static int StaticForMainAddr; // Just an address in this process. 135f71404c3SKadir Cetinkaya return CompilerInvocation::GetResourcesPath("clangd", 136f71404c3SKadir Cetinkaya (void *)&StaticForMainAddr); 13799768b24SSam McCall } 13899768b24SSam McCall 1392a3ac01bSSam McCall // The path passed to argv[0] is important: 1402a3ac01bSSam McCall // - its parent directory is Driver::Dir, used for library discovery 1412a3ac01bSSam McCall // - its basename affects CLI parsing (clang-cl) and other settings 1422a3ac01bSSam McCall // Where possible it should be an absolute path with sensible directory, but 1432a3ac01bSSam McCall // with the original basename. 1442a3ac01bSSam McCall static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink, 145f71ffd3bSKazu Hirata std::optional<std::string> ClangPath) { 1462a3ac01bSSam McCall auto SiblingOf = [&](llvm::StringRef AbsPath) { 1472a3ac01bSSam McCall llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath); 1482a3ac01bSSam McCall llvm::sys::path::append(Result, llvm::sys::path::filename(Driver)); 1492a3ac01bSSam McCall return Result.str().str(); 1502a3ac01bSSam McCall }; 1512a3ac01bSSam McCall 1522a3ac01bSSam McCall // First, eliminate relative paths. 1532a3ac01bSSam McCall std::string Storage; 1542a3ac01bSSam McCall if (!llvm::sys::path::is_absolute(Driver)) { 155a3b79340SSam McCall // If it's working-dir relative like bin/clang, we can't resolve it. 156a3b79340SSam McCall // FIXME: we could if we had the working directory here. 157a3b79340SSam McCall // Let's hope it's not a symlink. 158a3b79340SSam McCall if (llvm::any_of(Driver, 159a3b79340SSam McCall [](char C) { return llvm::sys::path::is_separator(C); })) 160a3b79340SSam McCall return Driver.str(); 1612a3ac01bSSam McCall // If the driver is a generic like "g++" with no path, add clang dir. 1622a3ac01bSSam McCall if (ClangPath && 1632a3ac01bSSam McCall (Driver == "clang" || Driver == "clang++" || Driver == "gcc" || 1642a3ac01bSSam McCall Driver == "g++" || Driver == "cc" || Driver == "c++")) { 1652a3ac01bSSam McCall return SiblingOf(*ClangPath); 1662a3ac01bSSam McCall } 1672a3ac01bSSam McCall // Otherwise try to look it up on PATH. This won't change basename. 1682a3ac01bSSam McCall auto Absolute = llvm::sys::findProgramByName(Driver); 1692a3ac01bSSam McCall if (Absolute && llvm::sys::path::is_absolute(*Absolute)) 1702a3ac01bSSam McCall Driver = Storage = std::move(*Absolute); 1712a3ac01bSSam McCall else if (ClangPath) // If we don't find it, use clang dir again. 1722a3ac01bSSam McCall return SiblingOf(*ClangPath); 1732a3ac01bSSam McCall else // Nothing to do: can't find the command and no detected dir. 1742a3ac01bSSam McCall return Driver.str(); 1752a3ac01bSSam McCall } 1762a3ac01bSSam McCall 1772a3ac01bSSam McCall // Now we have an absolute path, but it may be a symlink. 1782a3ac01bSSam McCall assert(llvm::sys::path::is_absolute(Driver)); 1792a3ac01bSSam McCall if (FollowSymlink) { 1802a3ac01bSSam McCall llvm::SmallString<256> Resolved; 1812a3ac01bSSam McCall if (!llvm::sys::fs::real_path(Driver, Resolved)) 1822a3ac01bSSam McCall return SiblingOf(Resolved); 1832a3ac01bSSam McCall } 1842a3ac01bSSam McCall return Driver.str(); 1852a3ac01bSSam McCall } 1862a3ac01bSSam McCall 18799768b24SSam McCall } // namespace 18899768b24SSam McCall 18999768b24SSam McCall CommandMangler CommandMangler::detect() { 19099768b24SSam McCall CommandMangler Result; 19199768b24SSam McCall Result.ClangPath = detectClangPath(); 19299768b24SSam McCall Result.ResourceDir = detectStandardResourceDir(); 19399768b24SSam McCall Result.Sysroot = detectSysroot(); 19499768b24SSam McCall return Result; 19599768b24SSam McCall } 19699768b24SSam McCall 197ab714ba0SKadir Cetinkaya CommandMangler CommandMangler::forTests() { return CommandMangler(); } 19899768b24SSam McCall 199afa22c56SNathan Ridge void CommandMangler::operator()(tooling::CompileCommand &Command, 200259e365dSKadir Cetinkaya llvm::StringRef File) const { 201afa22c56SNathan Ridge std::vector<std::string> &Cmd = Command.CommandLine; 202ab714ba0SKadir Cetinkaya trace::Span S("AdjustCompileFlags"); 203444a5f30SKadir Cetinkaya // Most of the modifications below assumes the Cmd starts with a driver name. 204444a5f30SKadir Cetinkaya // We might consider injecting a generic driver name like "cc" or "c++", but 205d60d3455SDmitry Polukhin // a Cmd missing the driver is probably rare enough in practice and erroneous. 206444a5f30SKadir Cetinkaya if (Cmd.empty()) 207444a5f30SKadir Cetinkaya return; 208d60d3455SDmitry Polukhin 209ab714ba0SKadir Cetinkaya auto &OptTable = clang::driver::getDriverOptTable(); 210ab714ba0SKadir Cetinkaya // OriginalArgs needs to outlive ArgList. 211ab714ba0SKadir Cetinkaya llvm::SmallVector<const char *, 16> OriginalArgs; 212ab714ba0SKadir Cetinkaya OriginalArgs.reserve(Cmd.size()); 213ab714ba0SKadir Cetinkaya for (const auto &S : Cmd) 214ab714ba0SKadir Cetinkaya OriginalArgs.push_back(S.c_str()); 215444a5f30SKadir Cetinkaya bool IsCLMode = driver::IsClangCL(driver::getDriverMode( 216984b800aSserge-sans-paille OriginalArgs[0], llvm::ArrayRef(OriginalArgs).slice(1))); 217d60d3455SDmitry Polukhin // ParseArgs propagates missing arg/opt counts on error, but preserves 218ab714ba0SKadir Cetinkaya // everything it could parse in ArgList. So we just ignore those counts. 219ab714ba0SKadir Cetinkaya unsigned IgnoredCount; 220259e365dSKadir Cetinkaya // Drop the executable name, as ParseArgs doesn't expect it. This means 221259e365dSKadir Cetinkaya // indices are actually of by one between ArgList and OriginalArgs. 222444a5f30SKadir Cetinkaya llvm::opt::InputArgList ArgList; 223444a5f30SKadir Cetinkaya ArgList = OptTable.ParseArgs( 224984b800aSserge-sans-paille llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount, 2259478f661SJustin Bogner llvm::opt::Visibility(IsCLMode ? driver::options::CLOption 2269478f661SJustin Bogner : driver::options::ClangOption)); 227ab714ba0SKadir Cetinkaya 2283bf77980SKadir Cetinkaya llvm::SmallVector<unsigned, 1> IndicesToDrop; 2293bf77980SKadir Cetinkaya // Having multiple architecture options (e.g. when building fat binaries) 2303bf77980SKadir Cetinkaya // results in multiple compiler jobs, which clangd cannot handle. In such 2313bf77980SKadir Cetinkaya // cases strip all the `-arch` options and fallback to default architecture. 2323bf77980SKadir Cetinkaya // As there are no signals to figure out which one user actually wants. They 2333bf77980SKadir Cetinkaya // can explicitly specify one through `CompileFlags.Add` if need be. 2343bf77980SKadir Cetinkaya unsigned ArchOptCount = 0; 2353bf77980SKadir Cetinkaya for (auto *Input : ArgList.filtered(driver::options::OPT_arch)) { 2363bf77980SKadir Cetinkaya ++ArchOptCount; 2373bf77980SKadir Cetinkaya for (auto I = 0U; I <= Input->getNumValues(); ++I) 2383bf77980SKadir Cetinkaya IndicesToDrop.push_back(Input->getIndex() + I); 2393bf77980SKadir Cetinkaya } 2403bf77980SKadir Cetinkaya // If there is a single `-arch` option, keep it. 2413bf77980SKadir Cetinkaya if (ArchOptCount < 2) 2423bf77980SKadir Cetinkaya IndicesToDrop.clear(); 24379c2616dSKadir Cetinkaya 2440683a1e5SSam McCall // In some cases people may try to reuse the command from another file, e.g. 2450683a1e5SSam McCall // { File: "foo.h", CommandLine: "clang foo.cpp" }. 2460683a1e5SSam McCall // We assume the intent is to parse foo.h the same way as foo.cpp, or as if 2470683a1e5SSam McCall // it were being included from foo.cpp. 2480683a1e5SSam McCall // 2490683a1e5SSam McCall // We're going to rewrite the command to refer to foo.h, and this may change 2500683a1e5SSam McCall // its semantics (e.g. by parsing the file as C). If we do this, we should 2510683a1e5SSam McCall // use transferCompileCommand to adjust the argv. 2520683a1e5SSam McCall // In practice only the extension of the file matters, so do this only when 2530683a1e5SSam McCall // it differs. 2540683a1e5SSam McCall llvm::StringRef FileExtension = llvm::sys::path::extension(File); 255f71ffd3bSKazu Hirata std::optional<std::string> TransferFrom; 2560683a1e5SSam McCall auto SawInput = [&](llvm::StringRef Input) { 2570683a1e5SSam McCall if (llvm::sys::path::extension(Input) != FileExtension) 2580683a1e5SSam McCall TransferFrom.emplace(Input); 2590683a1e5SSam McCall }; 2600683a1e5SSam McCall 26179c2616dSKadir Cetinkaya // Strip all the inputs and `--`. We'll put the input for the requested file 26279c2616dSKadir Cetinkaya // explicitly at the end of the flags. This ensures modifications done in the 26379c2616dSKadir Cetinkaya // following steps apply in more cases (like setting -x, which only affects 26479c2616dSKadir Cetinkaya // inputs that come after it). 2650683a1e5SSam McCall for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT)) { 2660683a1e5SSam McCall SawInput(Input->getValue(0)); 267259e365dSKadir Cetinkaya IndicesToDrop.push_back(Input->getIndex()); 2680683a1e5SSam McCall } 26979c2616dSKadir Cetinkaya // Anything after `--` is also treated as input, drop them as well. 27079c2616dSKadir Cetinkaya if (auto *DashDash = 27179c2616dSKadir Cetinkaya ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) { 2720683a1e5SSam McCall auto DashDashIndex = DashDash->getIndex() + 1; // +1 accounts for Cmd[0] 2730683a1e5SSam McCall for (unsigned I = DashDashIndex; I < Cmd.size(); ++I) 2740683a1e5SSam McCall SawInput(Cmd[I]); 2750683a1e5SSam McCall Cmd.resize(DashDashIndex); 2763bf77980SKadir Cetinkaya } 277259e365dSKadir Cetinkaya llvm::sort(IndicesToDrop); 278840a9682SKazu Hirata for (unsigned Idx : llvm::reverse(IndicesToDrop)) 279259e365dSKadir Cetinkaya // +1 to account for the executable name in Cmd[0] that 280259e365dSKadir Cetinkaya // doesn't exist in ArgList. 281840a9682SKazu Hirata Cmd.erase(Cmd.begin() + Idx + 1); 28279c2616dSKadir Cetinkaya // All the inputs are stripped, append the name for the requested file. Rest 28379c2616dSKadir Cetinkaya // of the modifications should respect `--`. 28479c2616dSKadir Cetinkaya Cmd.push_back("--"); 28579c2616dSKadir Cetinkaya Cmd.push_back(File.str()); 286ab714ba0SKadir Cetinkaya 2870683a1e5SSam McCall if (TransferFrom) { 2880683a1e5SSam McCall tooling::CompileCommand TransferCmd; 2890683a1e5SSam McCall TransferCmd.Filename = std::move(*TransferFrom); 2900683a1e5SSam McCall TransferCmd.CommandLine = std::move(Cmd); 2910683a1e5SSam McCall TransferCmd = transferCompileCommand(std::move(TransferCmd), File); 2920683a1e5SSam McCall Cmd = std::move(TransferCmd.CommandLine); 2934258d68dSSam McCall assert(Cmd.size() >= 2 && Cmd.back() == File && 2944258d68dSSam McCall Cmd[Cmd.size() - 2] == "--" && 2954258d68dSSam McCall "TransferCommand should produce a command ending in -- filename"); 2960683a1e5SSam McCall } 2970683a1e5SSam McCall 2989d3e9a3eSSam McCall for (auto &Edit : Config::current().CompileFlags.Edits) 299c5a6ee16SSam McCall Edit(Cmd); 300c5a6ee16SSam McCall 30168e230aaSNathan Ridge // The system include extractor needs to run: 30268e230aaSNathan Ridge // - AFTER transferCompileCommand(), because the -x flag it adds may be 30368e230aaSNathan Ridge // necessary for the system include extractor to identify the file type 30468e230aaSNathan Ridge // - AFTER applying CompileFlags.Edits, because the name of the compiler 30568e230aaSNathan Ridge // that needs to be invoked may come from the CompileFlags->Compiler key 306d60d3455SDmitry Polukhin // - BEFORE addTargetAndModeForProgramName(), because gcc doesn't support 307d60d3455SDmitry Polukhin // the target flag that might be added. 30868e230aaSNathan Ridge // - BEFORE resolveDriver() because that can mess up the driver path, 30968e230aaSNathan Ridge // e.g. changing gcc to /path/to/clang/bin/gcc 31068e230aaSNathan Ridge if (SystemIncludeExtractor) { 31168e230aaSNathan Ridge SystemIncludeExtractor(Command, File); 31268e230aaSNathan Ridge } 31368e230aaSNathan Ridge 314d60d3455SDmitry Polukhin tooling::addTargetAndModeForProgramName(Cmd, Cmd.front()); 315d60d3455SDmitry Polukhin 316f489fb3dSKon // Check whether the flag exists in the command. 317f489fb3dSKon auto HasExact = [&](llvm::StringRef Flag) { 318f489fb3dSKon return llvm::any_of(Cmd, [&](llvm::StringRef Arg) { return Arg == Flag; }); 319f489fb3dSKon }; 320f489fb3dSKon 321f489fb3dSKon // Check whether the flag appears in the command as a prefix. 322f489fb3dSKon auto HasPrefix = [&](llvm::StringRef Flag) { 323f489fb3dSKon return llvm::any_of( 324dabc9018SJie Fu Cmd, [&](llvm::StringRef Arg) { return Arg.starts_with(Flag); }); 32599768b24SSam McCall }; 32699768b24SSam McCall 3270a3c7960SKadir Cetinkaya llvm::erase_if(Cmd, [](llvm::StringRef Elem) { 328d5953e3eSKazu Hirata return Elem.starts_with("--save-temps") || Elem.starts_with("-save-temps"); 3290a3c7960SKadir Cetinkaya }); 33099768b24SSam McCall 331b6626515SKadir Cetinkaya std::vector<std::string> ToAppend; 332f489fb3dSKon if (ResourceDir && !HasExact("-resource-dir") && !HasPrefix("-resource-dir=")) 333b6626515SKadir Cetinkaya ToAppend.push_back(("-resource-dir=" + *ResourceDir)); 33499768b24SSam McCall 335ea9888b8SDavid Goldman // Don't set `-isysroot` if it is already set or if `--sysroot` is set. 336ea9888b8SDavid Goldman // `--sysroot` is a superset of the `-isysroot` argument. 337f489fb3dSKon if (Sysroot && !HasPrefix("-isysroot") && !HasExact("--sysroot") && 338f489fb3dSKon !HasPrefix("--sysroot=")) { 339b6626515SKadir Cetinkaya ToAppend.push_back("-isysroot"); 340b6626515SKadir Cetinkaya ToAppend.push_back(*Sysroot); 341b6626515SKadir Cetinkaya } 342b6626515SKadir Cetinkaya 343b6626515SKadir Cetinkaya if (!ToAppend.empty()) { 3440a3c7960SKadir Cetinkaya Cmd.insert(llvm::find(Cmd, "--"), std::make_move_iterator(ToAppend.begin()), 3450a3c7960SKadir Cetinkaya std::make_move_iterator(ToAppend.end())); 34699768b24SSam McCall } 34799768b24SSam McCall 3482a3ac01bSSam McCall if (!Cmd.empty()) { 349f489fb3dSKon bool FollowSymlink = !HasExact("-no-canonical-prefixes"); 3502a3ac01bSSam McCall Cmd.front() = 3512a3ac01bSSam McCall (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow) 3522a3ac01bSSam McCall .get(Cmd.front(), [&, this] { 3532a3ac01bSSam McCall return resolveDriver(Cmd.front(), FollowSymlink, ClangPath); 3542a3ac01bSSam McCall }); 35599768b24SSam McCall } 35699768b24SSam McCall } 35799768b24SSam McCall 3588eb8c92eSSam McCall // ArgStripper implementation 3598eb8c92eSSam McCall namespace { 3608eb8c92eSSam McCall 3618eb8c92eSSam McCall // Determine total number of args consumed by this option. 3628eb8c92eSSam McCall // Return answers for {Exact, Prefix} match. 0 means not allowed. 3638eb8c92eSSam McCall std::pair<unsigned, unsigned> getArgCount(const llvm::opt::Option &Opt) { 3648eb8c92eSSam McCall constexpr static unsigned Rest = 10000; // Should be all the rest! 3658eb8c92eSSam McCall // Reference is llvm::opt::Option::acceptInternal() 3668eb8c92eSSam McCall using llvm::opt::Option; 3678eb8c92eSSam McCall switch (Opt.getKind()) { 3688eb8c92eSSam McCall case Option::FlagClass: 3698eb8c92eSSam McCall return {1, 0}; 3708eb8c92eSSam McCall case Option::JoinedClass: 3718eb8c92eSSam McCall case Option::CommaJoinedClass: 3728eb8c92eSSam McCall return {1, 1}; 3738eb8c92eSSam McCall case Option::GroupClass: 3748eb8c92eSSam McCall case Option::InputClass: 3758eb8c92eSSam McCall case Option::UnknownClass: 3768eb8c92eSSam McCall case Option::ValuesClass: 3778eb8c92eSSam McCall return {1, 0}; 3788eb8c92eSSam McCall case Option::JoinedAndSeparateClass: 3798eb8c92eSSam McCall return {2, 2}; 3808eb8c92eSSam McCall case Option::SeparateClass: 3818eb8c92eSSam McCall return {2, 0}; 3828eb8c92eSSam McCall case Option::MultiArgClass: 3838eb8c92eSSam McCall return {1 + Opt.getNumArgs(), 0}; 3848eb8c92eSSam McCall case Option::JoinedOrSeparateClass: 3858eb8c92eSSam McCall return {2, 1}; 3868eb8c92eSSam McCall case Option::RemainingArgsClass: 3878eb8c92eSSam McCall return {Rest, 0}; 3888eb8c92eSSam McCall case Option::RemainingArgsJoinedClass: 3898eb8c92eSSam McCall return {Rest, Rest}; 3908eb8c92eSSam McCall } 39127433228SMikael Holmen llvm_unreachable("Unhandled option kind"); 3928eb8c92eSSam McCall } 3938eb8c92eSSam McCall 3948eb8c92eSSam McCall // Flag-parsing mode, which affects which flags are available. 3958eb8c92eSSam McCall enum DriverMode : unsigned char { 3968eb8c92eSSam McCall DM_None = 0, 3978eb8c92eSSam McCall DM_GCC = 1, // Default mode e.g. when invoked as 'clang' 3988eb8c92eSSam McCall DM_CL = 2, // MS CL.exe compatible mode e.g. when invoked as 'clang-cl' 3998eb8c92eSSam McCall DM_CC1 = 4, // When invoked as 'clang -cc1' or after '-Xclang' 4008eb8c92eSSam McCall DM_All = 7 4018eb8c92eSSam McCall }; 4028eb8c92eSSam McCall 4038eb8c92eSSam McCall // Examine args list to determine if we're in GCC, CL-compatible, or cc1 mode. 4048eb8c92eSSam McCall DriverMode getDriverMode(const std::vector<std::string> &Args) { 4058eb8c92eSSam McCall DriverMode Mode = DM_GCC; 4068eb8c92eSSam McCall llvm::StringRef Argv0 = Args.front(); 407ed1539c6SKazu Hirata if (Argv0.ends_with_insensitive(".exe")) 4088eb8c92eSSam McCall Argv0 = Argv0.drop_back(strlen(".exe")); 409ed1539c6SKazu Hirata if (Argv0.ends_with_insensitive("cl")) 4108eb8c92eSSam McCall Mode = DM_CL; 4118eb8c92eSSam McCall for (const llvm::StringRef Arg : Args) { 4128eb8c92eSSam McCall if (Arg == "--driver-mode=cl") { 4138eb8c92eSSam McCall Mode = DM_CL; 4148eb8c92eSSam McCall break; 4158eb8c92eSSam McCall } 4168eb8c92eSSam McCall if (Arg == "-cc1") { 4178eb8c92eSSam McCall Mode = DM_CC1; 4188eb8c92eSSam McCall break; 4198eb8c92eSSam McCall } 4208eb8c92eSSam McCall } 4218eb8c92eSSam McCall return Mode; 4228eb8c92eSSam McCall } 4238eb8c92eSSam McCall 4248eb8c92eSSam McCall // Returns the set of DriverModes where an option may be used. 4258eb8c92eSSam McCall unsigned char getModes(const llvm::opt::Option &Opt) { 4268eb8c92eSSam McCall unsigned char Result = DM_None; 4279478f661SJustin Bogner if (Opt.hasVisibilityFlag(driver::options::ClangOption)) 4288eb8c92eSSam McCall Result |= DM_GCC; 4299478f661SJustin Bogner if (Opt.hasVisibilityFlag(driver::options::CC1Option)) 4309478f661SJustin Bogner Result |= DM_CC1; 4319478f661SJustin Bogner if (Opt.hasVisibilityFlag(driver::options::CLOption)) 4328eb8c92eSSam McCall Result |= DM_CL; 4338eb8c92eSSam McCall return Result; 43427433228SMikael Holmen } 4358eb8c92eSSam McCall 4368eb8c92eSSam McCall } // namespace 4378eb8c92eSSam McCall 4388eb8c92eSSam McCall llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) { 4398eb8c92eSSam McCall // All the hard work is done once in a static initializer. 4408eb8c92eSSam McCall // We compute a table containing strings to look for and #args to skip. 4418eb8c92eSSam McCall // e.g. "-x" => {-x 2 args, -x* 1 arg, --language 2 args, --language=* 1 arg} 4428eb8c92eSSam McCall using TableTy = 4438eb8c92eSSam McCall llvm::StringMap<llvm::SmallVector<Rule, 4>, llvm::BumpPtrAllocator>; 4448eb8c92eSSam McCall static TableTy *Table = [] { 4458eb8c92eSSam McCall auto &DriverTable = driver::getDriverOptTable(); 4468eb8c92eSSam McCall using DriverID = clang::driver::options::ID; 4478eb8c92eSSam McCall 4488eb8c92eSSam McCall // Collect sets of aliases, so we can treat -foo and -foo= as synonyms. 4498eb8c92eSSam McCall // Conceptually a double-linked list: PrevAlias[I] -> I -> NextAlias[I]. 4508eb8c92eSSam McCall // If PrevAlias[I] is INVALID, then I is canonical. 4518eb8c92eSSam McCall DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID}; 4528eb8c92eSSam McCall DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID}; 4538eb8c92eSSam McCall auto AddAlias = [&](DriverID Self, DriverID T) { 4548eb8c92eSSam McCall if (NextAlias[T]) { 4558eb8c92eSSam McCall PrevAlias[NextAlias[T]] = Self; 4568eb8c92eSSam McCall NextAlias[Self] = NextAlias[T]; 4578eb8c92eSSam McCall } 4588eb8c92eSSam McCall PrevAlias[Self] = T; 4598eb8c92eSSam McCall NextAlias[T] = Self; 4608eb8c92eSSam McCall }; 4618eb8c92eSSam McCall 4623b64ab57SYuanfang Chen struct { 4633b64ab57SYuanfang Chen DriverID ID; 4643b64ab57SYuanfang Chen DriverID AliasID; 4654f30a526SYuanfang Chen const void *AliasArgs; 4663b64ab57SYuanfang Chen } AliasTable[] = { 467501f92d3SJan Svoboda #define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \ 468aff197ffSDavid Spickett FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \ 469aff197ffSDavid Spickett METAVAR, VALUES) \ 4704f30a526SYuanfang Chen {DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS}, 4713b64ab57SYuanfang Chen #include "clang/Driver/Options.inc" 4723b64ab57SYuanfang Chen #undef OPTION 4733b64ab57SYuanfang Chen }; 4743b64ab57SYuanfang Chen for (auto &E : AliasTable) 4753b64ab57SYuanfang Chen if (E.AliasID != DriverID::OPT_INVALID && E.AliasArgs == nullptr) 4763b64ab57SYuanfang Chen AddAlias(E.ID, E.AliasID); 4773b64ab57SYuanfang Chen 4788eb8c92eSSam McCall auto Result = std::make_unique<TableTy>(); 4798eb8c92eSSam McCall // Iterate over distinct options (represented by the canonical alias). 4808eb8c92eSSam McCall // Every spelling of this option will get the same set of rules. 4818eb8c92eSSam McCall for (unsigned ID = 1 /*Skip INVALID */; ID < DriverID::LastOption; ++ID) { 4828eb8c92eSSam McCall if (PrevAlias[ID] || ID == DriverID::OPT_Xclang) 4838eb8c92eSSam McCall continue; // Not canonical, or specially handled. 484ee02e20cSKirill Bobyrev llvm::SmallVector<Rule> Rules; 4858eb8c92eSSam McCall // Iterate over each alias, to add rules for parsing it. 4868eb8c92eSSam McCall for (unsigned A = ID; A != DriverID::OPT_INVALID; A = NextAlias[A]) { 487*dd647e3eSChandler Carruth llvm::SmallVector<llvm::StringRef, 4> Prefixes; 488*dd647e3eSChandler Carruth DriverTable.appendOptionPrefixes(A, Prefixes); 489*dd647e3eSChandler Carruth if (Prefixes.empty()) // option groups. 4908eb8c92eSSam McCall continue; 4918eb8c92eSSam McCall auto Opt = DriverTable.getOption(A); 4928eb8c92eSSam McCall // Exclude - and -foo pseudo-options. 4938eb8c92eSSam McCall if (Opt.getName().empty()) 4948eb8c92eSSam McCall continue; 4958eb8c92eSSam McCall auto Modes = getModes(Opt); 4968eb8c92eSSam McCall std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt); 4978eb8c92eSSam McCall // Iterate over each spelling of the alias, e.g. -foo vs --foo. 498*dd647e3eSChandler Carruth for (StringRef Prefix : Prefixes) { 499d9ab3e82Sserge-sans-paille llvm::SmallString<64> Buf(Prefix); 5008eb8c92eSSam McCall Buf.append(Opt.getName()); 5018eb8c92eSSam McCall llvm::StringRef Spelling = Result->try_emplace(Buf).first->getKey(); 5028eb8c92eSSam McCall Rules.emplace_back(); 5038eb8c92eSSam McCall Rule &R = Rules.back(); 5048eb8c92eSSam McCall R.Text = Spelling; 5058eb8c92eSSam McCall R.Modes = Modes; 5068eb8c92eSSam McCall R.ExactArgs = ArgCount.first; 5078eb8c92eSSam McCall R.PrefixArgs = ArgCount.second; 5088eb8c92eSSam McCall // Concrete priority is the index into the option table. 5098eb8c92eSSam McCall // Effectively, earlier entries take priority over later ones. 5108eb8c92eSSam McCall assert(ID < std::numeric_limits<decltype(R.Priority)>::max() && 5118eb8c92eSSam McCall "Rules::Priority overflowed by options table"); 5128eb8c92eSSam McCall R.Priority = ID; 5138eb8c92eSSam McCall } 5148eb8c92eSSam McCall } 5158eb8c92eSSam McCall // Register the set of rules under each possible name. 5168eb8c92eSSam McCall for (const auto &R : Rules) 5178eb8c92eSSam McCall Result->find(R.Text)->second.append(Rules.begin(), Rules.end()); 5188eb8c92eSSam McCall } 5198eb8c92eSSam McCall #ifndef NDEBUG 5208eb8c92eSSam McCall // Dump the table and various measures of its size. 5218eb8c92eSSam McCall unsigned RuleCount = 0; 5228eb8c92eSSam McCall dlog("ArgStripper Option spelling table"); 5238eb8c92eSSam McCall for (const auto &Entry : *Result) { 5248eb8c92eSSam McCall dlog("{0}", Entry.first()); 5258eb8c92eSSam McCall RuleCount += Entry.second.size(); 5268eb8c92eSSam McCall for (const auto &R : Entry.second) 5278eb8c92eSSam McCall dlog(" {0} #={1} *={2} Mode={3}", R.Text, R.ExactArgs, R.PrefixArgs, 5288eb8c92eSSam McCall int(R.Modes)); 5298eb8c92eSSam McCall } 5308eb8c92eSSam McCall dlog("Table spellings={0} rules={1} string-bytes={2}", Result->size(), 5318eb8c92eSSam McCall RuleCount, Result->getAllocator().getBytesAllocated()); 5328eb8c92eSSam McCall #endif 5338eb8c92eSSam McCall // The static table will never be destroyed. 5348eb8c92eSSam McCall return Result.release(); 5358eb8c92eSSam McCall }(); 5368eb8c92eSSam McCall 5378eb8c92eSSam McCall auto It = Table->find(Arg); 5388eb8c92eSSam McCall return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second; 5398eb8c92eSSam McCall } 5408eb8c92eSSam McCall 5418eb8c92eSSam McCall void ArgStripper::strip(llvm::StringRef Arg) { 5428eb8c92eSSam McCall auto OptionRules = rulesFor(Arg); 5438eb8c92eSSam McCall if (OptionRules.empty()) { 5448eb8c92eSSam McCall // Not a recognized flag. Strip it literally. 5458eb8c92eSSam McCall Storage.emplace_back(Arg); 5468eb8c92eSSam McCall Rules.emplace_back(); 5478eb8c92eSSam McCall Rules.back().Text = Storage.back(); 5488eb8c92eSSam McCall Rules.back().ExactArgs = 1; 5498eb8c92eSSam McCall if (Rules.back().Text.consume_back("*")) 5508eb8c92eSSam McCall Rules.back().PrefixArgs = 1; 5518eb8c92eSSam McCall Rules.back().Modes = DM_All; 5528eb8c92eSSam McCall Rules.back().Priority = -1; // Max unsigned = lowest priority. 5538eb8c92eSSam McCall } else { 5548eb8c92eSSam McCall Rules.append(OptionRules.begin(), OptionRules.end()); 5558eb8c92eSSam McCall } 5568eb8c92eSSam McCall } 5578eb8c92eSSam McCall 5588eb8c92eSSam McCall const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg, 5598eb8c92eSSam McCall unsigned Mode, 5608eb8c92eSSam McCall unsigned &ArgCount) const { 5618eb8c92eSSam McCall const ArgStripper::Rule *BestRule = nullptr; 5628eb8c92eSSam McCall for (const Rule &R : Rules) { 5638eb8c92eSSam McCall // Rule can fail to match if... 5648eb8c92eSSam McCall if (!(R.Modes & Mode)) 5658eb8c92eSSam McCall continue; // not applicable to current driver mode 5668eb8c92eSSam McCall if (BestRule && BestRule->Priority < R.Priority) 5678eb8c92eSSam McCall continue; // lower-priority than best candidate. 568d5953e3eSKazu Hirata if (!Arg.starts_with(R.Text)) 5698eb8c92eSSam McCall continue; // current arg doesn't match the prefix string 5708eb8c92eSSam McCall bool PrefixMatch = Arg.size() > R.Text.size(); 5718eb8c92eSSam McCall // Can rule apply as an exact/prefix match? 5728eb8c92eSSam McCall if (unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) { 5738eb8c92eSSam McCall BestRule = &R; 5748eb8c92eSSam McCall ArgCount = Count; 5758eb8c92eSSam McCall } 5768eb8c92eSSam McCall // Continue in case we find a higher-priority rule. 5778eb8c92eSSam McCall } 5788eb8c92eSSam McCall return BestRule; 5798eb8c92eSSam McCall } 5808eb8c92eSSam McCall 5818eb8c92eSSam McCall void ArgStripper::process(std::vector<std::string> &Args) const { 5828eb8c92eSSam McCall if (Args.empty()) 5838eb8c92eSSam McCall return; 5848eb8c92eSSam McCall 5858eb8c92eSSam McCall // We're parsing the args list in some mode (e.g. gcc-compatible) but may 5868eb8c92eSSam McCall // temporarily switch to another mode with the -Xclang flag. 5878eb8c92eSSam McCall DriverMode MainMode = getDriverMode(Args); 5888eb8c92eSSam McCall DriverMode CurrentMode = MainMode; 5898eb8c92eSSam McCall 5908eb8c92eSSam McCall // Read and write heads for in-place deletion. 5918eb8c92eSSam McCall unsigned Read = 0, Write = 0; 5928eb8c92eSSam McCall bool WasXclang = false; 5938eb8c92eSSam McCall while (Read < Args.size()) { 5948eb8c92eSSam McCall unsigned ArgCount = 0; 59527433228SMikael Holmen if (matchingRule(Args[Read], CurrentMode, ArgCount)) { 5968eb8c92eSSam McCall // Delete it and its args. 5978eb8c92eSSam McCall if (WasXclang) { 5988eb8c92eSSam McCall assert(Write > 0); 5998eb8c92eSSam McCall --Write; // Drop previous -Xclang arg 6008eb8c92eSSam McCall CurrentMode = MainMode; 6018eb8c92eSSam McCall WasXclang = false; 6028eb8c92eSSam McCall } 6038eb8c92eSSam McCall // Advance to last arg. An arg may be foo or -Xclang foo. 6048eb8c92eSSam McCall for (unsigned I = 1; Read < Args.size() && I < ArgCount; ++I) { 6058eb8c92eSSam McCall ++Read; 6068eb8c92eSSam McCall if (Read < Args.size() && Args[Read] == "-Xclang") 6078eb8c92eSSam McCall ++Read; 6088eb8c92eSSam McCall } 6098eb8c92eSSam McCall } else { 6108eb8c92eSSam McCall // No match, just copy the arg through. 6118eb8c92eSSam McCall WasXclang = Args[Read] == "-Xclang"; 6128eb8c92eSSam McCall CurrentMode = WasXclang ? DM_CC1 : MainMode; 6138eb8c92eSSam McCall if (Write != Read) 6148eb8c92eSSam McCall Args[Write] = std::move(Args[Read]); 6158eb8c92eSSam McCall ++Write; 6168eb8c92eSSam McCall } 6178eb8c92eSSam McCall ++Read; 6188eb8c92eSSam McCall } 6198eb8c92eSSam McCall Args.resize(Write); 6208eb8c92eSSam McCall } 6218eb8c92eSSam McCall 6227de711ecSSam McCall std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) { 6237de711ecSSam McCall std::string Buf; 6247de711ecSSam McCall llvm::raw_string_ostream OS(Buf); 6257de711ecSSam McCall bool Sep = false; 6267de711ecSSam McCall for (llvm::StringRef Arg : Args) { 6277de711ecSSam McCall if (Sep) 6287de711ecSSam McCall OS << ' '; 6297de711ecSSam McCall Sep = true; 6307de711ecSSam McCall if (llvm::all_of(Arg, llvm::isPrint) && 6317de711ecSSam McCall Arg.find_first_of(" \t\n\"\\") == llvm::StringRef::npos) { 6327de711ecSSam McCall OS << Arg; 6337de711ecSSam McCall continue; 6347de711ecSSam McCall } 6357de711ecSSam McCall OS << '"'; 6367de711ecSSam McCall OS.write_escaped(Arg, /*UseHexEscapes=*/true); 6377de711ecSSam McCall OS << '"'; 6387de711ecSSam McCall } 6397de711ecSSam McCall return std::move(OS.str()); 6407de711ecSSam McCall } 6417de711ecSSam McCall 6427de711ecSSam McCall std::string printArgv(llvm::ArrayRef<std::string> Args) { 6437de711ecSSam McCall std::vector<llvm::StringRef> Refs(Args.size()); 6447de711ecSSam McCall llvm::copy(Args, Refs.begin()); 6457de711ecSSam McCall return printArgv(Refs); 6467de711ecSSam McCall } 6477de711ecSSam McCall 64899768b24SSam McCall } // namespace clangd 64999768b24SSam McCall } // namespace clang 650