clang-tools 23.0.0git
CompileCommands.cpp
Go to the documentation of this file.
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/Frontend/CompilerInvocation.h"
15#include "clang/Options/Options.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 <iterator>
32#include <optional>
33#include <string>
34#include <vector>
35
36namespace clang {
37namespace clangd {
38namespace {
39
40// Query apple's `xcrun` launcher, which is the source of truth for "how should"
41// clang be invoked on this system.
42std::optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
43 auto Xcrun = llvm::sys::findProgramByName("xcrun");
44 if (!Xcrun) {
45 log("Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
46 return std::nullopt;
47 }
48 llvm::SmallString<64> OutFile;
49 llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile);
50 llvm::FileRemover OutRemover(OutFile);
51 std::optional<llvm::StringRef> Redirects[3] = {
52 /*stdin=*/{""}, /*stdout=*/{OutFile.str()}, /*stderr=*/{""}};
53 vlog("Invoking {0} to find clang installation", *Xcrun);
54 int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv,
55 /*Env=*/std::nullopt, Redirects,
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);
62 return std::nullopt;
63 }
64
65 auto Buf = llvm::MemoryBuffer::getFile(OutFile);
66 if (!Buf) {
67 log("Can't read xcrun output: {0}", Buf.getError().message());
68 return std::nullopt;
69 }
70 StringRef Path = Buf->get()->getBuffer().trim();
71 if (Path.empty()) {
72 log("xcrun produced no output");
73 return std::nullopt;
74 }
75 return Path.str();
76}
77
78// Resolve symlinks if possible.
79std::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 }
85 return std::string(Resolved);
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.
91std::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.
111 static int StaticForMainAddr;
112 std::string ClangdExecutable =
113 llvm::sys::fs::getMainExecutable("clangd", (void *)&StaticForMainAddr);
114 SmallString<128> ClangPath;
115 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
116 llvm::sys::path::append(ClangPath, "clang");
117 return std::string(ClangPath);
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.
122std::optional<std::string> detectSysroot() {
123#ifndef __APPLE__
124 return std::nullopt;
125#endif
126
127 // SDKROOT overridden in environment, respect it. Driver will set isysroot.
128 if (::getenv("SDKROOT"))
129 return std::nullopt;
130 return queryXcrun({"xcrun", "--show-sdk-path"});
131}
132
133std::string detectStandardResourceDir() {
134 static int StaticForMainAddr; // Just an address in this process.
135 return GetResourcesPath("clangd", (void *)&StaticForMainAddr);
136}
137
138std::optional<std::string>
139detectResourceDirWithClangPath(std::optional<std::string> ClangPath) {
140 std::string ResourceDir = detectStandardResourceDir();
141 if (llvm::sys::fs::exists(ResourceDir))
142 return ResourceDir;
143 vlog("Auto-detected standard resource directory '{0}' doesn't exist",
144 ResourceDir);
145
146 if (ClangPath) {
147 ResourceDir = GetResourcesPath(*ClangPath);
148 if (llvm::sys::fs::exists(ResourceDir))
149 return ResourceDir;
150 vlog("Auto-detected using clang path '{0}' "
151 "resource directory '{1}' doesn't exist",
152 *ClangPath, ResourceDir);
153 }
154
155 elog("Failed to auto-detect resource directory, "
156 "specify it manually via --resource-dir command line argument");
157 return std::nullopt;
158}
159
160// The path passed to argv[0] is important:
161// - its parent directory is Driver::Dir, used for library discovery
162// - its basename affects CLI parsing (clang-cl) and other settings
163// Where possible it should be an absolute path with sensible directory, but
164// with the original basename.
165static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink,
166 std::optional<std::string> ClangPath) {
167 auto SiblingOf = [&](llvm::StringRef AbsPath) {
168 llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath);
169 llvm::sys::path::append(Result, llvm::sys::path::filename(Driver));
170 return Result.str().str();
171 };
172
173 // First, eliminate relative paths.
174 std::string Storage;
175 if (!llvm::sys::path::is_absolute(Driver)) {
176 // If it's working-dir relative like bin/clang, we can't resolve it.
177 // FIXME: we could if we had the working directory here.
178 // Let's hope it's not a symlink.
179 if (llvm::any_of(Driver,
180 [](char C) { return llvm::sys::path::is_separator(C); }))
181 return Driver.str();
182 // If the driver is a generic like "g++" with no path, add clang dir.
183 if (ClangPath &&
184 (Driver == "clang" || Driver == "clang++" || Driver == "gcc" ||
185 Driver == "g++" || Driver == "cc" || Driver == "c++")) {
186 return SiblingOf(*ClangPath);
187 }
188 // Otherwise try to look it up on PATH. This won't change basename.
189 auto Absolute = llvm::sys::findProgramByName(Driver);
190 if (Absolute && llvm::sys::path::is_absolute(*Absolute))
191 Driver = Storage = std::move(*Absolute);
192 else if (ClangPath) // If we don't find it, use clang dir again.
193 return SiblingOf(*ClangPath);
194 else // Nothing to do: can't find the command and no detected dir.
195 return Driver.str();
196 }
197
198 // Now we have an absolute path, but it may be a symlink.
199 assert(llvm::sys::path::is_absolute(Driver));
200 if (FollowSymlink) {
201 llvm::SmallString<256> Resolved;
202 if (!llvm::sys::fs::real_path(Driver, Resolved))
203 return SiblingOf(Resolved);
204 }
205 return Driver.str();
206}
207
208} // namespace
209
211 CommandMangler Result;
212 Result.ClangPath = detectClangPath();
213 Result.ResourceDir = detectResourceDirWithClangPath(Result.ClangPath);
214 Result.Sysroot = detectSysroot();
215 return Result;
216}
217
219
220void CommandMangler::operator()(tooling::CompileCommand &Command,
221 llvm::StringRef File) const {
222 std::vector<std::string> &Cmd = Command.CommandLine;
223 trace::Span S("AdjustCompileFlags");
224 // Most of the modifications below assumes the Cmd starts with a driver name.
225 // We might consider injecting a generic driver name like "cc" or "c++", but
226 // a Cmd missing the driver is probably rare enough in practice and erroneous.
227 if (Cmd.empty())
228 return;
229
230 auto &OptTable = getDriverOptTable();
231 // OriginalArgs needs to outlive ArgList.
232 llvm::SmallVector<const char *, 16> OriginalArgs;
233 OriginalArgs.reserve(Cmd.size());
234 for (const auto &S : Cmd)
235 OriginalArgs.push_back(S.c_str());
236 bool IsCLMode = driver::IsClangCL(driver::getDriverMode(
237 OriginalArgs[0], llvm::ArrayRef(OriginalArgs).slice(1)));
238 // ParseArgs propagates missing arg/opt counts on error, but preserves
239 // everything it could parse in ArgList. So we just ignore those counts.
240 unsigned IgnoredCount;
241 // Drop the executable name, as ParseArgs doesn't expect it. This means
242 // indices are actually of by one between ArgList and OriginalArgs.
243 llvm::opt::InputArgList ArgList;
244 ArgList = OptTable.ParseArgs(
245 llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount,
246 llvm::opt::Visibility(IsCLMode ? options::CLOption
247 : options::ClangOption));
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(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(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 = ArgList.getLastArgNoClaim(options::OPT__DASH_DASH)) {
292 auto DashDashIndex = DashDash->getIndex() + 1; // +1 accounts for Cmd[0]
293 // Another +1 so we don't treat the `--` itself as an input.
294 for (unsigned I = DashDashIndex + 1; I < Cmd.size(); ++I)
295 SawInput(Cmd[I]);
296 Cmd.resize(DashDashIndex);
297 }
298
299 llvm::SmallVector<const char *, 16> UnknownArgs;
300
301 for (auto *UnknownArg : ArgList.filtered(options::OPT_UNKNOWN)) {
302 UnknownArgs.push_back(UnknownArg->getValue());
303 IndicesToDrop.push_back(UnknownArg->getIndex());
304 }
305
306 if (!UnknownArgs.empty()) {
307 log("Warning: detected unsupported options '{0}'",
308 llvm::join(UnknownArgs, ", "));
309 }
310
311 llvm::sort(IndicesToDrop);
312 for (unsigned Idx : llvm::reverse(IndicesToDrop))
313 // +1 to account for the executable name in Cmd[0] that
314 // doesn't exist in ArgList.
315 Cmd.erase(Cmd.begin() + Idx + 1);
316 // All the inputs are stripped, append the name for the requested file. Rest
317 // of the modifications should respect `--`.
318 Cmd.push_back("--");
319 Cmd.push_back(File.str());
320
321 if (TransferFrom) {
322 tooling::CompileCommand TransferCmd;
323 TransferCmd.Filename = std::move(*TransferFrom);
324 TransferCmd.CommandLine = std::move(Cmd);
325 TransferCmd = transferCompileCommand(std::move(TransferCmd), File);
326 Cmd = std::move(TransferCmd.CommandLine);
327 assert(Cmd.size() >= 2 && Cmd.back() == File &&
328 Cmd[Cmd.size() - 2] == "--" &&
329 "TransferCommand should produce a command ending in -- filename");
330 }
331
332 for (auto &Edit : Config::current().CompileFlags.Edits)
333 Edit(Cmd);
334
335 // The system include extractor needs to run:
336 // - AFTER transferCompileCommand(), because the -x flag it adds may be
337 // necessary for the system include extractor to identify the file type
338 // - AFTER applying CompileFlags.Edits, because the name of the compiler
339 // that needs to be invoked may come from the CompileFlags->Compiler key
340 // - BEFORE addTargetAndModeForProgramName(), because gcc doesn't support
341 // the target flag that might be added.
342 // - BEFORE resolveDriver() because that can mess up the driver path,
343 // e.g. changing gcc to /path/to/clang/bin/gcc
346 }
347
348 tooling::addTargetAndModeForProgramName(Cmd, Cmd.front());
349
350 // Check whether the flag exists in the command.
351 auto HasExact = [&](llvm::StringRef Flag) {
352 return llvm::is_contained(Cmd, Flag);
353 };
354
355 // Check whether the flag appears in the command as a prefix.
356 auto HasPrefix = [&](llvm::StringRef Flag) {
357 return llvm::any_of(
358 Cmd, [&](llvm::StringRef Arg) { return Arg.starts_with(Flag); });
359 };
360
361 llvm::erase_if(Cmd, [](llvm::StringRef Elem) {
362 return Elem.starts_with("--save-temps") || Elem.starts_with("-save-temps");
363 });
364
365 std::vector<std::string> ToAppend;
366 if (ResourceDir && !HasExact("-resource-dir") && !HasPrefix("-resource-dir="))
367 ToAppend.push_back(("-resource-dir=" + *ResourceDir));
368
369 // Don't set `-isysroot` if it is already set or if `--sysroot` is set.
370 // `--sysroot` is a superset of the `-isysroot` argument.
371 if (Sysroot && !HasPrefix("-isysroot") && !HasExact("--sysroot") &&
372 !HasPrefix("--sysroot=")) {
373 ToAppend.push_back("-isysroot");
374 ToAppend.push_back(*Sysroot);
375 }
376
377 if (!ToAppend.empty()) {
378 Cmd.insert(llvm::find(Cmd, "--"), std::make_move_iterator(ToAppend.begin()),
379 std::make_move_iterator(ToAppend.end()));
380 }
381
382 if (!Cmd.empty()) {
383 bool FollowSymlink = !HasExact("-no-canonical-prefixes");
384 Cmd.front() =
385 (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow)
386 .get(Cmd.front(), [&, this] {
387 return resolveDriver(Cmd.front(), FollowSymlink, ClangPath);
388 });
389 }
390}
391
392// ArgStripper implementation
393namespace {
394
395// Determine total number of args consumed by this option.
396// Return answers for {Exact, Prefix} match. 0 means not allowed.
397std::pair<unsigned, unsigned> getArgCount(const llvm::opt::Option &Opt) {
398 constexpr static unsigned Rest = 10000; // Should be all the rest!
399 // Reference is llvm::opt::Option::acceptInternal()
400 using llvm::opt::Option;
401 switch (Opt.getKind()) {
402 case Option::FlagClass:
403 return {1, 0};
404 case Option::JoinedClass:
405 case Option::CommaJoinedClass:
406 return {1, 1};
407 case Option::GroupClass:
408 case Option::InputClass:
409 case Option::UnknownClass:
410 case Option::ValuesClass:
411 return {1, 0};
412 case Option::JoinedAndSeparateClass:
413 return {2, 2};
414 case Option::SeparateClass:
415 return {2, 0};
416 case Option::MultiArgClass:
417 return {1 + Opt.getNumArgs(), 0};
418 case Option::JoinedOrSeparateClass:
419 return {2, 1};
420 case Option::RemainingArgsClass:
421 return {Rest, 0};
422 case Option::RemainingArgsJoinedClass:
423 return {Rest, Rest};
424 }
425 llvm_unreachable("Unhandled option kind");
426}
427
428// Flag-parsing mode, which affects which flags are available.
429enum DriverMode : unsigned char {
430 DM_None = 0,
431 DM_GCC = 1, // Default mode e.g. when invoked as 'clang'
432 DM_CL = 2, // MS CL.exe compatible mode e.g. when invoked as 'clang-cl'
433 DM_CC1 = 4, // When invoked as 'clang -cc1' or after '-Xclang'
434 DM_All = 7
435};
436
437// Examine args list to determine if we're in GCC, CL-compatible, or cc1 mode.
438DriverMode getDriverMode(const std::vector<std::string> &Args) {
439 DriverMode Mode = DM_GCC;
440 llvm::StringRef Argv0 = Args.front();
441 Argv0.consume_back_insensitive(".exe");
442 if (Argv0.ends_with_insensitive("cl"))
443 Mode = DM_CL;
444 for (const llvm::StringRef Arg : Args) {
445 if (Arg == "--driver-mode=cl") {
446 Mode = DM_CL;
447 break;
448 }
449 if (Arg == "-cc1") {
450 Mode = DM_CC1;
451 break;
452 }
453 }
454 return Mode;
455}
456
457// Returns the set of DriverModes where an option may be used.
458unsigned char getModes(const llvm::opt::Option &Opt) {
459 unsigned char Result = DM_None;
460 if (Opt.hasVisibilityFlag(options::ClangOption))
461 Result |= DM_GCC;
462 if (Opt.hasVisibilityFlag(options::CC1Option))
463 Result |= DM_CC1;
464 if (Opt.hasVisibilityFlag(options::CLOption))
465 Result |= DM_CL;
466 return Result;
467}
468
469} // namespace
470
471llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
472 // All the hard work is done once in a static initializer.
473 // We compute a table containing strings to look for and #args to skip.
474 // e.g. "-x" => {-x 2 args, -x* 1 arg, --language 2 args, --language=* 1 arg}
475 using TableTy =
476 llvm::StringMap<llvm::SmallVector<Rule, 4>, llvm::BumpPtrAllocator>;
477 static TableTy *Table = [] {
478 auto &DriverTable = getDriverOptTable();
479 using DriverID = clang::options::ID;
480
481 // Collect sets of aliases, so we can treat -foo and -foo= as synonyms.
482 // Conceptually a double-linked list: PrevAlias[I] -> I -> NextAlias[I].
483 // If PrevAlias[I] is INVALID, then I is canonical.
484 DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
485 DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
486 auto AddAlias = [&](DriverID Self, DriverID T) {
487 if (NextAlias[T]) {
488 PrevAlias[NextAlias[T]] = Self;
489 NextAlias[Self] = NextAlias[T];
490 }
491 PrevAlias[Self] = T;
492 NextAlias[T] = Self;
493 };
494
495 struct {
496 DriverID ID;
497 DriverID AliasID;
498 const void *AliasArgs;
499 } AliasTable[] = {
500#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
501 FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \
502 METAVAR, VALUES, SUBCOMMANDIDS_OFFSET) \
503 {DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS},
504#include "clang/Options/Options.inc"
505#undef OPTION
506 };
507 for (auto &E : AliasTable)
508 if (E.AliasID != DriverID::OPT_INVALID && E.AliasArgs == nullptr)
509 AddAlias(E.ID, E.AliasID);
510
511 auto Result = std::make_unique<TableTy>();
512 // Iterate over distinct options (represented by the canonical alias).
513 // Every spelling of this option will get the same set of rules.
514 for (unsigned ID = 1 /*Skip INVALID */; ID < DriverID::LastOption; ++ID) {
515 if (PrevAlias[ID] || ID == DriverID::OPT_Xclang)
516 continue; // Not canonical, or specially handled.
517 llvm::SmallVector<Rule> Rules;
518 // Iterate over each alias, to add rules for parsing it.
519 for (unsigned A = ID; A != DriverID::OPT_INVALID; A = NextAlias[A]) {
520 llvm::SmallVector<llvm::StringRef, 4> Prefixes;
521 DriverTable.appendOptionPrefixes(A, Prefixes);
522 if (Prefixes.empty()) // option groups.
523 continue;
524 auto Opt = DriverTable.getOption(A);
525 // Exclude - and -foo pseudo-options.
526 if (Opt.getName().empty())
527 continue;
528 auto Modes = getModes(Opt);
529 std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt);
530 // Iterate over each spelling of the alias, e.g. -foo vs --foo.
531 for (StringRef Prefix : Prefixes) {
532 llvm::SmallString<64> Buf(Prefix);
533 Buf.append(Opt.getName());
534 llvm::StringRef Spelling = Result->try_emplace(Buf).first->getKey();
535 Rules.emplace_back();
536 Rule &R = Rules.back();
537 R.Text = Spelling;
538 R.Modes = Modes;
539 R.ExactArgs = ArgCount.first;
540 R.PrefixArgs = ArgCount.second;
541 // Concrete priority is the index into the option table.
542 // Effectively, earlier entries take priority over later ones.
543 assert(ID < std::numeric_limits<decltype(R.Priority)>::max() &&
544 "Rules::Priority overflowed by options table");
545 R.Priority = ID;
546 }
547 }
548 // Register the set of rules under each possible name.
549 for (const auto &R : Rules)
550 Result->find(R.Text)->second.append(Rules.begin(), Rules.end());
551 }
552#ifndef NDEBUG
553 // Dump the table and various measures of its size.
554 unsigned RuleCount = 0;
555 dlog("ArgStripper Option spelling table");
556 for (const auto &Entry : *Result) {
557 dlog("{0}", Entry.first());
558 RuleCount += Entry.second.size();
559 for (const auto &R : Entry.second)
560 dlog(" {0} #={1} *={2} Mode={3}", R.Text, R.ExactArgs, R.PrefixArgs,
561 int(R.Modes));
562 }
563 dlog("Table spellings={0} rules={1} string-bytes={2}", Result->size(),
564 RuleCount, Result->getAllocator().getBytesAllocated());
565#endif
566 // The static table will never be destroyed.
567 return Result.release();
568 }();
569
570 auto It = Table->find(Arg);
571 return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second;
572}
573
574void ArgStripper::strip(llvm::StringRef Arg) {
575 auto OptionRules = rulesFor(Arg);
576 if (OptionRules.empty()) {
577 // Not a recognized flag. Strip it literally.
578 Storage.emplace_back(Arg);
579 Rules.emplace_back();
580 Rules.back().Text = Storage.back();
581 Rules.back().ExactArgs = 1;
582 if (Rules.back().Text.consume_back("*"))
583 Rules.back().PrefixArgs = 1;
584 Rules.back().Modes = DM_All;
585 Rules.back().Priority = -1; // Max unsigned = lowest priority.
586 } else {
587 Rules.append(OptionRules.begin(), OptionRules.end());
588 }
589}
590
591const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg,
592 unsigned Mode,
593 unsigned &ArgCount) const {
594 const ArgStripper::Rule *BestRule = nullptr;
595 for (const Rule &R : Rules) {
596 // Rule can fail to match if...
597 if (!(R.Modes & Mode))
598 continue; // not applicable to current driver mode
599 if (BestRule && BestRule->Priority < R.Priority)
600 continue; // lower-priority than best candidate.
601 if (!Arg.starts_with(R.Text))
602 continue; // current arg doesn't match the prefix string
603 bool PrefixMatch = Arg.size() > R.Text.size();
604 // Can rule apply as an exact/prefix match?
605 if (unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) {
606 BestRule = &R;
607 ArgCount = Count;
608 }
609 // Continue in case we find a higher-priority rule.
610 }
611 return BestRule;
612}
613
614void ArgStripper::process(std::vector<std::string> &Args) const {
615 if (Args.empty())
616 return;
617
618 // We're parsing the args list in some mode (e.g. gcc-compatible) but may
619 // temporarily switch to another mode with the -Xclang flag.
620 DriverMode MainMode = getDriverMode(Args);
621 DriverMode CurrentMode = MainMode;
622
623 // Read and write heads for in-place deletion.
624 unsigned Read = 0, Write = 0;
625 bool WasXclang = false;
626 while (Read < Args.size()) {
627 unsigned ArgCount = 0;
628 if (matchingRule(Args[Read], CurrentMode, ArgCount)) {
629 // Delete it and its args.
630 if (WasXclang) {
631 assert(Write > 0);
632 --Write; // Drop previous -Xclang arg
633 CurrentMode = MainMode;
634 WasXclang = false;
635 }
636 // Advance to last arg. An arg may be foo or -Xclang foo.
637 for (unsigned I = 1; Read < Args.size() && I < ArgCount; ++I) {
638 ++Read;
639 if (Read < Args.size() && Args[Read] == "-Xclang")
640 ++Read;
641 }
642 } else {
643 // No match, just copy the arg through.
644 WasXclang = Args[Read] == "-Xclang";
645 CurrentMode = WasXclang ? DM_CC1 : MainMode;
646 if (Write != Read)
647 Args[Write] = std::move(Args[Read]);
648 ++Write;
649 }
650 ++Read;
651 }
652 Args.resize(Write);
653}
654
655std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) {
656 std::string Buf;
657 llvm::raw_string_ostream OS(Buf);
658 bool Sep = false;
659 for (llvm::StringRef Arg : Args) {
660 if (Sep)
661 OS << ' ';
662 Sep = true;
663 if (llvm::all_of(Arg, llvm::isPrint) &&
664 Arg.find_first_of(" \t\n\"\\") == llvm::StringRef::npos) {
665 OS << Arg;
666 continue;
667 }
668 OS << '"';
669 OS.write_escaped(Arg, /*UseHexEscapes=*/true);
670 OS << '"';
671 }
672 return std::move(OS.str());
673}
674
675std::string printArgv(llvm::ArrayRef<std::string> Args) {
676 std::vector<llvm::StringRef> Refs(Args.size());
677 llvm::copy(Args, Refs.begin());
678 return printArgv(Refs);
679}
680
681} // namespace clangd
682} // namespace clang
#define dlog(...)
Definition Logger.h:101
const char * Argv0
void process(std::vector< std::string > &Args) const
void strip(llvm::StringRef Arg)
Records an event whose duration is the lifetime of the Span object.
Definition Trace.h:143
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
void vlog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:72
void log(const char *Fmt, Ts &&... Vals)
Definition Logger.h:67
std::string printArgv(llvm::ArrayRef< llvm::StringRef > Args)
std::string Path
A typedef to represent a file path.
Definition Path.h:26
void elog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:61
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::optional< std::string > ResourceDir
static CommandMangler detect()
SystemIncludeExtractorFn SystemIncludeExtractor
std::optional< std::string > ClangPath
std::optional< std::string > Sysroot
static CommandMangler forTests()
void operator()(tooling::CompileCommand &Cmd, llvm::StringRef TargetFile) const
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
Definition Config.cpp:17
A set of edits generated for a single file.
Definition SourceCode.h:189