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 constexpr
char const *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);
73 const auto EndIt = std::find(StartIt, Lines.end(), SIE);
74 if (EndIt == Lines.end()) {
75 elog(
"System include extraction: end marker missing: {0}", Output);
79 for (llvm::StringRef Line : llvm::make_range(StartIt, EndIt)) {
80 SystemIncludes.push_back(Line.trim().str());
81 vlog(
"System include extraction: adding {0}", Line);
83 return SystemIncludes;
86 std::vector<std::string> extractSystemIncludes(
PathRef Driver,
88 llvm::Regex &QueryDriverRegex) {
89 trace::Span Tracer(
"Extract system includes");
93 if (!QueryDriverRegex.match(Driver)) {
94 vlog(
"System include extraction: not whitelisted driver {0}", Driver);
98 if (!llvm::sys::fs::exists(Driver)) {
99 elog(
"System include extraction: {0} does not exist.", Driver);
102 if (!llvm::sys::fs::can_execute(Driver)) {
103 elog(
"System include extraction: {0} is not executable.", Driver);
107 llvm::SmallString<128> StdErrPath;
108 if (
auto EC = llvm::sys::fs::createTemporaryFile(
"system-includes",
"clangd",
110 elog(
"System include extraction: failed to create temporary file with " 115 auto CleanUp = llvm::make_scope_exit(
116 [&StdErrPath]() { llvm::sys::fs::remove(StdErrPath); });
118 llvm::Optional<llvm::StringRef> Redirects[] = {
119 {
""}, {
""}, llvm::StringRef(StdErrPath)};
122 const llvm::StringRef Args[] = {Driver,
"-E",
"-x", Lang,
"-",
"-v"};
124 if (
int RC = llvm::sys::ExecuteAndWait(Driver, Args,
llvm::None,
126 elog(
"System include extraction: driver execution failed with return code: " 128 llvm::to_string(RC));
132 auto BufOrError = llvm::MemoryBuffer::getFile(StdErrPath);
134 elog(
"System include extraction: failed to read {0} with error {1}",
135 StdErrPath, BufOrError.getError().message());
139 auto Includes = parseDriverOutput(BufOrError->get()->getBuffer());
140 log(
"System include extractor: succesfully executed {0}, got includes: " 146 tooling::CompileCommand &
147 addSystemIncludes(tooling::CompileCommand &Cmd,
148 llvm::ArrayRef<std::string> SystemIncludes) {
149 for (llvm::StringRef Include : SystemIncludes) {
151 Cmd.CommandLine.push_back(
"-isystem");
152 Cmd.CommandLine.push_back(Include.str());
158 std::string convertGlobToRegex(llvm::StringRef Glob) {
160 llvm::raw_string_ostream RegStream(RegText);
162 for (
size_t I = 0, E = Glob.size(); I < E; ++I) {
163 if (Glob[I] ==
'*') {
164 if (I + 1 < E && Glob[I + 1] ==
'*') {
171 RegStream <<
"[^/]*";
174 RegStream << llvm::Regex::escape(Glob.substr(I, 1));
183 llvm::Regex convertGlobsToRegex(llvm::ArrayRef<std::string> Globs) {
184 assert(!Globs.empty() &&
"Globs cannot be empty!");
185 std::vector<std::string> RegTexts;
186 RegTexts.reserve(Globs.size());
187 for (llvm::StringRef Glob : Globs)
188 RegTexts.push_back(convertGlobToRegex(Glob));
191 assert(Reg.isValid(RegTexts.front()) &&
192 "Created an invalid regex from globs");
199 class QueryDriverDatabase :
public GlobalCompilationDatabase {
201 QueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
202 std::unique_ptr<GlobalCompilationDatabase>
Base)
203 : QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)),
204 Base(std::move(Base)) {
207 this->Base->watch([
this](
const std::vector<std::string> &
Changes) {
208 OnCommandChanged.broadcast(Changes);
212 llvm::Optional<tooling::CompileCommand>
214 auto Cmd = Base->getCompileCommand(File);
215 if (!Cmd || Cmd->CommandLine.empty())
218 llvm::StringRef Lang;
219 for (
size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
220 llvm::StringRef Arg = Cmd->CommandLine[I];
221 if (Arg ==
"-x" && I + 1 < E)
222 Lang = Cmd->CommandLine[I + 1];
223 else if (Arg.startswith(
"-x"))
224 Lang = Arg.drop_front(2).trim();
227 llvm::StringRef Ext = llvm::sys::path::extension(File).trim(
'.');
228 auto Type = driver::types::lookupTypeForExtension(Ext);
229 if (
Type == driver::types::TY_INVALID) {
230 elog(
"System include extraction: invalid file type for {0}", Ext);
233 Lang = driver::types::getTypeName(
Type);
236 llvm::SmallString<128> Driver(Cmd->CommandLine.front());
238 auto Key = std::make_pair(Driver.str(), Lang);
240 std::vector<std::string> SystemIncludes;
242 std::lock_guard<std::mutex> Lock(Mu);
244 auto It = DriverToIncludesCache.find(
Key);
245 if (It != DriverToIncludesCache.end())
246 SystemIncludes = It->second;
248 DriverToIncludesCache[
Key] = SystemIncludes =
249 extractSystemIncludes(
Key.first,
Key.second, QueryDriverRegex);
252 return addSystemIncludes(*Cmd, SystemIncludes);
255 llvm::Optional<ProjectInfo> getProjectInfo(
PathRef File)
const override {
256 return Base->getProjectInfo(File);
260 mutable std::mutex Mu;
262 mutable std::map<std::pair<std::string, std::string>,
263 std::vector<std::string>>
264 DriverToIncludesCache;
265 mutable llvm::Regex QueryDriverRegex;
267 std::unique_ptr<GlobalCompilationDatabase>
Base;
268 CommandChanged::Subscription BaseChanged;
272 std::unique_ptr<GlobalCompilationDatabase>
274 std::unique_ptr<GlobalCompilationDatabase> Base) {
275 assert(Base &&
"Null base to SystemIncludeExtractor");
276 if (QueryDriverGlobs.empty())
278 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.