• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "clang/Driver/Options.h"
13 #include "clang/Frontend/CompilerInvocation.h"
14 #include "clang/Tooling/ArgumentsAdjusters.h"
15 #include "llvm/Option/Option.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Program.h"
23 
24 namespace clang {
25 namespace clangd {
26 namespace {
27 
28 // Query apple's `xcrun` launcher, which is the source of truth for "how should"
29 // clang be invoked on this system.
queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv)30 llvm::Optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
31   auto Xcrun = llvm::sys::findProgramByName("xcrun");
32   if (!Xcrun) {
33     log("Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
34     return llvm::None;
35   }
36   llvm::SmallString<64> OutFile;
37   llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile);
38   llvm::FileRemover OutRemover(OutFile);
39   llvm::Optional<llvm::StringRef> Redirects[3] = {
40       /*stdin=*/{""}, /*stdout=*/{OutFile}, /*stderr=*/{""}};
41   vlog("Invoking {0} to find clang installation", *Xcrun);
42   int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv,
43                                       /*Env=*/llvm::None, Redirects,
44                                       /*SecondsToWait=*/10);
45   if (Ret != 0) {
46     log("xcrun exists but failed with code {0}. "
47         "If you have a non-apple toolchain, this is OK. "
48         "Otherwise, try xcode-select --install.",
49         Ret);
50     return llvm::None;
51   }
52 
53   auto Buf = llvm::MemoryBuffer::getFile(OutFile);
54   if (!Buf) {
55     log("Can't read xcrun output: {0}", Buf.getError().message());
56     return llvm::None;
57   }
58   StringRef Path = Buf->get()->getBuffer().trim();
59   if (Path.empty()) {
60     log("xcrun produced no output");
61     return llvm::None;
62   }
63   return Path.str();
64 }
65 
66 // Resolve symlinks if possible.
resolve(std::string Path)67 std::string resolve(std::string Path) {
68   llvm::SmallString<128> Resolved;
69   if (llvm::sys::fs::real_path(Path, Resolved)) {
70     log("Failed to resolve possible symlink {0}", Path);
71     return Path;
72   }
73   return std::string(Resolved.str());
74 }
75 
76 // Get a plausible full `clang` path.
77 // This is used in the fallback compile command, or when the CDB returns a
78 // generic driver with no path.
detectClangPath()79 std::string detectClangPath() {
80   // The driver and/or cc1 sometimes depend on the binary name to compute
81   // useful things like the standard library location.
82   // We need to emulate what clang on this system is likely to see.
83   // cc1 in particular looks at the "real path" of the running process, and
84   // so if /usr/bin/clang is a symlink, it sees the resolved path.
85   // clangd doesn't have that luxury, so we resolve symlinks ourselves.
86 
87   // On Mac, `which clang` is /usr/bin/clang. It runs `xcrun clang`, which knows
88   // where the real clang is kept. We need to do the same thing,
89   // because cc1 (not the driver!) will find libc++ relative to argv[0].
90 #ifdef __APPLE__
91   if (auto MacClang = queryXcrun({"xcrun", "--find", "clang"}))
92     return resolve(std::move(*MacClang));
93 #endif
94   // On other platforms, just look for compilers on the PATH.
95   for (const char *Name : {"clang", "gcc", "cc"})
96     if (auto PathCC = llvm::sys::findProgramByName(Name))
97       return resolve(std::move(*PathCC));
98   // Fallback: a nonexistent 'clang' binary next to clangd.
99   static int Dummy;
100   std::string ClangdExecutable =
101       llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
102   SmallString<128> ClangPath;
103   ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
104   llvm::sys::path::append(ClangPath, "clang");
105   return std::string(ClangPath.str());
106 }
107 
108 // On mac, /usr/bin/clang sets SDKROOT and then invokes the real clang.
109 // The effect of this is to set -isysroot correctly. We do the same.
detectSysroot()110 const llvm::Optional<std::string> detectSysroot() {
111 #ifndef __APPLE__
112   return llvm::None;
113 #endif
114 
115   // SDKROOT overridden in environment, respect it. Driver will set isysroot.
116   if (::getenv("SDKROOT"))
117     return llvm::None;
118   return queryXcrun({"xcrun", "--show-sdk-path"});
119   return llvm::None;
120 }
121 
detectStandardResourceDir()122 std::string detectStandardResourceDir() {
123   static int Dummy; // Just an address in this process.
124   return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
125 }
126 
127 // The path passed to argv[0] is important:
128 //  - its parent directory is Driver::Dir, used for library discovery
129 //  - its basename affects CLI parsing (clang-cl) and other settings
130 // Where possible it should be an absolute path with sensible directory, but
131 // with the original basename.
resolveDriver(llvm::StringRef Driver,bool FollowSymlink,llvm::Optional<std::string> ClangPath)132 static std::string resolveDriver(llvm::StringRef Driver, bool FollowSymlink,
133                                  llvm::Optional<std::string> ClangPath) {
134   auto SiblingOf = [&](llvm::StringRef AbsPath) {
135     llvm::SmallString<128> Result = llvm::sys::path::parent_path(AbsPath);
136     llvm::sys::path::append(Result, llvm::sys::path::filename(Driver));
137     return Result.str().str();
138   };
139 
140   // First, eliminate relative paths.
141   std::string Storage;
142   if (!llvm::sys::path::is_absolute(Driver)) {
143     // If it's working-dir relative like bin/clang, we can't resolve it.
144     // FIXME: we could if we had the working directory here.
145     // Let's hope it's not a symlink.
146     if (llvm::any_of(Driver,
147                      [](char C) { return llvm::sys::path::is_separator(C); }))
148       return Driver.str();
149     // If the driver is a generic like "g++" with no path, add clang dir.
150     if (ClangPath &&
151         (Driver == "clang" || Driver == "clang++" || Driver == "gcc" ||
152          Driver == "g++" || Driver == "cc" || Driver == "c++")) {
153       return SiblingOf(*ClangPath);
154     }
155     // Otherwise try to look it up on PATH. This won't change basename.
156     auto Absolute = llvm::sys::findProgramByName(Driver);
157     if (Absolute && llvm::sys::path::is_absolute(*Absolute))
158       Driver = Storage = std::move(*Absolute);
159     else if (ClangPath) // If we don't find it, use clang dir again.
160       return SiblingOf(*ClangPath);
161     else // Nothing to do: can't find the command and no detected dir.
162       return Driver.str();
163   }
164 
165   // Now we have an absolute path, but it may be a symlink.
166   assert(llvm::sys::path::is_absolute(Driver));
167   if (FollowSymlink) {
168     llvm::SmallString<256> Resolved;
169     if (!llvm::sys::fs::real_path(Driver, Resolved))
170       return SiblingOf(Resolved);
171   }
172   return Driver.str();
173 }
174 
175 } // namespace
176 
detect()177 CommandMangler CommandMangler::detect() {
178   CommandMangler Result;
179   Result.ClangPath = detectClangPath();
180   Result.ResourceDir = detectStandardResourceDir();
181   Result.Sysroot = detectSysroot();
182   return Result;
183 }
184 
forTests()185 CommandMangler CommandMangler::forTests() {
186   return CommandMangler();
187 }
188 
adjust(std::vector<std::string> & Cmd) const189 void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
190   for (auto &Edit : Config::current().CompileFlags.Edits)
191     Edit(Cmd);
192 
193   // Check whether the flag exists, either as -flag or -flag=*
194   auto Has = [&](llvm::StringRef Flag) {
195     for (llvm::StringRef Arg : Cmd) {
196       if (Arg.consume_front(Flag) && (Arg.empty() || Arg[0] == '='))
197         return true;
198     }
199     return false;
200   };
201 
202   // clangd should not write files to disk, including dependency files
203   // requested on the command line.
204   Cmd = tooling::getClangStripDependencyFileAdjuster()(Cmd, "");
205   // Strip plugin related command line arguments. Clangd does
206   // not support plugins currently. Therefore it breaks if
207   // compiler tries to load plugins.
208   Cmd = tooling::getStripPluginsAdjuster()(Cmd, "");
209   Cmd = tooling::getClangSyntaxOnlyAdjuster()(Cmd, "");
210 
211   if (ResourceDir && !Has("-resource-dir"))
212     Cmd.push_back(("-resource-dir=" + *ResourceDir));
213 
214   // Don't set `-isysroot` if it is already set or if `--sysroot` is set.
215   // `--sysroot` is a superset of the `-isysroot` argument.
216   if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) {
217     Cmd.push_back("-isysroot");
218     Cmd.push_back(*Sysroot);
219   }
220 
221   if (!Cmd.empty()) {
222     bool FollowSymlink = !Has("-no-canonical-prefixes");
223     Cmd.front() =
224         (FollowSymlink ? ResolvedDrivers : ResolvedDriversNoFollow)
225             .get(Cmd.front(), [&, this] {
226               return resolveDriver(Cmd.front(), FollowSymlink, ClangPath);
227             });
228   }
229 }
230 
operator clang::tooling::ArgumentsAdjuster()231 CommandMangler::operator clang::tooling::ArgumentsAdjuster() && {
232   // ArgumentsAdjuster is a std::function and so must be copyable.
233   return [Mangler = std::make_shared<CommandMangler>(std::move(*this))](
234              const std::vector<std::string> &Args, llvm::StringRef File) {
235     auto Result = Args;
236     Mangler->adjust(Result);
237     return Result;
238   };
239 }
240 
241 // ArgStripper implementation
242 namespace {
243 
244 // Determine total number of args consumed by this option.
245 // Return answers for {Exact, Prefix} match. 0 means not allowed.
getArgCount(const llvm::opt::Option & Opt)246 std::pair<unsigned, unsigned> getArgCount(const llvm::opt::Option &Opt) {
247   constexpr static unsigned Rest = 10000; // Should be all the rest!
248   // Reference is llvm::opt::Option::acceptInternal()
249   using llvm::opt::Option;
250   switch (Opt.getKind()) {
251   case Option::FlagClass:
252     return {1, 0};
253   case Option::JoinedClass:
254   case Option::CommaJoinedClass:
255     return {1, 1};
256   case Option::GroupClass:
257   case Option::InputClass:
258   case Option::UnknownClass:
259   case Option::ValuesClass:
260     return {1, 0};
261   case Option::JoinedAndSeparateClass:
262     return {2, 2};
263   case Option::SeparateClass:
264     return {2, 0};
265   case Option::MultiArgClass:
266     return {1 + Opt.getNumArgs(), 0};
267   case Option::JoinedOrSeparateClass:
268     return {2, 1};
269   case Option::RemainingArgsClass:
270     return {Rest, 0};
271   case Option::RemainingArgsJoinedClass:
272     return {Rest, Rest};
273   }
274   llvm_unreachable("Unhandled option kind");
275 }
276 
277 // Flag-parsing mode, which affects which flags are available.
278 enum DriverMode : unsigned char {
279   DM_None = 0,
280   DM_GCC = 1, // Default mode e.g. when invoked as 'clang'
281   DM_CL = 2,  // MS CL.exe compatible mode e.g. when invoked as 'clang-cl'
282   DM_CC1 = 4, // When invoked as 'clang -cc1' or after '-Xclang'
283   DM_All = 7
284 };
285 
286 // Examine args list to determine if we're in GCC, CL-compatible, or cc1 mode.
getDriverMode(const std::vector<std::string> & Args)287 DriverMode getDriverMode(const std::vector<std::string> &Args) {
288   DriverMode Mode = DM_GCC;
289   llvm::StringRef Argv0 = Args.front();
290   if (Argv0.endswith_lower(".exe"))
291     Argv0 = Argv0.drop_back(strlen(".exe"));
292   if (Argv0.endswith_lower("cl"))
293     Mode = DM_CL;
294   for (const llvm::StringRef Arg : Args) {
295     if (Arg == "--driver-mode=cl") {
296       Mode = DM_CL;
297       break;
298     }
299     if (Arg == "-cc1") {
300       Mode = DM_CC1;
301       break;
302     }
303   }
304   return Mode;
305 }
306 
307 // Returns the set of DriverModes where an option may be used.
getModes(const llvm::opt::Option & Opt)308 unsigned char getModes(const llvm::opt::Option &Opt) {
309   // Why is this so complicated?!
310   // Reference is clang::driver::Driver::getIncludeExcludeOptionFlagMasks()
311   unsigned char Result = DM_None;
312   if (Opt.hasFlag(driver::options::CC1Option))
313     Result |= DM_CC1;
314   if (!Opt.hasFlag(driver::options::NoDriverOption)) {
315     if (Opt.hasFlag(driver::options::CLOption)) {
316       Result |= DM_CL;
317     } else {
318       Result |= DM_GCC;
319       if (Opt.hasFlag(driver::options::CoreOption)) {
320         Result |= DM_CL;
321       }
322     }
323   }
324   return Result;
325 }
326 
327 } // namespace
328 
rulesFor(llvm::StringRef Arg)329 llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
330   // All the hard work is done once in a static initializer.
331   // We compute a table containing strings to look for and #args to skip.
332   // e.g. "-x" => {-x 2 args, -x* 1 arg, --language 2 args, --language=* 1 arg}
333   using TableTy =
334       llvm::StringMap<llvm::SmallVector<Rule, 4>, llvm::BumpPtrAllocator>;
335   static TableTy *Table = [] {
336     auto &DriverTable = driver::getDriverOptTable();
337     using DriverID = clang::driver::options::ID;
338 
339     // Collect sets of aliases, so we can treat -foo and -foo= as synonyms.
340     // Conceptually a double-linked list: PrevAlias[I] -> I -> NextAlias[I].
341     // If PrevAlias[I] is INVALID, then I is canonical.
342     DriverID PrevAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
343     DriverID NextAlias[DriverID::LastOption] = {DriverID::OPT_INVALID};
344     auto AddAlias = [&](DriverID Self, DriverID T) {
345       if (NextAlias[T]) {
346         PrevAlias[NextAlias[T]] = Self;
347         NextAlias[Self] = NextAlias[T];
348       }
349       PrevAlias[Self] = T;
350       NextAlias[T] = Self;
351     };
352     // Also grab prefixes for each option, these are not fully exposed.
353     const char *const *Prefixes[DriverID::LastOption] = {nullptr};
354 #define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
355 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
356                HELP, METAVAR, VALUES)                                          \
357   if (DriverID::OPT_##ALIAS != DriverID::OPT_INVALID && ALIASARGS == nullptr)  \
358     AddAlias(DriverID::OPT_##ID, DriverID::OPT_##ALIAS);                       \
359   Prefixes[DriverID::OPT_##ID] = PREFIX;
360 #include "clang/Driver/Options.inc"
361 #undef OPTION
362 #undef PREFIX
363 
364     auto Result = std::make_unique<TableTy>();
365     // Iterate over distinct options (represented by the canonical alias).
366     // Every spelling of this option will get the same set of rules.
367     for (unsigned ID = 1 /*Skip INVALID */; ID < DriverID::LastOption; ++ID) {
368       if (PrevAlias[ID] || ID == DriverID::OPT_Xclang)
369         continue; // Not canonical, or specially handled.
370       llvm::SmallVector<Rule, 8> Rules;
371       // Iterate over each alias, to add rules for parsing it.
372       for (unsigned A = ID; A != DriverID::OPT_INVALID; A = NextAlias[A]) {
373         if (Prefixes[A] == nullptr) // option groups.
374           continue;
375         auto Opt = DriverTable.getOption(A);
376         // Exclude - and -foo pseudo-options.
377         if (Opt.getName().empty())
378           continue;
379         auto Modes = getModes(Opt);
380         std::pair<unsigned, unsigned> ArgCount = getArgCount(Opt);
381         // Iterate over each spelling of the alias, e.g. -foo vs --foo.
382         for (auto *Prefix = Prefixes[A]; *Prefix != nullptr; ++Prefix) {
383           llvm::SmallString<64> Buf(*Prefix);
384           Buf.append(Opt.getName());
385           llvm::StringRef Spelling = Result->try_emplace(Buf).first->getKey();
386           Rules.emplace_back();
387           Rule &R = Rules.back();
388           R.Text = Spelling;
389           R.Modes = Modes;
390           R.ExactArgs = ArgCount.first;
391           R.PrefixArgs = ArgCount.second;
392           // Concrete priority is the index into the option table.
393           // Effectively, earlier entries take priority over later ones.
394           assert(ID < std::numeric_limits<decltype(R.Priority)>::max() &&
395                  "Rules::Priority overflowed by options table");
396           R.Priority = ID;
397         }
398       }
399       // Register the set of rules under each possible name.
400       for (const auto &R : Rules)
401         Result->find(R.Text)->second.append(Rules.begin(), Rules.end());
402     }
403 #ifndef NDEBUG
404     // Dump the table and various measures of its size.
405     unsigned RuleCount = 0;
406     dlog("ArgStripper Option spelling table");
407     for (const auto &Entry : *Result) {
408       dlog("{0}", Entry.first());
409       RuleCount += Entry.second.size();
410       for (const auto &R : Entry.second)
411         dlog("  {0} #={1} *={2} Mode={3}", R.Text, R.ExactArgs, R.PrefixArgs,
412              int(R.Modes));
413     }
414     dlog("Table spellings={0} rules={1} string-bytes={2}", Result->size(),
415          RuleCount, Result->getAllocator().getBytesAllocated());
416 #endif
417     // The static table will never be destroyed.
418     return Result.release();
419   }();
420 
421   auto It = Table->find(Arg);
422   return (It == Table->end()) ? llvm::ArrayRef<Rule>() : It->second;
423 }
424 
strip(llvm::StringRef Arg)425 void ArgStripper::strip(llvm::StringRef Arg) {
426   auto OptionRules = rulesFor(Arg);
427   if (OptionRules.empty()) {
428     // Not a recognized flag. Strip it literally.
429     Storage.emplace_back(Arg);
430     Rules.emplace_back();
431     Rules.back().Text = Storage.back();
432     Rules.back().ExactArgs = 1;
433     if (Rules.back().Text.consume_back("*"))
434       Rules.back().PrefixArgs = 1;
435     Rules.back().Modes = DM_All;
436     Rules.back().Priority = -1; // Max unsigned = lowest priority.
437   } else {
438     Rules.append(OptionRules.begin(), OptionRules.end());
439   }
440 }
441 
matchingRule(llvm::StringRef Arg,unsigned Mode,unsigned & ArgCount) const442 const ArgStripper::Rule *ArgStripper::matchingRule(llvm::StringRef Arg,
443                                                    unsigned Mode,
444                                                    unsigned &ArgCount) const {
445   const ArgStripper::Rule *BestRule = nullptr;
446   for (const Rule &R : Rules) {
447     // Rule can fail to match if...
448     if (!(R.Modes & Mode))
449       continue; // not applicable to current driver mode
450     if (BestRule && BestRule->Priority < R.Priority)
451       continue; // lower-priority than best candidate.
452     if (!Arg.startswith(R.Text))
453       continue; // current arg doesn't match the prefix string
454     bool PrefixMatch = Arg.size() > R.Text.size();
455     // Can rule apply as an exact/prefix match?
456     if (unsigned Count = PrefixMatch ? R.PrefixArgs : R.ExactArgs) {
457       BestRule = &R;
458       ArgCount = Count;
459     }
460     // Continue in case we find a higher-priority rule.
461   }
462   return BestRule;
463 }
464 
process(std::vector<std::string> & Args) const465 void ArgStripper::process(std::vector<std::string> &Args) const {
466   if (Args.empty())
467     return;
468 
469   // We're parsing the args list in some mode (e.g. gcc-compatible) but may
470   // temporarily switch to another mode with the -Xclang flag.
471   DriverMode MainMode = getDriverMode(Args);
472   DriverMode CurrentMode = MainMode;
473 
474   // Read and write heads for in-place deletion.
475   unsigned Read = 0, Write = 0;
476   bool WasXclang = false;
477   while (Read < Args.size()) {
478     unsigned ArgCount = 0;
479     if (matchingRule(Args[Read], CurrentMode, ArgCount)) {
480       // Delete it and its args.
481       if (WasXclang) {
482         assert(Write > 0);
483         --Write; // Drop previous -Xclang arg
484         CurrentMode = MainMode;
485         WasXclang = false;
486       }
487       // Advance to last arg. An arg may be foo or -Xclang foo.
488       for (unsigned I = 1; Read < Args.size() && I < ArgCount; ++I) {
489         ++Read;
490         if (Read < Args.size() && Args[Read] == "-Xclang")
491           ++Read;
492       }
493     } else {
494       // No match, just copy the arg through.
495       WasXclang = Args[Read] == "-Xclang";
496       CurrentMode = WasXclang ? DM_CC1 : MainMode;
497       if (Write != Read)
498         Args[Write] = std::move(Args[Read]);
499       ++Write;
500     }
501     ++Read;
502   }
503   Args.resize(Write);
504 }
505 
506 } // namespace clangd
507 } // namespace clang
508