36 #include "clang/Driver/Types.h" 37 #include "clang/Tooling/CompilationDatabase.h" 38 #include "llvm/ADT/DenseMap.h" 39 #include "llvm/ADT/ScopeExit.h" 40 #include "llvm/ADT/SmallString.h" 41 #include "llvm/ADT/StringExtras.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/ADT/iterator_range.h" 44 #include "llvm/Support/FileSystem.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/Path.h" 47 #include "llvm/Support/Program.h" 48 #include "llvm/Support/Regex.h" 49 #include "llvm/Support/ScopedPrinter.h" 59 std::vector<std::string> parseDriverOutput(llvm::StringRef Output) {
60 std::vector<std::string> SystemIncludes;
61 const char SIS[] =
"#include <...> search starts here:";
62 const char SIE[] =
"End of search list.";
63 llvm::SmallVector<llvm::StringRef, 8>
Lines;
64 Output.split(Lines,
'\n', -1,
false);
66 auto StartIt = llvm::find_if(
67 Lines, [SIS](llvm::StringRef
Line) {
return Line.trim() == SIS; });
68 if (StartIt == Lines.end()) {
69 elog(
"System include extraction: start marker not found: {0}", Output);
74 llvm::find_if(llvm::make_range(StartIt, Lines.end()),
75 [SIE](llvm::StringRef Line) {
return Line.trim() == SIE; });
76 if (EndIt == Lines.end()) {
77 elog(
"System include extraction: end marker missing: {0}", Output);
81 for (llvm::StringRef Line : llvm::make_range(StartIt, EndIt)) {
82 SystemIncludes.push_back(Line.trim().str());
83 vlog(
"System include extraction: adding {0}", Line);
85 return SystemIncludes;
88 std::vector<std::string> extractSystemIncludes(
PathRef Driver,
90 llvm::Regex &QueryDriverRegex) {
91 trace::Span Tracer(
"Extract system includes");
95 if (!QueryDriverRegex.match(Driver)) {
96 vlog(
"System include extraction: not whitelisted driver {0}", Driver);
100 if (!llvm::sys::fs::exists(Driver)) {
101 elog(
"System include extraction: {0} does not exist.", Driver);
104 if (!llvm::sys::fs::can_execute(Driver)) {
105 elog(
"System include extraction: {0} is not executable.", Driver);
109 llvm::SmallString<128> StdErrPath;
110 if (
auto EC = llvm::sys::fs::createTemporaryFile(
"system-includes",
"clangd",
112 elog(
"System include extraction: failed to create temporary file with " 117 auto CleanUp = llvm::make_scope_exit(
118 [&StdErrPath]() { llvm::sys::fs::remove(StdErrPath); });
120 llvm::Optional<llvm::StringRef> Redirects[] = {
121 {
""}, {
""}, llvm::StringRef(StdErrPath)};
124 const llvm::StringRef Args[] = {Driver,
"-E",
"-x", Lang,
"-",
"-v"};
126 if (
int RC = llvm::sys::ExecuteAndWait(Driver, Args,
llvm::None,
128 elog(
"System include extraction: driver execution failed with return code: " 130 llvm::to_string(RC));
134 auto BufOrError = llvm::MemoryBuffer::getFile(StdErrPath);
136 elog(
"System include extraction: failed to read {0} with error {1}",
137 StdErrPath, BufOrError.getError().message());
141 auto Includes = parseDriverOutput(BufOrError->get()->getBuffer());
142 log(
"System include extractor: succesfully executed {0}, got includes: " 148 tooling::CompileCommand &
149 addSystemIncludes(tooling::CompileCommand &Cmd,
150 llvm::ArrayRef<std::string> SystemIncludes) {
151 for (llvm::StringRef Include : SystemIncludes) {
153 Cmd.CommandLine.push_back(
"-isystem");
154 Cmd.CommandLine.push_back(Include.str());
160 std::string convertGlobToRegex(llvm::StringRef Glob) {
162 llvm::raw_string_ostream RegStream(RegText);
164 for (
size_t I = 0, E = Glob.size(); I < E; ++I) {
165 if (Glob[I] ==
'*') {
166 if (I + 1 < E && Glob[I + 1] ==
'*') {
173 RegStream <<
"[^/]*";
176 RegStream << llvm::Regex::escape(Glob.substr(I, 1));
185 llvm::Regex convertGlobsToRegex(llvm::ArrayRef<std::string> Globs) {
186 assert(!Globs.empty() &&
"Globs cannot be empty!");
187 std::vector<std::string> RegTexts;
188 RegTexts.reserve(Globs.size());
189 for (llvm::StringRef Glob : Globs)
190 RegTexts.push_back(convertGlobToRegex(Glob));
193 assert(Reg.isValid(RegTexts.front()) &&
194 "Created an invalid regex from globs");
201 class QueryDriverDatabase :
public GlobalCompilationDatabase {
203 QueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
204 std::unique_ptr<GlobalCompilationDatabase>
Base)
205 : QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)),
206 Base(std::move(Base)) {
209 this->Base->watch([
this](
const std::vector<std::string> &
Changes) {
210 OnCommandChanged.broadcast(Changes);
214 llvm::Optional<tooling::CompileCommand>
216 auto Cmd = Base->getCompileCommand(File);
217 if (!Cmd || Cmd->CommandLine.empty())
220 llvm::StringRef Lang;
221 for (
size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
222 llvm::StringRef Arg = Cmd->CommandLine[I];
223 if (Arg ==
"-x" && I + 1 < E)
224 Lang = Cmd->CommandLine[I + 1];
225 else if (Arg.startswith(
"-x"))
226 Lang = Arg.drop_front(2).trim();
229 llvm::StringRef Ext = llvm::sys::path::extension(File).trim(
'.');
230 auto Type = driver::types::lookupTypeForExtension(Ext);
231 if (
Type == driver::types::TY_INVALID) {
232 elog(
"System include extraction: invalid file type for {0}", Ext);
235 Lang = driver::types::getTypeName(
Type);
238 llvm::SmallString<128> Driver(Cmd->CommandLine.front());
240 auto Key = std::make_pair(Driver.str(), Lang);
242 std::vector<std::string> SystemIncludes;
244 std::lock_guard<std::mutex> Lock(Mu);
246 auto It = DriverToIncludesCache.find(
Key);
247 if (It != DriverToIncludesCache.end())
248 SystemIncludes = It->second;
250 DriverToIncludesCache[
Key] = SystemIncludes =
251 extractSystemIncludes(
Key.first,
Key.second, QueryDriverRegex);
254 return addSystemIncludes(*Cmd, SystemIncludes);
257 llvm::Optional<ProjectInfo> getProjectInfo(
PathRef File)
const override {
258 return Base->getProjectInfo(File);
262 mutable std::mutex Mu;
264 mutable std::map<std::pair<std::string, std::string>,
265 std::vector<std::string>>
266 DriverToIncludesCache;
267 mutable llvm::Regex QueryDriverRegex;
269 std::unique_ptr<GlobalCompilationDatabase>
Base;
270 CommandChanged::Subscription BaseChanged;
274 std::unique_ptr<GlobalCompilationDatabase>
276 std::unique_ptr<GlobalCompilationDatabase> Base) {
277 assert(Base &&
"Null base to SystemIncludeExtractor");
278 if (QueryDriverGlobs.empty())
280 return llvm::make_unique<QueryDriverDatabase>(QueryDriverGlobs,
std::unique_ptr< GlobalCompilationDatabase > getQueryDriverDatabase(llvm::ArrayRef< std::string > QueryDriverGlobs, std::unique_ptr< GlobalCompilationDatabase > Base)
Extracts system include search path from drivers matching QueryDriverGlobs and adds them to the compi...
llvm::StringRef PathRef
A typedef to represent a ref to file path.
def make_absolute(f, directory)
Documents should not be synced at all.
void vlog(const char *Fmt, Ts &&... Vals)
void elog(const char *Fmt, Ts &&... Vals)
void log(const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)
std::unique_ptr< GlobalCompilationDatabase > Base
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.