Thread safety analysis: Consider global variables in scope
authorAaron Puchert <aaronpuchert@alice-dsl.net>
Sun, 25 Oct 2020 18:31:53 +0000 (19:31 +0100)
committerAaron Puchert <aaronpuchert@alice-dsl.net>
Sun, 25 Oct 2020 18:32:26 +0000 (19:32 +0100)
commit5250a03a9959c2701a8338fe1a1ffa8bd93d6173
tree69fe3e88cf3bb24642d7026004b802f834722b89
parent1ff313f0989009f8a080418e93fe510ec66e8999
Thread safety analysis: Consider global variables in scope

Instead of just mutex members we also consider mutex globals.
Unsurprisingly they are always in scope. Now the paper [1] says that

> The scope of a class member is assumed to be its enclosing class,
> while the scope of a global variable is the translation unit in
> which it is defined.

But I don't think we should limit this to TUs where a definition is
available - a declaration is enough to acquire the mutex, and if a mutex
is really limited in scope to a translation unit, it should probably be
only declared there.

The previous attempt in 9dcc82f34ea was causing false positives because
I wrongly assumed that LiteralPtrs were always globals, which they are
not. This should be fixed now.

[1] https://static.googleusercontent.com/media/research.google.com/en/us/pubs/archive/42958.pdf

Fixes PR46354.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D84604
clang/lib/Analysis/ThreadSafety.cpp
clang/test/SemaCXX/warn-thread-safety-analysis.cpp
clang/test/SemaCXX/warn-thread-safety-negative.cpp