[clangd] Pass Context implicitly using TLS.

Authored by sammccall on Jan 31 2018, 5:40 AM.


[clangd] Pass Context implicitly using TLS.

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


sammccallJan 31 2018, 5:40 AM
Differential Revision
D42517: [clangd] Pass Context implicitly using TLS.
rGdd48c6b5194c: Update header guard.