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