1. sanitizer_common: sanitize time functions (details)
  2. [clang-repl] Implement partial translation units and error recovery. (details)
Commit 8df3c7ded26fb9ee1ecf5a5099da7645174a2e26 by dvyukov
sanitizer_common: sanitize time functions

We have SleepForSeconds, SleepForMillis and internal_sleep.
Some are implemented in terms of libc functions, some -- in terms
of syscalls. Some are implemented in per OS files,
some -- in libc/nolibc files. That's unnecessary complex
and libc functions cause crashes in some contexts because
we intercept them. There is no single reason to have calls to libc
when we have syscalls (and we have them anyway).

Add internal_usleep that is implemented in terms of syscalls per OS.
Make SleepForSeconds/SleepForMillis/internal_sleep a wrapper
around internal_usleep that is implemented in sanitizer_common.cpp once.

Also remove return values for internal_sleep, it's not used anywhere.

Eventually it would be nice to remove SleepForSeconds/SleepForMillis/internal_sleep.
There is no point in having that many different names for the same thing.

Reviewed By: vitalybuka

Differential Revision:
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_common.h
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_common_nolibc.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_libc.h
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_win.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_common.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp
The file was modifiedcompiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Commit 6775fc6ffa3ca1c36b20c25fa4e7f48f81213cf2 by v.g.vassilev
[clang-repl] Implement partial translation units and error recovery. contained a discussion regarding efficient
modeling of error recovery. @rjmccall has outlined the key ideas:

Conceptually, we can split the translation unit into a sequence of partial
translation units (PTUs). Every declaration will be associated with a unique PTU
that owns it.

The first key insight here is that the owning PTU isn't always the "active"
(most recent) PTU, and it isn't always the PTU that the declaration
"comes from". A new declaration (that isn't a redeclaration or specialization of
anything) does belong to the active PTU. A template specialization, however,
belongs to the most recent PTU of all the declarations in its signature - mostly
that means that it can be pulled into a more recent PTU by its template

The second key insight is that processing a PTU might extend an earlier PTU.
Rolling back the later PTU shouldn't throw that extension away. For example, if
the second PTU defines a template, and the third PTU requires that template to
be instantiated at float, that template specialization is still part of the
second PTU. Similarly, if the fifth PTU uses an inline function belonging to the
fourth, that definition still belongs to the fourth. When we go to emit code in
a new PTU, we map each declaration we have to emit back to its owning PTU and
emit it in a new module for just the extensions to that PTU. We keep track of
all the modules we've emitted for a PTU so that we can unload them all if we
decide to roll it back.

Most declarations/definitions will only refer to entities from the same or
earlier PTUs. However, it is possible (primarily by defining a
previously-declared entity, but also through templates or ADL) for an entity
that belongs to one PTU to refer to something from a later PTU. We will have to
keep track of this and prevent unwinding to later PTU when we recognize it.
Fortunately, this should be very rare; and crucially, we don't have to do the
bookkeeping for this if we've only got one PTU, e.g. in normal compilation.
Otherwise, PTUs after the first just need to record enough metadata to be able
to revert any changes they've made to declarations belonging to earlier PTUs,
e.g. to redeclaration chains or template specialization lists.

It should even eventually be possible for PTUs to provide their own slab
allocators which can be thrown away as part of rolling back the PTU. We can
maintain a notion of the active allocator and allocate things like Stmt/Expr
nodes in it, temporarily changing it to the appropriate PTU whenever we go to do
something like instantiate a function template. More care will be required when
allocating declarations and types, though.

We would want the PTU to be efficiently recoverable from a Decl; I'm not sure
how best to do that. An easy option that would cover most declarations would be
to make multiple TranslationUnitDecls and parent the declarations appropriately,
but I don't think that's good enough for things like member function templates,
since an instantiation of that would still be parented by its original class.
Maybe we can work this into the DC chain somehow, like how lexical DCs are.

We add a different kind of translation unit `TU_Incremental` which is a
complete translation unit that we might nonetheless incrementally extend later.
Because it is complete (and we might want to generate code for it), we do
perform template instantiation, but because it might be extended later, we don't
warn if it declares or uses undefined internal-linkage symbols.

This patch teaches clang-repl how to recover from errors by disconnecting the
most recent PTU and update the primary PTU lookup tables. For instance:

clang-repl> int i = 12; error;
In file included from <<< inputs >>>:1:
input_line_0:1:13: error: C++ requires a type specifier for all declarations
int i = 12; error;
error: Parsing failed.
clang-repl> int i = 13; extern "C" int printf(const char*,...);
clang-repl> auto r1 = printf("i=%d\n", i);
clang-repl> quit

Differential revision:
The file was removedclang/include/clang/Interpreter/Transaction.h
The file was modifiedclang/include/clang/AST/ASTContext.h
The file was modifiedclang/unittests/Interpreter/InterpreterTest.cpp
The file was modifiedclang/lib/Frontend/ASTUnit.cpp
The file was modifiedclang/lib/AST/Decl.cpp
The file was modifiedclang/lib/Frontend/CompilerInstance.cpp
The file was modifiedclang/lib/Serialization/ASTReader.cpp
The file was modifiedclang/lib/Interpreter/IncrementalParser.cpp
The file was addedclang/include/clang/Interpreter/PartialTranslationUnit.h
The file was modifiedclang/lib/Interpreter/Interpreter.cpp
The file was modifiedclang/include/clang/Lex/Preprocessor.h
The file was modifiedclang/lib/AST/ASTContext.cpp
The file was modifiedclang/lib/Sema/Sema.cpp
The file was modifiedclang/unittests/AST/ASTVectorTest.cpp
The file was modifiedclang/lib/AST/DeclBase.cpp
The file was modifiedclang/unittests/Interpreter/IncrementalProcessingTest.cpp
The file was modifiedclang/include/clang/AST/Decl.h
The file was modifiedclang/unittests/Lex/PPCallbacksTest.cpp
The file was modifiedclang/include/clang/AST/Redeclarable.h
The file was modifiedclang/include/clang/Basic/LangOptions.h
The file was modifiedclang/lib/Interpreter/IncrementalParser.h
The file was modifiedclang/include/clang/Interpreter/Interpreter.h
The file was modifiedclang/include/clang/Sema/Sema.h
The file was modifiedclang/tools/clang-import-test/clang-import-test.cpp