clang-tools  14.0.0git
CompileCommands.h
Go to the documentation of this file.
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 "llvm/ADT/StringRef.h"
16 #include <deque>
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
30  // Absolute path to clang.
31  llvm::Optional<std::string> ClangPath;
32  // Directory containing builtin headers.
33  llvm::Optional<std::string> ResourceDir;
34  // Root for searching for standard library (passed to -isysroot).
35  llvm::Optional<std::string> Sysroot;
36 
37  // A command-mangler that doesn't know anything about the system.
38  // This is hermetic for unit-tests, but won't work well in production.
39  static CommandMangler forTests();
40  // Probe the system and build a command-mangler that knows the toolchain.
41  // - try to find clang on $PATH, otherwise fake a path near clangd
42  // - find the resource directory installed near clangd
43  // - on mac, find clang and isysroot by querying the `xcrun` launcher
44  static CommandMangler detect();
45 
46  void adjust(std::vector<std::string> &Cmd, llvm::StringRef File) const;
47  explicit operator clang::tooling::ArgumentsAdjuster() &&;
48 
49 private:
50  CommandMangler() = default;
51  Memoize<llvm::StringMap<std::string>> ResolvedDrivers;
52  Memoize<llvm::StringMap<std::string>> ResolvedDriversNoFollow;
53 };
54 
55 // Removes args from a command-line in a semantically-aware way.
56 //
57 // Internally this builds a large (0.5MB) table of clang options on first use.
58 // Both strip() and process() are fairly cheap after that.
59 //
60 // FIXME: this reimplements much of OptTable, it might be nice to expose more.
61 // The table-building strategy may not make sense outside clangd.
62 class ArgStripper {
63 public:
64  ArgStripper() = default;
65  ArgStripper(ArgStripper &&) = default;
66  ArgStripper(const ArgStripper &) = delete;
67  ArgStripper &operator=(ArgStripper &&) = default;
68  ArgStripper &operator=(const ArgStripper &) = delete;
69 
70  // Adds the arg to the set which should be removed.
71  //
72  // Recognized clang flags are stripped semantically. When "-I" is stripped:
73  // - so is its value (either as -Ifoo or -I foo)
74  // - aliases like --include-directory=foo are also stripped
75  // - CL-style /Ifoo will be removed if the args indicate MS-compatible mode
76  // Compile args not recognized as flags are removed literally, except:
77  // - strip("ABC*") will remove any arg with an ABC prefix.
78  //
79  // In either case, the -Xclang prefix will be dropped if present.
80  void strip(llvm::StringRef Arg);
81  // Remove the targets from a compile command, in-place.
82  void process(std::vector<std::string> &Args) const;
83 
84 private:
85  // Deletion rules, to be checked for each arg.
86  struct Rule {
87  llvm::StringRef Text; // Rule applies only if arg begins with Text.
88  unsigned char Modes = 0; // Rule applies only in specified driver modes.
89  uint16_t Priority = 0; // Lower is better.
90  uint16_t ExactArgs = 0; // Num args consumed when Arg == Text.
91  uint16_t PrefixArgs = 0; // Num args consumed when Arg starts with Text.
92  };
93  static llvm::ArrayRef<Rule> rulesFor(llvm::StringRef Arg);
94  const Rule *matchingRule(llvm::StringRef Arg, unsigned Mode,
95  unsigned &ArgCount) const;
96  llvm::SmallVector<Rule> Rules;
97  std::deque<std::string> Storage; // Store strings not found in option table.
98 };
99 
100 // Renders an argv list, with arguments separated by spaces.
101 // Where needed, arguments are "quoted" and escaped.
102 std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args);
103 std::string printArgv(llvm::ArrayRef<std::string> Args);
104 
105 } // namespace clangd
106 } // namespace clang
107 
108 #endif
clang::clangd::printArgv
std::string printArgv(llvm::ArrayRef< llvm::StringRef > Args)
Definition: CompileCommands.cpp:582
clang::clangd::ArgStripper::strip
void strip(llvm::StringRef Arg)
Definition: CompileCommands.cpp:501
clang::clangd::ArgStripper::process
void process(std::vector< std::string > &Args) const
Definition: CompileCommands.cpp:541
Text
std::string Text
Definition: HTMLGenerator.cpp:80
clang::clangd::CommandMangler
Definition: CompileCommands.h:29
clang::clangd::CommandMangler::ResourceDir
llvm::Optional< std::string > ResourceDir
Definition: CompileCommands.h:33
clang::clangd::ArgStripper::ArgStripper
ArgStripper()=default
clang::clangd::CommandMangler::detect
static CommandMangler detect()
Definition: CompileCommands.cpp:188
clang::clangd::ArgStripper::operator=
ArgStripper & operator=(ArgStripper &&)=default
Args
llvm::json::Object Args
Definition: Trace.cpp:139
Threading.h
clang::clangd::CommandMangler::adjust
void adjust(std::vector< std::string > &Cmd, llvm::StringRef File) const
Definition: CompileCommands.cpp:198
clang::clangd::CommandMangler::Sysroot
llvm::Optional< std::string > Sysroot
Definition: CompileCommands.h:35
clang::clangd::ArgStripper
Definition: CompileCommands.h:62
clang::clangd::CommandMangler::ClangPath
llvm::Optional< std::string > ClangPath
Definition: CompileCommands.h:31
clang::clangd::Memoize
Memoize is a cache to store and reuse computation results based on a key.
Definition: Threading.h:145
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::CommandMangler::forTests
static CommandMangler forTests()
Definition: CompileCommands.cpp:196