aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2023-07-19 12:31:40 +0200
committerKadir Cetinkaya <kadircet@google.com>2023-07-19 16:30:46 +0200
commit27ade4b554774187d2c0afcf64cd16fa6d5f619d (patch)
tree5a56d624682f0c314bbfbf2106d4153fab6f5038 /clang-tools-extra
parenta48d8c238f461eadfcb28b065341ae4b55920cf6 (diff)
downloadllvm-27ade4b554774187d2c0afcf64cd16fa6d5f619d.zip
llvm-27ade4b554774187d2c0afcf64cd16fa6d5f619d.tar.gz
llvm-27ade4b554774187d2c0afcf64cd16fa6d5f619d.tar.bz2
Reland "[clangd] Always run preamble indexing on a separate thread"
This reverts commit 92c0546941190973e9201a08fa10edf27cb6992d. Prevents tsan issues by resetting ref-counted-pointers eagerly, before passing the copies into a new thread.
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/clangd/ClangdServer.cpp10
-rw-r--r--clang-tools-extra/clangd/ClangdServer.h4
-rw-r--r--clang-tools-extra/clangd/Preamble.cpp18
3 files changed, 20 insertions, 12 deletions
diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index af002e0..2939019 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -68,11 +68,10 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
UpdateIndexCallbacks(FileIndex *FIndex,
ClangdServer::Callbacks *ServerCallbacks,
const ThreadsafeFS &TFS, AsyncTaskRunner *Tasks,
- bool CollectInactiveRegions,
- const ClangdServer::Options &Opts)
+ bool CollectInactiveRegions)
: FIndex(FIndex), ServerCallbacks(ServerCallbacks), TFS(TFS),
Stdlib{std::make_shared<StdLibSet>()}, Tasks(Tasks),
- CollectInactiveRegions(CollectInactiveRegions), Opts(Opts) {}
+ CollectInactiveRegions(CollectInactiveRegions) {}
void onPreambleAST(
PathRef Path, llvm::StringRef Version, CapturedASTCtx ASTCtx,
@@ -94,7 +93,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
ASTCtx.getPreprocessor(), *PI);
};
- if (Opts.AsyncPreambleIndexing && Tasks) {
+ if (Tasks) {
Tasks->runAsync("Preamble indexing for:" + Path + Version,
std::move(Task));
} else
@@ -164,7 +163,6 @@ private:
std::shared_ptr<StdLibSet> Stdlib;
AsyncTaskRunner *Tasks;
bool CollectInactiveRegions;
- const ClangdServer::Options &Opts;
};
class DraftStoreFS : public ThreadsafeFS {
@@ -229,7 +227,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
std::make_unique<UpdateIndexCallbacks>(
DynamicIdx.get(), Callbacks, TFS,
IndexTasks ? &*IndexTasks : nullptr,
- PublishInactiveRegions, Opts));
+ PublishInactiveRegions));
// Adds an index to the stack, at higher priority than existing indexes.
auto AddIndex = [&](SymbolIndex *Idx) {
if (this->Index != nullptr) {
diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h
index 88b6d2f..2bc8f02 100644
--- a/clang-tools-extra/clangd/ClangdServer.h
+++ b/clang-tools-extra/clangd/ClangdServer.h
@@ -185,10 +185,6 @@ public:
/// regions in the document.
bool PublishInactiveRegions = false;
- /// Whether to run preamble indexing asynchronously in an independent
- /// thread.
- bool AsyncPreambleIndexing = false;
-
explicit operator TUScheduler::Options() const;
};
// Sensible default options for use in tests.
diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp
index f4547a5..31b38d0 100644
--- a/clang-tools-extra/clangd/Preamble.cpp
+++ b/clang-tools-extra/clangd/Preamble.cpp
@@ -11,6 +11,8 @@
#include "Compiler.h"
#include "Config.h"
#include "Diagnostics.h"
+#include "FS.h"
+#include "FeatureModule.h"
#include "Headers.h"
#include "Protocol.h"
#include "SourceCode.h"
@@ -20,8 +22,10 @@
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/Type.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/DiagnosticLex.h"
+#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
@@ -50,12 +54,17 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
+#include <cassert>
+#include <chrono>
#include <cstddef>
+#include <cstdint>
+#include <cstdlib>
#include <functional>
#include <memory>
#include <optional>
@@ -606,7 +615,8 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
});
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine =
CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(),
- &PreambleDiagnostics, false);
+ &PreambleDiagnostics,
+ /*ShouldOwnClient=*/false);
const Config &Cfg = Config::current();
PreambleDiagnostics.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) {
@@ -653,8 +663,12 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
auto BuiltPreamble = PrecompiledPreamble::Build(
CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine,
Stats ? TimedFS : StatCacheFS, std::make_shared<PCHContainerOperations>(),
- StoreInMemory, /*StoragePath=*/StringRef(), CapturedInfo);
+ StoreInMemory, /*StoragePath=*/"", CapturedInfo);
PreambleTimer.stopTimer();
+ // Reset references to ref-counted-ptrs before executing the callbacks, to
+ // prevent resetting them concurrently.
+ PreambleDiagsEngine.reset();
+ CI.DiagnosticOpts.reset();
// When building the AST for the main file, we do want the function
// bodies.