Go to file
Eric Fiselier 341e825eae Implement __attribute__((require_constant_initialization)) for safe static initialization.
Summary:
This attribute specifies expectations about the initialization of static and
thread local variables. Specifically that the variable has a
[constant initializer](http://en.cppreference.com/w/cpp/language/constant_initialization)
according to the rules of [basic.start.static]. Failure to meet this expectation
will result in an error.

Static objects with constant initializers avoid hard-to-find bugs caused by
the indeterminate order of dynamic initialization. They can also be safely
used by other static constructors across translation units.

This attribute acts as a compile time assertion that the requirements
for constant initialization have been met. Since these requirements change
between dialects and have subtle pitfalls it's important to fail fast instead
of silently falling back on dynamic initialization.

```c++
  // -std=c++14
  #define SAFE_STATIC __attribute__((require_constant_initialization)) static
  struct T {
    constexpr T(int) {}
    ~T();
  };
  SAFE_STATIC T x = {42}; // OK.
  SAFE_STATIC T y = 42; // error: variable does not have a constant initializer
  // copy initialization is not a constant expression on a non-literal type.
```
This attribute can only be applied to objects with static or thread-local storage
duration.

Reviewers: majnemer, rsmith, aaron.ballman

Subscribers: jroelofs, cfe-commits

Differential Revision: https://reviews.llvm.org/D23385

llvm-svn: 280525
2016-09-02 18:53:31 +00:00
clang Implement __attribute__((require_constant_initialization)) for safe static initialization. 2016-09-02 18:53:31 +00:00
clang-tools-extra Add clang-reorder-fields to clang-tools-extra 2016-09-02 02:56:07 +00:00
compiler-rt [asan] Move scariness_score_test.cc to a common subdirectory 2016-09-02 07:18:35 +00:00
debuginfo-tests New round of fixes for "Always compile debuginfo-tests for the host triple" 2014-10-18 23:47:59 +00:00
libclc configure.py: Add polaris10 and polaris11 2016-08-30 18:00:41 +00:00
libcxx cstdio: limit gets to CRT versions below 14 2016-09-01 21:09:19 +00:00
libcxxabi [lit] Replace print with lit_config.note(). 2016-09-02 13:56:05 +00:00
libunwind consistently add \n to log and trace messages 2016-08-30 15:38:10 +00:00
lld Dispatch without hash table lookup. 2016-09-02 18:52:41 +00:00
lldb Check for null 2016-09-02 18:15:48 +00:00
llgo [llgo] add llgo source path to LLVM_GO_PACKAGES 2016-07-27 03:01:00 +00:00
llvm AMDGPU: Reorganize store tests 2016-09-02 18:52:28 +00:00
openmp Use 'critical' reduction method when 'atomic' is not available but requested. 2016-09-02 18:29:45 +00:00
parallel-libs [SE] Remove broken doc ref 2016-09-02 18:07:48 +00:00
polly Drop '@brief' from doxygen comments 2016-09-02 06:33:33 +00:00