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