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"
43std::optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
44 auto Xcrun = llvm::sys::findProgramByName(
"xcrun");
46 log(
"Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
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 {
""}, {OutFile.str()}, {
""}};
54 vlog(
"Invoking {0} to find clang installation", *Xcrun);
55 int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv,
56 std::nullopt, Redirects,
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.",
66 auto Buf = llvm::MemoryBuffer::getFile(OutFile);
68 log(
"Can't read xcrun output: {0}", Buf.getError().message());
71 StringRef
Path = Buf->get()->getBuffer().trim();
73 log(
"xcrun produced no output");
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);
86 return std::string(Resolved.str());
92std::string detectClangPath() {
104 if (
auto MacClang = queryXcrun({
"xcrun",
"--find",
"clang"}))
105 return resolve(std::move(*MacClang));
108 for (
const char *
Name : {
"clang",
"gcc",
"cc"})
109 if (
auto PathCC = llvm::sys::findProgramByName(
Name))
110 return resolve(std::move(*PathCC));
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());
123std::optional<std::string> detectSysroot() {
129 if (::getenv(
"SDKROOT"))
131 return queryXcrun({
"xcrun",
"--show-sdk-path"});
134std::string detectStandardResourceDir() {
135 static int StaticForMainAddr;
136 return CompilerInvocation::GetResourcesPath(
"clangd",
137 (
void *)&StaticForMainAddr);
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();
155 if (!llvm::sys::path::is_absolute(
Driver)) {
160 [](
char C) {
return llvm::sys::path::is_separator(
C); }))
166 return SiblingOf(*ClangPath);
169 auto Absolute = llvm::sys::findProgramByName(
Driver);
170 if (Absolute && llvm::sys::path::is_absolute(*Absolute))
171 Driver = Storage = std::move(*Absolute);
173 return SiblingOf(*ClangPath);
179 assert(llvm::sys::path::is_absolute(
Driver));
181 llvm::SmallString<256> Resolved;
182 if (!llvm::sys::fs::real_path(
Driver, Resolved))
183 return SiblingOf(Resolved);
190CommandMangler::CommandMangler() {
191 Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
192 ? llvm::cl::TokenizeWindowsCommandLine
193 : llvm::cl::TokenizeGNUCommandLine;
198 Result.ClangPath = detectClangPath();
199 Result.ResourceDir = detectStandardResourceDir();
200 Result.Sysroot = detectSysroot();
207 llvm::StringRef
File)
const {
208 std::vector<std::string> &Cmd = Command.CommandLine;
221 auto FS = llvm::vfs::getRealFileSystem();
222 tooling::addExpandedResponseFiles(Cmd, Command.Directory, Tokenizer, *FS);
224 auto &OptTable = clang::driver::getDriverOptTable();
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)));
234 unsigned IgnoredCount;
237 llvm::opt::InputArgList ArgList;
238 ArgList = OptTable.ParseArgs(
239 llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount,
241 IsCLMode ? (driver::options::CLOption | driver::options::CoreOption |
242 driver::options::CLDXCOption)
244 driver::options::NoDriverOption |
247 : (driver::options::CLOption | driver::options::CLDXCOption)));
249 llvm::SmallVector<unsigned, 1> IndicesToDrop;
255 unsigned ArchOptCount = 0;
256 for (
auto *Input : ArgList.filtered(driver::options::OPT_arch)) {
258 for (
auto I = 0U; I <= Input->getNumValues(); ++I)
259 IndicesToDrop.push_back(Input->getIndex() + I);
262 if (ArchOptCount < 2)
263 IndicesToDrop.clear();
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);
286 for (
auto *Input : ArgList.filtered(driver::options::OPT_INPUT)) {
287 SawInput(Input->getValue(0));
288 IndicesToDrop.push_back(Input->getIndex());
292 ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) {
293 auto DashDashIndex = DashDash->getIndex() + 1;
294 for (
unsigned I = DashDashIndex; I < Cmd.size(); ++I)
296 Cmd.resize(DashDashIndex);
298 llvm::sort(IndicesToDrop);
299 llvm::for_each(llvm::reverse(IndicesToDrop),
302 [&Cmd](
unsigned Idx) { Cmd.erase(Cmd.begin() + Idx + 1); });
306 Cmd.push_back(
File.str());
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");
335 tooling::addTargetAndModeForProgramName(Cmd, Cmd.front());
338 auto Has = [&](llvm::StringRef Flag) {
339 for (llvm::StringRef Arg : Cmd) {
340 if (Arg.consume_front(Flag) && (Arg.empty() || Arg[0] ==
'='))
346 llvm::erase_if(Cmd, [](llvm::StringRef Elem) {
347 return Elem.startswith(
"--save-temps") || Elem.startswith(
"-save-temps");
350 std::vector<std::string> ToAppend;
352 ToAppend.push_back((
"-resource-dir=" + *
ResourceDir));
356 if (
Sysroot && !Has(
"-isysroot") && !Has(
"--sysroot")) {
357 ToAppend.push_back(
"-isysroot");
361 if (!ToAppend.empty()) {
362 Cmd.insert(llvm::find(Cmd,
"--"), std::make_move_iterator(ToAppend.begin()),
363 std::make_move_iterator(ToAppend.end()));
367 bool FollowSymlink = !Has(
"-no-canonical-prefixes");
369 (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow)
370 .get(Cmd.front(), [&,
this] {
371 return resolveDriver(Cmd.front(), FollowSymlink,
ClangPath);
381std::pair<unsigned, unsigned> getArgCount(
const llvm::opt::Option &Opt) {
382 constexpr static unsigned Rest = 10000;
384 using llvm::opt::Option;
385 switch (Opt.getKind()) {
386 case Option::FlagClass:
388 case Option::JoinedClass:
389 case Option::CommaJoinedClass:
391 case Option::GroupClass:
392 case Option::InputClass:
393 case Option::UnknownClass:
394 case Option::ValuesClass:
396 case Option::JoinedAndSeparateClass:
398 case Option::SeparateClass:
400 case Option::MultiArgClass:
401 return {1 + Opt.getNumArgs(), 0};
402 case Option::JoinedOrSeparateClass:
404 case Option::RemainingArgsClass:
406 case Option::RemainingArgsJoinedClass:
409 llvm_unreachable(
"Unhandled option kind");
413enum DriverMode :
unsigned char {
422DriverMode getDriverMode(
const std::vector<std::string> &
Args) {
423 DriverMode Mode = DM_GCC;
425 if (
Argv0.ends_with_insensitive(
".exe"))
427 if (
Argv0.ends_with_insensitive(
"cl"))
429 for (
const llvm::StringRef Arg :
Args) {
430 if (Arg ==
"--driver-mode=cl") {
443unsigned char getModes(
const llvm::opt::Option &Opt) {
446 unsigned char Result = DM_None;
447 if (Opt.hasFlag(driver::options::CC1Option))
449 if (!Opt.hasFlag(driver::options::NoDriverOption)) {
450 if (Opt.hasFlag(driver::options::CLOption)) {
452 }
else if (Opt.hasFlag(driver::options::CLDXCOption)) {
456 if (Opt.hasFlag(driver::options::CoreOption)) {
466llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
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;
479 DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
480 DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
481 auto AddAlias = [&](DriverID Self, DriverID T) {
483 PrevAlias[NextAlias[T]] = Self;
484 NextAlias[Self] = NextAlias[T];
490 llvm::ArrayRef<llvm::StringLiteral> Prefixes[DriverID::LastOption];
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"
506 const void *AliasArgs;
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"
514 for (
auto &
E : AliasTable)
515 if (
E.AliasID != DriverID::OPT_INVALID &&
E.AliasArgs ==
nullptr)
516 AddAlias(
E.ID,
E.AliasID);
518 auto Result = std::make_unique<TableTy>();
521 for (
unsigned ID = 1 ; ID < DriverID::LastOption; ++ID) {
522 if (PrevAlias[ID] || ID == DriverID::OPT_Xclang)
524 llvm::SmallVector<Rule> Rules;
526 for (
unsigned A = ID;
A != DriverID::OPT_INVALID;
A = NextAlias[
A]) {
527 if (!Prefixes[A].size())
529 auto Opt = DriverTable.getOption(A);
531 if (Opt.getName().empty())
533 auto Modes = getModes(Opt);
534 std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt);
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();
544 R.ExactArgs = ArgCount.first;
545 R.PrefixArgs = ArgCount.second;
548 assert(ID < std::numeric_limits<
decltype(R.Priority)>::max() &&
549 "Rules::Priority overflowed by options table");
554 for (
const auto &R : Rules)
555 Result->find(R.Text)->second.append(Rules.begin(), Rules.end());
559 unsigned RuleCount = 0;
560 dlog(
"ArgStripper Option spelling table");
561 for (
const auto &
Entry : *Result) {
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,
568 dlog(
"Table spellings={0} rules={1} string-bytes={2}", Result->size(),
569 RuleCount, Result->getAllocator().getBytesAllocated());
572 return Result.release();
575 auto It = Table->find(Arg);
576 return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second;
580 auto OptionRules = rulesFor(Arg);
581 if (OptionRules.empty()) {
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;
592 Rules.append(OptionRules.begin(), OptionRules.end());
596const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg,
598 unsigned &ArgCount)
const {
599 const ArgStripper::Rule *BestRule =
nullptr;
600 for (
const Rule &R : Rules) {
602 if (!(R.Modes & Mode))
604 if (BestRule && BestRule->Priority < R.Priority)
606 if (!Arg.startswith(R.Text))
608 bool PrefixMatch = Arg.size() > R.Text.size();
610 if (
unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) {
625 DriverMode MainMode = getDriverMode(
Args);
626 DriverMode CurrentMode = MainMode;
630 bool WasXclang =
false;
632 unsigned ArgCount = 0;
633 if (matchingRule(
Args[
Read], CurrentMode, ArgCount)) {
638 CurrentMode = MainMode;
642 for (
unsigned I = 1;
Read <
Args.size() && I < ArgCount; ++I) {
649 WasXclang =
Args[
Read] ==
"-Xclang";
650 CurrentMode = WasXclang ? DM_CC1 : MainMode;
662 llvm::raw_string_ostream
OS(Buf);
664 for (llvm::StringRef Arg :
Args) {
668 if (llvm::all_of(Arg, llvm::isPrint) &&
669 Arg.find_first_of(
" \t\n\"\\") == llvm::StringRef::npos) {
674 OS.write_escaped(Arg,
true);
677 return std::move(
OS.str());
681 std::vector<llvm::StringRef> Refs(
Args.size());
682 llvm::copy(
Args, Refs.begin());
llvm::raw_string_ostream OS
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.
std::string Path
A typedef to represent a file path.
void vlog(const char *Fmt, Ts &&... Vals)
void log(const char *Fmt, Ts &&... Vals)
std::string printArgv(llvm::ArrayRef< llvm::StringRef > Args)
===– 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.
std::vector< llvm::unique_function< void(std::vector< std::string > &) const > > Edits
Edits to apply to the compile command, in sequence.
A set of edits generated for a single file.