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