2018-08-15 00:03:32 +08:00
|
|
|
//===--- Context.cpp ---------------------------------------------*- C++-*-===//
|
2017-12-12 19:16:45 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-12 19:16:45 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-12 19:16:45 +08:00
|
|
|
|
|
|
|
#include "Context.h"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2018-02-25 15:21:16 +08:00
|
|
|
Context Context::empty() { return Context(/*DataPtr=*/nullptr); }
|
2017-12-12 19:16:45 +08:00
|
|
|
|
|
|
|
Context::Context(std::shared_ptr<const Data> DataPtr)
|
|
|
|
: DataPtr(std::move(DataPtr)) {}
|
|
|
|
|
|
|
|
Context Context::clone() const { return Context(DataPtr); }
|
|
|
|
|
2018-02-06 22:25:02 +08:00
|
|
|
static Context ¤tContext() {
|
|
|
|
static thread_local auto C = Context::empty();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
const Context &Context::current() { return currentContext(); }
|
|
|
|
|
|
|
|
Context Context::swapCurrent(Context Replacement) {
|
|
|
|
std::swap(Replacement, currentContext());
|
|
|
|
return Replacement;
|
|
|
|
}
|
|
|
|
|
2017-12-12 19:16:45 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|