Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clangd] Preserve -nostdinc and --sysroot when calling query driver
Solves this issue: clangd/clangd#157 This is my first contribution to an llvm project, so I hope I'm doing it right! Patch by @topisani (Tobias Pisani)! Reviewers: kadircet, klimek Differential Revision: https://reviews.llvm.org/D73811 (cherry picked from commit 6e8d6bc)
- Loading branch information
1 parent
c900824
commit b6efa23
Showing
2 changed files
with
47 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,9 +85,10 @@ std::vector<std::string> parseDriverOutput(llvm::StringRef Output) { | |
return SystemIncludes; | ||
} | ||
|
||
std::vector<std::string> extractSystemIncludes(PathRef Driver, | ||
llvm::StringRef Lang, | ||
llvm::Regex &QueryDriverRegex) { | ||
std::vector<std::string> | ||
extractSystemIncludes(PathRef Driver, llvm::StringRef Lang, | ||
llvm::ArrayRef<std::string> CommandLine, | ||
llvm::Regex &QueryDriverRegex) { | ||
trace::Span Tracer("Extract system includes"); | ||
SPAN_ATTACH(Tracer, "driver", Driver); | ||
SPAN_ATTACH(Tracer, "lang", Lang); | ||
|
@@ -120,14 +121,43 @@ std::vector<std::string> extractSystemIncludes(PathRef Driver, | |
llvm::Optional<llvm::StringRef> Redirects[] = { | ||
{""}, {""}, llvm::StringRef(StdErrPath)}; | ||
|
||
// Should we also preserve flags like "-sysroot", "-nostdinc" ? | ||
const llvm::StringRef Args[] = {Driver, "-E", "-x", Lang, "-", "-v"}; | ||
llvm::SmallVector<llvm::StringRef, 12> Args = {Driver, "-E", "-x", | ||
Lang, "-", "-v"}; | ||
|
||
// These flags will be preserved | ||
const llvm::StringRef FlagsToPreserve[] = { | ||
"-nostdinc", "--no-standard-includes", "-nostdinc++", "-nobuiltininc"}; | ||
// Preserves these flags and their values, either as separate args or with an | ||
// equalsbetween them | ||
const llvm::StringRef ArgsToPreserve[] = {"--sysroot", "-isysroot"}; | ||
|
||
for (size_t I = 0, E = CommandLine.size(); I < E; ++I) { | ||
llvm::StringRef Arg = CommandLine[I]; | ||
if (llvm::any_of(FlagsToPreserve, | ||
[&Arg](llvm::StringRef S) { return S == Arg; })) { | ||
Args.push_back(Arg); | ||
} else { | ||
const auto *Found = | ||
llvm::find_if(ArgsToPreserve, [&Arg](llvm::StringRef S) { | ||
return Arg.startswith(S); | ||
}); | ||
if (Found == std::end(ArgsToPreserve)) | ||
continue; | ||
Arg.consume_front(*Found); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
kadircet
Author
Member
|
||
if (Arg.empty() && I + 1 < E) { | ||
Args.push_back(CommandLine[I]); | ||
Args.push_back(CommandLine[++I]); | ||
} else if (Arg.startswith("=")) { | ||
Args.push_back(CommandLine[I]); | ||
} | ||
} | ||
} | ||
|
||
if (int RC = llvm::sys::ExecuteAndWait(Driver, Args, /*Env=*/llvm::None, | ||
Redirects)) { | ||
elog("System include extraction: driver execution failed with return code: " | ||
"{0}", | ||
llvm::to_string(RC)); | ||
"{0}. Args: ['{1}']", | ||
llvm::to_string(RC), llvm::join(Args, "', '")); | ||
return {}; | ||
} | ||
|
||
|
@@ -237,7 +267,7 @@ class QueryDriverDatabase : public GlobalCompilationDatabase { | |
|
||
llvm::SmallString<128> Driver(Cmd->CommandLine.front()); | ||
llvm::sys::fs::make_absolute(Cmd->Directory, Driver); | ||
auto Key = std::make_pair(Driver.str(), Lang); | ||
auto Key = std::make_pair(Driver.str().str(), Lang.str()); | ||
|
||
std::vector<std::string> SystemIncludes; | ||
{ | ||
|
@@ -247,8 +277,8 @@ class QueryDriverDatabase : public GlobalCompilationDatabase { | |
if (It != DriverToIncludesCache.end()) | ||
SystemIncludes = It->second; | ||
else | ||
DriverToIncludesCache[Key] = SystemIncludes = | ||
extractSystemIncludes(Key.first, Key.second, QueryDriverRegex); | ||
DriverToIncludesCache[Key] = SystemIncludes = extractSystemIncludes( | ||
Key.first, Key.second, Cmd->CommandLine, QueryDriverRegex); | ||
} | ||
|
||
return addSystemIncludes(*Cmd, SystemIncludes); | ||
|
@@ -278,7 +308,7 @@ getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs, | |
if (QueryDriverGlobs.empty()) | ||
return Base; | ||
return std::make_unique<QueryDriverDatabase>(QueryDriverGlobs, | ||
std::move(Base)); | ||
std::move(Base)); | ||
} | ||
|
||
} // namespace clangd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
consume_front
performs prefix check, but we already know thatArg
starts with that prefix. So wouldn't it be better to replace it withsubstr
:Arg = Arg.substr(Found->size());
Thanks!