Skip to content

[clangd] Add includes from source to non-self-contained headers #72479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions clang-tools-extra/clangd/TUScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "Config.h"
#include "Diagnostics.h"
#include "GlobalCompilationDatabase.h"
#include "HeaderSourceSwitch.h"
#include "ParsedAST.h"
#include "Preamble.h"
#include "clang-include-cleaner/Record.h"
Expand All @@ -64,6 +65,7 @@
#include "support/Threading.h"
#include "support/Trace.h"
#include "clang/Basic/Stack.h"
#include "clang/Driver/Driver.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/ADT/FunctionExtras.h"
Expand Down Expand Up @@ -622,7 +624,7 @@ class ASTWorker {
const TUScheduler::Options &Opts, ParsingCallbacks &Callbacks);
~ASTWorker();

void update(ParseInputs Inputs, WantDiagnostics, bool ContentChanged);
void update(ParseInputs Inputs, WantDiagnostics, bool ContentChanged, std::optional<std::vector<std::string>> SourceInclides);
void
runWithAST(llvm::StringRef Name,
llvm::unique_function<void(llvm::Expected<InputsAndAST>)> Action,
Expand Down Expand Up @@ -857,7 +859,7 @@ ASTWorker::~ASTWorker() {
}

void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags,
bool ContentChanged) {
bool ContentChanged, std::optional<std::vector<std::string>> SourceInclides) {
llvm::StringLiteral TaskName = "Update";
auto Task = [=]() mutable {
// Get the actual command as `Inputs` does not have a command.
Expand All @@ -881,10 +883,24 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags,
}
}
}
if (Cmd)
if (!Cmd)
Cmd = CDB.getFallbackCommand(FileName);
if (Cmd) {
if (SourceInclides && !SourceInclides->empty()) {
Cmd->CommandLine.reserve(Cmd->CommandLine.size() +
SourceInclides->size());
auto PosArg =
std::find(Cmd->CommandLine.begin(), Cmd->CommandLine.end(), "--");
auto UsePushBack = PosArg == Cmd->CommandLine.end();
for (auto &&Arg : *SourceInclides) {
if (UsePushBack)
Cmd->CommandLine.push_back(std::move(Arg));
else
PosArg = std::next(Cmd->CommandLine.insert(PosArg, std::move(Arg)));
}
}
Inputs.CompileCommand = std::move(*Cmd);
else
Inputs.CompileCommand = CDB.getFallbackCommand(FileName);
}

bool InputsAreTheSame =
std::tie(FileInputs.CompileCommand, FileInputs.Contents) ==
Expand Down Expand Up @@ -1684,7 +1700,39 @@ bool TUScheduler::update(PathRef File, ParseInputs Inputs,
ContentChanged = true;
FD->Contents = Inputs.Contents;
}
FD->Worker->update(std::move(Inputs), WantDiags, ContentChanged);
std::optional<std::vector<std::string>> SourceInclides;
if (isHeaderFile(File)) {
std::string SourceFile;
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
if (auto CorrespondingFile =
clang::clangd::getCorrespondingHeaderOrSource(File, std::move(VFS)))
SourceFile = *CorrespondingFile;
if (SourceFile.empty())
SourceFile = HeaderIncluders->get(File);
if (!SourceFile.empty() && Files.contains(SourceFile)) {
std::unique_ptr<FileData> &FD = Files[SourceFile];
if (FD && FD->Worker->isASTCached()) {
if (auto AST = IdleASTs->take(FD->Worker.lock().get())) {
auto &Headers = AST.value()->getIncludeStructure().MainFileIncludes;
std::vector<std::string> Includes;
Includes.reserve(Headers.size());
for (const auto &H : Headers) {
if (H.Resolved == File) {
if (isSystem(H.FileKind))
Includes.clear();
break;
}
auto Include = "-include" + H.Resolved;
Includes.push_back(std::move(Include));
}
if (!Includes.empty())
SourceInclides = std::move(Includes);
IdleASTs->put(FD->Worker.lock().get(), std::move(AST.value()));
}
}
}
}
FD->Worker->update(std::move(Inputs), WantDiags, ContentChanged, SourceInclides);
// There might be synthetic update requests, don't change the LastActiveFile
// in such cases.
if (ContentChanged)
Expand Down