Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 1 | //===--- CompileCommands.cpp ----------------------------------------------===// |
| 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 "CompileCommands.h" |
Sam McCall | c5a6ee1 | 2020-06-29 21:46:40 +0200 | [diff] [blame] | 10 | #include "Config.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 11 | #include "support/Logger.h" |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 12 | #include "support/Trace.h" |
| 13 | #include "clang/Driver/Driver.h" |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 14 | #include "clang/Driver/Options.h" |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 15 | #include "clang/Frontend/CompilerInvocation.h" |
Sam McCall | 4d00652 | 2022-02-26 11:40:12 +0100 | [diff] [blame] | 16 | #include "clang/Tooling/CompilationDatabase.h" |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 17 | #include "clang/Tooling/Tooling.h" |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 22 | #include "llvm/Option/ArgList.h" |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 23 | #include "llvm/Option/Option.h" |
| 24 | #include "llvm/Support/Allocator.h" |
| 25 | #include "llvm/Support/Debug.h" |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
| 27 | #include "llvm/Support/FileUtilities.h" |
| 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/Path.h" |
| 30 | #include "llvm/Support/Program.h" |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 31 | #include <iterator> |
Matt Arsenault | f885ae2 | 2022-12-01 17:58:38 -0500 | [diff] [blame] | 32 | #include <optional> |
Kadir Cetinkaya | b662651 | 2021-03-19 10:01:14 +0100 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <vector> |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 35 | |
| 36 | namespace clang { |
| 37 | namespace clangd { |
| 38 | namespace { |
| 39 | |
| 40 | // Query apple's `xcrun` launcher, which is the source of truth for "how should" |
| 41 | // clang be invoked on this system. |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 42 | std::optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) { |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 43 | auto Xcrun = llvm::sys::findProgramByName("xcrun"); |
| 44 | if (!Xcrun) { |
| 45 | log("Couldn't find xcrun. Hopefully you have a non-apple toolchain..."); |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 46 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 47 | } |
| 48 | llvm::SmallString<64> OutFile; |
| 49 | llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile); |
| 50 | llvm::FileRemover OutRemover(OutFile); |
Fangrui Song | f4fb2b3 | 2022-12-01 22:18:34 +0000 | [diff] [blame] | 51 | std::optional<llvm::StringRef> Redirects[3] = { |
Aaron Ballman | 9878e17 | 2022-06-27 12:02:34 -0400 | [diff] [blame] | 52 | /*stdin=*/{""}, /*stdout=*/{OutFile.str()}, /*stderr=*/{""}}; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 53 | vlog("Invoking {0} to find clang installation", *Xcrun); |
| 54 | int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv, |
Fangrui Song | f4fb2b3 | 2022-12-01 22:18:34 +0000 | [diff] [blame] | 55 | /*Env=*/std::nullopt, Redirects, |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 56 | /*SecondsToWait=*/10); |
| 57 | if (Ret != 0) { |
| 58 | log("xcrun exists but failed with code {0}. " |
| 59 | "If you have a non-apple toolchain, this is OK. " |
| 60 | "Otherwise, try xcode-select --install.", |
| 61 | Ret); |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 62 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | auto Buf = llvm::MemoryBuffer::getFile(OutFile); |
| 66 | if (!Buf) { |
| 67 | log("Can't read xcrun output: {0}", Buf.getError().message()); |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 68 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 69 | } |
| 70 | StringRef Path = Buf->get()->getBuffer().trim(); |
| 71 | if (Path.empty()) { |
| 72 | log("xcrun produced no output"); |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 73 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 74 | } |
| 75 | return Path.str(); |
| 76 | } |
| 77 | |
| 78 | // Resolve symlinks if possible. |
| 79 | std::string resolve(std::string Path) { |
| 80 | llvm::SmallString<128> Resolved; |
| 81 | if (llvm::sys::fs::real_path(Path, Resolved)) { |
| 82 | log("Failed to resolve possible symlink {0}", Path); |
| 83 | return Path; |
| 84 | } |
Kazu Hirata | 5b2772e | 2024-01-24 22:11:54 -0800 | [diff] [blame] | 85 | return std::string(Resolved); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Get a plausible full `clang` path. |
| 89 | // This is used in the fallback compile command, or when the CDB returns a |
| 90 | // generic driver with no path. |
| 91 | std::string detectClangPath() { |
| 92 | // The driver and/or cc1 sometimes depend on the binary name to compute |
| 93 | // useful things like the standard library location. |
| 94 | // We need to emulate what clang on this system is likely to see. |
| 95 | // cc1 in particular looks at the "real path" of the running process, and |
| 96 | // so if /usr/bin/clang is a symlink, it sees the resolved path. |
| 97 | // clangd doesn't have that luxury, so we resolve symlinks ourselves. |
| 98 | |
| 99 | // On Mac, `which clang` is /usr/bin/clang. It runs `xcrun clang`, which knows |
| 100 | // where the real clang is kept. We need to do the same thing, |
| 101 | // because cc1 (not the driver!) will find libc++ relative to argv[0]. |
| 102 | #ifdef __APPLE__ |
| 103 | if (auto MacClang = queryXcrun({"xcrun", "--find", "clang"})) |
| 104 | return resolve(std::move(*MacClang)); |
| 105 | #endif |
| 106 | // On other platforms, just look for compilers on the PATH. |
| 107 | for (const char *Name : {"clang", "gcc", "cc"}) |
| 108 | if (auto PathCC = llvm::sys::findProgramByName(Name)) |
| 109 | return resolve(std::move(*PathCC)); |
| 110 | // Fallback: a nonexistent 'clang' binary next to clangd. |
Kadir Cetinkaya | f71404c | 2021-03-22 11:18:18 +0100 | [diff] [blame] | 111 | static int StaticForMainAddr; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 112 | std::string ClangdExecutable = |
Kadir Cetinkaya | f71404c | 2021-03-22 11:18:18 +0100 | [diff] [blame] | 113 | llvm::sys::fs::getMainExecutable("clangd", (void *)&StaticForMainAddr); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 114 | SmallString<128> ClangPath; |
| 115 | ClangPath = llvm::sys::path::parent_path(ClangdExecutable); |
| 116 | llvm::sys::path::append(ClangPath, "clang"); |
Kazu Hirata | 5b2772e | 2024-01-24 22:11:54 -0800 | [diff] [blame] | 117 | return std::string(ClangPath); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // On mac, /usr/bin/clang sets SDKROOT and then invokes the real clang. |
| 121 | // The effect of this is to set -isysroot correctly. We do the same. |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 122 | std::optional<std::string> detectSysroot() { |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 123 | #ifndef __APPLE__ |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 124 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 125 | #endif |
| 126 | |
| 127 | // SDKROOT overridden in environment, respect it. Driver will set isysroot. |
| 128 | if (::getenv("SDKROOT")) |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 129 | return std::nullopt; |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 130 | return queryXcrun({"xcrun", "--show-sdk-path"}); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | std::string detectStandardResourceDir() { |
Kadir Cetinkaya | f71404c | 2021-03-22 11:18:18 +0100 | [diff] [blame] | 134 | static int StaticForMainAddr; // Just an address in this process. |
| 135 | return CompilerInvocation::GetResourcesPath("clangd", |
| 136 | (void *)&StaticForMainAddr); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 139 | // The path passed to argv[0] is important: |
| 140 | // - its parent directory is Driver::Dir, used for library discovery |
| 141 | // - its basename affects CLI parsing (clang-cl) and other settings |
| 142 | // Where possible it should be an absolute path with sensible directory, but |
| 143 | // with the original basename. |
| 144 | static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink, |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 145 | std::optional<std::string> ClangPath) { |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 146 | auto SiblingOf = [&](llvm::StringRef AbsPath) { |
| 147 | llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath); |
| 148 | llvm::sys::path::append(Result, llvm::sys::path::filename(Driver)); |
| 149 | return Result.str().str(); |
| 150 | }; |
| 151 | |
| 152 | // First, eliminate relative paths. |
| 153 | std::string Storage; |
| 154 | if (!llvm::sys::path::is_absolute(Driver)) { |
Sam McCall | a3b7934 | 2020-06-17 15:56:01 +0200 | [diff] [blame] | 155 | // If it's working-dir relative like bin/clang, we can't resolve it. |
| 156 | // FIXME: we could if we had the working directory here. |
| 157 | // Let's hope it's not a symlink. |
| 158 | if (llvm::any_of(Driver, |
| 159 | [](char C) { return llvm::sys::path::is_separator(C); })) |
| 160 | return Driver.str(); |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 161 | // If the driver is a generic like "g++" with no path, add clang dir. |
| 162 | if (ClangPath && |
| 163 | (Driver == "clang" || Driver == "clang++" || Driver == "gcc" || |
| 164 | Driver == "g++" || Driver == "cc" || Driver == "c++")) { |
| 165 | return SiblingOf(*ClangPath); |
| 166 | } |
| 167 | // Otherwise try to look it up on PATH. This won't change basename. |
| 168 | auto Absolute = llvm::sys::findProgramByName(Driver); |
| 169 | if (Absolute && llvm::sys::path::is_absolute(*Absolute)) |
| 170 | Driver = Storage = std::move(*Absolute); |
| 171 | else if (ClangPath) // If we don't find it, use clang dir again. |
| 172 | return SiblingOf(*ClangPath); |
| 173 | else // Nothing to do: can't find the command and no detected dir. |
| 174 | return Driver.str(); |
| 175 | } |
| 176 | |
| 177 | // Now we have an absolute path, but it may be a symlink. |
| 178 | assert(llvm::sys::path::is_absolute(Driver)); |
| 179 | if (FollowSymlink) { |
| 180 | llvm::SmallString<256> Resolved; |
| 181 | if (!llvm::sys::fs::real_path(Driver, Resolved)) |
| 182 | return SiblingOf(Resolved); |
| 183 | } |
| 184 | return Driver.str(); |
| 185 | } |
| 186 | |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 187 | } // namespace |
| 188 | |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 189 | CommandMangler CommandMangler::detect() { |
| 190 | CommandMangler Result; |
| 191 | Result.ClangPath = detectClangPath(); |
| 192 | Result.ResourceDir = detectStandardResourceDir(); |
| 193 | Result.Sysroot = detectSysroot(); |
| 194 | return Result; |
| 195 | } |
| 196 | |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 197 | CommandMangler CommandMangler::forTests() { return CommandMangler(); } |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 198 | |
Nathan Ridge | afa22c5 | 2022-09-25 01:07:58 -0700 | [diff] [blame] | 199 | void CommandMangler::operator()(tooling::CompileCommand &Command, |
| 200 | llvm::StringRef File) const { |
| 201 | std::vector<std::string> &Cmd = Command.CommandLine; |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 202 | trace::Span S("AdjustCompileFlags"); |
Kadir Cetinkaya | 444a5f3 | 2021-09-16 17:43:15 +0200 | [diff] [blame] | 203 | // Most of the modifications below assumes the Cmd starts with a driver name. |
| 204 | // We might consider injecting a generic driver name like "cc" or "c++", but |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 205 | // a Cmd missing the driver is probably rare enough in practice and erroneous. |
Kadir Cetinkaya | 444a5f3 | 2021-09-16 17:43:15 +0200 | [diff] [blame] | 206 | if (Cmd.empty()) |
| 207 | return; |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 208 | |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 209 | auto &OptTable = clang::driver::getDriverOptTable(); |
| 210 | // OriginalArgs needs to outlive ArgList. |
| 211 | llvm::SmallVector<const char *, 16> OriginalArgs; |
| 212 | OriginalArgs.reserve(Cmd.size()); |
| 213 | for (const auto &S : Cmd) |
| 214 | OriginalArgs.push_back(S.c_str()); |
Kadir Cetinkaya | 444a5f3 | 2021-09-16 17:43:15 +0200 | [diff] [blame] | 215 | bool IsCLMode = driver::IsClangCL(driver::getDriverMode( |
serge-sans-paille | 984b800 | 2023-01-09 18:11:07 +0100 | [diff] [blame] | 216 | OriginalArgs[0], llvm::ArrayRef(OriginalArgs).slice(1))); |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 217 | // ParseArgs propagates missing arg/opt counts on error, but preserves |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 218 | // everything it could parse in ArgList. So we just ignore those counts. |
| 219 | unsigned IgnoredCount; |
Kadir Cetinkaya | 259e365 | 2021-07-25 20:38:00 +0200 | [diff] [blame] | 220 | // Drop the executable name, as ParseArgs doesn't expect it. This means |
| 221 | // indices are actually of by one between ArgList and OriginalArgs. |
Kadir Cetinkaya | 444a5f3 | 2021-09-16 17:43:15 +0200 | [diff] [blame] | 222 | llvm::opt::InputArgList ArgList; |
| 223 | ArgList = OptTable.ParseArgs( |
serge-sans-paille | 984b800 | 2023-01-09 18:11:07 +0100 | [diff] [blame] | 224 | llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount, |
Justin Bogner | 9478f66 | 2023-08-02 16:04:28 -0700 | [diff] [blame] | 225 | llvm::opt::Visibility(IsCLMode ? driver::options::CLOption |
| 226 | : driver::options::ClangOption)); |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 227 | |
Kadir Cetinkaya | 3bf7798 | 2021-08-06 12:52:41 +0200 | [diff] [blame] | 228 | llvm::SmallVector<unsigned, 1> IndicesToDrop; |
| 229 | // Having multiple architecture options (e.g. when building fat binaries) |
| 230 | // results in multiple compiler jobs, which clangd cannot handle. In such |
| 231 | // cases strip all the `-arch` options and fallback to default architecture. |
| 232 | // As there are no signals to figure out which one user actually wants. They |
| 233 | // can explicitly specify one through `CompileFlags.Add` if need be. |
| 234 | unsigned ArchOptCount = 0; |
| 235 | for (auto *Input : ArgList.filtered(driver::options::OPT_arch)) { |
| 236 | ++ArchOptCount; |
| 237 | for (auto I = 0U; I <= Input->getNumValues(); ++I) |
| 238 | IndicesToDrop.push_back(Input->getIndex() + I); |
| 239 | } |
| 240 | // If there is a single `-arch` option, keep it. |
| 241 | if (ArchOptCount < 2) |
| 242 | IndicesToDrop.clear(); |
Kadir Cetinkaya | 79c2616 | 2021-08-06 13:25:14 +0200 | [diff] [blame] | 243 | |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 244 | // In some cases people may try to reuse the command from another file, e.g. |
| 245 | // { File: "foo.h", CommandLine: "clang foo.cpp" }. |
| 246 | // We assume the intent is to parse foo.h the same way as foo.cpp, or as if |
| 247 | // it were being included from foo.cpp. |
| 248 | // |
| 249 | // We're going to rewrite the command to refer to foo.h, and this may change |
| 250 | // its semantics (e.g. by parsing the file as C). If we do this, we should |
| 251 | // use transferCompileCommand to adjust the argv. |
| 252 | // In practice only the extension of the file matters, so do this only when |
| 253 | // it differs. |
| 254 | llvm::StringRef FileExtension = llvm::sys::path::extension(File); |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 255 | std::optional<std::string> TransferFrom; |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 256 | auto SawInput = [&](llvm::StringRef Input) { |
| 257 | if (llvm::sys::path::extension(Input) != FileExtension) |
| 258 | TransferFrom.emplace(Input); |
| 259 | }; |
| 260 | |
Kadir Cetinkaya | 79c2616 | 2021-08-06 13:25:14 +0200 | [diff] [blame] | 261 | // Strip all the inputs and `--`. We'll put the input for the requested file |
| 262 | // explicitly at the end of the flags. This ensures modifications done in the |
| 263 | // following steps apply in more cases (like setting -x, which only affects |
| 264 | // inputs that come after it). |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 265 | for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT)) { |
| 266 | SawInput(Input->getValue(0)); |
Kadir Cetinkaya | 79c2616 | 2021-08-06 13:25:14 +0200 | [diff] [blame] | 267 | IndicesToDrop.push_back(Input->getIndex()); |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 268 | } |
Kadir Cetinkaya | 79c2616 | 2021-08-06 13:25:14 +0200 | [diff] [blame] | 269 | // Anything after `--` is also treated as input, drop them as well. |
| 270 | if (auto *DashDash = |
| 271 | ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) { |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 272 | auto DashDashIndex = DashDash->getIndex() + 1; // +1 accounts for Cmd[0] |
| 273 | for (unsigned I = DashDashIndex; I < Cmd.size(); ++I) |
| 274 | SawInput(Cmd[I]); |
| 275 | Cmd.resize(DashDashIndex); |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 276 | } |
Kadir Cetinkaya | 3bf7798 | 2021-08-06 12:52:41 +0200 | [diff] [blame] | 277 | llvm::sort(IndicesToDrop); |
Kazu Hirata | 840a968 | 2023-09-02 12:12:16 -0700 | [diff] [blame] | 278 | for (unsigned Idx : llvm::reverse(IndicesToDrop)) |
| 279 | // +1 to account for the executable name in Cmd[0] that |
| 280 | // doesn't exist in ArgList. |
| 281 | Cmd.erase(Cmd.begin() + Idx + 1); |
Kadir Cetinkaya | 79c2616 | 2021-08-06 13:25:14 +0200 | [diff] [blame] | 282 | // All the inputs are stripped, append the name for the requested file. Rest |
| 283 | // of the modifications should respect `--`. |
| 284 | Cmd.push_back("--"); |
| 285 | Cmd.push_back(File.str()); |
Kadir Cetinkaya | ab714ba | 2021-07-26 11:20:47 +0200 | [diff] [blame] | 286 | |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 287 | if (TransferFrom) { |
| 288 | tooling::CompileCommand TransferCmd; |
| 289 | TransferCmd.Filename = std::move(*TransferFrom); |
| 290 | TransferCmd.CommandLine = std::move(Cmd); |
| 291 | TransferCmd = transferCompileCommand(std::move(TransferCmd), File); |
| 292 | Cmd = std::move(TransferCmd.CommandLine); |
Sam McCall | 4258d68 | 2022-01-06 05:00:11 +0100 | [diff] [blame] | 293 | assert(Cmd.size() >= 2 && Cmd.back() == File && |
| 294 | Cmd[Cmd.size() - 2] == "--" && |
| 295 | "TransferCommand should produce a command ending in -- filename"); |
Sam McCall | 0683a1e | 2021-12-22 16:57:59 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Sam McCall | 9d3e9a3 | 2020-07-13 20:44:03 +0200 | [diff] [blame] | 298 | for (auto &Edit : Config::current().CompileFlags.Edits) |
Sam McCall | c5a6ee1 | 2020-06-29 21:46:40 +0200 | [diff] [blame] | 299 | Edit(Cmd); |
| 300 | |
Nathan Ridge | 68e230a | 2022-09-12 04:29:29 -0400 | [diff] [blame] | 301 | // The system include extractor needs to run: |
| 302 | // - AFTER transferCompileCommand(), because the -x flag it adds may be |
| 303 | // necessary for the system include extractor to identify the file type |
| 304 | // - AFTER applying CompileFlags.Edits, because the name of the compiler |
| 305 | // that needs to be invoked may come from the CompileFlags->Compiler key |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 306 | // - BEFORE addTargetAndModeForProgramName(), because gcc doesn't support |
| 307 | // the target flag that might be added. |
Nathan Ridge | 68e230a | 2022-09-12 04:29:29 -0400 | [diff] [blame] | 308 | // - BEFORE resolveDriver() because that can mess up the driver path, |
| 309 | // e.g. changing gcc to /path/to/clang/bin/gcc |
| 310 | if (SystemIncludeExtractor) { |
| 311 | SystemIncludeExtractor(Command, File); |
| 312 | } |
| 313 | |
Dmitry Polukhin | d60d345 | 2023-02-16 05:06:53 -0800 | [diff] [blame] | 314 | tooling::addTargetAndModeForProgramName(Cmd, Cmd.front()); |
| 315 | |
Kon | f489fb3 | 2024-01-12 17:11:38 +0900 | [diff] [blame] | 316 | // Check whether the flag exists in the command. |
| 317 | auto HasExact = [&](llvm::StringRef Flag) { |
| 318 | return llvm::any_of(Cmd, [&](llvm::StringRef Arg) { return Arg == Flag; }); |
| 319 | }; |
| 320 | |
| 321 | // Check whether the flag appears in the command as a prefix. |
| 322 | auto HasPrefix = [&](llvm::StringRef Flag) { |
| 323 | return llvm::any_of( |
Jie Fu | dabc901 | 2024-01-12 16:18:14 +0800 | [diff] [blame] | 324 | Cmd, [&](llvm::StringRef Arg) { return Arg.starts_with(Flag); }); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 325 | }; |
| 326 | |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 327 | llvm::erase_if(Cmd, [](llvm::StringRef Elem) { |
Kazu Hirata | d5953e3 | 2023-12-13 23:26:09 -0800 | [diff] [blame] | 328 | return Elem.starts_with("--save-temps") || Elem.starts_with("-save-temps"); |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 329 | }); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 330 | |
Kadir Cetinkaya | b662651 | 2021-03-19 10:01:14 +0100 | [diff] [blame] | 331 | std::vector<std::string> ToAppend; |
Kon | f489fb3 | 2024-01-12 17:11:38 +0900 | [diff] [blame] | 332 | if (ResourceDir && !HasExact("-resource-dir") && !HasPrefix("-resource-dir=")) |
Kadir Cetinkaya | b662651 | 2021-03-19 10:01:14 +0100 | [diff] [blame] | 333 | ToAppend.push_back(("-resource-dir=" + *ResourceDir)); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 334 | |
David Goldman | ea9888b | 2020-01-08 17:01:59 -0500 | [diff] [blame] | 335 | // Don't set `-isysroot` if it is already set or if `--sysroot` is set. |
| 336 | // `--sysroot` is a superset of the `-isysroot` argument. |
Kon | f489fb3 | 2024-01-12 17:11:38 +0900 | [diff] [blame] | 337 | if (Sysroot && !HasPrefix("-isysroot") && !HasExact("--sysroot") && |
| 338 | !HasPrefix("--sysroot=")) { |
Kadir Cetinkaya | b662651 | 2021-03-19 10:01:14 +0100 | [diff] [blame] | 339 | ToAppend.push_back("-isysroot"); |
| 340 | ToAppend.push_back(*Sysroot); |
| 341 | } |
| 342 | |
| 343 | if (!ToAppend.empty()) { |
Kadir Cetinkaya | 0a3c796 | 2021-07-24 19:44:15 +0200 | [diff] [blame] | 344 | Cmd.insert(llvm::find(Cmd, "--"), std::make_move_iterator(ToAppend.begin()), |
| 345 | std::make_move_iterator(ToAppend.end())); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 348 | if (!Cmd.empty()) { |
Kon | f489fb3 | 2024-01-12 17:11:38 +0900 | [diff] [blame] | 349 | bool FollowSymlink = !HasExact("-no-canonical-prefixes"); |
Sam McCall | 2a3ac01 | 2020-06-09 22:54:42 +0200 | [diff] [blame] | 350 | Cmd.front() = |
| 351 | (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow) |
| 352 | .get(Cmd.front(), [&, this] { |
| 353 | return resolveDriver(Cmd.front(), FollowSymlink, ClangPath); |
| 354 | }); |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 358 | // ArgStripper implementation |
| 359 | namespace { |
| 360 | |
| 361 | // Determine total number of args consumed by this option. |
| 362 | // Return answers for {Exact, Prefix} match. 0 means not allowed. |
| 363 | std::pair<unsigned, unsigned> getArgCount(const llvm::opt::Option &Opt) { |
| 364 | constexpr static unsigned Rest = 10000; // Should be all the rest! |
| 365 | // Reference is llvm::opt::Option::acceptInternal() |
| 366 | using llvm::opt::Option; |
| 367 | switch (Opt.getKind()) { |
| 368 | case Option::FlagClass: |
| 369 | return {1, 0}; |
| 370 | case Option::JoinedClass: |
| 371 | case Option::CommaJoinedClass: |
| 372 | return {1, 1}; |
| 373 | case Option::GroupClass: |
| 374 | case Option::InputClass: |
| 375 | case Option::UnknownClass: |
| 376 | case Option::ValuesClass: |
| 377 | return {1, 0}; |
| 378 | case Option::JoinedAndSeparateClass: |
| 379 | return {2, 2}; |
| 380 | case Option::SeparateClass: |
| 381 | return {2, 0}; |
| 382 | case Option::MultiArgClass: |
| 383 | return {1 + Opt.getNumArgs(), 0}; |
| 384 | case Option::JoinedOrSeparateClass: |
| 385 | return {2, 1}; |
| 386 | case Option::RemainingArgsClass: |
| 387 | return {Rest, 0}; |
| 388 | case Option::RemainingArgsJoinedClass: |
| 389 | return {Rest, Rest}; |
| 390 | } |
Mikael Holmen | 2743322 | 2020-07-16 09:28:34 +0200 | [diff] [blame] | 391 | llvm_unreachable("Unhandled option kind"); |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Flag-parsing mode, which affects which flags are available. |
| 395 | enum DriverMode : unsigned char { |
| 396 | DM_None = 0, |
| 397 | DM_GCC = 1, // Default mode e.g. when invoked as 'clang' |
| 398 | DM_CL = 2, // MS CL.exe compatible mode e.g. when invoked as 'clang-cl' |
| 399 | DM_CC1 = 4, // When invoked as 'clang -cc1' or after '-Xclang' |
| 400 | DM_All = 7 |
| 401 | }; |
| 402 | |
| 403 | // Examine args list to determine if we're in GCC, CL-compatible, or cc1 mode. |
| 404 | DriverMode getDriverMode(const std::vector<std::string> &Args) { |
| 405 | DriverMode Mode = DM_GCC; |
| 406 | llvm::StringRef Argv0 = Args.front(); |
Kazu Hirata | ed1539c | 2023-05-16 10:12:42 -0700 | [diff] [blame] | 407 | if (Argv0.ends_with_insensitive(".exe")) |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 408 | Argv0 = Argv0.drop_back(strlen(".exe")); |
Kazu Hirata | ed1539c | 2023-05-16 10:12:42 -0700 | [diff] [blame] | 409 | if (Argv0.ends_with_insensitive("cl")) |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 410 | Mode = DM_CL; |
| 411 | for (const llvm::StringRef Arg : Args) { |
| 412 | if (Arg == "--driver-mode=cl") { |
| 413 | Mode = DM_CL; |
| 414 | break; |
| 415 | } |
| 416 | if (Arg == "-cc1") { |
| 417 | Mode = DM_CC1; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | return Mode; |
| 422 | } |
| 423 | |
| 424 | // Returns the set of DriverModes where an option may be used. |
| 425 | unsigned char getModes(const llvm::opt::Option &Opt) { |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 426 | unsigned char Result = DM_None; |
Justin Bogner | 9478f66 | 2023-08-02 16:04:28 -0700 | [diff] [blame] | 427 | if (Opt.hasVisibilityFlag(driver::options::ClangOption)) |
| 428 | Result |= DM_GCC; |
| 429 | if (Opt.hasVisibilityFlag(driver::options::CC1Option)) |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 430 | Result |= DM_CC1; |
Justin Bogner | 9478f66 | 2023-08-02 16:04:28 -0700 | [diff] [blame] | 431 | if (Opt.hasVisibilityFlag(driver::options::CLOption)) |
| 432 | Result |= DM_CL; |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 433 | return Result; |
Mikael Holmen | 2743322 | 2020-07-16 09:28:34 +0200 | [diff] [blame] | 434 | } |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 435 | |
| 436 | } // namespace |
| 437 | |
| 438 | llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) { |
| 439 | // All the hard work is done once in a static initializer. |
| 440 | // We compute a table containing strings to look for and #args to skip. |
| 441 | // e.g. "-x" => {-x 2 args, -x* 1 arg, --language 2 args, --language=* 1 arg} |
| 442 | using TableTy = |
| 443 | llvm::StringMap<llvm::SmallVector<Rule, 4>, llvm::BumpPtrAllocator>; |
| 444 | static TableTy *Table = [] { |
| 445 | auto &DriverTable = driver::getDriverOptTable(); |
| 446 | using DriverID = clang::driver::options::ID; |
| 447 | |
| 448 | // Collect sets of aliases, so we can treat -foo and -foo= as synonyms. |
| 449 | // Conceptually a double-linked list: PrevAlias[I] -> I -> NextAlias[I]. |
| 450 | // If PrevAlias[I] is INVALID, then I is canonical. |
| 451 | DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID}; |
| 452 | DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID}; |
| 453 | auto AddAlias = [&](DriverID Self, DriverID T) { |
| 454 | if (NextAlias[T]) { |
| 455 | PrevAlias[NextAlias[T]] = Self; |
| 456 | NextAlias[Self] = NextAlias[T]; |
| 457 | } |
| 458 | PrevAlias[Self] = T; |
| 459 | NextAlias[T] = Self; |
| 460 | }; |
| 461 | // Also grab prefixes for each option, these are not fully exposed. |
serge-sans-paille | d9ab3e8 | 2022-12-26 09:19:09 +0100 | [diff] [blame] | 462 | llvm::ArrayRef<llvm::StringLiteral> Prefixes[DriverID::LastOption]; |
| 463 | |
| 464 | #define PREFIX(NAME, VALUE) \ |
| 465 | static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \ |
| 466 | static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \ |
| 467 | NAME##_init, std::size(NAME##_init) - 1); |
Jan Svoboda | 501f92d | 2023-08-09 09:23:40 -0700 | [diff] [blame] | 468 | #define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \ |
David Spickett | aff197f | 2024-04-05 08:16:38 +0000 | [diff] [blame] | 469 | FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \ |
| 470 | METAVAR, VALUES) \ |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 471 | Prefixes[DriverID::OPT_##ID] = PREFIX; |
| 472 | #include "clang/Driver/Options.inc" |
| 473 | #undef OPTION |
| 474 | #undef PREFIX |
| 475 | |
Yuanfang Chen | 3b64ab5 | 2022-01-24 13:42:04 -0800 | [diff] [blame] | 476 | struct { |
| 477 | DriverID ID; |
| 478 | DriverID AliasID; |
Yuanfang Chen | 4f30a52 | 2022-02-09 18:03:04 -0800 | [diff] [blame] | 479 | const void *AliasArgs; |
Yuanfang Chen | 3b64ab5 | 2022-01-24 13:42:04 -0800 | [diff] [blame] | 480 | } AliasTable[] = { |
Jan Svoboda | 501f92d | 2023-08-09 09:23:40 -0700 | [diff] [blame] | 481 | #define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \ |
David Spickett | aff197f | 2024-04-05 08:16:38 +0000 | [diff] [blame] | 482 | FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \ |
| 483 | METAVAR, VALUES) \ |
Yuanfang Chen | 4f30a52 | 2022-02-09 18:03:04 -0800 | [diff] [blame] | 484 | {DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS}, |
Yuanfang Chen | 3b64ab5 | 2022-01-24 13:42:04 -0800 | [diff] [blame] | 485 | #include "clang/Driver/Options.inc" |
| 486 | #undef OPTION |
| 487 | }; |
| 488 | for (auto &E : AliasTable) |
| 489 | if (E.AliasID != DriverID::OPT_INVALID && E.AliasArgs == nullptr) |
| 490 | AddAlias(E.ID, E.AliasID); |
| 491 | |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 492 | auto Result = std::make_unique<TableTy>(); |
| 493 | // Iterate over distinct options (represented by the canonical alias). |
| 494 | // Every spelling of this option will get the same set of rules. |
| 495 | for (unsigned ID = 1 /*Skip INVALID */; ID < DriverID::LastOption; ++ID) { |
| 496 | if (PrevAlias[ID] || ID == DriverID::OPT_Xclang) |
| 497 | continue; // Not canonical, or specially handled. |
Kirill Bobyrev | ee02e20 | 2020-12-10 13:36:35 +0100 | [diff] [blame] | 498 | llvm::SmallVector<Rule> Rules; |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 499 | // Iterate over each alias, to add rules for parsing it. |
| 500 | for (unsigned A = ID; A != DriverID::OPT_INVALID; A = NextAlias[A]) { |
serge-sans-paille | d9ab3e8 | 2022-12-26 09:19:09 +0100 | [diff] [blame] | 501 | if (!Prefixes[A].size()) // option groups. |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 502 | continue; |
| 503 | auto Opt = DriverTable.getOption(A); |
| 504 | // Exclude - and -foo pseudo-options. |
| 505 | if (Opt.getName().empty()) |
| 506 | continue; |
| 507 | auto Modes = getModes(Opt); |
| 508 | std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt); |
| 509 | // Iterate over each spelling of the alias, e.g. -foo vs --foo. |
serge-sans-paille | d9ab3e8 | 2022-12-26 09:19:09 +0100 | [diff] [blame] | 510 | for (StringRef Prefix : Prefixes[A]) { |
| 511 | llvm::SmallString<64> Buf(Prefix); |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 512 | Buf.append(Opt.getName()); |
| 513 | llvm::StringRef Spelling = Result->try_emplace(Buf).first->getKey(); |
| 514 | Rules.emplace_back(); |
| 515 | Rule &R = Rules.back(); |
| 516 | R.Text = Spelling; |
| 517 | R.Modes = Modes; |
| 518 | R.ExactArgs = ArgCount.first; |
| 519 | R.PrefixArgs = ArgCount.second; |
| 520 | // Concrete priority is the index into the option table. |
| 521 | // Effectively, earlier entries take priority over later ones. |
| 522 | assert(ID < std::numeric_limits<decltype(R.Priority)>::max() && |
| 523 | "Rules::Priority overflowed by options table"); |
| 524 | R.Priority = ID; |
| 525 | } |
| 526 | } |
| 527 | // Register the set of rules under each possible name. |
| 528 | for (const auto &R : Rules) |
| 529 | Result->find(R.Text)->second.append(Rules.begin(), Rules.end()); |
| 530 | } |
| 531 | #ifndef NDEBUG |
| 532 | // Dump the table and various measures of its size. |
| 533 | unsigned RuleCount = 0; |
| 534 | dlog("ArgStripper Option spelling table"); |
| 535 | for (const auto &Entry : *Result) { |
| 536 | dlog("{0}", Entry.first()); |
| 537 | RuleCount += Entry.second.size(); |
| 538 | for (const auto &R : Entry.second) |
| 539 | dlog(" {0} #={1} *={2} Mode={3}", R.Text, R.ExactArgs, R.PrefixArgs, |
| 540 | int(R.Modes)); |
| 541 | } |
| 542 | dlog("Table spellings={0} rules={1} string-bytes={2}", Result->size(), |
| 543 | RuleCount, Result->getAllocator().getBytesAllocated()); |
| 544 | #endif |
| 545 | // The static table will never be destroyed. |
| 546 | return Result.release(); |
| 547 | }(); |
| 548 | |
| 549 | auto It = Table->find(Arg); |
| 550 | return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second; |
| 551 | } |
| 552 | |
| 553 | void ArgStripper::strip(llvm::StringRef Arg) { |
| 554 | auto OptionRules = rulesFor(Arg); |
| 555 | if (OptionRules.empty()) { |
| 556 | // Not a recognized flag. Strip it literally. |
| 557 | Storage.emplace_back(Arg); |
| 558 | Rules.emplace_back(); |
| 559 | Rules.back().Text = Storage.back(); |
| 560 | Rules.back().ExactArgs = 1; |
| 561 | if (Rules.back().Text.consume_back("*")) |
| 562 | Rules.back().PrefixArgs = 1; |
| 563 | Rules.back().Modes = DM_All; |
| 564 | Rules.back().Priority = -1; // Max unsigned = lowest priority. |
| 565 | } else { |
| 566 | Rules.append(OptionRules.begin(), OptionRules.end()); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg, |
| 571 | unsigned Mode, |
| 572 | unsigned &ArgCount) const { |
| 573 | const ArgStripper::Rule *BestRule = nullptr; |
| 574 | for (const Rule &R : Rules) { |
| 575 | // Rule can fail to match if... |
| 576 | if (!(R.Modes & Mode)) |
| 577 | continue; // not applicable to current driver mode |
| 578 | if (BestRule && BestRule->Priority < R.Priority) |
| 579 | continue; // lower-priority than best candidate. |
Kazu Hirata | d5953e3 | 2023-12-13 23:26:09 -0800 | [diff] [blame] | 580 | if (!Arg.starts_with(R.Text)) |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 581 | continue; // current arg doesn't match the prefix string |
| 582 | bool PrefixMatch = Arg.size() > R.Text.size(); |
| 583 | // Can rule apply as an exact/prefix match? |
| 584 | if (unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) { |
| 585 | BestRule = &R; |
| 586 | ArgCount = Count; |
| 587 | } |
| 588 | // Continue in case we find a higher-priority rule. |
| 589 | } |
| 590 | return BestRule; |
| 591 | } |
| 592 | |
| 593 | void ArgStripper::process(std::vector<std::string> &Args) const { |
| 594 | if (Args.empty()) |
| 595 | return; |
| 596 | |
| 597 | // We're parsing the args list in some mode (e.g. gcc-compatible) but may |
| 598 | // temporarily switch to another mode with the -Xclang flag. |
| 599 | DriverMode MainMode = getDriverMode(Args); |
| 600 | DriverMode CurrentMode = MainMode; |
| 601 | |
| 602 | // Read and write heads for in-place deletion. |
| 603 | unsigned Read = 0, Write = 0; |
| 604 | bool WasXclang = false; |
| 605 | while (Read < Args.size()) { |
| 606 | unsigned ArgCount = 0; |
Mikael Holmen | 2743322 | 2020-07-16 09:28:34 +0200 | [diff] [blame] | 607 | if (matchingRule(Args[Read], CurrentMode, ArgCount)) { |
Sam McCall | 8eb8c92 | 2020-06-16 21:21:32 +0200 | [diff] [blame] | 608 | // Delete it and its args. |
| 609 | if (WasXclang) { |
| 610 | assert(Write > 0); |
| 611 | --Write; // Drop previous -Xclang arg |
| 612 | CurrentMode = MainMode; |
| 613 | WasXclang = false; |
| 614 | } |
| 615 | // Advance to last arg. An arg may be foo or -Xclang foo. |
| 616 | for (unsigned I = 1; Read < Args.size() && I < ArgCount; ++I) { |
| 617 | ++Read; |
| 618 | if (Read < Args.size() && Args[Read] == "-Xclang") |
| 619 | ++Read; |
| 620 | } |
| 621 | } else { |
| 622 | // No match, just copy the arg through. |
| 623 | WasXclang = Args[Read] == "-Xclang"; |
| 624 | CurrentMode = WasXclang ? DM_CC1 : MainMode; |
| 625 | if (Write != Read) |
| 626 | Args[Write] = std::move(Args[Read]); |
| 627 | ++Write; |
| 628 | } |
| 629 | ++Read; |
| 630 | } |
| 631 | Args.resize(Write); |
| 632 | } |
| 633 | |
Sam McCall | 7de711e | 2021-01-31 16:37:42 +0100 | [diff] [blame] | 634 | std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) { |
| 635 | std::string Buf; |
| 636 | llvm::raw_string_ostream OS(Buf); |
| 637 | bool Sep = false; |
| 638 | for (llvm::StringRef Arg : Args) { |
| 639 | if (Sep) |
| 640 | OS << ' '; |
| 641 | Sep = true; |
| 642 | if (llvm::all_of(Arg, llvm::isPrint) && |
| 643 | Arg.find_first_of(" \t\n\"\\") == llvm::StringRef::npos) { |
| 644 | OS << Arg; |
| 645 | continue; |
| 646 | } |
| 647 | OS << '"'; |
| 648 | OS.write_escaped(Arg, /*UseHexEscapes=*/true); |
| 649 | OS << '"'; |
| 650 | } |
| 651 | return std::move(OS.str()); |
| 652 | } |
| 653 | |
| 654 | std::string printArgv(llvm::ArrayRef<std::string> Args) { |
| 655 | std::vector<llvm::StringRef> Refs(Args.size()); |
| 656 | llvm::copy(Args, Refs.begin()); |
| 657 | return printArgv(Refs); |
| 658 | } |
| 659 | |
Sam McCall | 99768b2 | 2019-11-29 19:37:48 +0100 | [diff] [blame] | 660 | } // namespace clangd |
| 661 | } // namespace clang |