Debian Codegraph @

llvm-project/clang-tools-extra/clangd/CompileCommands.h

raw
1 //===--- CompileCommands.h - Manipulation of compile flags -------*- C++-*-===//
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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILECOMMANDS_H
9 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILECOMMANDS_H
10
11 #include "GlobalCompilationDatabase.h"
12 #include "support/Threading.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <deque>
16 #include <optional>
17 #include <string>
18 #include <vector>
19
20 namespace clang {
21 namespace clangd {
22
23 // CommandMangler transforms compile commands from some external source
24 // for use in clangd. This means:
25 // - running the frontend only, stripping args regarding output files etc
26 // - forcing the use of clangd's builtin headers rather than clang's
27 // - resolving argv0 as cc1 expects
28 // - injecting -isysroot flags on mac as the system clang does
29 struct CommandMangler {
30 // Absolute path to clang.
31 std::optional<std::string> ClangPath;
32 // Directory containing builtin headers.
33 std::optional<std::string> ResourceDir;
34 // Root for searching for standard library (passed to -isysroot).
35 std::optional<std::string> Sysroot;
36 SystemIncludeExtractorFn SystemIncludeExtractor;
37
38 // A command-mangler that doesn't know anything about the system.
39 // This is hermetic for unit-tests, but won't work well in production.
40 static CommandMangler forTests();
41 // Probe the system and build a command-mangler that knows the toolchain.
42 // - try to find clang on $PATH, otherwise fake a path near clangd
43 // - find the resource directory installed near clangd
44 // - on mac, find clang and isysroot by querying the `xcrun` launcher
45 static CommandMangler detect();
46
47 // `Cmd` may describe compilation of a different file, and will be updated
48 // for parsing `TargetFile`.
49 void operator()(tooling::CompileCommand &Cmd,
50 llvm::StringRef TargetFile) const;
51
52 private:
53 CommandMangler() = default;
54
55 Memoize<llvm::StringMap<std::string>> ResolvedDrivers;
56 Memoize<llvm::StringMap<std::string>> ResolvedDriversNoFollow;
57 };
58
59 // Removes args from a command-line in a semantically-aware way.
60 //
61 // Internally this builds a large (0.5MB) table of clang options on first use.
62 // Both strip() and process() are fairly cheap after that.
63 //
64 // FIXME: this reimplements much of OptTable, it might be nice to expose more.
65 // The table-building strategy may not make sense outside clangd.
66 class ArgStripper {
67 public:
68 ArgStripper() = default;
69 ArgStripper(ArgStripper &&) = default;
70 ArgStripper(const ArgStripper &) = delete;
71 ArgStripper &operator=(ArgStripper &&) = default;
72 ArgStripper &operator=(const ArgStripper &) = delete;
73
74 // Adds the arg to the set which should be removed.
75 //
76 // Recognized clang flags are stripped semantically. When "-I" is stripped:
77 // - so is its value (either as -Ifoo or -I foo)
78 // - aliases like --include-directory=foo are also stripped
79 // - CL-style /Ifoo will be removed if the args indicate MS-compatible mode
80 // Compile args not recognized as flags are removed literally, except:
81 // - strip("ABC*") will remove any arg with an ABC prefix.
82 //
83 // In either case, the -Xclang prefix will be dropped if present.
84 void strip(llvm::StringRef Arg);
85 // Remove the targets from a compile command, in-place.
86 void process(std::vector<std::string> &Args) const;
87
88 private:
89 // Deletion rules, to be checked for each arg.
90 struct Rule {
91 llvm::StringRef Text; // Rule applies only if arg begins with Text.
92 unsigned char Modes = 0; // Rule applies only in specified driver modes.
93 uint16_t Priority = 0; // Lower is better.
94 uint16_t ExactArgs = 0; // Num args consumed when Arg == Text.
95 uint16_t PrefixArgs = 0; // Num args consumed when Arg starts with Text.
96 };
97 static llvm::ArrayRef<Rule> rulesFor(llvm::StringRef Arg);
98 const Rule *matchingRule(llvm::StringRef Arg, unsigned Mode,
99 unsigned &ArgCount) const;
100 llvm::SmallVector<Rule> Rules;
101 std::deque<std::string> Storage; // Store strings not found in option table.
102 };
103
104 // Renders an argv list, with arguments separated by spaces.
105 // Where needed, arguments are "quoted" and escaped.
106 std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args);
107 std::string printArgv(llvm::ArrayRef<std::string> Args);
108
109 } // namespace clangd
110 } // namespace clang
111
112 #endif