#include "CompileCommands.h"
#include "Config.h"
#include "support/Logger.h"
#include "support/Trace.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Options.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include <iterator>
#include <optional>
#include <string>
#include <vector>
namespace clang {
namespace clangd {
namespace {
std::optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
auto Xcrun = llvm::sys::findProgramByName("xcrun");
if (!Xcrun) {
log("Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
return std::nullopt;
}
llvm::SmallString<64> OutFile;
llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile);
llvm::FileRemover OutRemover(OutFile);
std::optional<llvm::StringRef> Redirects[3] = {
{""}, {OutFile.str()}, {""}};
vlog("Invoking {0} to find clang installation", *Xcrun);
int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv,
std::nullopt, Redirects,
10);
if (Ret != 0) {
log("xcrun exists but failed with code {0}. "
"If you have a non-apple toolchain, this is OK. "
"Otherwise, try xcode-select --install.",
Ret);
return std::nullopt;
}
auto Buf = llvm::MemoryBuffer::getFile(OutFile);
if (!Buf) {
log("Can't read xcrun output: {0}", Buf.getError().message());
return std::nullopt;
}
StringRef Path = Buf->get()->getBuffer().trim();
if (Path.empty()) {
log("xcrun produced no output");
return std::nullopt;
}
return Path.str();
}
std::string resolve(std::string Path) {
llvm::SmallString<128> Resolved;
if (llvm::sys::fs::real_path(Path, Resolved)) {
log("Failed to resolve possible symlink {0}", Path);
return Path;
}
return std::string(Resolved);
}
std::string detectClangPath() {
#ifdef __APPLE__
if (auto MacClang = queryXcrun({"xcrun", "--find", "clang"}))
return resolve(std::move(*MacClang));
#endif
for (const char *Name : {"clang", "gcc", "cc"})
if (auto PathCC = llvm::sys::findProgramByName(Name))
return resolve(std::move(*PathCC));
static int StaticForMainAddr;
std::string ClangdExecutable =
llvm::sys::fs::getMainExecutable("clangd", (void *)&StaticForMainAddr);
SmallString<128> ClangPath;
ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
llvm::sys::path::append(ClangPath, "clang");
return std::string(ClangPath);
}
std::optional<std::string> detectSysroot() {
#ifndef __APPLE__
return std::nullopt;
#endif
if (::getenv("SDKROOT"))
return std::nullopt;
return queryXcrun({"xcrun", "--show-sdk-path"});
}
std::string detectStandardResourceDir() {
static int StaticForMainAddr;
return CompilerInvocation::GetResourcesPath("clangd",
(void *)&StaticForMainAddr);
}
static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink,
std::optional<std::string> ClangPath) {
auto SiblingOf = [&](llvm::StringRef AbsPath) {
llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath);
llvm::sys::path::append(Result, llvm::sys::path::filename(Driver));
return Result.str().str();
};
std::string Storage;
if (!llvm::sys::path::is_absolute(Driver)) {
if (llvm::any_of(Driver,
[](char C) { return llvm::sys::path::is_separator(C); }))
return Driver.str();
if (ClangPath &&
(Driver == "clang" || Driver == "clang++" || Driver == "gcc" ||
Driver == "g++" || Driver == "cc" || Driver == "c++")) {
return SiblingOf(*ClangPath);
}
auto Absolute = llvm::sys::findProgramByName(Driver);
if (Absolute && llvm::sys::path::is_absolute(*Absolute))
Driver = Storage = std::move(*Absolute);
else if (ClangPath)
return SiblingOf(*ClangPath);
else
return Driver.str();
}
assert(llvm::sys::path::is_absolute(Driver));
if (FollowSymlink) {
llvm::SmallString<256> Resolved;
if (!llvm::sys::fs::real_path(Driver, Resolved))
return SiblingOf(Resolved);
}
return Driver.str();
}
}
CommandMangler CommandMangler::detect() {
CommandMangler Result;
Result.ClangPath = detectClangPath();
Result.ResourceDir = detectStandardResourceDir();
Result.Sysroot = detectSysroot();
return Result;
}
CommandMangler CommandMangler::forTests() { return CommandMangler(); }
void CommandMangler::operator()(tooling::CompileCommand &Command,
llvm::StringRef File) const {
std::vector<std::string> &Cmd = Command.CommandLine;
trace::Span S("AdjustCompileFlags");
if (Cmd.empty())
return;
auto &OptTable = clang::driver::getDriverOptTable();
llvm::SmallVector<const char *, 16> OriginalArgs;
OriginalArgs.reserve(Cmd.size());
for (const auto &S : Cmd)
OriginalArgs.push_back(S.c_str());
bool IsCLMode = driver::IsClangCL(driver::getDriverMode(
OriginalArgs[0], llvm::ArrayRef(OriginalArgs).slice(1)));
unsigned IgnoredCount;
llvm::opt::InputArgList ArgList;
ArgList = OptTable.ParseArgs(
llvm::ArrayRef(OriginalArgs).drop_front(), IgnoredCount, IgnoredCount,
llvm::opt::Visibility(IsCLMode ? driver::options::CLOption
: driver::options::ClangOption));
llvm::SmallVector<unsigned, 1> IndicesToDrop;
unsigned ArchOptCount = 0;
for (auto *Input : ArgList.filtered(driver::options::OPT_arch)) {
++ArchOptCount;
for (auto I = 0U; I <= Input->getNumValues(); ++I)
IndicesToDrop.push_back(Input->getIndex() + I);
}
if (ArchOptCount < 2)
IndicesToDrop.clear();
llvm::StringRef FileExtension = llvm::sys::path::extension(File);
std::optional<std::string> TransferFrom;
auto SawInput = [&](llvm::StringRef Input) {
if (llvm::sys::path::extension(Input) != FileExtension)
TransferFrom.emplace(Input);
};
for (auto *Input : ArgList.filtered(driver::options::OPT_INPUT)) {
SawInput(Input->getValue(0));
IndicesToDrop.push_back(Input->getIndex());
}
if (auto *DashDash =
ArgList.getLastArgNoClaim(driver::options::OPT__DASH_DASH)) {
auto DashDashIndex = DashDash->getIndex() + 1;
for (unsigned I = DashDashIndex; I < Cmd.size(); ++I)
SawInput(Cmd[I]);
Cmd.resize(DashDashIndex);
}
llvm::sort(IndicesToDrop);
for (unsigned Idx : llvm::reverse(IndicesToDrop))
Cmd.erase(Cmd.begin() + Idx + 1);
Cmd.push_back("--");
Cmd.push_back(File.str());
if (TransferFrom) {
tooling::CompileCommand TransferCmd;
TransferCmd.Filename = std::move(*TransferFrom);
TransferCmd.CommandLine = std::move(Cmd);
TransferCmd = transferCompileCommand(std::move(TransferCmd), File);
Cmd = std::move(TransferCmd.CommandLine);
assert(Cmd.size() >= 2 && Cmd.back() == File &&
Cmd[Cmd.size() - 2] == "--" &&
"TransferCommand should produce a command ending in -- filename");
}
for (auto &Edit : Config::current().CompileFlags.Edits)
Edit(Cmd);
if (SystemIncludeExtractor) {
SystemIncludeExtractor(Command, File);
}
tooling::addTargetAndModeForProgramName(Cmd, Cmd.front());
auto HasExact = [&](llvm::StringRef Flag) {
return llvm::any_of(Cmd, [&](llvm::StringRef Arg) { return Arg == Flag; });
};
auto HasPrefix = [&](llvm::StringRef Flag) {
return llvm::any_of(
Cmd, [&](llvm::StringRef Arg) { return Arg.starts_with(Flag); });
};
llvm::erase_if(Cmd, [](llvm::StringRef Elem) {
return Elem.starts_with("--save-temps") || Elem.starts_with("-save-temps");
});
std::vector<std::string> ToAppend;
if (ResourceDir && !HasExact("-resource-dir") && !HasPrefix("-resource-dir="))
ToAppend.push_back(("-resource-dir=" + *ResourceDir));
if (Sysroot && !HasPrefix("-isysroot") && !HasExact("--sysroot") &&
!HasPrefix("--sysroot=")) {
ToAppend.push_back("-isysroot");
ToAppend.push_back(*Sysroot);
}
if (!ToAppend.empty()) {
Cmd.insert(llvm::find(Cmd, "--"), std::make_move_iterator(ToAppend.begin()),
std::make_move_iterator(ToAppend.end()));
}
if (!Cmd.empty()) {
bool FollowSymlink = !HasExact("-no-canonical-prefixes");
Cmd.front() =
(FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow)
.get(Cmd.front(), [&, this] {
return resolveDriver(Cmd.front(), FollowSymlink, ClangPath);
});
}
}
namespace {
std::pair<unsigned, unsigned> getArgCount(const llvm::opt::Option &Opt) {
constexpr static unsigned Rest = 10000;
using llvm::opt::Option;
switch (Opt.getKind()) {
case Option::FlagClass:
return {1, 0};
case Option::JoinedClass:
case Option::CommaJoinedClass:
return {1, 1};
case Option::GroupClass:
case Option::InputClass:
case Option::UnknownClass:
case Option::ValuesClass:
return {1, 0};
case Option::JoinedAndSeparateClass:
return {2, 2};
case Option::SeparateClass:
return {2, 0};
case Option::MultiArgClass:
return {1 + Opt.getNumArgs(), 0};
case Option::JoinedOrSeparateClass:
return {2, 1};
case Option::RemainingArgsClass:
return {Rest, 0};
case Option::RemainingArgsJoinedClass:
return {Rest, Rest};
}
llvm_unreachable("Unhandled option kind");
}
enum DriverMode : unsigned char {
DM_None = 0,
DM_GCC = 1,
DM_CL = 2,
DM_CC1 = 4,
DM_All = 7
};
DriverMode getDriverMode(const std::vector<std::string> &Args) {
DriverMode Mode = DM_GCC;
llvm::StringRef Argv0 = Args.front();
if (Argv0.ends_with_insensitive(".exe"))
Argv0 = Argv0.drop_back(strlen(".exe"));
if (Argv0.ends_with_insensitive("cl"))
Mode = DM_CL;
for (const llvm::StringRef Arg : Args) {
if (Arg == "--driver-mode=cl") {
Mode = DM_CL;
break;
}
if (Arg == "-cc1") {
Mode = DM_CC1;
break;
}
}
return Mode;
}
unsigned char getModes(const llvm::opt::Option &Opt) {
unsigned char Result = DM_None;
if (Opt.hasVisibilityFlag(driver::options::ClangOption))
Result |= DM_GCC;
if (Opt.hasVisibilityFlag(driver::options::CC1Option))
Result |= DM_CC1;
if (Opt.hasVisibilityFlag(driver::options::CLOption))
Result |= DM_CL;
return Result;
}
}
llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
using TableTy =
llvm::StringMap<llvm::SmallVector<Rule, 4>, llvm::BumpPtrAllocator>;
static TableTy *Table = [] {
auto &DriverTable = driver::getDriverOptTable();
using DriverID = clang::driver::options::ID;
DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
auto AddAlias = [&](DriverID Self, DriverID T) {
if (NextAlias[T]) {
PrevAlias[NextAlias[T]] = Self;
NextAlias[Self] = NextAlias[T];
}
PrevAlias[Self] = T;
NextAlias[T] = Self;
};
llvm::ArrayRef<llvm::StringLiteral> Prefixes[DriverID::LastOption];
#define PREFIX(NAME, VALUE) \
static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
NAME##_init, std::size(NAME##_init) - 1);
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \
METAVAR, VALUES) \
Prefixes[DriverID::OPT_##ID] = PREFIX;
#include "clang/Driver/Options.inc"
#undef OPTION
#undef PREFIX
struct {
DriverID ID;
DriverID AliasID;
const void *AliasArgs;
} AliasTable[] = {
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \
METAVAR, VALUES) \
{DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS},
#include "clang/Driver/Options.inc"
#undef OPTION
};
for (auto &E : AliasTable)
if (E.AliasID != DriverID::OPT_INVALID && E.AliasArgs == nullptr)
AddAlias(E.ID, E.AliasID);
auto Result = std::make_unique<TableTy>();
for (unsigned ID = 1 ; ID < DriverID::LastOption; ++ID) {
if (PrevAlias[ID] || ID == DriverID::OPT_Xclang)
continue;
llvm::SmallVector<Rule> Rules;
for (unsigned A = ID; A != DriverID::OPT_INVALID; A = NextAlias[A]) {
if (!Prefixes[A].size())
continue;
auto Opt = DriverTable.getOption(A);
if (Opt.getName().empty())
continue;
auto Modes = getModes(Opt);
std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt);
for (StringRef Prefix : Prefixes[A]) {
llvm::SmallString<64> Buf(Prefix);
Buf.append(Opt.getName());
llvm::StringRef Spelling = Result->try_emplace(Buf).first->getKey();
Rules.emplace_back();
Rule &R = Rules.back();
R.Text = Spelling;
R.Modes = Modes;
R.ExactArgs = ArgCount.first;
R.PrefixArgs = ArgCount.second;
assert(ID < std::numeric_limits<decltype(R.Priority)>::max() &&
"Rules::Priority overflowed by options table");
R.Priority = ID;
}
}
for (const auto &R : Rules)
Result->find(R.Text)->second.append(Rules.begin(), Rules.end());
}
#ifndef NDEBUG
unsigned RuleCount = 0;
dlog("ArgStripper Option spelling table");
for (const auto &Entry : *Result) {
dlog("{0}", Entry.first());
RuleCount += Entry.second.size();
for (const auto &R : Entry.second)
dlog(" {0} #={1} *={2} Mode={3}", R.Text, R.ExactArgs, R.PrefixArgs,
int(R.Modes));
}
dlog("Table spellings={0} rules={1} string-bytes={2}", Result->size(),
RuleCount, Result->getAllocator().getBytesAllocated());
#endif
return Result.release();
}();
auto It = Table->find(Arg);
return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second;
}
void ArgStripper::strip(llvm::StringRef Arg) {
auto OptionRules = rulesFor(Arg);
if (OptionRules.empty()) {
Storage.emplace_back(Arg);
Rules.emplace_back();
Rules.back().Text = Storage.back();
Rules.back().ExactArgs = 1;
if (Rules.back().Text.consume_back("*"))
Rules.back().PrefixArgs = 1;
Rules.back().Modes = DM_All;
Rules.back().Priority = -1;
} else {
Rules.append(OptionRules.begin(), OptionRules.end());
}
}
const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg,
unsigned Mode,
unsigned &ArgCount) const {
const ArgStripper::Rule *BestRule = nullptr;
for (const Rule &R : Rules) {
if (!(R.Modes & Mode))
continue;
if (BestRule && BestRule->Priority < R.Priority)
continue;
if (!Arg.starts_with(R.Text))
continue;
bool PrefixMatch = Arg.size() > R.Text.size();
if (unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) {
BestRule = &R;
ArgCount = Count;
}
}
return BestRule;
}
void ArgStripper::process(std::vector<std::string> &Args) const {
if (Args.empty())
return;
DriverMode MainMode = getDriverMode(Args);
DriverMode CurrentMode = MainMode;
unsigned Read = 0, Write = 0;
bool WasXclang = false;
while (Read < Args.size()) {
unsigned ArgCount = 0;
if (matchingRule(Args[Read], CurrentMode, ArgCount)) {
if (WasXclang) {
assert(Write > 0);
--Write;
CurrentMode = MainMode;
WasXclang = false;
}
for (unsigned I = 1; Read < Args.size() && I < ArgCount; ++I) {
++Read;
if (Read < Args.size() && Args[Read] == "-Xclang")
++Read;
}
} else {
WasXclang = Args[Read] == "-Xclang";
CurrentMode = WasXclang ? DM_CC1 : MainMode;
if (Write != Read)
Args[Write] = std::move(Args[Read]);
++Write;
}
++Read;
}
Args.resize(Write);
}
std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) {
std::string Buf;
llvm::raw_string_ostream OS(Buf);
bool Sep = false;
for (llvm::StringRef Arg : Args) {
if (Sep)
OS << ' ';
Sep = true;
if (llvm::all_of(Arg, llvm::isPrint) &&
Arg.find_first_of(" \t\n\"\\") == llvm::StringRef::npos) {
OS << Arg;
continue;
}
OS << '"';
OS.write_escaped(Arg, true);
OS << '"';
}
return std::move(OS.str());
}
std::string printArgv(llvm::ArrayRef<std::string> Args) {
std::vector<llvm::StringRef> Refs(Args.size());
llvm::copy(Args, Refs.begin());
return printArgv(Refs);
}
}
}