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