diff --git a/.gitignore b/.gitignore index 6c34e37f4cb..3d07e81baf3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,7 @@ # Byte compiled python modules. *.pyc # vim swap files -.*.swp +.*.sw? .sw? #==============================================================================# diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ce621c04b8..ab70f1dab61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,9 @@ set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." ) set(DEFAULT_SYSROOT "" CACHE PATH "Default to all compiler invocations for --sysroot=." ) +set(CLANG_DEFAULT_OPENMP_RUNTIME "libgomp" CACHE STRING + "Default OpenMP runtime used by -fopenmp.") + set(CLANG_VENDOR "" CACHE STRING "Vendor-specific text for showing with version information.") @@ -317,6 +320,17 @@ function(clang_tablegen) endif() endfunction(clang_tablegen) +macro(set_clang_windows_version_resource_properties name) + if(DEFINED windows_resource_file) + set_windows_version_resource_properties(${name} ${windows_resource_file} + VERSION_MAJOR ${CLANG_VERSION_MAJOR} + VERSION_MINOR ${CLANG_VERSION_MINOR} + VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL} + VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})" + PRODUCT_NAME "clang") + endif() +endmacro() + macro(add_clang_library name) cmake_parse_arguments(ARG "" @@ -362,6 +376,7 @@ macro(add_clang_library name) if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang") install(TARGETS ${name} + EXPORT ClangTargets LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} RUNTIME DESTINATION bin) @@ -373,11 +388,13 @@ macro(add_clang_library name) endif() set_target_properties(${name} PROPERTIES FOLDER "Clang libraries") + set_clang_windows_version_resource_properties(${name}) endmacro(add_clang_library) macro(add_clang_executable name) add_llvm_executable( ${name} ${ARGN} ) set_target_properties(${name} PROPERTIES FOLDER "Clang executables") + set_clang_windows_version_resource_properties(${name}) endmacro(add_clang_executable) set(CMAKE_INCLUDE_CURRENT_DIR ON) @@ -521,9 +538,10 @@ if (CLANG_BUILT_STANDALONE) # Install a /share/clang/cmake/ClangConfig.cmake file so that # find_package(Clang) works. Install the target list with it. + install(EXPORT ClangTargets DESTINATION ${CLANG_INSTALL_PACKAGE_DIR}) + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/ClangConfig.cmake - ${CLANG_BINARY_DIR}/share/clang/cmake/ClangTargets.cmake DESTINATION share/clang/cmake) # Also copy ClangConfig.cmake to the build directory so that dependent projects diff --git a/CODE_OWNERS.TXT b/CODE_OWNERS.TXT index 34990932e26..905303fe11c 100644 --- a/CODE_OWNERS.TXT +++ b/CODE_OWNERS.TXT @@ -12,6 +12,10 @@ N: Aaron Ballman E: aaron@aaronballman.com D: Clang attributes +N: Alexey Bataev +E: a.bataev@hotmail.com +D: OpenMP support + N: Chandler Carruth E: chandlerc@gmail.com E: chandlerc@google.com diff --git a/OWNERS b/OWNERS deleted file mode 100644 index ac015a5aadb..00000000000 --- a/OWNERS +++ /dev/null @@ -1,8 +0,0 @@ -dschuff@chromium.org -eliben@chromium.org -jfb@chromium.org -jvoung@chromium.org -kschimpf@chromium.org -mseaborn@chromium.org -sehr@chromium.org -stichnot@chromium.org diff --git a/PRESUBMIT.py b/PRESUBMIT.py deleted file mode 100644 index d81168ea093..00000000000 --- a/PRESUBMIT.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright (c) 2012 The Native Client Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -# Documentation on PRESUBMIT.py can be found at: -# http://www.chromium.org/developers/how-tos/depottools/presubmit-scripts - -EXCLUDE_PROJECT_CHECKS_DIRS = [ '.' ] - -import subprocess -def CheckGitBranch(): - p = subprocess.Popen("git branch -vv", shell=True, - stdout=subprocess.PIPE) - output, _ = p.communicate() - - lines = output.split('\n') - for line in lines: - # output format for checked-out branch should be - # * branchname hash [TrackedBranchName ... - toks = line.split() - if '*' not in toks[0]: - continue - if not 'origin/master' in toks[3]: - warning = 'Warning: your current branch:\n' + line - warning += '\nis not tracking origin/master. git cl push may silently ' - warning += 'fail to push your change. To fix this, do\n' - warning += 'git branch -u origin/master' - return warning - return None - print 'Warning: presubmit check could not determine local git branch' - return None - -def _CommonChecks(input_api, output_api): - """Checks for both upload and commit.""" - results = [] - results.extend(input_api.canned_checks.PanProjectChecks( - input_api, output_api, project_name='Native Client', - excluded_paths=tuple(EXCLUDE_PROJECT_CHECKS_DIRS))) - branch_warning = CheckGitBranch() - if branch_warning: - results.append(output_api.PresubmitPromptWarning(branch_warning)) - return results - -def CheckChangeOnUpload(input_api, output_api): - """Verifies all changes in all files. - Args: - input_api: the limited set of input modules allowed in presubmit. - output_api: the limited set of output modules allowed in presubmit. - """ - report = [] - report.extend(_CommonChecks(input_api, output_api)) - return report - -def CheckChangeOnCommit(input_api, output_api): - """Verifies all changes in all files and verifies that the - tree is open and can accept a commit. - Args: - input_api: the limited set of input modules allowed in presubmit. - output_api: the limited set of output modules allowed in presubmit. - """ - report = [] - report.extend(CheckChangeOnUpload(input_api, output_api)) - return report - -def GetPreferredTrySlaves(project, change): - return [] diff --git a/codereview.settings b/codereview.settings deleted file mode 100644 index fae9103b6a1..00000000000 --- a/codereview.settings +++ /dev/null @@ -1,12 +0,0 @@ -# This file is used by gcl to get repository specific information. -CODE_REVIEW_SERVER: codereview.chromium.org -CC_LIST: native-client-reviews@googlegroups.com -VIEW_VC: -https://gerrit.chromium.org/gerrit/gitweb?p=native_client/pnacl-clang.git;a=commit;h= -STATUS: http://nativeclient-status.appspot.com/status -TRY_ON_UPLOAD: False -TRYSERVER_PROJECT: nacl -TRYSERVER_SVN_URL: svn://svn.chromium.org/chrome-try/try-nacl -PUSH_URL_CONFIG: url.ssh://gerrit.chromium.org.pushinsteadof -ORIGIN_URL_CONFIG: http://git.chromium.org - diff --git a/docs/AddressSanitizer.rst b/docs/AddressSanitizer.rst index 617543334d0..66ed3447fdb 100644 --- a/docs/AddressSanitizer.rst +++ b/docs/AddressSanitizer.rst @@ -60,7 +60,28 @@ or: % clang -g -fsanitize=address example_UseAfterFree.o If a bug is detected, the program will print an error message to stderr and -exit with a non-zero exit code. To make AddressSanitizer symbolize its output +exit with a non-zero exit code. AddressSanitizer exits on the first detected error. +This is by design: + +* This approach allows AddressSanitizer to produce faster and smaller generated code + (both by ~5%). +* Fixing bugs becomes unavoidable. AddressSanitizer does not produce + false alarms. Once a memory corruption occurs, the program is in an inconsistent + state, which could lead to confusing results and potentially misleading + subsequent reports. + +If your process is sandboxed and you are running on OS X 10.10 or earlier, you +will need to set ``DYLD_INSERT_LIBRARIES`` environment variable and point it to +the ASan library that is packaged with the compiler used to build the +executable. (You can find the library by searching for dynamic libraries with +``asan`` in their name.) If the environment variable is not set, the process will +try to re-exec. Also keep in mind that when moving the executable to another machine, +the ASan library will also need to be copied over. + +Symbolizing the Reports +========================= + +To make AddressSanitizer symbolize its output you need to set the ``ASAN_SYMBOLIZER_PATH`` environment variable to point to the ``llvm-symbolizer`` binary (or make sure ``llvm-symbolizer`` is in your ``$PATH``): @@ -100,14 +121,63 @@ force disabled by setting ``ASAN_OPTIONS=symbolize=0``): Note that on OS X you may need to run ``dsymutil`` on your binary to have the file\:line info in the AddressSanitizer reports. -AddressSanitizer exits on the first detected error. This is by design. -One reason: it makes the generated code smaller and faster (both by -~5%). Another reason: this makes fixing bugs unavoidable. With Valgrind, -it is often the case that users treat Valgrind warnings as false -positives (which they are not) and don't fix them. +Additional Checks +================= -``__has_feature(address_sanitizer)`` ------------------------------------- +Initialization order checking +----------------------------- + +AddressSanitizer can optionally detect dynamic initialization order problems, +when initialization of globals defined in one translation unit uses +globals defined in another translation unit. To enable this check at runtime, +you should set environment variable +``ASAN_OPTIONS=check_initialization_order=1``. + +Note that this option is not supported on OS X. + +Memory leak detection +--------------------- + +For more information on leak detector in AddressSanitizer, see +:doc:`LeakSanitizer`. The leak detection is turned on by default on Linux; +however, it is not yet supported on other platforms. + +Issue Suppression +================= + +AddressSanitizer is not expected to produce false positives. If you see one, +look again; most likely it is a true positive! + +Suppressing Reports in External Libraries +----------------------------------------- +Runtime interposition allows AddressSanitizer to find bugs in code that is +not being recompiled. If you run into an issue in external libraries, we +recommend immediately reporting it to the library maintainer so that it +gets addressed. However, you can use the following suppression mechanism +to unblock yourself and continue on with the testing. This suppression +mechanism should only be used for suppressing issues in external code; it +does not work on code recompiled with AddressSanitizer. To suppress errors +in external libraries, set the ``ASAN_OPTIONS`` environment variable to point +to a suppression file. You can either specify the full path to the file or the +path of the file relative to the location of your executable. + +.. code-block:: bash + + ASAN_OPTIONS=suppressions=MyASan.supp + +Use the following format to specify the names of the functions or libraries +you want to suppress. You can see these in the error report. Remember that +the narrower the scope of the suppression, the more bugs you will be able to +catch. + +.. code-block:: bash + + interceptor_via_fun:NameOfCFunctionToSuppress + interceptor_via_fun:-[ClassName objCMethodToSuppress:] + interceptor_via_lib:NameOfTheLibraryToSuppress + +Conditional Compilation with ``__has_feature(address_sanitizer)`` +----------------------------------------------------------------- In some cases one may need to execute different code depending on whether AddressSanitizer is enabled. @@ -122,28 +192,19 @@ this purpose. # endif #endif -``__attribute__((no_sanitize_address))`` ------------------------------------------------ +Disabling Instrumentation with ``__attribute__((no_sanitize("address")))`` +-------------------------------------------------------------------------- Some code should not be instrumented by AddressSanitizer. One may use the -function attribute -:ref:`no_sanitize_address ` -(or a deprecated synonym `no_address_safety_analysis`) -to disable instrumentation of a particular function. This attribute may not be -supported by other compilers, so we suggest to use it together with -``__has_feature(address_sanitizer)``. - -Initialization order checking ------------------------------ - -AddressSanitizer can optionally detect dynamic initialization order problems, -when initialization of globals defined in one translation unit uses -globals defined in another translation unit. To enable this check at runtime, -you should set environment variable -``ASAN_OPTIONS=check_initialization_order=1``. +function attribute ``__attribute__((no_sanitize("address")))`` +(which has deprecated synonyms +:ref:`no_sanitize_address ` and +`no_address_safety_analysis`) to disable instrumentation of a particular +function. This attribute may not be supported by other compilers, so we suggest +to use it together with ``__has_feature(address_sanitizer)``. -Blacklist ---------- +Suppressing Errors in Recompiled Code (Blacklist) +------------------------------------------------- AddressSanitizer supports ``src`` and ``fun`` entity types in :doc:`SanitizerSpecialCaseList`, that can be used to suppress error reports @@ -172,24 +233,6 @@ problems happening in certain source files or with certain global variables. type:*BadInitClassSubstring*=init src:bad/init/files/*=init -Memory leak detection ---------------------- - -For the experimental memory leak detector in AddressSanitizer, see -:doc:`LeakSanitizer`. - -Supported Platforms -=================== - -AddressSanitizer is supported on - -* Linux i386/x86\_64 (tested on Ubuntu 12.04); -* MacOS 10.6 - 10.9 (i386/x86\_64). -* Android ARM -* FreeBSD i386/x86\_64 (tested on FreeBSD 11-current) - -Ports to various other platforms are in progress. - Limitations =========== @@ -202,6 +245,19 @@ Limitations usually expected. * Static linking is not supported. +Supported Platforms +=================== + +AddressSanitizer is supported on: + +* Linux i386/x86\_64 (tested on Ubuntu 12.04) +* OS X 10.7 - 10.11 (i386/x86\_64) +* iOS Simulator +* Android ARM +* FreeBSD i386/x86\_64 (tested on FreeBSD 11-current) + +Ports to various other platforms are in progress. + Current Status ============== diff --git a/docs/AttributeReference.rst b/docs/AttributeReference.rst index 115a217c4c1..a763ddeaeb1 100644 --- a/docs/AttributeReference.rst +++ b/docs/AttributeReference.rst @@ -1,1116 +1,13 @@ .. ------------------------------------------------------------------- NOTE: This file is automatically generated by running clang-tblgen - -gen-attr-docs. Do not edit this file by hand!! + -gen-attr-docs. Do not edit this file by hand!! The contents for + this file are automatically generated by a server-side process. + + Please do not commit this file. The file exists for local testing + purposes only. ------------------------------------------------------------------- =================== Attributes in Clang -=================== -.. contents:: - :local: - -Introduction -============ - -This page lists the attributes currently supported by Clang. - -Function Attributes -=================== - - -interrupt ---------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Clang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on -ARM targets. This attribute may be attached to a function definition and -instructs the backend to generate appropriate function entry/exit code so that -it can be used directly as an interrupt service routine. - -The parameter passed to the interrupt attribute is optional, but if -provided it must be a string literal with one of the following values: "IRQ", -"FIQ", "SWI", "ABORT", "UNDEF". - -The semantics are as follows: - -- If the function is AAPCS, Clang instructs the backend to realign the stack to - 8 bytes on entry. This is a general requirement of the AAPCS at public - interfaces, but may not hold when an exception is taken. Doing this allows - other AAPCS functions to be called. -- If the CPU is M-class this is all that needs to be done since the architecture - itself is designed in such a way that functions obeying the normal AAPCS ABI - constraints are valid exception handlers. -- If the CPU is not M-class, the prologue and epilogue are modified to save all - non-banked registers that are used, so that upon return the user-mode state - will not be corrupted. Note that to avoid unnecessary overhead, only - general-purpose (integer) registers are saved in this way. If VFP operations - are needed, that state must be saved manually. - - Specifically, interrupt kinds other than "FIQ" will save all core registers - except "lr" and "sp". "FIQ" interrupts will save r0-r7. -- If the CPU is not M-class, the return instruction is changed to one of the - canonical sequences permitted by the architecture for exception return. Where - possible the function itself will make the necessary "lr" adjustments so that - the "preferred return address" is selected. - - Unfortunately the compiler is unable to make this guarantee for an "UNDEF" - handler, where the offset from "lr" to the preferred return address depends on - the execution state of the code which generated the exception. In this case - a sequence equivalent to "movs pc, lr" will be used. - - -acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability) ------------------------------------------------------------------------------------------------------------ -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -Marks a function as acquiring a capability. - - -assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability) -------------------------------------------------------------------------------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -Marks a function that dynamically tests whether a capability is held, and halts -the program if it is not held. - - -availability ------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -The ``availability`` attribute can be placed on declarations to describe the -lifecycle of that declaration relative to operating system versions. Consider -the function declaration for a hypothetical function ``f``: - -.. code-block:: c++ - - void f(void) __attribute__((availability(macosx,introduced=10.4,deprecated=10.6,obsoleted=10.7))); - -The availability attribute states that ``f`` was introduced in Mac OS X 10.4, -deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information -is used by Clang to determine when it is safe to use ``f``: for example, if -Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()`` -succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call -succeeds but Clang emits a warning specifying that the function is deprecated. -Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call -fails because ``f()`` is no longer available. - -The availability attribute is a comma-separated list starting with the -platform name and then including clauses specifying important milestones in the -declaration's lifetime (in any order) along with additional information. Those -clauses can be: - -introduced=\ *version* - The first version in which this declaration was introduced. - -deprecated=\ *version* - The first version in which this declaration was deprecated, meaning that - users should migrate away from this API. - -obsoleted=\ *version* - The first version in which this declaration was obsoleted, meaning that it - was removed completely and can no longer be used. - -unavailable - This declaration is never available on this platform. - -message=\ *string-literal* - Additional message text that Clang will provide when emitting a warning or - error about use of a deprecated or obsoleted declaration. Useful to direct - users to replacement APIs. - -Multiple availability attributes can be placed on a declaration, which may -correspond to different platforms. Only the availability attribute with the -platform corresponding to the target platform will be used; any others will be -ignored. If no availability attribute specifies availability for the current -target platform, the availability attributes are ignored. Supported platforms -are: - -``ios`` - Apple's iOS operating system. The minimum deployment target is specified by - the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*`` - command-line arguments. - -``macosx`` - Apple's Mac OS X operating system. The minimum deployment target is - specified by the ``-mmacosx-version-min=*version*`` command-line argument. - -A declaration can be used even when deploying back to a platform version prior -to when the declaration was introduced. When this happens, the declaration is -`weakly linked -`_, -as if the ``weak_import`` attribute were added to the declaration. A -weakly-linked declaration may or may not be present a run-time, and a program -can determine whether the declaration is present by checking whether the -address of that declaration is non-NULL. - -If there are multiple declarations of the same entity, the availability -attributes must either match on a per-platform basis or later -declarations must not have availability attributes for that -platform. For example: - -.. code-block:: c - - void g(void) __attribute__((availability(macosx,introduced=10.4))); - void g(void) __attribute__((availability(macosx,introduced=10.4))); // okay, matches - void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform - void g(void); // okay, inherits both macosx and ios availability from above. - void g(void) __attribute__((availability(macosx,introduced=10.5))); // error: mismatch - -When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,: - -.. code-block:: objc - - @interface A - - (id)method __attribute__((availability(macosx,introduced=10.4))); - - (id)method2 __attribute__((availability(macosx,introduced=10.4))); - @end - - @interface B : A - - (id)method __attribute__((availability(macosx,introduced=10.3))); // okay: method moved into base class later - - (id)method __attribute__((availability(macosx,introduced=10.5))); // error: this method was available via the base class in 10.4 - @end - - -_Noreturn ---------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "","","","X" - -A function declared as ``_Noreturn`` shall not return to its caller. The -compiler will generate a diagnostic for a function declared as ``_Noreturn`` -that appears to be capable of returning to its caller. - - -noreturn --------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "","X","","" - -A function declared as ``[[noreturn]]`` shall not return to its caller. The -compiler will generate a diagnostic for a function declared as ``[[noreturn]]`` -that appears to be capable of returning to its caller. - - -carries_dependency ------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``carries_dependency`` attribute specifies dependency propagation into and -out of functions. - -When specified on a function or Objective-C method, the ``carries_dependency`` -attribute means that the return value carries a dependency out of the function, -so that the implementation need not constrain ordering upon return from that -function. Implementations of the function and its caller may choose to preserve -dependencies instead of emitting memory ordering instructions such as fences. - -Note, this attribute does not change the meaning of the program, but may result -in generation of more efficient code. - - -enable_if ---------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -.. Note:: Some features of this attribute are experimental. The meaning of - multiple enable_if attributes on a single declaration is subject to change in - a future version of clang. Also, the ABI is not standardized and the name - mangling may change in future versions. To avoid that, use asm labels. - -The ``enable_if`` attribute can be placed on function declarations to control -which overload is selected based on the values of the function's arguments. -When combined with the ``overloadable`` attribute, this feature is also -available in C. - -.. code-block:: c++ - - int isdigit(int c); - int isdigit(int c) __attribute__((enable_if(c <= -1 || c > 255, "chosen when 'c' is out of range"))) __attribute__((unavailable("'c' must have the value of an unsigned char or EOF"))); - - void foo(char c) { - isdigit(c); - isdigit(10); - isdigit(-10); // results in a compile-time error. - } - -The enable_if attribute takes two arguments, the first is an expression written -in terms of the function parameters, the second is a string explaining why this -overload candidate could not be selected to be displayed in diagnostics. The -expression is part of the function signature for the purposes of determining -whether it is a redeclaration (following the rules used when determining -whether a C++ template specialization is ODR-equivalent), but is not part of -the type. - -The enable_if expression is evaluated as if it were the body of a -bool-returning constexpr function declared with the arguments of the function -it is being applied to, then called with the parameters at the callsite. If the -result is false or could not be determined through constant expression -evaluation, then this overload will not be chosen and the provided string may -be used in a diagnostic if the compile fails as a result. - -Because the enable_if expression is an unevaluated context, there are no global -state changes, nor the ability to pass information from the enable_if -expression to the function body. For example, suppose we want calls to -strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of -strbuf) only if the size of strbuf can be determined: - -.. code-block:: c++ - - __attribute__((always_inline)) - static inline size_t strnlen(const char *s, size_t maxlen) - __attribute__((overloadable)) - __attribute__((enable_if(__builtin_object_size(s, 0) != -1))), - "chosen when the buffer size is known but 'maxlen' is not"))) - { - return strnlen_chk(s, maxlen, __builtin_object_size(s, 0)); - } - -Multiple enable_if attributes may be applied to a single declaration. In this -case, the enable_if expressions are evaluated from left to right in the -following manner. First, the candidates whose enable_if expressions evaluate to -false or cannot be evaluated are discarded. If the remaining candidates do not -share ODR-equivalent enable_if expressions, the overload resolution is -ambiguous. Otherwise, enable_if overload resolution continues with the next -enable_if attribute on the candidates that have not been discarded and have -remaining enable_if attributes. In this way, we pick the most specific -overload out of a number of viable overloads using enable_if. - -.. code-block:: c++ - - void f() __attribute__((enable_if(true, ""))); // #1 - void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2 - - void g(int i, int j) __attribute__((enable_if(i, ""))); // #1 - void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2 - -In this example, a call to f() is always resolved to #2, as the first enable_if -expression is ODR-equivalent for both declarations, but #1 does not have another -enable_if expression to continue evaluating, so the next round of evaluation has -only a single candidate. In a call to g(1, 1), the call is ambiguous even though -#2 has more enable_if attributes, because the first enable_if expressions are -not ODR-equivalent. - -Query for this feature with ``__has_attribute(enable_if)``. - - -flatten (gnu::flatten) ----------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``flatten`` attribute causes calls within the attributed function to -be inlined unless it is impossible to do so, for example if the body of the -callee is unavailable or if the callee has the ``noinline`` attribute. - - -format (gnu::format) --------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -Clang supports the ``format`` attribute, which indicates that the function -accepts a ``printf`` or ``scanf``-like format string and corresponding -arguments or a ``va_list`` that contains these arguments. - -Please see `GCC documentation about format attribute -`_ to find details -about attribute syntax. - -Clang implements two kinds of checks with this attribute. - -#. Clang checks that the function with the ``format`` attribute is called with - a format string that uses format specifiers that are allowed, and that - arguments match the format string. This is the ``-Wformat`` warning, it is - on by default. - -#. Clang checks that the format string argument is a literal string. This is - the ``-Wformat-nonliteral`` warning, it is off by default. - - Clang implements this mostly the same way as GCC, but there is a difference - for functions that accept a ``va_list`` argument (for example, ``vprintf``). - GCC does not emit ``-Wformat-nonliteral`` warning for calls to such - fuctions. Clang does not warn if the format string comes from a function - parameter, where the function is annotated with a compatible attribute, - otherwise it warns. For example: - - .. code-block:: c - - __attribute__((__format__ (__scanf__, 1, 3))) - void foo(const char* s, char *buf, ...) { - va_list ap; - va_start(ap, buf); - - vprintf(s, ap); // warning: format string is not a string literal - } - - In this case we warn because ``s`` contains a format string for a - ``scanf``-like function, but it is passed to a ``printf``-like function. - - If the attribute is removed, clang still warns, because the format string is - not a string literal. - - Another example: - - .. code-block:: c - - __attribute__((__format__ (__printf__, 1, 3))) - void foo(const char* s, char *buf, ...) { - va_list ap; - va_start(ap, buf); - - vprintf(s, ap); // warning - } - - In this case Clang does not warn because the format string ``s`` and - the corresponding arguments are annotated. If the arguments are - incorrect, the caller of ``foo`` will receive a warning. - - -noduplicate (clang::noduplicate) --------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``noduplicate`` attribute can be placed on function declarations to control -whether function calls to this function can be duplicated or not as a result of -optimizations. This is required for the implementation of functions with -certain special requirements, like the OpenCL "barrier" function, that might -need to be run concurrently by all the threads that are executing in lockstep -on the hardware. For example this attribute applied on the function -"nodupfunc" in the code below avoids that: - -.. code-block:: c - - void nodupfunc() __attribute__((noduplicate)); - // Setting it as a C++11 attribute is also valid - // void nodupfunc() [[clang::noduplicate]]; - void foo(); - void bar(); - - nodupfunc(); - if (a > n) { - foo(); - } else { - bar(); - } - -gets possibly modified by some optimizations into code similar to this: - -.. code-block:: c - - if (a > n) { - nodupfunc(); - foo(); - } else { - nodupfunc(); - bar(); - } - -where the call to "nodupfunc" is duplicated and sunk into the two branches -of the condition. - - -no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address) ------------------------------------------------------------------------------------------------------------ -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -.. _langext-address_sanitizer: - -Use ``__attribute__((no_sanitize_address))`` on a function declaration to -specify that address safety instrumentation (e.g. AddressSanitizer) should -not be applied to that function. - - -no_sanitize_memory ------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -.. _langext-memory_sanitizer: - -Use ``__attribute__((no_sanitize_memory))`` on a function declaration to -specify that checks for uninitialized memory should not be inserted -(e.g. by MemorySanitizer). The function may still be instrumented by the tool -to avoid false positives in other places. - - -no_sanitize_thread ------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -.. _langext-thread_sanitizer: - -Use ``__attribute__((no_sanitize_thread))`` on a function declaration to -specify that checks for data races on plain (non-atomic) memory accesses should -not be inserted by ThreadSanitizer. The function is still instrumented by the -tool to avoid false positives and provide meaningful stack traces. - - -no_split_stack (gnu::no_split_stack) ------------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``no_split_stack`` attribute disables the emission of the split stack -preamble for a particular function. It has no effect if ``-fsplit-stack`` -is not specified. - - -objc_method_family ------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Many methods in Objective-C have conventional meanings determined by their -selectors. It is sometimes useful to be able to mark a method as having a -particular conventional meaning despite not having the right selector, or as -not having the conventional meaning that its selector would suggest. For these -use cases, we provide an attribute to specifically describe the "method family" -that a method belongs to. - -**Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of -``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This -attribute can only be placed at the end of a method declaration: - -.. code-block:: objc - - - (NSString *)initMyStringValue __attribute__((objc_method_family(none))); - -Users who do not wish to change the conventional meaning of a method, and who -merely want to document its non-standard retain and release semantics, should -use the retaining behavior attributes (``ns_returns_retained``, -``ns_returns_not_retained``, etc). - -Query for this feature with ``__has_attribute(objc_method_family)``. - - -objc_requires_super -------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Some Objective-C classes allow a subclass to override a particular method in a -parent class but expect that the overriding method also calls the overridden -method in the parent class. For these cases, we provide an attribute to -designate that a method requires a "call to ``super``" in the overriding -method in the subclass. - -**Usage**: ``__attribute__((objc_requires_super))``. This attribute can only -be placed at the end of a method declaration: - -.. code-block:: objc - - - (void)foo __attribute__((objc_requires_super)); - -This attribute can only be applied the method declarations within a class, and -not a protocol. Currently this attribute does not enforce any placement of -where the call occurs in the overriding method (such as in the case of -``-dealloc`` where the call must appear at the end). It checks only that it -exists. - -Note that on both OS X and iOS that the Foundation framework provides a -convenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this -attribute: - -.. code-block:: objc - - - (void)foo NS_REQUIRES_SUPER; - -This macro is conditionally defined depending on the compiler's support for -this attribute. If the compiler does not support the attribute the macro -expands to nothing. - -Operationally, when a method has this annotation the compiler will warn if the -implementation of an override in a subclass does not call super. For example: - -.. code-block:: objc - - warning: method possibly missing a [super AnnotMeth] call - - (void) AnnotMeth{}; - ^ - - -optnone (clang::optnone) ------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``optnone`` attribute suppresses essentially all optimizations -on a function or method, regardless of the optimization level applied to -the compilation unit as a whole. This is particularly useful when you -need to debug a particular function, but it is infeasible to build the -entire application without optimization. Avoiding optimization on the -specified function can improve the quality of the debugging information -for that function. - -This attribute is incompatible with the ``always_inline`` attribute. - - -overloadable ------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Clang provides support for C++ function overloading in C. Function overloading -in C is introduced using the ``overloadable`` attribute. For example, one -might provide several overloaded versions of a ``tgsin`` function that invokes -the appropriate standard function computing the sine of a value with ``float``, -``double``, or ``long double`` precision: - -.. code-block:: c - - #include - float __attribute__((overloadable)) tgsin(float x) { return sinf(x); } - double __attribute__((overloadable)) tgsin(double x) { return sin(x); } - long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); } - -Given these declarations, one can call ``tgsin`` with a ``float`` value to -receive a ``float`` result, with a ``double`` to receive a ``double`` result, -etc. Function overloading in C follows the rules of C++ function overloading -to pick the best overload given the call arguments, with a few C-specific -semantics: - -* Conversion from ``float`` or ``double`` to ``long double`` is ranked as a - floating-point promotion (per C99) rather than as a floating-point conversion - (as in C++). - -* A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is - considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are - compatible types. - -* A conversion from type ``T`` to a value of type ``U`` is permitted if ``T`` - and ``U`` are compatible types. This conversion is given "conversion" rank. - -The declaration of ``overloadable`` functions is restricted to function -declarations and definitions. Most importantly, if any function with a given -name is given the ``overloadable`` attribute, then all function declarations -and definitions with that name (and in that scope) must have the -``overloadable`` attribute. This rule even applies to redeclarations of -functions whose original declaration had the ``overloadable`` attribute, e.g., - -.. code-block:: c - - int f(int) __attribute__((overloadable)); - float f(float); // error: declaration of "f" must have the "overloadable" attribute - - int g(int) __attribute__((overloadable)); - int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute - -Functions marked ``overloadable`` must have prototypes. Therefore, the -following code is ill-formed: - -.. code-block:: c - - int h() __attribute__((overloadable)); // error: h does not have a prototype - -However, ``overloadable`` functions are allowed to use a ellipsis even if there -are no named parameters (as is permitted in C++). This feature is particularly -useful when combined with the ``unavailable`` attribute: - -.. code-block:: c++ - - void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error - -Functions declared with the ``overloadable`` attribute have their names mangled -according to the same rules as C++ function names. For example, the three -``tgsin`` functions in our motivating example get the mangled names -``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two -caveats to this use of name mangling: - -* Future versions of Clang may change the name mangling of functions overloaded - in C, so you should not depend on an specific mangling. To be completely - safe, we strongly urge the use of ``static inline`` with ``overloadable`` - functions. - -* The ``overloadable`` attribute has almost no meaning when used in C++, - because names will already be mangled and functions are already overloadable. - However, when an ``overloadable`` function occurs within an ``extern "C"`` - linkage specification, it's name *will* be mangled in the same way as it - would in C. - -Query for this feature with ``__has_extension(attribute_overloadable)``. - - -pcs (gnu::pcs) --------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -On ARM targets, this can attribute can be used to select calling conventions, -similar to ``stdcall`` on x86. Valid parameter values are "aapcs" and -"aapcs-vfp". - - -release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability) ------------------------------------------------------------------------------------------------------------ -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -Marks a function as releasing a capability. - - -try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability) ---------------------------------------------------------------------------------------------------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -Marks a function that attempts to acquire a capability. This function may fail to -actually acquire the capability; they accept a Boolean value determining -whether acquiring the capability means success (true), or failing to acquire -the capability means success (false). - - -Variable Attributes -=================== - - -section (gnu::section, __declspec(allocate)) --------------------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","X","" - -The ``section`` attribute allows you to specify a specific section a -global variable or function should be in after translation. - - -tls_model (gnu::tls_model) --------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","X","","" - -The ``tls_model`` attribute allows you to specify which thread-local storage -model to use. It accepts the following strings: - -* global-dynamic -* local-dynamic -* initial-exec -* local-exec - -TLS models are mutually exclusive. - - -thread ------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "","","X","" - -The ``__declspec(thread)`` attribute declares a variable with thread local -storage. It is available under the ``-fms-extensions`` flag for MSVC -compatibility. Documentation for the Visual C++ attribute is available on MSDN_. - -.. _MSDN: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx - -In Clang, ``__declspec(thread)`` is generally equivalent in functionality to the -GNU ``__thread`` keyword. The variable must not have a destructor and must have -a constant initializer, if any. The attribute only applies to variables -declared with static storage duration, such as globals, class static data -members, and static locals. - - -Type Attributes -=============== - - -__single_inhertiance, __multiple_inheritance, __virtual_inheritance -------------------------------------------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "","","","X" - -This collection of keywords is enabled under ``-fms-extensions`` and controls -the pointer-to-member representation used on ``*-*-win32`` targets. - -The ``*-*-win32`` targets utilize a pointer-to-member representation which -varies in size and alignment depending on the definition of the underlying -class. - -However, this is problematic when a forward declaration is only available and -no definition has been made yet. In such cases, Clang is forced to utilize the -most general representation that is available to it. - -These keywords make it possible to use a pointer-to-member representation other -than the most general one regardless of whether or not the definition will ever -be present in the current translation unit. - -This family of keywords belong between the ``class-key`` and ``class-name``: - -.. code-block:: c++ - - struct __single_inheritance S; - int S::*i; - struct S {}; - -This keyword can be applied to class templates but only has an effect when used -on full specializations: - -.. code-block:: c++ - - template struct __single_inheritance A; // warning: inheritance model ignored on primary template - template struct __multiple_inheritance A; // warning: inheritance model ignored on partial specialization - template <> struct __single_inheritance A; - -Note that choosing an inheritance model less general than strictly necessary is -an error: - -.. code-block:: c++ - - struct __multiple_inheritance S; // error: inheritance model does not match definition - int S::*i; - struct S {}; - - -Statement Attributes -==================== - - -fallthrough (clang::fallthrough) --------------------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "","X","","" - -The ``clang::fallthrough`` attribute is used along with the -``-Wimplicit-fallthrough`` argument to annotate intentional fall-through -between switch labels. It can only be applied to a null statement placed at a -point of execution between any statement and the next switch label. It is -common to mark these places with a specific comment, but this attribute is -meant to replace comments with a more strict annotation, which can be checked -by the compiler. This attribute doesn't change semantics of the code and can -be used wherever an intended fall-through occurs. It is designed to mimic -control-flow statements like ``break;``, so it can be placed in most places -where ``break;`` can, but only if there are no statements on the execution path -between it and the next switch label. - -Here is an example: - -.. code-block:: c++ - - // compile with -Wimplicit-fallthrough - switch (n) { - case 22: - case 33: // no warning: no statements between case labels - f(); - case 44: // warning: unannotated fall-through - g(); - [[clang::fallthrough]]; - case 55: // no warning - if (x) { - h(); - break; - } - else { - i(); - [[clang::fallthrough]]; - } - case 66: // no warning - p(); - [[clang::fallthrough]]; // warning: fallthrough annotation does not - // directly precede case label - q(); - case 77: // warning: unannotated fall-through - r(); - } - - -Consumed Annotation Checking -============================ -Clang supports additional attributes for checking basic resource management -properties, specifically for unique objects that have a single owning reference. -The following attributes are currently supported, although **the implementation -for these annotations is currently in development and are subject to change.** - -callable_when -------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Use ``__attribute__((callable_when(...)))`` to indicate what states a method -may be called in. Valid states are unconsumed, consumed, or unknown. Each -argument to this attribute must be a quoted string. E.g.: - -``__attribute__((callable_when("unconsumed", "unknown")))`` - - -consumable ----------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Each ``class`` that uses any of the typestate annotations must first be marked -using the ``consumable`` attribute. Failure to do so will result in a warning. - -This attribute accepts a single parameter that must be one of the following: -``unknown``, ``consumed``, or ``unconsumed``. - - -param_typestate ---------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -This attribute specifies expectations about function parameters. Calls to an -function with annotated parameters will issue a warning if the corresponding -argument isn't in the expected state. The attribute is also used to set the -initial state of the parameter when analyzing the function's body. - - -return_typestate ----------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -The ``return_typestate`` attribute can be applied to functions or parameters. -When applied to a function the attribute specifies the state of the returned -value. The function's body is checked to ensure that it always returns a value -in the specified state. On the caller side, values returned by the annotated -function are initialized to the given state. - -When applied to a function parameter it modifies the state of an argument after -a call to the function returns. The function's body is checked to ensure that -the parameter is in the expected state before returning. - - -set_typestate -------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Annotate methods that transition an object into a new state with -``__attribute__((set_typestate(new_state)))``. The new new state must be -unconsumed, consumed, or unknown. - - -test_typestate --------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Use ``__attribute__((test_typestate(tested_state)))`` to indicate that a method -returns true if the object is in the specified state.. - - -Type Safety Checking -==================== -Clang supports additional attributes to enable checking type safety properties -that can't be enforced by the C type system. Use cases include: - -* MPI library implementations, where these attributes enable checking that - the buffer type matches the passed ``MPI_Datatype``; -* for HDF5 library there is a similar use case to MPI; -* checking types of variadic functions' arguments for functions like - ``fcntl()`` and ``ioctl()``. - -You can detect support for these attributes with ``__has_attribute()``. For -example: - -.. code-block:: c++ - - #if defined(__has_attribute) - # if __has_attribute(argument_with_type_tag) && \ - __has_attribute(pointer_with_type_tag) && \ - __has_attribute(type_tag_for_datatype) - # define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx))) - /* ... other macros ... */ - # endif - #endif - - #if !defined(ATTR_MPI_PWT) - # define ATTR_MPI_PWT(buffer_idx, type_idx) - #endif - - int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) - ATTR_MPI_PWT(1,3); - -argument_with_type_tag ----------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx, -type_tag_idx)))`` on a function declaration to specify that the function -accepts a type tag that determines the type of some other argument. -``arg_kind`` is an identifier that should be used when annotating all -applicable type tags. - -This attribute is primarily useful for checking arguments of variadic functions -(``pointer_with_type_tag`` can be used in most non-variadic cases). - -For example: - -.. code-block:: c++ - - int fcntl(int fd, int cmd, ...) - __attribute__(( argument_with_type_tag(fcntl,3,2) )); - - -pointer_with_type_tag ---------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))`` -on a function declaration to specify that the function accepts a type tag that -determines the pointee type of some other pointer argument. - -For example: - -.. code-block:: c++ - - int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */) - __attribute__(( pointer_with_type_tag(mpi,1,3) )); - - -type_tag_for_datatype ---------------------- -.. csv-table:: Supported Syntaxes - :header: "GNU", "C++11", "__declspec", "Keyword" - - "X","","","" - -Clang supports annotating type tags of two forms. - -* **Type tag that is an expression containing a reference to some declared - identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a - declaration with that identifier: - - .. code-block:: c++ - - extern struct mpi_datatype mpi_datatype_int - __attribute__(( type_tag_for_datatype(mpi,int) )); - #define MPI_INT ((MPI_Datatype) &mpi_datatype_int) - -* **Type tag that is an integral literal.** Introduce a ``static const`` - variable with a corresponding initializer value and attach - ``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration, - for example: - - .. code-block:: c++ - - #define MPI_INT ((MPI_Datatype) 42) - static const MPI_Datatype mpi_datatype_int - __attribute__(( type_tag_for_datatype(mpi,int) )) = 42 - -The attribute also accepts an optional third argument that determines how the -expression is compared to the type tag. There are two supported flags: - -* ``layout_compatible`` will cause types to be compared according to - layout-compatibility rules (C++11 [class.mem] p 17, 18). This is - implemented to support annotating types like ``MPI_DOUBLE_INT``. - - For example: - - .. code-block:: c++ - - /* In mpi.h */ - struct internal_mpi_double_int { double d; int i; }; - extern struct mpi_datatype mpi_datatype_double_int - __attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) )); - - #define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int) - - /* In user code */ - struct my_pair { double a; int b; }; - struct my_pair *buffer; - MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning - - struct my_int_pair { int a; int b; } - struct my_int_pair *buffer2; - MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element - // type 'struct my_int_pair' - // doesn't match specified MPI_Datatype - -* ``must_be_null`` specifies that the expression should be a null pointer - constant, for example: - - .. code-block:: c++ - - /* In mpi.h */ - extern struct mpi_datatype mpi_datatype_null - __attribute__(( type_tag_for_datatype(mpi, void, must_be_null) )); - - #define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null) - - /* In user code */ - MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL - // was specified but buffer - // is not a null pointer - - +=================== \ No newline at end of file diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 20a1396c487..47bb2e0c4b9 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -87,5 +87,8 @@ if (LLVM_ENABLE_SPHINX) if (${SPHINX_OUTPUT_HTML}) add_sphinx_target(html clang) endif() + if (${SPHINX_OUTPUT_MAN}) + add_sphinx_target(man clang) + endif() endif() endif() diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index ce6fae19c09..031daeeaa12 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -160,6 +160,17 @@ the configuration (without a prefix: ``Auto``). argument2); \endcode +**AlignConsecutiveAssignments** (``bool``) + If ``true``, aligns consecutive assignments. + + This will align the assignment operators of consecutive lines. This + will result in formattings like + \code + int aaaa = 12; + int b = 23; + int ccc = 23; + \endcode + **AlignEscapedNewlinesLeft** (``bool``) If ``true``, aligns escaped newlines as far left as possible. Otherwise puts them into the right-most column. @@ -191,10 +202,10 @@ the configuration (without a prefix: ``Auto``). * ``SFS_None`` (in configuration: ``None``) Never merge functions into a single line. - * ``SFS_Inline`` (in configuration: ``Inline``) - Only merge functions defined inside a class. * ``SFS_Empty`` (in configuration: ``Empty``) Only merge empty functions. + * ``SFS_Inline`` (in configuration: ``Inline``) + Only merge functions defined inside a class. Implies "empty". * ``SFS_All`` (in configuration: ``All``) Merge all functions fitting on a single line. @@ -207,16 +218,28 @@ the configuration (without a prefix: ``Auto``). If ``true``, ``while (true) continue;`` can be put on a single line. -**AlwaysBreakAfterDefinitionReturnType** (``bool``) - If ``true``, always break after function definition return types. +**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) + The function definition return type breaking style to use. + + Possible values: + + * ``DRTBS_None`` (in configuration: ``None``) + Break after return type automatically. + ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. + * ``DRTBS_All`` (in configuration: ``All``) + Always break after the return type. + * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) + Always break after the return types of top level functions. - More truthfully called 'break before the identifier following the type - in a function definition'. PenaltyReturnTypeOnItsOwnLine becomes - irrelevant. **AlwaysBreakBeforeMultilineStrings** (``bool``) If ``true``, always break before multiline string literals. + This flag is mean to make cases where there are multiple multiline strings + in a file look more consistent. Thus, it will only take effect if wrapping + the string at that point leads to it being indented + ``ContinuationIndentWidth`` spaces from the start of the line. + **AlwaysBreakTemplateDeclarations** (``bool``) If ``true``, always break after the ``template<...>`` of a template declaration. @@ -252,6 +275,9 @@ the configuration (without a prefix: ``Auto``). * ``BS_Linux`` (in configuration: ``Linux``) Like ``Attach``, but break before braces on function, namespace and class definitions. + * ``BS_Mozilla`` (in configuration: ``Mozilla``) + Like ``Attach``, but break before braces on enum, function, and record + definitions. * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) Like ``Attach``, but break before function definitions, and 'else'. * ``BS_Allman`` (in configuration: ``Allman``) @@ -311,7 +337,7 @@ the configuration (without a prefix: ``Auto``). alignment of & and \*. ``PointerAlignment`` is then used only as fallback. **DisableFormat** (``bool``) - Disables formatting at all. + Disables formatting completely. **ExperimentalAutoDetectBinPacking** (``bool``) If ``true``, clang-format detects whether function calls and @@ -371,6 +397,12 @@ the configuration (without a prefix: ``Auto``). (https://developers.google.com/protocol-buffers/). +**MacroBlockBegin** (``std::string``) + A regular expression matching macros that start a block. + +**MacroBlockEnd** (``std::string``) + A regular expression matching macros that end a block. + **MaxEmptyLinesToKeep** (``unsigned``) The maximum number of consecutive empty lines to keep. diff --git a/docs/CommandGuide/clang.rst b/docs/CommandGuide/clang.rst new file mode 100644 index 00000000000..39dbe02a0b4 --- /dev/null +++ b/docs/CommandGuide/clang.rst @@ -0,0 +1,484 @@ +clang - the Clang C, C++, and Objective-C compiler +================================================== + +SYNOPSIS +-------- + +:program:`clang` [*options*] *filename ...* + +DESCRIPTION +----------- + +:program:`clang` is a C, C++, and Objective-C compiler which encompasses +preprocessing, parsing, optimization, code generation, assembly, and linking. +Depending on which high-level mode setting is passed, Clang will stop before +doing a full link. While Clang is highly integrated, it is important to +understand the stages of compilation, to understand how to invoke it. These +stages are: + +Driver + The clang executable is actually a small driver which controls the overall + execution of other tools such as the compiler, assembler and linker. + Typically you do not need to interact with the driver, but you + transparently use it to run the other tools. + +Preprocessing + This stage handles tokenization of the input source file, macro expansion, + #include expansion and handling of other preprocessor directives. The + output of this stage is typically called a ".i" (for C), ".ii" (for C++), + ".mi" (for Objective-C), or ".mii" (for Objective-C++) file. + +Parsing and Semantic Analysis + This stage parses the input file, translating preprocessor tokens into a + parse tree. Once in the form of a parse tree, it applies semantic + analysis to compute types for expressions as well and determine whether + the code is well formed. This stage is responsible for generating most of + the compiler warnings as well as parse errors. The output of this stage is + an "Abstract Syntax Tree" (AST). + +Code Generation and Optimization + This stage translates an AST into low-level intermediate code (known as + "LLVM IR") and ultimately to machine code. This phase is responsible for + optimizing the generated code and handling target-specific code generation. + The output of this stage is typically called a ".s" file or "assembly" file. + + Clang also supports the use of an integrated assembler, in which the code + generator produces object files directly. This avoids the overhead of + generating the ".s" file and of calling the target assembler. + +Assembler + This stage runs the target assembler to translate the output of the + compiler into a target object file. The output of this stage is typically + called a ".o" file or "object" file. + +Linker + This stage runs the target linker to merge multiple object files into an + executable or dynamic library. The output of this stage is typically called + an "a.out", ".dylib" or ".so" file. + +:program:`Clang Static Analyzer` + +The Clang Static Analyzer is a tool that scans source code to try to find bugs +through code analysis. This tool uses many parts of Clang and is built into +the same driver. Please see for more details +on how to use the static analyzer. + +OPTIONS +------- + +Stage Selection Options +~~~~~~~~~~~~~~~~~~~~~~~ + +.. option:: -E + + Run the preprocessor stage. + +.. option:: -fsyntax-only + + Run the preprocessor, parser and type checking stages. + +.. option:: -S + + Run the previous stages as well as LLVM generation and optimization stages + and target-specific code generation, producing an assembly file. + +.. option:: -c + + Run all of the above, plus the assembler, generating a target ".o" object file. + +.. option:: no stage selection option + + If no stage selection option is specified, all stages above are run, and the + linker is run to combine the results into an executable or shared library. + +Language Selection and Mode Options +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. option:: -x + + Treat subsequent input files as having type language. + +.. option:: -std= + + Specify the language standard to compile for. + +.. option:: -stdlib= + + Specify the C++ standard library to use; supported options are libstdc++ and + libc++. + +.. option:: -ansi + + Same as -std=c89. + +.. option:: -ObjC, -ObjC++ + + Treat source input files as Objective-C and Object-C++ inputs respectively. + +.. option:: -trigraphs + + Enable trigraphs. + +.. option:: -ffreestanding + + Indicate that the file should be compiled for a freestanding, not a hosted, + environment. + +.. option:: -fno-builtin + + Disable special handling and optimizations of builtin functions like + :c:func:`strlen` and :c:func:`malloc`. + +.. option:: -fmath-errno + + Indicate that math functions should be treated as updating :c:data:`errno`. + +.. option:: -fpascal-strings + + Enable support for Pascal-style strings with "\\pfoo". + +.. option:: -fms-extensions + + Enable support for Microsoft extensions. + +.. option:: -fmsc-version= + + Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise. + +.. option:: -fborland-extensions + + Enable support for Borland extensions. + +.. option:: -fwritable-strings + + Make all string literals default to writable. This disables uniquing of + strings and other optimizations. + +.. option:: -flax-vector-conversions + + Allow loose type checking rules for implicit vector conversions. + +.. option:: -fblocks + + Enable the "Blocks" language feature. + +.. option:: -fobjc-gc-only + + Indicate that Objective-C code should be compiled in GC-only mode, which only + works when Objective-C Garbage Collection is enabled. + +.. option:: -fobjc-gc + + Indicate that Objective-C code should be compiled in hybrid-GC mode, which + works with both GC and non-GC mode. + +.. option:: -fobjc-abi-version=version + + Select the Objective-C ABI version to use. Available versions are 1 (legacy + "fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2). + +.. option:: -fobjc-nonfragile-abi-version= + + Select the Objective-C non-fragile ABI version to use by default. This will + only be used as the Objective-C ABI when the non-fragile ABI is enabled + (either via :option:`-fobjc-nonfragile-abi`, or because it is the platform + default). + +.. option:: -fobjc-nonfragile-abi + + Enable use of the Objective-C non-fragile ABI. On platforms for which this is + the default ABI, it can be disabled with :option:`-fno-objc-nonfragile-abi`. + +Target Selection Options +~~~~~~~~~~~~~~~~~~~~~~~~ + +Clang fully supports cross compilation as an inherent part of its design. +Depending on how your version of Clang is configured, it may have support for a +number of cross compilers, or may only support a native target. + +.. option:: -arch + + Specify the architecture to build for. + +.. option:: -mmacosx-version-min= + + When building for Mac OS X, specify the minimum version supported by your + application. + +.. option:: -miphoneos-version-min + + When building for iPhone OS, specify the minimum version supported by your + application. + +.. option:: -march= + + Specify that Clang should generate code for a specific processor family + member and later. For example, if you specify -march=i486, the compiler is + allowed to generate instructions that are valid on i486 and later processors, + but which may not exist on earlier ones. + + +Code Generation Options +~~~~~~~~~~~~~~~~~~~~~~~ + +.. option:: -O0, -O1, -O2, -O3, -Ofast, -Os, -Oz, -O, -O4 + + Specify which optimization level to use: + + :option:`-O0` Means "no optimization": this level compiles the fastest and + generates the most debuggable code. + + :option:`-O1` Somewhere between :option:`-O0` and :option:`-O2`. + + :option:`-O2` Moderate level of optimization which enables most + optimizations. + + :option:`-O3` Like :option:`-O2`, except that it enables optimizations that + take longer to perform or that may generate larger code (in an attempt to + make the program run faster). + + :option:`-Ofast` Enables all the optimizations from :option:`-O3` along + with other aggressive optimizations that may violate strict compliance with + language standards. + + :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code + size. + + :option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code + size further. + + :option:`-O` Equivalent to :option:`-O2`. + + :option:`-O4` and higher + + Currently equivalent to :option:`-O3` + +.. option:: -g + + Generate debug information. Note that Clang debug information works best at -O0. + +.. option:: -fstandalone-debug -fno-standalone-debug + + Clang supports a number of optimizations to reduce the size of debug + information in the binary. They work based on the assumption that the + debug type information can be spread out over multiple compilation units. + For instance, Clang will not emit type definitions for types that are not + needed by a module and could be replaced with a forward declaration. + Further, Clang will only emit type info for a dynamic C++ class in the + module that contains the vtable for the class. + + The :option:`-fstandalone-debug` option turns off these optimizations. + This is useful when working with 3rd-party libraries that don't come with + debug information. This is the default on Darwin. Note that Clang will + never emit type information for types that are not referenced at all by the + program. + +.. option:: -fexceptions + + Enable generation of unwind information. This allows exceptions to be thrown + through Clang compiled stack frames. This is on by default in x86-64. + +.. option:: -ftrapv + + Generate code to catch integer overflow errors. Signed integer overflow is + undefined in C. With this flag, extra code is generated to detect this and + abort when it happens. + +.. option:: -fvisibility + + This flag sets the default visibility level. + +.. option:: -fcommon + + This flag specifies that variables without initializers get common linkage. + It can be disabled with :option:`-fno-common`. + +.. option:: -ftls-model= + + Set the default thread-local storage (TLS) model to use for thread-local + variables. Valid values are: "global-dynamic", "local-dynamic", + "initial-exec" and "local-exec". The default is "global-dynamic". The default + model can be overridden with the tls_model attribute. The compiler will try + to choose a more efficient model if possible. + +.. option:: -flto, -emit-llvm + + Generate output files in LLVM formats, suitable for link time optimization. + When used with :option:`-S` this generates LLVM intermediate language + assembly files, otherwise this generates LLVM bitcode format object files + (which may be passed to the linker depending on the stage selection options). + +Driver Options +~~~~~~~~~~~~~~ + +.. option:: -### + + Print (but do not run) the commands to run for this compilation. + +.. option:: --help + + Display available options. + +.. option:: -Qunused-arguments + + Do not emit any warnings for unused driver arguments. + +.. option:: -Wa, + + Pass the comma separated arguments in args to the assembler. + +.. option:: -Wl, + + Pass the comma separated arguments in args to the linker. + +.. option:: -Wp, + + Pass the comma separated arguments in args to the preprocessor. + +.. option:: -Xanalyzer + + Pass arg to the static analyzer. + +.. option:: -Xassembler + + Pass arg to the assembler. + +.. option:: -Xlinker + + Pass arg to the linker. + +.. option:: -Xpreprocessor + + Pass arg to the preprocessor. + +.. option:: -o + + Write output to file. + +.. option:: -print-file-name= + + Print the full library path of file. + +.. option:: -print-libgcc-file-name + + Print the library path for "libgcc.a". + +.. option:: -print-prog-name= + + Print the full program path of name. + +.. option:: -print-search-dirs + + Print the paths used for finding libraries and programs. + +.. option:: -save-temps + + Save intermediate compilation results. + +.. option:: -integrated-as, -no-integrated-as + + Used to enable and disable, respectively, the use of the integrated + assembler. Whether the integrated assembler is on by default is target + dependent. + +.. option:: -time + + Time individual commands. + +.. option:: -ftime-report + + Print timing summary of each stage of compilation. + +.. option:: -v + + Show commands to run and use verbose output. + + +Diagnostics Options +~~~~~~~~~~~~~~~~~~~ + +.. option:: -fshow-column, -fshow-source-location, -fcaret-diagnostics, -fdiagnostics-fixit-info, -fdiagnostics-parseable-fixits, -fdiagnostics-print-source-range-info, -fprint-source-range-info, -fdiagnostics-show-option, -fmessage-length + + These options control how Clang prints out information about diagnostics + (errors and warnings). Please see the Clang User's Manual for more information. + +Preprocessor Options +~~~~~~~~~~~~~~~~~~~~ + +.. option:: -D= + + Adds an implicit #define into the predefines buffer which is read before the + source file is preprocessed. + +.. option:: -U + + Adds an implicit #undef into the predefines buffer which is read before the + source file is preprocessed. + +.. option:: -include + + Adds an implicit #include into the predefines buffer which is read before the + source file is preprocessed. + +.. option:: -I + + Add the specified directory to the search path for include files. + +.. option:: -F + + Add the specified directory to the search path for framework include files. + +.. option:: -nostdinc + + Do not search the standard system directories or compiler builtin directories + for include files. + +.. option:: -nostdlibinc + + Do not search the standard system directories for include files, but do + search compiler builtin include directories. + +.. option:: -nobuiltininc + + Do not search clang's builtin directory for include files. + + +ENVIRONMENT +----------- + +.. envvar:: TMPDIR, TEMP, TMP + + These environment variables are checked, in order, for the location to write + temporary files used during the compilation process. + +.. envvar:: CPATH + + If this environment variable is present, it is treated as a delimited list of + paths to be added to the default system include path list. The delimiter is + the platform dependent delimiter, as used in the PATH environment variable. + + Empty components in the environment variable are ignored. + +.. envvar:: C_INCLUDE_PATH, OBJC_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH + + These environment variables specify additional paths, as for :envvar:`CPATH`, which are + only used when processing the appropriate language. + +.. envvar:: MACOSX_DEPLOYMENT_TARGET + + If :option:`-mmacosx-version-min` is unspecified, the default deployment + target is read from this environment variable. This option only affects + Darwin targets. + +BUGS +---- + +To report bugs, please visit . Most bug reports should +include preprocessed source files (use the :option:`-E` option) and the full +output of the compiler, along with information to reproduce. + +SEE ALSO +-------- + +:manpage:`as(1)`, :manpage:`ld(1)` + diff --git a/docs/CommandGuide/index.rst b/docs/CommandGuide/index.rst new file mode 100644 index 00000000000..826ed971198 --- /dev/null +++ b/docs/CommandGuide/index.rst @@ -0,0 +1,17 @@ +Clang "man" pages +----------------- + +The following documents are command descriptions for all of the Clang tools. +These pages describe how to use the Clang commands and what their options are. +Note that these pages do not describe all of the options available for all +tools. To get a complete listing, pass the ``--help`` (general options) or +``--help-hidden`` (general and debugging options) arguments to the tool you are +interested in. + +Basic Commands +~~~~~~~~~~~~~~ + +.. toctree:: + :maxdepth: 1 + + clang diff --git a/docs/ControlFlowIntegrity.rst b/docs/ControlFlowIntegrity.rst index 915385b7b19..b043d24a142 100644 --- a/docs/ControlFlowIntegrity.rst +++ b/docs/ControlFlowIntegrity.rst @@ -20,8 +20,8 @@ program's control flow. These schemes have been optimized for performance, allowing developers to enable them in release builds. To enable Clang's available CFI schemes, use the flag ``-fsanitize=cfi``. -As currently implemented, CFI relies on link-time optimization (LTO); the CFI -schemes imply ``-flto``, and the linker used must support LTO, for example +As currently implemented, CFI relies on link-time optimization (LTO); so it is +required to specify ``-flto``, and the linker used must support LTO, for example via the `gold plugin`_. To allow the checks to be implemented efficiently, the program must be structured such that certain object files are compiled with CFI enabled, and are statically linked into the program. This may @@ -41,11 +41,9 @@ derived class of the static type of the object used to make the call. This CFI scheme can be enabled on its own using ``-fsanitize=cfi-vcall``. For this scheme to work, all translation units containing the definition -of a virtual member function (whether inline or not) must be compiled -with ``-fsanitize=cfi-vcall`` enabled and be statically linked into the -program. Classes in the C++ standard library (under namespace ``std``) are -exempted from checking, and therefore programs may be linked against a -pre-built standard library, but this may change in the future. +of a virtual member function (whether inline or not), other than members +of :ref:`blacklisted ` types, must be compiled with +``-fsanitize=cfi-vcall`` enabled and be statically linked into the program. Performance ~~~~~~~~~~~ @@ -85,15 +83,13 @@ If a program as a matter of policy forbids the second type of cast, that restriction can normally be enforced. However it may in some cases be necessary for a function to perform a forbidden cast to conform with an external API (e.g. the ``allocate`` member function of a standard library allocator). Such -functions may be blacklisted using a :doc:`SanitizerSpecialCaseList`. +functions may be :ref:`blacklisted `. For this scheme to work, all translation units containing the definition -of a virtual member function (whether inline or not) must be compiled with +of a virtual member function (whether inline or not), other than members +of :ref:`blacklisted ` types, must be compiled with ``-fsanitize=cfi-derived-cast`` or ``-fsanitize=cfi-unrelated-cast`` enabled -and be statically linked into the program. Classes in the C++ standard library -(under namespace ``std``) are exempted from checking, and therefore programs -may be linked against a pre-built standard library, but this may change in -the future. +and be statically linked into the program. Non-Virtual Member Function Call Checking ----------------------------------------- @@ -106,11 +102,9 @@ polymorphic class type. This CFI scheme can be enabled on its own using ``-fsanitize=cfi-nvcall``. For this scheme to work, all translation units containing the definition -of a virtual member function (whether inline or not) must be compiled -with ``-fsanitize=cfi-nvcall`` enabled and be statically linked into the -program. Classes in the C++ standard library (under namespace ``std``) are -exempted from checking, and therefore programs may be linked against a -pre-built standard library, but this may change in the future. +of a virtual member function (whether inline or not), other than members +of :ref:`blacklisted ` types, must be compiled with +``-fsanitize=cfi-nvcall`` enabled and be statically linked into the program. .. _cfi-strictness: @@ -129,6 +123,32 @@ member functions on class instances with specific properties that works under most compilers and should not have security implications, so we allow it by default. It can be disabled with ``-fsanitize=cfi-cast-strict``. +.. _cfi-blacklist: + +Blacklist +--------- + +A :doc:`SanitizerSpecialCaseList` can be used to relax CFI checks for certain +source files, functions and types using the ``src``, ``fun`` and ``type`` +entity types. + +In addition, if a type has a ``uuid`` attribute and the blacklist contains +the type entry ``attr:uuid``, CFI checks are suppressed for that type. This +allows all COM types to be easily blacklisted, which is useful as COM types +are typically defined outside of the linked program. + +.. code-block:: bash + + # Suppress checking for code in a file. + src:bad_file.cpp + src:bad_header.h + # Ignore all functions with names containing MyFooBar. + fun:*MyFooBar* + # Ignore all types in the standard library. + type:std::* + # Ignore all types with a uuid attribute. + type:attr:uuid + Design ------ diff --git a/docs/CrossCompilation.rst b/docs/CrossCompilation.rst index fd42856ec3a..8a802711737 100644 --- a/docs/CrossCompilation.rst +++ b/docs/CrossCompilation.rst @@ -92,7 +92,7 @@ will be assumed, which sets the defaults for the specified architecture. The system name is generally the OS (linux, darwin), but could be special like the bare-metal "none". -When a parameter is not important, they can be omitted, or you can +When a parameter is not important, it can be omitted, or you can choose ``unknown`` and the defaults will be used. If you choose a parameter that Clang doesn't know, like ``blerg``, it'll ignore and assume ``unknown``, which is not always desired, so be careful. @@ -110,7 +110,7 @@ will be chosen, so you'll almost always have to change it via flags. Typical flags include: * ``-mcpu=``, like x86-64, swift, cortex-a15 - * ``-fpu=``, like SSE3, NEON, controlling the FP unit available + * ``-mfpu=``, like SSE3, NEON, controlling the FP unit available * ``-mfloat-abi=``, like soft, hard, controlling which registers to use for floating-point diff --git a/docs/DriverInternals.rst b/docs/DriverInternals.rst index c779555dae3..6bc5f7dac95 100644 --- a/docs/DriverInternals.rst +++ b/docs/DriverInternals.rst @@ -138,12 +138,12 @@ The driver functionality is conceptually divided into five stages: this vector instead of storing its values directly. The clang driver can dump the results of this stage using the - ``-ccc-print-options`` flag (which must precede any actual command + ``-###`` flag (which must precede any actual command line arguments). For example: .. code-block:: console - $ clang -ccc-print-options -Xarch_i386 -fomit-frame-pointer -Wa,-fast -Ifoo -I foo t.c + $ clang -### -Xarch_i386 -fomit-frame-pointer -Wa,-fast -Ifoo -I foo t.c Option 0 - Name: "-Xarch_", Values: {"i386", "-fomit-frame-pointer"} Option 1 - Name: "-Wa,", Values: {"-fast"} Option 2 - Name: "-I", Values: {"foo"} @@ -155,7 +155,7 @@ The driver functionality is conceptually divided into five stages: Subsequent stages should rarely, if ever, need to do any string processing. -#. **Pipeline: Compilation Job Construction** +#. **Pipeline: Compilation Action Construction** Once the arguments are parsed, the tree of subprocess jobs needed for the desired compilation sequence are constructed. This involves @@ -266,7 +266,7 @@ The driver functionality is conceptually divided into five stages: #. **Translate: Tool Specific Argument Translation** Once a Tool has been selected to perform a particular Action, the - Tool must construct concrete Jobs which will be executed during + Tool must construct concrete Commands which will be executed during compilation. The main work is in translating from the gcc style command line options to whatever options the subprocess expects. @@ -280,7 +280,7 @@ The driver functionality is conceptually divided into five stages: last of arguments corresponding to some option, or all arguments for an option. - The result of this stage is a list of Jobs (executable paths and + The result of this stage is a list of Commands (executable paths and argument strings) to execute. #. **Execute** diff --git a/docs/InternalsManual.rst b/docs/InternalsManual.rst index 7bb34b788a5..7f2a8fafccc 100644 --- a/docs/InternalsManual.rst +++ b/docs/InternalsManual.rst @@ -1621,192 +1621,304 @@ How to change Clang How to add an attribute ----------------------- +Attributes are a form of metadata that can be attached to a program construct, +allowing the programmer to pass semantic information along to the compiler for +various uses. For example, attributes may be used to alter the code generation +for a program construct, or to provide extra semantic information for static +analysis. This document explains how to add a custom attribute to Clang. +Documentation on existing attributes can be found `here +`_. Attribute Basics ^^^^^^^^^^^^^^^^ - -Attributes in clang come in two forms: parsed form, and semantic form. Both -forms are represented via a tablegen definition of the attribute, specified in -Attr.td. +Attributes in Clang are handled in three stages: parsing into a parsed attribute +representation, conversion from a parsed attribute into a semantic attribute, +and then the semantic handling of the attribute. + +Parsing of the attribute is determined by the various syntactic forms attributes +can take, such as GNU, C++11, and Microsoft style attributes, as well as other +information provided by the table definition of the attribute. Ultimately, the +parsed representation of an attribute object is an ``AttributeList`` object. +These parsed attributes chain together as a list of parsed attributes attached +to a declarator or declaration specifier. The parsing of attributes is handled +automatically by Clang, except for attributes spelled as keywords. When +implementing a keyword attribute, the parsing of the keyword and creation of the +``AttributeList`` object must be done manually. + +Eventually, ``Sema::ProcessDeclAttributeList()`` is called with a ``Decl`` and +an ``AttributeList``, at which point the parsed attribute can be transformed +into a semantic attribute. The process by which a parsed attribute is converted +into a semantic attribute depends on the attribute definition and semantic +requirements of the attribute. The end result, however, is that the semantic +attribute object is attached to the ``Decl`` object, and can be obtained by a +call to ``Decl::getAttr()``. + +The structure of the semantic attribute is also governed by the attribute +definition given in Attr.td. This definition is used to automatically generate +functionality used for the implementation of the attribute, such as a class +derived from ``clang::Attr``, information for the parser to use, automated +semantic checking for some attributes, etc. ``include/clang/Basic/Attr.td`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The first step to adding a new attribute to Clang is to add its definition to +`include/clang/Basic/Attr.td +`_. +This tablegen definition must derive from the ``Attr`` (tablegen, not +semantic) type, or one of its derivatives. Most attributes will derive from the +``InheritableAttr`` type, which specifies that the attribute can be inherited by +later redeclarations of the ``Decl`` it is associated with. +``InheritableParamAttr`` is similar to ``InheritableAttr``, except that the +attribute is written on a parameter instead of a declaration. If the attribute +is intended to apply to a type instead of a declaration, such an attribute +should derive from ``TypeAttr``, and will generally not be given an AST +representation. (Note that this document does not cover the creation of type +attributes.) An attribute that inherits from ``IgnoredAttr`` is parsed, but will +generate an ignored attribute diagnostic when used, which may be useful when an +attribute is supported by another vendor but not supported by clang. + +The definition will specify several key pieces of information, such as the +semantic name of the attribute, the spellings the attribute supports, the +arguments the attribute expects, and more. Most members of the ``Attr`` tablegen +type do not require definitions in the derived definition as the default +suffice. However, every attribute must specify at least a spelling list, a +subject list, and a documentation list. + +Spellings +~~~~~~~~~ +All attributes are required to specify a spelling list that denotes the ways in +which the attribute can be spelled. For instance, a single semantic attribute +may have a keyword spelling, as well as a C++11 spelling and a GNU spelling. An +empty spelling list is also permissible and may be useful for attributes which +are created implicitly. The following spellings are accepted: + + ============ ================================================================ + Spelling Description + ============ ================================================================ + ``GNU`` Spelled with a GNU-style ``__attribute__((attr))`` syntax and + placement. + ``CXX11`` Spelled with a C++-style ``[[attr]]`` syntax. If the attribute + is meant to be used by Clang, it should set the namespace to + ``"clang"``. + ``Declspec`` Spelled with a Microsoft-style ``__declspec(attr)`` syntax. + ``Keyword`` The attribute is spelled as a keyword, and required custom + parsing. + ``GCC`` Specifies two spellings: the first is a GNU-style spelling, and + the second is a C++-style spelling with the ``gnu`` namespace. + Attributes should only specify this spelling for attributes + supported by GCC. + ``Pragma`` The attribute is spelled as a ``#pragma``, and requires custom + processing within the preprocessor. If the attribute is meant to + be used by Clang, it should set the namespace to ``"clang"``. + Note that this spelling is not used for declaration attributes. + ============ ================================================================ + +Subjects +~~~~~~~~ +Attributes appertain to one or more ``Decl`` subjects. If the attribute attempts +to attach to a subject that is not in the subject list, a diagnostic is issued +automatically. Whether the diagnostic is a warning or an error depends on how +the attribute's ``SubjectList`` is defined, but the default behavior is to warn. +The diagnostics displayed to the user are automatically determined based on the +subjects in the list, but a custom diagnostic parameter can also be specified in +the ``SubjectList``. The diagnostics generated for subject list violations are +either ``diag::warn_attribute_wrong_decl_type`` or +``diag::err_attribute_wrong_decl_type``, and the parameter enumeration is found +in `include/clang/Sema/AttributeList.h +`_ +If a previously unused Decl node is added to the ``SubjectList``, the logic used +to automatically determine the diagnostic parameter in `utils/TableGen/ClangAttrEmitter.cpp +`_ +may need to be updated. + +By default, all subjects in the SubjectList must either be a Decl node defined +in ``DeclNodes.td``, or a statement node defined in ``StmtNodes.td``. However, +more complex subjects can be created by creating a ``SubsetSubject`` object. +Each such object has a base subject which it appertains to (which must be a +Decl or Stmt node, and not a SubsetSubject node), and some custom code which is +called when determining whether an attribute appertains to the subject. For +instance, a ``NonBitField`` SubsetSubject appertains to a ``FieldDecl``, and +tests whether the given FieldDecl is a bit field. When a SubsetSubject is +specified in a SubjectList, a custom diagnostic parameter must also be provided. -First, add your attribute to the `include/clang/Basic/Attr.td -`_ -file. - -Each attribute gets a ``def`` inheriting from ``Attr`` or one of its -subclasses. ``InheritableAttr`` means that the attribute also applies to -subsequent declarations of the same name. ``InheritableParamAttr`` is similar -to ``InheritableAttr``, except that the attribute is written on a parameter -instead of a declaration, type or statement. Attributes inheriting from -``TypeAttr`` are pure type attributes which generally are not given a -representation in the AST. Attributes inheriting from ``TargetSpecificAttr`` -are attributes specific to one or more target architectures. An attribute that -inherits from ``IgnoredAttr`` is parsed, but will generate an ignored attribute -diagnostic when used. The attribute type may be useful when an attribute is -supported by another vendor, but not supported by clang. - -``Spellings`` lists the strings that can appear in ``__attribute__((here))`` or -``[[here]]``. All such strings will be synonymous. Possible ``Spellings`` -are: ``GNU`` (for use with GNU-style __attribute__ spellings), ``Declspec`` -(for use with Microsoft Visual Studio-style __declspec spellings), ``CXX11` -(for use with C++11-style [[foo]] and [[foo::bar]] spellings), and ``Keyword`` -(for use with attributes that are implemented as keywords, like C++11's -``override`` or ``final``). If you want to allow the ``[[]]`` C++11 syntax, you -have to define a list of ``Namespaces``, which will let users write -``[[namespace::spelling]]``. Using the empty string for a namespace will allow -users to write just the spelling with no "``::``". Attributes which g++-4.8 -or later accepts should also have a ``CXX11<"gnu", "spelling">`` spelling. - -``Subjects`` restricts what kinds of AST node to which this attribute can -appertain (roughly, attach). The subjects are specified via a ``SubjectList``, -which specify the list of subjects. Additionally, subject-related diagnostics -can be specified to be warnings or errors, with the default being a warning. -The diagnostics displayed to the user are automatically determined based on -the subjects in the list, but a custom diagnostic parameter can also be -specified in the ``SubjectList``. The diagnostics generated for subject list -violations are either ``diag::warn_attribute_wrong_decl_type`` or -``diag::err_attribute_wrong_decl_type``, and the parameter enumeration is -found in `include/clang/Sema/AttributeList.h -`_ -If you add new Decl nodes to the ``SubjectList``, you may need to update the -logic used to automatically determine the diagnostic parameter in `utils/TableGen/ClangAttrEmitter.cpp -`_. - -Diagnostic checking for attribute subject lists is automated except when +Diagnostic checking for attribute subject lists is automated except when ``HasCustomParsing`` is set to ``1``. -By default, all subjects in the SubjectList must either be a Decl node defined -in ``DeclNodes.td``, or a statement node defined in ``StmtNodes.td``. However, -more complex subjects can be created by creating a ``SubsetSubject`` object. -Each such object has a base subject which it appertains to (which must be a -Decl or Stmt node, and not a SubsetSubject node), and some custom code which is -called when determining whether an attribute appertains to the subject. For -instance, a ``NonBitField`` SubsetSubject appertains to a ``FieldDecl``, and -tests whether the given FieldDecl is a bit field. When a SubsetSubject is -specified in a SubjectList, a custom diagnostic parameter must also be provided. - -``Args`` names the arguments the attribute takes, in order. If ``Args`` is +Documentation +~~~~~~~~~~~~~ +All attributes must have some form of documentation associated with them. +Documentation is table generated on the public web server by a server-side +process that runs daily. Generally, the documentation for an attribute is a +stand-alone definition in `include/clang/Basic/AttrDocs.td +`_ +that is named after the attribute being documented. + +If the attribute is not for public consumption, or is an implicitly-created +attribute that has no visible spelling, the documentation list can specify the +``Undocumented`` object. Otherwise, the attribute should have its documentation +added to AttrDocs.td. + +Documentation derives from the ``Documentation`` tablegen type. All derived +types must specify a documentation category and the actual documentation itself. +Additionally, it can specify a custom heading for the attribute, though a +default heading will be chosen when possible. + +There are four predefined documentation categories: ``DocCatFunction`` for +attributes that appertain to function-like subjects, ``DocCatVariable`` for +attributes that appertain to variable-like subjects, ``DocCatType`` for type +attributes, and ``DocCatStmt`` for statement attributes. A custom documentation +category should be used for groups of attributes with similar functionality. +Custom categories are good for providing overview information for the attributes +grouped under it. For instance, the consumed annotation attributes define a +custom category, ``DocCatConsumed``, that explains what consumed annotations are +at a high level. + +Documentation content (whether it is for an attribute or a category) is written +using reStructuredText (RST) syntax. + +After writing the documentation for the attribute, it should be locally tested +to ensure that there are no issues generating the documentation on the server. +Local testing requires a fresh build of clang-tblgen. To generate the attribute +documentation, execute the following command:: + + clang-tblgen -gen-attr-docs -I /path/to/clang/include /path/to/clang/include/clang/Basic/Attr.td -o /path/to/clang/docs/AttributeReference.rst + +When testing locally, *do not* commit changes to ``AttributeReference.rst``. +This file is generated by the server automatically, and any changes made to this +file will be overwritten. + +Arguments +~~~~~~~~~ +Attributes may optionally specify a list of arguments that can be passed to the +attribute. Attribute arguments specify both the parsed form and the semantic +form of the attribute. For example, if ``Args`` is ``[StringArgument<"Arg1">, IntArgument<"Arg2">]`` then -``__attribute__((myattribute("Hello", 3)))`` will be a valid use. Attribute -arguments specify both the parsed form and the semantic form of the attribute. -The previous example shows an attribute which requires two attributes while -parsing, and the Attr subclass' constructor for the attribute will require a -string and integer argument. - -Diagnostic checking for argument counts is automated except when -``HasCustomParsing`` is set to ``1``, or when the attribute uses an optional or -variadic argument. Diagnostic checking for argument semantics is not automated. - -If the parsed form of the attribute is more complex, or differs from the -semantic form, the ``HasCustomParsing`` bit can be set to ``1`` for the class, -and the parsing code in `Parser::ParseGNUAttributeArgs -`_ -can be updated for the special case. Note that this only applies to arguments -with a GNU spelling -- attributes with a __declspec spelling currently ignore +``__attribute__((myattribute("Hello", 3)))`` will be a valid use; it requires +two arguments while parsing, and the Attr subclass' constructor for the +semantic attribute will require a string and integer argument. + +All arguments have a name and a flag that specifies whether the argument is +optional. The associated C++ type of the argument is determined by the argument +definition type. If the existing argument types are insufficient, new types can +be created, but it requires modifying `utils/TableGen/ClangAttrEmitter.cpp +`_ +to properly support the type. + +Other Properties +~~~~~~~~~~~~~~~~ +The ``Attr`` definition has other members which control the behavior of the +attribute. Many of them are special-purpose and beyond the scope of this +document, however a few deserve mention. + +If the parsed form of the attribute is more complex, or differs from the +semantic form, the ``HasCustomParsing`` bit can be set to ``1`` for the class, +and the parsing code in `Parser::ParseGNUAttributeArgs() +`_ +can be updated for the special case. Note that this only applies to arguments +with a GNU spelling -- attributes with a __declspec spelling currently ignore this flag and are handled by ``Parser::ParseMicrosoftDeclSpec``. -Custom accessors can be generated for an attribute based on the spelling list -for that attribute. For instance, if an attribute has two different spellings: -'Foo' and 'Bar', accessors can be created: -``[Accessor<"isFoo", [GNU<"Foo">]>, Accessor<"isBar", [GNU<"Bar">]>]`` -These accessors will be generated on the semantic form of the attribute, -accepting no arguments and returning a Boolean. +Note that setting this member to 1 will opt out of common attribute semantic +handling, requiring extra implementation efforts to ensure the attribute +appertains to the appropriate subject, etc. -Attributes which do not require an AST node should set the ``ASTNode`` field to -``0`` to avoid polluting the AST. Note that anything inheriting from -``TypeAttr`` or ``IgnoredAttr`` automatically do not generate an AST node. All -other attributes generate an AST node by default. The AST node is the semantic +If the attribute should not be propagated from from a template declaration to an +instantiation of the template, set the ``Clone`` member to 0. By default, all +attributes will be cloned to template instantiations. + +Attributes that do not require an AST node should set the ``ASTNode`` field to +``0`` to avoid polluting the AST. Note that anything inheriting from +``TypeAttr`` or ``IgnoredAttr`` automatically do not generate an AST node. All +other attributes generate an AST node by default. The AST node is the semantic representation of the attribute. -Attributes which do not require custom semantic handling should set the -``SemaHandler`` field to ``0``. Note that anything inheriting from -``IgnoredAttr`` automatically do not get a semantic handler. All other -attributes are assumed to use a semantic handler by default. Attributes -without a semantic handler are not given a parsed attribute Kind enumeration. - -The ``LangOpts`` field can be used to specify a list of language options -required by the attribute. For instance, all of the CUDA-specific attributes -specify ``[CUDA]`` for the ``LangOpts`` field, and when the CUDA language -option is not enabled, an "attribute ignored" warning diagnostic is emitted. -Since language options are not table generated nodes, new language options must -be created manually and should specify the spelling used by ``LangOptions`` class. - -Target-specific attribute sometimes share a spelling with other attributes in -different targets. For instance, the ARM and MSP430 targets both have an -attribute spelled ``GNU<"interrupt">``, but with different parsing and semantic -requirements. To support this feature, an attribute inheriting from -``TargetSpecificAttribute`` make specify a ``ParseKind`` field. This field -should be the same value between all arguments sharing a spelling, and -corresponds to the parsed attribute's Kind enumeration. This allows attributes -to share a parsed attribute kind, but have distinct semantic attribute classes. -For instance, ``AttributeList::AT_Interrupt`` is the shared parsed attribute -kind, but ARMInterruptAttr and MSP430InterruptAttr are the semantic attributes -generated. - -By default, when declarations are merging attributes, an attribute will not be -duplicated. However, if an attribute can be duplicated during this merging -stage, set ``DuplicatesAllowedWhileMerging`` to ``1``, and the attribute will +The ``LangOpts`` field specifies a list of language options required by the +attribute. For instance, all of the CUDA-specific attributes specify ``[CUDA]`` +for the ``LangOpts`` field, and when the CUDA language option is not enabled, an +"attribute ignored" warning diagnostic is emitted. Since language options are +not table generated nodes, new language options must be created manually and +should specify the spelling used by ``LangOptions`` class. + +Custom accessors can be generated for an attribute based on the spelling list +for that attribute. For instance, if an attribute has two different spellings: +'Foo' and 'Bar', accessors can be created: +``[Accessor<"isFoo", [GNU<"Foo">]>, Accessor<"isBar", [GNU<"Bar">]>]`` +These accessors will be generated on the semantic form of the attribute, +accepting no arguments and returning a ``bool``. + +Attributes that do not require custom semantic handling should set the +``SemaHandler`` field to ``0``. Note that anything inheriting from +``IgnoredAttr`` automatically do not get a semantic handler. All other +attributes are assumed to use a semantic handler by default. Attributes +without a semantic handler are not given a parsed attribute ``Kind`` enumerator. + +Target-specific attributes may share a spelling with other attributes in +different targets. For instance, the ARM and MSP430 targets both have an +attribute spelled ``GNU<"interrupt">``, but with different parsing and semantic +requirements. To support this feature, an attribute inheriting from +``TargetSpecificAttribute`` may specify a ``ParseKind`` field. This field +should be the same value between all arguments sharing a spelling, and +corresponds to the parsed attribute's ``Kind`` enumerator. This allows +attributes to share a parsed attribute kind, but have distinct semantic +attribute classes. For instance, ``AttributeList::AT_Interrupt`` is the shared +parsed attribute kind, but ARMInterruptAttr and MSP430InterruptAttr are the +semantic attributes generated. + +By default, when declarations are merging attributes, an attribute will not be +duplicated. However, if an attribute can be duplicated during this merging +stage, set ``DuplicatesAllowedWhileMerging`` to ``1``, and the attribute will be merged. -By default, attribute arguments are parsed in an evaluated context. If the -arguments for an attribute should be parsed in an unevaluated context (akin to -the way the argument to a ``sizeof`` expression is parsed), you can set +By default, attribute arguments are parsed in an evaluated context. If the +arguments for an attribute should be parsed in an unevaluated context (akin to +the way the argument to a ``sizeof`` expression is parsed), set ``ParseArgumentsAsUnevaluated`` to ``1``. -If additional functionality is desired for the semantic form of the attribute, -the ``AdditionalMembers`` field specifies code to be copied verbatim into the -semantic attribute class object. - -All attributes must have one or more form of documentation, which is provided -in the ``Documentation`` list. Generally, the documentation for an attribute -is a stand-alone definition in `include/clang/Basic/AttrDocs.td -`_ -that is named after the attribute being documented. Each documentation element -is given a ``Category`` (variable, function, or type) and ``Content``. A single -attribute may contain multiple documentation elements for distinct categories. -For instance, an attribute which can appertain to both function and types (such -as a calling convention attribute), should contain two documentation elements. -The ``Content`` for an attribute uses reStructuredText (RST) syntax. - -If an attribute is used internally by the compiler, but is not written by users -(such as attributes with an empty spelling list), it can use the -``Undocumented`` documentation element. +If additional functionality is desired for the semantic form of the attribute, +the ``AdditionalMembers`` field specifies code to be copied verbatim into the +semantic attribute class object, with ``public`` access. Boilerplate ^^^^^^^^^^^ - All semantic processing of declaration attributes happens in `lib/Sema/SemaDeclAttr.cpp -`_, -and generally starts in the ``ProcessDeclAttribute`` function. If your -attribute is a "simple" attribute -- meaning that it requires no custom -semantic processing aside from what is automatically provided for you, you can -add a call to ``handleSimpleAttribute(S, D, Attr);`` to the switch -statement. Otherwise, write a new ``handleYourAttr()`` function, and add that -to the switch statement. - -If your attribute causes extra warnings to fire, define a ``DiagGroup`` in +`_, +and generally starts in the ``ProcessDeclAttribute()`` function. If the +attribute is a "simple" attribute -- meaning that it requires no custom semantic +processing aside from what is automatically provided, add a call to +``handleSimpleAttribute(S, D, Attr);`` to the switch statement. +Otherwise, write a new ``handleYourAttr()`` function, and add that to the switch +statement. Please do not implement handling logic directly in the ``case`` for +the attribute. + +Unless otherwise specified by the attribute definition, common semantic checking +of the parsed attribute is handled automatically. This includes diagnosing +parsed attributes that do not appertain to the given ``Decl``, ensuring the +correct minimum number of arguments are passed, etc. + +If the attribute adds additional warnings, define a ``DiagGroup`` in `include/clang/Basic/DiagnosticGroups.td `_ -named after the attribute's ``Spelling`` with "_"s replaced by "-"s. If you're -only defining one diagnostic, you can skip ``DiagnosticGroups.td`` and use -``InGroup>`` directly in `DiagnosticSemaKinds.td +named after the attribute's ``Spelling`` with "_"s replaced by "-"s. If there +is only a single diagnostic, it is permissible to use ``InGroup>`` +directly in `DiagnosticSemaKinds.td `_ All semantic diagnostics generated for your attribute, including automatically- -generated ones (such as subjects and argument counts), should have a +generated ones (such as subjects and argument counts), should have a corresponding test case. -The meat of your attribute -^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Find an appropriate place in Clang to do whatever your attribute needs to do. -Check for the attribute's presence using ``Decl::getAttr()``. - -Update the :doc:`LanguageExtensions` document to describe your new attribute. +Semantic handling +^^^^^^^^^^^^^^^^^ +Most attributes are implemented to have some effect on the compiler. For +instance, to modify the way code is generated, or to add extra semantic checks +for an analysis pass, etc. Having added the attribute definition and conversion +to the semantic representation for the attribute, what remains is to implement +the custom logic requiring use of the attribute. + +The ``clang::Decl`` object can be queried for the presence or absence of an +attribute using ``hasAttr()``. To obtain a pointer to the semantic +representation of the attribute, ``getAttr`` may be used. How to add an expression or statement ------------------------------------- diff --git a/docs/LanguageExtensions.rst b/docs/LanguageExtensions.rst index bd5992e3d6e..6a4dd5ccbf3 100644 --- a/docs/LanguageExtensions.rst +++ b/docs/LanguageExtensions.rst @@ -1845,6 +1845,9 @@ with :doc:`ThreadSanitizer`. Use ``__has_feature(memory_sanitizer)`` to check if the code is being built with :doc:`MemorySanitizer`. +Use ``__has_feature(safe_stack)`` to check if the code is being built +with :doc:`SafeStack`. + Extensions for selectively disabling optimization ================================================= @@ -1988,10 +1991,10 @@ compile time. Partial unrolling replicates the loop body within the loop and reduces the trip count. If ``unroll(full)`` is specified the unroller will attempt to fully unroll the -loop if the trip count is known at compile time. If the loop count is not known -or the fully unrolled code size is greater than the limit specified by the -`-pragma-unroll-threshold` command line option the loop will be partially -unrolled subject to the same limit. +loop if the trip count is known at compile time. If the fully unrolled code size +is greater than an internal limit the loop will be partially unrolled up to this +limit. If the loop count is not known at compile time the loop will not be +unrolled. .. code-block:: c++ @@ -2003,7 +2006,7 @@ unrolled subject to the same limit. The unroll count can be specified explicitly with ``unroll_count(_value_)`` where _value_ is a positive integer. If this value is greater than the trip count the loop will be fully unrolled. Otherwise the loop is partially unrolled subject -to the `-pragma-unroll-threshold` limit. +to the same code size limit as with ``unroll(full)``. .. code-block:: c++ diff --git a/docs/LeakSanitizer.rst b/docs/LeakSanitizer.rst index b1071efd1aa..d4b4fa02d73 100644 --- a/docs/LeakSanitizer.rst +++ b/docs/LeakSanitizer.rst @@ -17,7 +17,8 @@ only, at a minimal performance cost. Current status ============== -LeakSanitizer is experimental and supported only on x86\_64 Linux. +LeakSanitizer is turned on by default, but it is only supported on x86\_64 +Linux. The combined mode has been tested on fairly large software projects. The stand-alone mode has received much less testing. diff --git a/docs/LibASTMatchersReference.html b/docs/LibASTMatchersReference.html index 74bbf9e4733..c977872f383 100644 --- a/docs/LibASTMatchersReference.html +++ b/docs/LibASTMatchersReference.html @@ -1434,6 +1434,16 @@

Narrowing Matchers

Matcher<FloatingLiteral>, Matcher<IntegerLiteral> +Matcher<CXXCatchStmt>isCatchAll +
Matches a C++ catch statement that has a handler that catches any exception type.
+
+Example matches catch(...) (matcher = catchStmt(isCatchAll()))
+  try {
+    // ...
+  } catch(...) {
+  }
+
+ Matcher<CXXConstructExpr>argumentCountIsunsigned N
Checks that a call expression or a constructor call expression has
@@ -1478,6 +1488,25 @@ 

Narrowing Matchers

+Matcher<CXXMethodDecl>isFinal +
Matches if the given method declaration is final.
+
+Given:
+
+struct A {
+  virtual void foo();
+  virtual void bar();
+};
+
+struct B : A {
+  void foo() final;
+  void bar();
+};
+
+methodDecl(isFinal()) matches B::foo() but not B::bar(), A::foo(), or A::bar()
+
+ + Matcher<CXXMethodDecl>isOverride
Matches if the given method declaration overrides another method.
 
@@ -1563,6 +1592,18 @@ 

Narrowing Matchers

+Matcher<CXXRecordDecl>isFinal +
Matches if the given class declaration is final.
+
+Given:
+
+struct A {};
+
+struct B final : A {};
+
+recordDecl(isFinal()) matches B but not A.
+
+ Matcher<CXXRecordDecl>isTemplateInstantiation
Matches template instantiations of function, class, or static
 member variable template instantiations.
@@ -1682,7 +1723,8 @@ 

Narrowing Matchers

Given __attribute__((device)) void f() { ... } decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of -f. +f. If the matcher is use from clang-query, attr::Kind parameter should be +passed as a quoted string. e.g., hasAttr("attr::CUDADevice").
@@ -1744,7 +1786,7 @@

Narrowing Matchers

private: int c; }; fieldDecl(isPrivate()) - matches 'int c;' + matches 'int c;' @@ -1758,7 +1800,7 @@

Narrowing Matchers

private: int c; }; fieldDecl(isProtected()) - matches 'int b;' + matches 'int b;' @@ -1772,7 +1814,7 @@

Narrowing Matchers

private: int c; }; fieldDecl(isPublic()) - matches 'int a;' + matches 'int a;' @@ -2283,6 +2325,18 @@

Narrowing Matchers

+Matcher<VarDecl>isExceptionVariable +
Matches if a variable declaration is for a C++ catch handler or Objective-C @catch variable.
+
+Example matches x (matcher = varDecl(isExceptionVariable())
+  void f(int y) {
+    try {
+    } catch (int x) {
+    }
+  }
+
+ + Matcher<VarDecl>isExplicitTemplateSpecialization
Matches explicit template specializations of function, class, or
 static member variable template instantiations.
diff --git a/docs/LibASTMatchersTutorial.rst b/docs/LibASTMatchersTutorial.rst
index 1e88ec203c9..fe36511a0cf 100644
--- a/docs/LibASTMatchersTutorial.rst
+++ b/docs/LibASTMatchersTutorial.rst
@@ -169,7 +169,7 @@ You should now be able to run the syntax checker, which is located in
 
 .. code-block:: console
 
-      cat "int main() { return 0; }" > test.cpp
+      echo "int main() { return 0; }" > test.cpp
       bin/loop-convert test.cpp --
 
 Note the two dashes after we specify the source file. The additional
diff --git a/docs/MSVCCompatibility.rst b/docs/MSVCCompatibility.rst
index 1dba9e865b6..3794813db81 100644
--- a/docs/MSVCCompatibility.rst
+++ b/docs/MSVCCompatibility.rst
@@ -84,14 +84,23 @@ The status of major ABI-impacting C++ features:
 * RTTI: :good:`Complete`.  Generation of RTTI data structures has been
   finished, along with support for the ``/GR`` flag.
 
-* Exceptions and SEH: :partial:`Minimal`.  Clang can parse both constructs, but
-  does not know how to emit compatible handlers.  Clang can throw and rethrow
-  C++ exceptions.
-
-* Thread-safe initialization of local statics: :none:`Unstarted`.  We are ABI
-  compatible with MSVC 2013, which does not support thread-safe local statics.
-  MSVC "14" changed the ABI to make initialization of local statics thread safe,
-  and we have not yet implemented this.
+* Exceptions and SEH: :partial:`Partial`.
+  C++ exceptions (``try`` / ``catch`` / ``throw``) and
+  structured exceptions (``__try`` / ``__except`` / ``__finally``) mostly
+  work on x64. 32-bit exception handling support is being worked on.  LLVM does
+  not model asynchronous exceptions, so it is currently impossible to catch an
+  asynchronous exception generated in the same frame as the catching ``__try``.
+  C++ exception specifications are ignored, but this is `consistent with Visual
+  C++`_.
+
+.. _consistent with Visual C++:
+  https://msdn.microsoft.com/en-us/library/wfa0edys.aspx
+
+* Thread-safe initialization of local statics: :good:`Complete`.  MSVC 2015
+  added support for thread-safe initialization of such variables by taking an
+  ABI break.
+  We are ABI compatible with both the MSVC 2013 and 2015 ABI for static local
+  variables.
 
 * Lambdas: :good:`Mostly complete`.  Clang is compatible with Microsoft's
   implementation of lambdas except for providing overloads for conversion to
diff --git a/docs/Makefile b/docs/Makefile
index 96978d893ef..a409cf6025b 100644
--- a/docs/Makefile
+++ b/docs/Makefile
@@ -8,7 +8,6 @@
 ##===----------------------------------------------------------------------===##
 
 CLANG_LEVEL := ..
-DIRS       := tools
 
 ifdef BUILD_FOR_WEBSITE
 PROJ_OBJ_DIR = .
diff --git a/docs/Modules.rst b/docs/Modules.rst
index 9999106914f..1f3d8989751 100644
--- a/docs/Modules.rst
+++ b/docs/Modules.rst
@@ -159,7 +159,7 @@ Module maps are specified as separate files (each named ``module.modulemap``) al
 
   To actually see any benefits from modules, one first has to introduce module maps for the underlying C standard library and the libraries and headers on which it depends. The section `Modularizing a Platform`_ describes the steps one must take to write these module maps.
   
-One can use module maps without modules to check the integrity of the use of header files. To do this, use the ``-fmodule-maps`` option instead of the ``-fmodules`` option.
+One can use module maps without modules to check the integrity of the use of header files. To do this, use the ``-fimplicit-module-maps`` option instead of the ``-fmodules`` option, or use ``-fmodule-map-file=`` option to explicitly specify the module map files to load.
 
 Compilation model
 -----------------
@@ -174,8 +174,8 @@ Command-line parameters
 ``-fmodules``
   Enable the modules feature.
 
-``-fmodule-maps``
-  Enable interpretation of module maps. This option is implied by ``-fmodules``.
+``-fimplicit-module-maps``
+  Enable implicit search for module map files named ``module.modulemap`` and similar. This option is implied by ``-fmodules``. If this is disabled with ``-fno-implicit-module-maps``, module map files will only be loaded if they are explicitly specified via ``-fmodule-map-file`` or transitively used by another module map file.
 
 ``-fmodules-cache-path=``
   Specify the path to the modules cache. If not provided, Clang will select a system-appropriate default.
@@ -207,9 +207,6 @@ Command-line parameters
 ``-fmodules-search-all``
   If a symbol is not found, search modules referenced in the current module maps but not imported for symbols, so the error message can reference the module by name.  Note that if the global module index has not been built before, this might take some time as it needs to build all the modules.  Note that this option doesn't apply in module builds, to avoid the recursion.
 
-``-fno-modules-implicit-maps``
-  Suppresses the implicit search for files called ``module.modulemap`` and similar. Instead, module files need to be explicitly specified via ``-fmodule-map-file`` or transitively used.
-
 ``-fno-implicit-modules``
   All modules used by the build must be specified with ``-fmodule-file``.
 
@@ -682,7 +679,7 @@ A *link-declaration* with the ``framework`` specifies that the linker should lin
 
 Configuration macros declaration
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The *config-macros-declaration* specifies the set of configuration macros that have an effect on the the API of the enclosing module.
+The *config-macros-declaration* specifies the set of configuration macros that have an effect on the API of the enclosing module.
 
 .. parsed-literal::
 
diff --git a/docs/ObjectiveCLiterals.rst b/docs/ObjectiveCLiterals.rst
index 8907c1efc77..9fe7f66dffb 100644
--- a/docs/ObjectiveCLiterals.rst
+++ b/docs/ObjectiveCLiterals.rst
@@ -119,8 +119,8 @@ Objective-C provides a new syntax for boxing C expressions:
 
     @(  )
 
-Expressions of scalar (numeric, enumerated, BOOL) and C string pointer
-types are supported:
+Expressions of scalar (numeric, enumerated, BOOL), C string pointer
+and some C structures (via NSValue) are supported:
 
 .. code-block:: objc
 
@@ -136,6 +136,12 @@ types are supported:
     NSString *path = @(getenv("PATH"));       // [NSString stringWithUTF8String:(getenv("PATH"))]
     NSArray *pathComponents = [path componentsSeparatedByString:@":"];
 
+    // structs.
+    NSValue *center = @(view.center);         // Point p = view.center;
+                                              // [NSValue valueWithBytes:&p objCType:@encode(Point)];
+    NSValue *frame = @(view.frame);           // Rect r = view.frame;
+                                              // [NSValue valueWithBytes:&r objCType:@encode(Rect)];
+
 Boxed Enums
 -----------
 
@@ -218,6 +224,42 @@ character data is valid. Passing ``NULL`` as the character pointer will
 raise an exception at runtime. When possible, the compiler will reject
 ``NULL`` character pointers used in boxed expressions.
 
+Boxed C Structures
+------------------
+
+Boxed expressions support construction of NSValue objects.
+It said that C structures can be used, the only requirement is:
+structure should be marked with ``objc_boxable`` attribute.
+To support older version of frameworks and/or third-party libraries
+you may need to add the attribute via ``typedef``.
+
+.. code-block:: objc
+
+    struct __attribute__((objc_boxable)) Point {
+        // ...
+    };
+
+    typedef struct __attribute__((objc_boxable)) _Size {
+        // ...
+    } Size;
+
+    typedef struct _Rect {
+        // ...
+    } Rect;
+
+    struct Point p;
+    NSValue *point = @(p);          // ok
+    Size s;
+    NSValue *size = @(s);           // ok
+
+    Rect r;
+    NSValue *bad_rect = @(r);       // error
+
+    typedef struct __attribute__((objc_boxable)) _Rect Rect;
+
+    NSValue *good_rect = @(r);      // ok
+
+
 Container Literals
 ==================
 
@@ -539,6 +581,22 @@ checks. Here are examples of their use:
         }
     #endif
 
+    #if __has_attribute(objc_boxable)
+        typedef struct __attribute__((objc_boxable)) _Rect Rect;
+    #endif
+
+    #if __has_feature(objc_boxed_nsvalue_expressions)
+        CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"];
+        animation.fromValue = @(layer.position);
+        animation.toValue = @(newPosition);
+        [layer addAnimation:animation forKey:@"move"];
+    #else
+        CABasicAnimation animation = [CABasicAnimation animationWithKeyPath:@"position"];
+        animation.fromValue = [NSValue valueWithCGPoint:layer.position];
+        animation.toValue = [NSValue valueWithCGPoint:newPosition];
+        [layer addAnimation:animation forKey:@"move"];
+    #endif
+
 Code can use also ``__has_feature(objc_bool)`` to check for the
 availability of numeric literals support. This checks for the new
 ``__objc_yes / __objc_no`` keywords, which enable the use of
diff --git a/docs/PCHInternals.rst b/docs/PCHInternals.rst
index a36e65c199c..8f66ddf0e1d 100644
--- a/docs/PCHInternals.rst
+++ b/docs/PCHInternals.rst
@@ -124,20 +124,30 @@ section `.
 AST File Contents
 -----------------
 
-Clang's AST files are organized into several different blocks, each of which
-contains the serialized representation of a part of Clang's internal
+An AST file produced by clang is an object file container with a ``clangast``
+(COFF) or ``__clangast`` (ELF and Mach-O) section containing the serialized AST.
+Other target-specific sections in the object file container are used to hold
+debug information for the data types defined in the AST.  Tools built on top of
+libclang that do not need debug information may also produce raw AST files that
+only contain the serialized AST.
+
+The ``clangast`` section is organized into several different blocks, each of
+which contains the serialized representation of a part of Clang's internal
 representation.  Each of the blocks corresponds to either a block or a record
 within `LLVM's bitstream format `_.
 The contents of each of these logical blocks are described below.
 
 .. image:: PCHLayout.png
 
-For a given AST file, the `llvm-bcanalyzer
-`_ utility can be used
-to examine the actual structure of the bitstream for the AST file.  This
-information can be used both to help understand the structure of the AST file
-and to isolate areas where AST files can still be optimized, e.g., through the
-introduction of abbreviations.
+The ``llvm-objdump`` utility provides a ``-raw-clang-ast`` option to extract the
+binary contents of the AST section from an object file container.
+
+The `llvm-bcanalyzer `_
+utility can be used to examine the actual structure of the bitstream for the AST
+section.  This information can be used both to help understand the structure of
+the AST section and to isolate areas where the AST representation can still be
+optimized, e.g., through the introduction of abbreviations.
+
 
 Metadata Block
 ^^^^^^^^^^^^^^
diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst
index 90655d4d6f7..77eb2e3db6f 100644
--- a/docs/ReleaseNotes.rst
+++ b/docs/ReleaseNotes.rst
@@ -1,5 +1,5 @@
 =====================================
-Clang 3.7 (In-Progress) Release Notes
+Clang 3.8 (In-Progress) Release Notes
 =====================================
 
 .. contents::
@@ -10,15 +10,15 @@ Written by the `LLVM Team `_
 
 .. warning::
 
-   These are in-progress notes for the upcoming Clang 3.7 release. You may
-   prefer the `Clang 3.5 Release Notes
-   `_.
+   These are in-progress notes for the upcoming Clang 3.8 release. You may
+   prefer the `Clang 3.6 Release Notes
+   `_.
 
 Introduction
 ============
 
 This document contains the release notes for the Clang C/C++/Objective-C
-frontend, part of the LLVM Compiler Infrastructure, release 3.7. Here we
+frontend, part of the LLVM Compiler Infrastructure, release 3.8. Here we
 describe the status of Clang in some detail, including major
 improvements from the previous release and new feature work. For the
 general LLVM release notes, see `the LLVM
@@ -36,7 +36,7 @@ main Clang web page, this document applies to the *next* release, not
 the current one. To see the release notes for a specific release, please
 see the `releases page `_.
 
-What's New in Clang 3.7?
+What's New in Clang 3.8?
 ========================
 
 Some of the major new features and improvements to Clang are listed
@@ -47,8 +47,7 @@ sections with improvements to Clang's support for those languages.
 Major New Features
 ------------------
 
-- Feature ...
-
+- Feature1...
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -62,11 +61,6 @@ about them. The improvements since the 3.5 release include:
 New Compiler Flags
 ------------------
 
-The sized deallocation feature of C++14 is now controlled by the
-``-fsized-deallocation`` flag. This feature relies on library support that
-isn't yet widely deployed, so the user must supply an extra flag to get the
-extra functionality.
-
 The option ....
 
 
@@ -114,11 +108,11 @@ OpenCL C Language Changes in Clang
 Internal API Changes
 --------------------
 
-These are major API changes that have happened since the 3.6 release of
+These are major API changes that have happened since the 3.7 release of
 Clang. If upgrading an external codebase that uses Clang as a library,
 this section should help get you past the largest hurdles of upgrading.
 
-...
+-  ...
 
 libclang
 --------
diff --git a/docs/SafeStack.rst b/docs/SafeStack.rst
new file mode 100644
index 00000000000..21e9b6c621a
--- /dev/null
+++ b/docs/SafeStack.rst
@@ -0,0 +1,187 @@
+=========
+SafeStack
+=========
+
+.. contents::
+   :local:
+
+Introduction
+============
+
+SafeStack is an instrumentation pass that protects programs against attacks
+based on stack buffer overflows, without introducing any measurable performance
+overhead. It works by separating the program stack into two distinct regions:
+the safe stack and the unsafe stack. The safe stack stores return addresses,
+register spills, and local variables that are always accessed in a safe way,
+while the unsafe stack stores everything else. This separation ensures that
+buffer overflows on the unsafe stack cannot be used to overwrite anything
+on the safe stack.
+
+SafeStack is a part of the `Code-Pointer Integrity (CPI) Project
+`_.
+
+Performance
+-----------
+
+The performance overhead of the SafeStack instrumentation is less than 0.1% on
+average across a variety of benchmarks (see the `Code-Pointer Integrity
+`__ paper for details). This is mainly
+because most small functions do not have any variables that require the unsafe
+stack and, hence, do not need unsafe stack frames to be created. The cost of
+creating unsafe stack frames for large functions is amortized by the cost of
+executing the function.
+
+In some cases, SafeStack actually improves the performance. Objects that end up
+being moved to the unsafe stack are usually large arrays or variables that are
+used through multiple stack frames. Moving such objects away from the safe
+stack increases the locality of frequently accessed values on the stack, such
+as register spills, return addresses, and small local variables.
+
+Compatibility
+-------------
+
+Most programs, static libraries, or individual files can be compiled
+with SafeStack as is. SafeStack requires basic runtime support, which, on most
+platforms, is implemented as a compiler-rt library that is automatically linked
+in when the program is compiled with SafeStack.
+
+Linking a DSO with SafeStack is not currently supported.
+
+Known compatibility limitations
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Certain code that relies on low-level stack manipulations requires adaption to
+work with SafeStack. One example is mark-and-sweep garbage collection
+implementations for C/C++ (e.g., Oilpan in chromium/blink), which must be
+changed to look for the live pointers on both safe and unsafe stacks.
+
+SafeStack supports linking statically modules that are compiled with and
+without SafeStack. An executable compiled with SafeStack can load dynamic
+libraries that are not compiled with SafeStack. At the moment, compiling
+dynamic libraries with SafeStack is not supported.
+
+Signal handlers that use ``sigaltstack()`` must not use the unsafe stack (see
+``__attribute__((no_sanitize("safe-stack")))`` below).
+
+Programs that use APIs from ``ucontext.h`` are not supported yet.
+
+Security
+--------
+
+SafeStack protects return addresses, spilled registers and local variables that
+are always accessed in a safe way by separating them in a dedicated safe stack
+region. The safe stack is automatically protected against stack-based buffer
+overflows, since it is disjoint from the unsafe stack in memory, and it itself
+is always accessed in a safe way. In the current implementation, the safe stack
+is protected against arbitrary memory write vulnerabilities though
+randomization and information hiding: the safe stack is allocated at a random
+address and the instrumentation ensures that no pointers to the safe stack are
+ever stored outside of the safe stack itself (see limitations below).
+
+Known security limitations
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+A complete protection against control-flow hijack attacks requires combining
+SafeStack with another mechanism that enforces the integrity of code pointers
+that are stored on the heap or the unsafe stack, such as `CPI
+`_, or a forward-edge control flow integrity
+mechanism that enforces correct calling conventions at indirect call sites,
+such as `IFCC `_ with arity
+checks. Clang has control-flow integrity protection scheme for :doc:`C++ virtual
+calls `, but not non-virtual indirect calls. With
+SafeStack alone, an attacker can overwrite a function pointer on the heap or
+the unsafe stack and cause a program to call arbitrary location, which in turn
+might enable stack pivoting and return-oriented programming.
+
+In its current implementation, SafeStack provides precise protection against
+stack-based buffer overflows, but protection against arbitrary memory write
+vulnerabilities is probabilistic and relies on randomization and information
+hiding. The randomization is currently based on system-enforced ASLR and shares
+its known security limitations. The safe stack pointer hiding is not perfect
+yet either: system library functions such as ``swapcontext``, exception
+handling mechanisms, intrinsics such as ``__builtin_frame_address``, or
+low-level bugs in runtime support could leak the safe stack pointer. In the
+future, such leaks could be detected by static or dynamic analysis tools and
+prevented by adjusting such functions to either encrypt the stack pointer when
+storing it in the heap (as already done e.g., by ``setjmp``/``longjmp``
+implementation in glibc), or store it in a safe region instead.
+
+The `CPI paper `_ describes two alternative,
+stronger safe stack protection mechanisms, that rely on software fault
+isolation, or hardware segmentation (as available on x86-32 and some x86-64
+CPUs).
+
+At the moment, SafeStack assumes that the compiler's implementation is correct.
+This has not been verified except through manual code inspection, and could
+always regress in the future. It's therefore desirable to have a separate
+static or dynamic binary verification tool that would check the correctness of
+the SafeStack instrumentation in final binaries.
+
+Usage
+=====
+
+To enable SafeStack, just pass ``-fsanitize=safe-stack`` flag to both compile
+and link command lines.
+
+Supported Platforms
+-------------------
+
+SafeStack was tested on Linux, FreeBSD and MacOSX.
+
+Low-level API
+-------------
+
+``__has_feature(safe_stack)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+In some rare cases one may need to execute different code depending on
+whether SafeStack is enabled. The macro ``__has_feature(safe_stack)`` can
+be used for this purpose.
+
+.. code-block:: c
+
+    #if __has_feature(safe_stack)
+    // code that builds only under SafeStack
+    #endif
+
+``__attribute__((no_sanitize("safe-stack")))``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Use ``__attribute__((no_sanitize("safe-stack")))`` on a function declaration
+to specify that the safe stack instrumentation should not be applied to that
+function, even if enabled globally (see ``-fsanitize=safe-stack`` flag). This
+attribute may be required for functions that make assumptions about the
+exact layout of their stack frames.
+
+All local variables in functions with this attribute will be stored on the safe
+stack. The safe stack remains unprotected against memory errors when accessing
+these variables, so extra care must be taken to manually ensure that all such
+accesses are safe. Furthermore, the addresses of such local variables should
+never be stored on the heap, as it would leak the location of the SafeStack.
+
+``__builtin___get_unsafe_stack_ptr()``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This builtin function returns current unsafe stack pointer of the current
+thread.
+
+``__builtin___get_unsafe_stack_start()``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This builtin function returns a pointer to the start of the unsafe stack of the
+current thread.
+
+Design
+======
+
+Please refer to the `Code-Pointer Integrity `__
+project page for more information about the design of the SafeStack and its
+related technologies.
+
+Publications
+------------
+
+`Code-Pointer Integrity `__.
+Volodymyr Kuznetsov, Laszlo Szekeres, Mathias Payer, George Candea, R. Sekar, Dawn Song.
+USENIX Symposium on Operating Systems Design and Implementation
+(`OSDI `_), Broomfield, CO, October 2014
diff --git a/docs/SanitizerCoverage.rst b/docs/SanitizerCoverage.rst
new file mode 100644
index 00000000000..65af6ffbf01
--- /dev/null
+++ b/docs/SanitizerCoverage.rst
@@ -0,0 +1,355 @@
+=================
+SanitizerCoverage
+=================
+
+.. contents::
+   :local:
+
+Introduction
+============
+
+Sanitizer tools have a very simple code coverage tool built in. It allows to
+get function-level, basic-block-level, and edge-level coverage at a very low
+cost.
+
+How to build and run
+====================
+
+SanitizerCoverage can be used with :doc:`AddressSanitizer`,
+:doc:`LeakSanitizer`, :doc:`MemorySanitizer`, and UndefinedBehaviorSanitizer.
+In addition to ``-fsanitize=``, pass one of the following compile-time flags:
+
+* ``-fsanitize-coverage=func`` for function-level coverage (very fast).
+* ``-fsanitize-coverage=bb`` for basic-block-level coverage (may add up to 30%
+  **extra** slowdown).
+* ``-fsanitize-coverage=edge`` for edge-level coverage (up to 40% slowdown).
+
+You may also specify ``-fsanitize-coverage=indirect-calls`` for
+additional `caller-callee coverage`_.
+
+At run time, pass ``coverage=1`` in ``ASAN_OPTIONS``, ``LSAN_OPTIONS``,
+``MSAN_OPTIONS`` or ``UBSAN_OPTIONS``, as appropriate.
+
+To get `Coverage counters`_, add ``-fsanitize-coverage=8bit-counters``
+to one of the above compile-time flags. At runtime, use
+``*SAN_OPTIONS=coverage=1:coverage_counters=1``.
+
+Example:
+
+.. code-block:: console
+
+    % cat -n cov.cc
+         1  #include 
+         2  __attribute__((noinline))
+         3  void foo() { printf("foo\n"); }
+         4
+         5  int main(int argc, char **argv) {
+         6    if (argc == 2)
+         7      foo();
+         8    printf("main\n");
+         9  }
+    % clang++ -g cov.cc -fsanitize=address -fsanitize-coverage=func
+    % ASAN_OPTIONS=coverage=1 ./a.out; ls -l *sancov
+    main
+    -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
+    % ASAN_OPTIONS=coverage=1 ./a.out foo ; ls -l *sancov
+    foo
+    main
+    -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
+    -rw-r----- 1 kcc eng 8 Nov 27 12:21 a.out.22679.sancov
+
+Every time you run an executable instrumented with SanitizerCoverage
+one ``*.sancov`` file is created during the process shutdown.
+If the executable is dynamically linked against instrumented DSOs,
+one ``*.sancov`` file will be also created for every DSO.
+
+Postprocessing
+==============
+
+The format of ``*.sancov`` files is very simple: the first 8 bytes is the magic,
+one of ``0xC0BFFFFFFFFFFF64`` and ``0xC0BFFFFFFFFFFF32``. The last byte of the
+magic defines the size of the following offsets. The rest of the data is the
+offsets in the corresponding binary/DSO that were executed during the run.
+
+A simple script
+``$LLVM/projects/compiler-rt/lib/sanitizer_common/scripts/sancov.py`` is
+provided to dump these offsets.
+
+.. code-block:: console
+
+    % sancov.py print a.out.22679.sancov a.out.22673.sancov
+    sancov.py: read 2 PCs from a.out.22679.sancov
+    sancov.py: read 1 PCs from a.out.22673.sancov
+    sancov.py: 2 files merged; 2 PCs total
+    0x465250
+    0x4652a0
+
+You can then filter the output of ``sancov.py`` through ``addr2line --exe
+ObjectFile`` or ``llvm-symbolizer --obj ObjectFile`` to get file names and line
+numbers:
+
+.. code-block:: console
+
+    % sancov.py print a.out.22679.sancov a.out.22673.sancov 2> /dev/null | llvm-symbolizer --obj a.out
+    cov.cc:3
+    cov.cc:5
+
+How good is the coverage?
+=========================
+
+It is possible to find out which PCs are not covered, by subtracting the covered
+set from the set of all instrumented PCs. The latter can be obtained by listing
+all callsites of ``__sanitizer_cov()`` in the binary. On Linux, ``sancov.py``
+can do this for you. Just supply the path to binary and a list of covered PCs:
+
+.. code-block:: console
+
+    % sancov.py print a.out.12345.sancov > covered.txt
+    sancov.py: read 2 64-bit PCs from a.out.12345.sancov
+    sancov.py: 1 file merged; 2 PCs total
+    % sancov.py missing a.out < covered.txt
+    sancov.py: found 3 instrumented PCs in a.out
+    sancov.py: read 2 PCs from stdin
+    sancov.py: 1 PCs missing from coverage
+    0x4cc61c
+
+Edge coverage
+=============
+
+Consider this code:
+
+.. code-block:: c++
+
+    void foo(int *a) {
+      if (a)
+        *a = 0;
+    }
+
+It contains 3 basic blocks, let's name them A, B, C:
+
+.. code-block:: none
+
+    A
+    |\
+    | \
+    |  B
+    | /
+    |/
+    C
+
+If blocks A, B, and C are all covered we know for certain that the edges A=>B
+and B=>C were executed, but we still don't know if the edge A=>C was executed.
+Such edges of control flow graph are called
+`critical `_. The
+edge-level coverage (``-fsanitize-coverage=edge``) simply splits all critical
+edges by introducing new dummy blocks and then instruments those blocks:
+
+.. code-block:: none
+
+    A
+    |\
+    | \
+    D  B
+    | /
+    |/
+    C
+
+Bitset
+======
+
+When ``coverage_bitset=1`` run-time flag is given, the coverage will also be
+dumped as a bitset (text file with 1 for blocks that have been executed and 0
+for blocks that were not).
+
+.. code-block:: console
+
+    % clang++ -fsanitize=address -fsanitize-coverage=edge cov.cc
+    % ASAN_OPTIONS="coverage=1:coverage_bitset=1" ./a.out
+    main
+    % ASAN_OPTIONS="coverage=1:coverage_bitset=1" ./a.out 1
+    foo
+    main
+    % head *bitset*
+    ==> a.out.38214.bitset-sancov <==
+    01101
+    ==> a.out.6128.bitset-sancov <==
+    11011%
+
+For a given executable the length of the bitset is always the same (well,
+unless dlopen/dlclose come into play), so the bitset coverage can be
+easily used for bitset-based corpus distillation.
+
+Caller-callee coverage
+======================
+
+(Experimental!)
+Every indirect function call is instrumented with a run-time function call that
+captures caller and callee.  At the shutdown time the process dumps a separate
+file called ``caller-callee.PID.sancov`` which contains caller/callee pairs as
+pairs of lines (odd lines are callers, even lines are callees)
+
+.. code-block:: console
+
+    a.out 0x4a2e0c
+    a.out 0x4a6510
+    a.out 0x4a2e0c
+    a.out 0x4a87f0
+
+Current limitations:
+
+* Only the first 14 callees for every caller are recorded, the rest are silently
+  ignored.
+* The output format is not very compact since caller and callee may reside in
+  different modules and we need to spell out the module names.
+* The routine that dumps the output is not optimized for speed
+* Only Linux x86_64 is tested so far.
+* Sandboxes are not supported.
+
+Coverage counters
+=================
+
+This experimental feature is inspired by
+`AFL `_'s coverage
+instrumentation. With additional compile-time and run-time flags you can get
+more sensitive coverage information.  In addition to boolean values assigned to
+every basic block (edge) the instrumentation will collect imprecise counters.
+On exit, every counter will be mapped to a 8-bit bitset representing counter
+ranges: ``1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+`` and those 8-bit bitsets will
+be dumped to disk.
+
+.. code-block:: console
+
+    % clang++ -g cov.cc -fsanitize=address -fsanitize-coverage=edge,8bit-counters
+    % ASAN_OPTIONS="coverage=1:coverage_counters=1" ./a.out
+    % ls -l *counters-sancov
+    ... a.out.17110.counters-sancov
+    % xxd *counters-sancov
+    0000000: 0001 0100 01
+
+These counters may also be used for in-process coverage-guided fuzzers. See
+``include/sanitizer/coverage_interface.h``:
+
+.. code-block:: c++
+
+    // The coverage instrumentation may optionally provide imprecise counters.
+    // Rather than exposing the counter values to the user we instead map
+    // the counters to a bitset.
+    // Every counter is associated with 8 bits in the bitset.
+    // We define 8 value ranges: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+
+    // The i-th bit is set to 1 if the counter value is in the i-th range.
+    // This counter-based coverage implementation is *not* thread-safe.
+
+    // Returns the number of registered coverage counters.
+    uintptr_t __sanitizer_get_number_of_counters();
+    // Updates the counter 'bitset', clears the counters and returns the number of
+    // new bits in 'bitset'.
+    // If 'bitset' is nullptr, only clears the counters.
+    // Otherwise 'bitset' should be at least
+    // __sanitizer_get_number_of_counters bytes long and 8-aligned.
+    uintptr_t
+    __sanitizer_update_counter_bitset_and_clear_counters(uint8_t *bitset);
+
+Output directory
+================
+
+By default, .sancov files are created in the current working directory.
+This can be changed with ``ASAN_OPTIONS=coverage_dir=/path``:
+
+.. code-block:: console
+
+    % ASAN_OPTIONS="coverage=1:coverage_dir=/tmp/cov" ./a.out foo
+    % ls -l /tmp/cov/*sancov
+    -rw-r----- 1 kcc eng 4 Nov 27 12:21 a.out.22673.sancov
+    -rw-r----- 1 kcc eng 8 Nov 27 12:21 a.out.22679.sancov
+
+Sudden death
+============
+
+Normally, coverage data is collected in memory and saved to disk when the
+program exits (with an ``atexit()`` handler), when a SIGSEGV is caught, or when
+``__sanitizer_cov_dump()`` is called.
+
+If the program ends with a signal that ASan does not handle (or can not handle
+at all, like SIGKILL), coverage data will be lost. This is a big problem on
+Android, where SIGKILL is a normal way of evicting applications from memory.
+
+With ``ASAN_OPTIONS=coverage=1:coverage_direct=1`` coverage data is written to a
+memory-mapped file as soon as it collected.
+
+.. code-block:: console
+
+    % ASAN_OPTIONS="coverage=1:coverage_direct=1" ./a.out
+    main
+    % ls
+    7036.sancov.map  7036.sancov.raw  a.out
+    % sancov.py rawunpack 7036.sancov.raw
+    sancov.py: reading map 7036.sancov.map
+    sancov.py: unpacking 7036.sancov.raw
+    writing 1 PCs to a.out.7036.sancov
+    % sancov.py print a.out.7036.sancov
+    sancov.py: read 1 PCs from a.out.7036.sancov
+    sancov.py: 1 files merged; 1 PCs total
+    0x4b2bae
+
+Note that on 64-bit platforms, this method writes 2x more data than the default,
+because it stores full PC values instead of 32-bit offsets.
+
+In-process fuzzing
+==================
+
+Coverage data could be useful for fuzzers and sometimes it is preferable to run
+a fuzzer in the same process as the code being fuzzed (in-process fuzzer).
+
+You can use ``__sanitizer_get_total_unique_coverage()`` from
+```` which returns the number of currently
+covered entities in the program. This will tell the fuzzer if the coverage has
+increased after testing every new input.
+
+If a fuzzer finds a bug in the ASan run, you will need to save the reproducer
+before exiting the process.  Use ``__asan_set_death_callback`` from
+```` to do that.
+
+An example of such fuzzer can be found in `the LLVM tree
+`_.
+
+Performance
+===========
+
+This coverage implementation is **fast**. With function-level coverage
+(``-fsanitize-coverage=func``) the overhead is not measurable. With
+basic-block-level coverage (``-fsanitize-coverage=bb``) the overhead varies
+between 0 and 25%.
+
+==============  =========  =========  =========  =========  =========  =========
+     benchmark      cov0        cov1   diff 0-1       cov2   diff 0-2   diff 1-2
+==============  =========  =========  =========  =========  =========  =========
+ 400.perlbench    1296.00    1307.00       1.01    1465.00       1.13       1.12
+     401.bzip2     858.00     854.00       1.00    1010.00       1.18       1.18
+       403.gcc     613.00     617.00       1.01     683.00       1.11       1.11
+       429.mcf     605.00     582.00       0.96     610.00       1.01       1.05
+     445.gobmk     896.00     880.00       0.98    1050.00       1.17       1.19
+     456.hmmer     892.00     892.00       1.00     918.00       1.03       1.03
+     458.sjeng     995.00    1009.00       1.01    1217.00       1.22       1.21
+462.libquantum     497.00     492.00       0.99     534.00       1.07       1.09
+   464.h264ref    1461.00    1467.00       1.00    1543.00       1.06       1.05
+   471.omnetpp     575.00     590.00       1.03     660.00       1.15       1.12
+     473.astar     658.00     652.00       0.99     715.00       1.09       1.10
+ 483.xalancbmk     471.00     491.00       1.04     582.00       1.24       1.19
+      433.milc     616.00     627.00       1.02     627.00       1.02       1.00
+      444.namd     602.00     601.00       1.00     654.00       1.09       1.09
+    447.dealII     630.00     634.00       1.01     653.00       1.04       1.03
+    450.soplex     365.00     368.00       1.01     395.00       1.08       1.07
+    453.povray     427.00     434.00       1.02     495.00       1.16       1.14
+       470.lbm     357.00     375.00       1.05     370.00       1.04       0.99
+   482.sphinx3     927.00     928.00       1.00    1000.00       1.08       1.08
+==============  =========  =========  =========  =========  =========  =========
+
+Why another coverage?
+=====================
+
+Why did we implement yet another code coverage?
+  * We needed something that is lightning fast, plays well with
+    AddressSanitizer, and does not significantly increase the binary size.
+  * Traditional coverage implementations based in global counters
+    `suffer from contention on counters
+    `_.
diff --git a/docs/ThreadSafetyAnalysis.rst b/docs/ThreadSafetyAnalysis.rst
index 0a1b8049e46..19ec2352ff2 100644
--- a/docs/ThreadSafetyAnalysis.rst
+++ b/docs/ThreadSafetyAnalysis.rst
@@ -857,6 +857,9 @@ implementation.
 
     // Assert that is mutex is currently held for read operations.
     void AssertReaderHeld() ASSERT_SHARED_CAPABILITY(this);
+    
+    // For negative capabilities.
+    const Mutex& operator!() const { return *this; }
   };
 
 
diff --git a/docs/UsersManual.rst b/docs/UsersManual.rst
index bf8ce78036f..53d7286e265 100644
--- a/docs/UsersManual.rst
+++ b/docs/UsersManual.rst
@@ -589,6 +589,25 @@ Current limitations
    translated from debug annotations. That translation can be lossy,
    which results in some remarks having no location information.
 
+Other Options
+-------------
+Clang options that that don't fit neatly into other categories.
+
+.. option:: -MV
+
+  When emitting a dependency file, use formatting conventions appropriate
+  for NMake or Jom. Ignored unless another option causes Clang to emit a
+  dependency file.
+
+When Clang emits a dependency file (e.g., you supplied the -M option)
+most filenames can be written to the file without any special formatting.
+Different Make tools will treat different sets of characters as "special"
+and use different conventions for telling the Make tool that the character
+is actually part of the filename. Normally Clang uses backslash to "escape"
+a special character, which is the convention used by GNU Make. The -MV
+option tells Clang to put double-quotes around the entire filename, which
+is the convention used by NMake and Jom.
+
 
 Language and Target-Independent Features
 ========================================
@@ -911,6 +930,8 @@ number of cases where the compilation environment is tightly controlled
 and the precompiled header cannot be generated after headers have been
 installed.
 
+.. _controlling-code-generation:
+
 Controlling Code Generation
 ---------------------------
 
@@ -949,16 +970,15 @@ are listed below.
       includes all of the checks listed below other than
       ``unsigned-integer-overflow``.
 
-   -  ``-fsanitize=undefined-trap``: This includes all sanitizers
-      included by ``-fsanitize=undefined``, except those that require
-      runtime support. This group of sanitizers is intended to be
-      used in conjunction with the ``-fsanitize-undefined-trap-on-error``
-      flag. This includes all of the checks listed below other than
-      ``unsigned-integer-overflow`` and ``vptr``.
+   -  ``-fsanitize=undefined-trap``: This is a deprecated alias for
+      ``-fsanitize=undefined``.
+
    -  ``-fsanitize=dataflow``: :doc:`DataFlowSanitizer`, a general data
       flow analysis.
    -  ``-fsanitize=cfi``: :doc:`control flow integrity `
-      checks. Implies ``-flto``.
+      checks. Requires ``-flto``.
+   -  ``-fsanitize=safe-stack``: :doc:`safe stack `
+      protection against stack-based memory corruption errors.
 
    The following more fine-grained checks are also available:
 
@@ -971,13 +991,13 @@ are listed below.
    -  ``-fsanitize=cfi-cast-strict``: Enables :ref:`strict cast checks
       `.
    -  ``-fsanitize=cfi-derived-cast``: Base-to-derived cast to the wrong
-      dynamic type. Implies ``-flto``.
+      dynamic type. Requires ``-flto``.
    -  ``-fsanitize=cfi-unrelated-cast``: Cast from ``void*`` or another
-      unrelated type to the wrong dynamic type. Implies ``-flto``.
+      unrelated type to the wrong dynamic type. Requires ``-flto``.
    -  ``-fsanitize=cfi-nvcall``: Non-virtual call via an object whose vptr is of
-      the wrong dynamic type. Implies ``-flto``.
+      the wrong dynamic type. Requires ``-flto``.
    -  ``-fsanitize=cfi-vcall``: Virtual call via an object whose vptr is of the
-      wrong dynamic type. Implies ``-flto``.
+      wrong dynamic type. Requires ``-flto``.
    -  ``-fsanitize=enum``: Load of a value of an enumerated type which
       is not in the range of representable values for that enumerated
       type.
@@ -1046,15 +1066,6 @@ are listed below.
       through. This mode may use extra memory in programs that copy
       uninitialized memory a lot.
 
-   Extra features of UndefinedBehaviorSanitizer:
-
-   -  ``-fsanitize-undefined-trap-on-error``: Causes traps to be emitted
-      rather than calls to runtime libraries when a problem is detected.
-      This option is intended for use in cases where the sanitizer runtime
-      cannot be used (for instance, when building libc or a kernel module).
-      This is only compatible with the sanitizers in the ``undefined-trap``
-      group.
-
    The ``-fsanitize=`` argument must also be provided when linking, in
    order to link to the appropriate runtime library. When using
    ``-fsanitize=vptr`` (or a group that includes it, such as
@@ -1078,6 +1089,41 @@ are listed below.
    sanitizers (e.g. :doc:`AddressSanitizer`) may not support recovery,
    and always crash the program after the issue is detected.
 
+   Note that the ``-fsanitize-trap`` flag has precedence over this flag.
+   This means that if a check has been configured to trap elsewhere on the
+   command line, or if the check traps by default, this flag will not have
+   any effect unless that sanitizer's trapping behavior is disabled with
+   ``-fno-sanitize-trap``.
+
+   For example, if a command line contains the flags ``-fsanitize=undefined
+   -fsanitize-trap=undefined``, the flag ``-fsanitize-recover=alignment``
+   will have no effect on its own; it will need to be accompanied by
+   ``-fno-sanitize-trap=alignment``.
+
+**-f[no-]sanitize-trap=check1,check2,...**
+
+   Controls which checks enabled by the ``-fsanitize=`` flag trap. This
+   option is intended for use in cases where the sanitizer runtime cannot
+   be used (for instance, when building libc or a kernel module), or where
+   the binary size increase caused by the sanitizer runtime is a concern.
+
+   This flag is only compatible with ``local-bounds``,
+   ``unsigned-integer-overflow``, sanitizers in the ``cfi`` group and
+   sanitizers in the ``undefined`` group other than ``vptr``. If this flag
+   is supplied together with ``-fsanitize=undefined``, the ``vptr`` sanitizer
+   will be implicitly disabled.
+
+   This flag is enabled by default for sanitizers in the ``cfi`` group.
+
+**-f[no-]sanitize-coverage=[type,features,...]**
+
+   Enable simple code coverage in addition to certain sanitizers.
+   See :doc:`SanitizerCoverage` for more details.
+
+.. option:: -fsanitize-undefined-trap-on-error
+
+   Deprecated alias for ``-fsanitize-trap=undefined``.
+
 .. option:: -fno-assume-sane-operator-new
 
    Don't assume that the C++'s new operator is sane.
@@ -1111,6 +1157,13 @@ are listed below.
    efficient model can be used. The TLS model can be overridden per
    variable using the ``tls_model`` attribute.
 
+.. option:: -femulated-tls
+
+   Select emulated TLS model, which overrides all -ftls-model choices.
+
+   In emulated TLS mode, all access to TLS variables are converted to
+   calls to __emutls_get_address in the runtime library.
+
 .. option:: -mhwdiv=[values]
 
    Select the ARM modes (arm or thumb) that support hardware division
@@ -1190,6 +1243,32 @@ behavior. Code that is not exercised in the profile will be optimized as if it
 is unimportant, and the compiler may make poor optimization choices for code
 that is disproportionately used while profiling.
 
+Differences Between Sampling and Instrumentation
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Although both techniques are used for similar purposes, there are important
+differences between the two:
+
+1. Profile data generated with one cannot be used by the other, and there is no
+   conversion tool that can convert one to the other. So, a profile generated
+   via ``-fprofile-instr-generate`` must be used with ``-fprofile-instr-use``.
+   Similarly, sampling profiles generated by external profilers must be
+   converted and used with ``-fprofile-sample-use``.
+
+2. Instrumentation profile data can be used for code coverage analysis and
+   optimization.
+
+3. Sampling profiles can only be used for optimization. They cannot be used for
+   code coverage analysis. Although it would be technically possible to use
+   sampling profiles for code coverage, sample-based profiles are too
+   coarse-grained for code coverage purposes; it would yield poor results.
+
+4. Sampling profiles must be generated by an external tool. The profile
+   generated by that tool must then be converted into a format that can be read
+   by LLVM. The section on sampling profilers describes one of the supported
+   sampling profile formats.
+
+
 Using Sampling Profilers
 ^^^^^^^^^^^^^^^^^^^^^^^^
 
@@ -1257,17 +1336,38 @@ usual build cycle when using sample profilers for optimization:
      $ clang++ -O2 -gline-tables-only -fprofile-sample-use=code.prof code.cc -o code
 
 
-Sample Profile Format
-"""""""""""""""""""""
+Sample Profile Formats
+""""""""""""""""""""""
+
+Since external profilers generate profile data in a variety of custom formats,
+the data generated by the profiler must be converted into a format that can be
+read by the backend. LLVM supports three different sample profile formats:
+
+1. ASCII text. This is the easiest one to generate. The file is divided into
+   sections, which correspond to each of the functions with profile
+   information. The format is described below.
 
-If you are not using Linux Perf to collect profiles, you will need to
-write a conversion tool from your profiler to LLVM's format. This section
-explains the file format expected by the backend.
+2. Binary encoding. This uses a more efficient encoding that yields smaller
+   profile files, which may be useful when generating large profiles. It can be
+   generated from the text format using the ``llvm-profdata`` tool.
 
-Sample profiles are written as ASCII text. The file is divided into sections,
-which correspond to each of the functions executed at runtime. Each
-section has the following format (taken from
-https://github.com/google/autofdo/blob/master/profile_writer.h):
+3. GCC encoding. This is based on the gcov format, which is accepted by GCC. It
+   is only interesting in environments where GCC and Clang co-exist. Similarly
+   to the binary encoding, it can be generated using the ``llvm-profdata`` tool.
+
+If you are using Linux Perf to generate sampling profiles, you can use the
+conversion tool ``create_llvm_prof`` described in the previous section.
+Otherwise, you will need to write a conversion tool that converts your
+profiler's native format into one of these three.
+
+
+Sample Profile Text Format
+""""""""""""""""""""""""""
+
+This section describes the ASCII text format for sampling profiles. It is,
+arguably, the easiest one to generate. If you are interested in generating any
+of the other two, consult the ``ProfileData`` library in in LLVM's source tree
+(specifically, ``llvm/lib/ProfileData/SampleProfWriter.cpp``).
 
 .. code-block:: console
 
@@ -1374,8 +1474,8 @@ instrumentation:
      $ LLVM_PROFILE_FILE="code-%p.profraw" ./code
 
 3. Combine profiles from multiple runs and convert the "raw" profile format to
-   the input expected by clang. Use the ``merge`` command of the llvm-profdata
-   tool to do this.
+   the input expected by clang. Use the ``merge`` command of the
+   ``llvm-profdata`` tool to do this.
 
    .. code-block:: console
 
@@ -1395,6 +1495,45 @@ instrumentation:
    profile. As you make changes to your code, clang may no longer be able to
    use the profile data. It will warn you when this happens.
 
+Profile generation and use can also be controlled by the GCC-compatible flags
+``-fprofile-generate`` and ``-fprofile-use``. Although these flags are
+semantically equivalent to their GCC counterparts, they *do not* handle
+GCC-compatible profiles. They are only meant to implement GCC's semantics
+with respect to profile creation and use.
+
+.. option:: -fprofile-generate[=]
+
+  Without any other arguments, ``-fprofile-generate`` behaves identically to
+  ``-fprofile-instr-generate``. When given a directory name, it generates the
+  profile file ``default.profraw`` in the directory named ``dirname``. If
+  ``dirname`` does not exist, it will be created at runtime. The environment
+  variable ``LLVM_PROFILE_FILE`` can be used to override the directory and
+  filename for the profile file at runtime. For example,
+
+  .. code-block:: console
+
+    $ clang++ -O2 -fprofile-generate=yyy/zzz code.cc -o code
+
+  When ``code`` is executed, the profile will be written to the file
+  ``yyy/zzz/default.profraw``. This can be altered at runtime via the
+  ``LLVM_PROFILE_FILE`` environment variable:
+
+  .. code-block:: console
+
+    $ LLVM_PROFILE_FILE=/tmp/myprofile/code.profraw ./code
+
+  The above invocation will produce the profile file
+  ``/tmp/myprofile/code.profraw`` instead of ``yyy/zzz/default.profraw``.
+  Notice that ``LLVM_PROFILE_FILE`` overrides the directory *and* the file
+  name for the profile file.
+
+.. option:: -fprofile-use[=]
+
+  Without any other arguments, ``-fprofile-use`` behaves identically to
+  ``-fprofile-instr-use``. Otherwise, if ``pathname`` is the full path to a
+  profile file, it reads from that file. If ``pathname`` is a directory name,
+  it reads from ``pathname/default.profdata``.
+
 
 Controlling Size of Debug Information
 -------------------------------------
diff --git a/docs/conf.py b/docs/conf.py
index 9030c2c2875..edb2d37c9c9 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -41,16 +41,16 @@
 
 # General information about the project.
 project = u'Clang'
-copyright = u'2007-2014, The Clang Team'
+copyright = u'2007-2015, The Clang Team'
 
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
 # built documents.
 #
 # The short X.Y version.
-version = '3.7'
+version = '3.8'
 # The full version, including alpha/beta/rc tags.
-release = '3.7'
+release = '3.8'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
@@ -212,10 +212,40 @@
 
 # One entry per manual page. List of tuples
 # (source start file, name, description, authors, manual section).
-man_pages = [
-    ('index', 'clang', u'Clang Documentation',
-     [u'The Clang Team'], 1)
-]
+man_pages = []
+
+# Automatically derive the list of man pages from the contents of the command
+# guide subdirectory. This was copied from llvm/docs/conf.py.
+basedir = os.path.dirname(__file__)
+man_page_authors = u'Maintained by the Clang / LLVM Team ()'
+command_guide_subpath = 'CommandGuide'
+command_guide_path = os.path.join(basedir, command_guide_subpath)
+for name in os.listdir(command_guide_path):
+    # Ignore non-ReST files and the index page.
+    if not name.endswith('.rst') or name in ('index.rst',):
+        continue
+
+    # Otherwise, automatically extract the description.
+    file_subpath = os.path.join(command_guide_subpath, name)
+    with open(os.path.join(command_guide_path, name)) as f:
+        title = f.readline().rstrip('\n')
+        header = f.readline().rstrip('\n')
+
+        if len(header) != len(title):
+            print >>sys.stderr, (
+                "error: invalid header in %r (does not match title)" % (
+                    file_subpath,))
+        if ' - ' not in title:
+            print >>sys.stderr, (
+                ("error: invalid title in %r "
+                 "(expected ' - ')") % (
+                    file_subpath,))
+
+        # Split the name out of the title.
+        name,description = title.split(' - ', 1)
+        man_pages.append((file_subpath.replace('.rst',''), name,
+                          description, man_page_authors, 1))
+
 
 # If true, show URL addresses after external links.
 #man_show_urls = False
diff --git a/docs/doxygen.cfg.in b/docs/doxygen.cfg.in
index 39c9aed57e0..fe5674a1668 100644
--- a/docs/doxygen.cfg.in
+++ b/docs/doxygen.cfg.in
@@ -169,7 +169,7 @@ SHORT_NAMES            = NO
 # description.)
 # The default value is: NO.
 
-JAVADOC_AUTOBRIEF      = NO
+JAVADOC_AUTOBRIEF      = YES
 
 # If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first
 # line (until the first dot) of a Qt-style comment as the brief description. If
@@ -177,7 +177,7 @@ JAVADOC_AUTOBRIEF      = NO
 # requiring an explicit \brief command for a brief description.)
 # The default value is: NO.
 
-QT_AUTOBRIEF           = NO
+QT_AUTOBRIEF           = YES
 
 # The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a
 # multi-line C++ special comment block (i.e. a block of //! or /// comments) as
diff --git a/docs/index.rst b/docs/index.rst
index 67bdf689e00..d50667d5669 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -26,10 +26,13 @@ Using Clang as a Compiler
    MemorySanitizer
    DataFlowSanitizer
    LeakSanitizer
+   SanitizerCoverage
    SanitizerSpecialCaseList
    ControlFlowIntegrity
+   SafeStack
    Modules
    MSVCCompatibility
+   CommandGuide/index
    FAQ
 
 Using Clang as a Library
diff --git a/docs/tools/Makefile b/docs/tools/Makefile
deleted file mode 100644
index 5521d6b764c..00000000000
--- a/docs/tools/Makefile
+++ /dev/null
@@ -1,116 +0,0 @@
-##===- docs/tools/Makefile ---------------------------------*- Makefile -*-===##
-# 
-#                     The LLVM Compiler Infrastructure
-#
-# This file is distributed under the University of Illinois Open Source
-# License. See LICENSE.TXT for details.
-# 
-##===----------------------------------------------------------------------===##
-
-ifdef BUILD_FOR_WEBSITE
-
-# FIXME: This was copied from the CommandGuide makefile. Figure out
-# how to get this stuff on the website.
-
-# This special case is for keeping the CommandGuide on the LLVM web site
-# up to date automatically as the documents are checked in. It must build
-# the POD files to HTML only and keep them in the src directories. It must also
-# build in an unconfigured tree, hence the ifdef. To use this, run
-# make -s BUILD_FOR_WEBSITE=1 inside the cvs commit script.
-SRC_DOC_DIR=
-DST_HTML_DIR=html/
-DST_MAN_DIR=man/man1/
-DST_PS_DIR=ps/
-CLANG_VERSION := trunk
-
-# If we are in BUILD_FOR_WEBSITE mode, default to the all target.
-all:: html man ps
-
-clean:
-	rm -f pod2htm*.*~~ $(HTML) $(MAN) $(PS)
-
-# To create other directories, as needed, and timestamp their creation
-%/.dir:
-	-mkdir $* > /dev/null
-	date > $@
-
-else
-
-# Otherwise, if not in BUILD_FOR_WEBSITE mode, use the project info.
-CLANG_LEVEL := ../..
-include $(CLANG_LEVEL)/Makefile
-
-CLANG_VERSION := $(word 3,$(shell grep "CLANG_VERSION " \
-	$(PROJ_OBJ_DIR)/$(CLANG_LEVEL)/include/clang/Basic/Version.inc))
-
-SRC_DOC_DIR=$(PROJ_SRC_DIR)/
-DST_HTML_DIR=$(PROJ_OBJ_DIR)/
-DST_MAN_DIR=$(PROJ_OBJ_DIR)/
-DST_PS_DIR=$(PROJ_OBJ_DIR)/
-
-endif
-
-
-POD  := $(wildcard $(SRC_DOC_DIR)*.pod)
-HTML := $(patsubst $(SRC_DOC_DIR)%.pod, $(DST_HTML_DIR)%.html, $(POD))
-MAN  := $(patsubst $(SRC_DOC_DIR)%.pod, $(DST_MAN_DIR)%.1, $(POD))
-PS   := $(patsubst $(SRC_DOC_DIR)%.pod, $(DST_PS_DIR)%.ps, $(POD))
-
-ifdef ONLY_MAN_DOCS
-INSTALL_TARGETS := install-man
-else
-INSTALL_TARGETS := install-html install-man install-ps
-endif
-
-.SUFFIXES:
-.SUFFIXES: .html .pod .1 .ps
-
-$(DST_HTML_DIR)%.html: %.pod $(DST_HTML_DIR)/.dir
-	pod2html --css=manpage.css --htmlroot=. \
-	  --podpath=. --infile=$< --outfile=$@ --title=$*
-
-$(DST_MAN_DIR)%.1: %.pod $(DST_MAN_DIR)/.dir
-	pod2man --release "clang $(CLANG_VERSION)" --center="Clang Tools Documentation" $< $@
-
-$(DST_PS_DIR)%.ps: $(DST_MAN_DIR)%.1 $(DST_PS_DIR)/.dir
-	groff -Tps -man $< > $@
-
-
-html: $(HTML)
-man: $(MAN)
-ps: $(PS)
-
-EXTRA_DIST := $(POD)
-
-clean-local::
-	$(Verb) $(RM) -f pod2htm*.*~~ $(HTML) $(MAN) $(PS)
-
-HTML_DIR := $(DESTDIR)$(PROJ_docsdir)/html/clang
-MAN_DIR  := $(DESTDIR)$(PROJ_mandir)/man1
-PS_DIR   := $(DESTDIR)$(PROJ_docsdir)/ps
-
-install-html:: $(HTML)
-	$(Echo) Installing HTML Clang Tools Documentation
-	$(Verb) $(MKDIR) $(HTML_DIR)
-	$(Verb) $(DataInstall) $(HTML) $(HTML_DIR)
-	$(Verb) $(DataInstall) $(PROJ_SRC_DIR)/manpage.css $(HTML_DIR)
-
-install-man:: $(MAN)
-	$(Echo) Installing MAN Clang Tools Documentation
-	$(Verb) $(MKDIR) $(MAN_DIR)
-	$(Verb) $(DataInstall) $(MAN) $(MAN_DIR)
-
-install-ps:: $(PS)
-	$(Echo) Installing PS Clang Tools Documentation
-	$(Verb) $(MKDIR) $(PS_DIR)
-	$(Verb) $(DataInstall) $(PS) $(PS_DIR)
-
-install-local:: $(INSTALL_TARGETS)
-
-uninstall-local::
-	$(Echo) Uninstalling Clang Tools Documentation
-	$(Verb) $(RM) -rf $(HTML_DIR) $(MAN_DIR) $(PS_DIR)
-
-printvars::
-	$(Echo) "POD            : " '$(POD)'
-	$(Echo) "HTML           : " '$(HTML)'
diff --git a/docs/tools/clang.pod b/docs/tools/clang.pod
deleted file mode 100644
index f7d2eaf683c..00000000000
--- a/docs/tools/clang.pod
+++ /dev/null
@@ -1,614 +0,0 @@
-=pod
-
-=head1 NAME
-
-clang - the Clang C, C++, and Objective-C compiler
-
-=head1 SYNOPSIS
-
-B [B<-c>|B<-S>|B<-E>] B<-std=>I B<-g>
-  [B<-O0>|B<-O1>|B<-O2>|B<-O3>|B<-Ofast>|B<-Os>|B<-Oz>|B<-O>|B<-O4>]
-  B<-W>I B<-pedantic>
-  B<-I>I B<-L>I
-  B<-D>I
-  B<-f>I
-  B<-m>I
-  B<-o> I
-  B<-stdlib=>I 
-  I
-
-=head1 DESCRIPTION
-
-B is a C, C++, and Objective-C compiler which encompasses preprocessing,
-parsing, optimization, code generation, assembly, and linking.  Depending on
-which high-level mode setting is passed, Clang will stop before doing a full
-link.  While Clang is highly integrated, it is important to understand the
-stages of compilation, to understand how to invoke it.  These stages are:
-
-=over
-
-=item B
-
-The B executable is actually a small driver which controls the overall
-execution of other tools such as the compiler, assembler and linker.  Typically
-you do not need to interact with the driver, but you transparently use it to run
-the other tools.
-
-=item B
-
-This stage handles tokenization of the input source file, macro expansion,
-#include expansion and handling of other preprocessor directives.  The output of
-this stage is typically called a ".i" (for C), ".ii" (for C++), ".mi" (for 
-Objective-C) , or ".mii" (for Objective-C++) file.
-
-=item B
-
-This stage parses the input file, translating preprocessor tokens into a parse
-tree.  Once in the form of a parser tree, it applies semantic analysis to compute
-types for expressions as well and determine whether the code is well formed. This
-stage is responsible for generating most of the compiler warnings as well as
-parse errors.  The output of this stage is an "Abstract Syntax Tree" (AST).
-
-=item B
-
-This stage translates an AST into low-level intermediate code (known as "LLVM
-IR") and ultimately to machine code.  This phase is responsible for optimizing
-the generated code and handling target-specific code generation.  The output of
-this stage is typically called a ".s" file or "assembly" file.
-
-Clang also supports the use of an integrated assembler, in which the code
-generator produces object files directly. This avoids the overhead of generating
-the ".s" file and of calling the target assembler.
-
-=item B
-
-This stage runs the target assembler to translate the output of the compiler
-into a target object file.  The output of this stage is typically called a ".o"
-file or "object" file.
-
-=item B
-
-This stage runs the target linker to merge multiple object files into an
-executable or dynamic library.  The output of this stage is typically called an
-"a.out", ".dylib" or ".so" file.
-
-=back
-
-The Clang compiler supports a large number of options to control each of these
-stages.  In addition to compilation of code, Clang also supports other tools:
-
-B
-
-The Clang Static Analyzer is a tool that scans source code to try to find bugs
-through code analysis.  This tool uses many parts of Clang and is built into the
-same driver.  Please see L for more details
-on how to use the static analyzer.
-
-
-=head1 OPTIONS
-
-=head2 Stage Selection Options
-
-=over
-
-=item B<-E>
-
-Run the preprocessor stage.
-
-=item B<-fsyntax-only>
-
-Run the preprocessor, parser and type checking stages.
-
-=item B<-S>
-
-Run the previous stages as well as LLVM generation and optimization stages and
-target-specific code generation, producing an assembly file.
-
-=item B<-c>
-
-Run all of the above, plus the assembler, generating a target ".o" object file.
-
-=item B
-
-If no stage selection option is specified, all stages above are run, and the
-linker is run to combine the results into an executable or shared library.
-
-=back
-
-
-
-=head2 Language Selection and Mode Options
-
-=over
-
-=item B<-x> I
-
-Treat subsequent input files as having type I.
-
-=item B<-std>=I
-
-Specify the language standard to compile for.
-
-=item B<-stdlib>=I
-
-Specify the C++ standard library to use; supported options are libstdc++ and
-libc++.
-
-=item B<-ansi>
-
-Same as B<-std=c89>.
-
-=item B<-ObjC++>
-
-Treat source input files as Objective-C++ inputs.
-
-=item B<-ObjC>
-
-Treat source input files as Objective-C inputs.
-
-=item B<-trigraphs>
-
-Enable trigraphs.
-
-=item B<-ffreestanding>
-
-Indicate that the file should be compiled for a freestanding, not a hosted,
-environment.
-
-=item B<-fno-builtin>
-
-Disable special handling and optimizations of builtin functions like strlen and
-malloc.
-
-=item B<-fmath-errno>
-
-Indicate that math functions should be treated as updating errno.
-
-=item B<-fpascal-strings>
-
-Enable support for Pascal-style strings with "\pfoo".
-
-=item B<-fms-extensions>
-
-Enable support for Microsoft extensions.
-
-=item B<-fmsc-version=>
-
-Set _MSC_VER. Defaults to 1300 on Windows. Not set otherwise.
-
-=item B<-fborland-extensions>
-
-Enable support for Borland extensions.
-
-=item B<-fwritable-strings>
-
-Make all string literals default to writable.  This disables uniquing of
-strings and other optimizations.
-
-=item B<-flax-vector-conversions>
-
-Allow loose type checking rules for implicit vector conversions.
-
-=item B<-fblocks>
-
-Enable the "Blocks" language feature.
-
-=item B<-fobjc-gc-only>
-
-Indicate that Objective-C code should be compiled in GC-only mode, which only
-works when Objective-C Garbage Collection is enabled.
-
-=item B<-fobjc-gc>
-
-Indicate that Objective-C code should be compiled in hybrid-GC mode, which works
-with both GC and non-GC mode.
-
-=item B<-fobjc-abi-version>=I
-
-Select the Objective-C ABI version to use. Available versions are 1 (legacy
-"fragile" ABI), 2 (non-fragile ABI 1), and 3 (non-fragile ABI 2).
-
-=item B<-fobjc-nonfragile-abi-version>=I
-
-Select the Objective-C non-fragile ABI version to use by default. This will only
-be used as the Objective-C ABI when the non-fragile ABI is enabled (either via
--fobjc-nonfragile-abi, or because it is the platform default).
-
-=item B<-fobjc-nonfragile-abi>
-
-Enable use of the Objective-C non-fragile ABI. On platforms for which this is
-the default ABI, it can be disabled with B<-fno-objc-nonfragile-abi>.
-
-=back
-
-
-
-=head2 Target Selection Options
-
-Clang fully supports cross compilation as an inherent part of its design.
-Depending on how your version of Clang is configured, it may have support for
-a number of cross compilers, or may only support a native target.
-
-=over
-
-=item B<-arch> I
-
-Specify the architecture to build for.
-
-=item B<-mmacosx-version-min>=I
-
-When building for Mac OS X, specify the minimum version supported by your
-application.
-
-=item B<-miphoneos-version-min>
-
-When building for iPhone OS, specify the minimum version supported by your
-application.
-
-
-=item B<-march>=I
-
-Specify that Clang should generate code for a specific processor family member
-and later.  For example, if you specify -march=i486, the compiler is allowed to
-generate instructions that are valid on i486 and later processors, but which
-may not exist on earlier ones.
-
-=back
-
-
-=head2 Code Generation Options
-
-=over
-
-=item B<-O0> B<-O1> B<-O2> B<-O3> B<-Ofast> B<-Os> B<-Oz> B<-O> B<-O4>
-
-Specify which optimization level to use:
-
-=over
-
-=item B<-O0>
-
-Means "no optimization": this level compiles the fastest and
-generates the most debuggable code.
-
-=item B<-O1>
-
-Somewhere between B<-O0> and B<-O2>.
-
-=item B<-O2>
-
-Moderate level of optimization which enables most optimizations.
-
-=item B<-O3>
-
-Like B<-O2>, except that it enables optimizations that take longer to perform
-or that may generate larger code (in an attempt to make the program run faster).
-
-=item B<-Ofast>
-
-Enables all the optimizations from B<-O3> along with other aggressive
-optimizations that may violate strict compliance with language standards.
-
-=item B<-Os>
-
-Like B<-O2> with extra optimizations to reduce code size.
-
-=item B<-Oz>
-
-Like B<-Os> (and thus B<-O2>), but reduces code size further.
-
-=item B<-O>
-
-Equivalent to B<-O2>.
-
-=item B<-O4> and higher
-
-Currently equivalent to B<-O3>
-
-=back
-
-=item B<-g>
-
-Generate debug information.  Note that Clang debug information works best at
-B<-O0>.
-
-=item B<-fstandalone-debug> B<-fno-standalone-debug>
-
-Clang supports a number of optimizations to reduce the size of debug
-information in the binary. They work based on the assumption that the
-debug type information can be spread out over multiple compilation
-units.  For instance, Clang will not emit type definitions for types
-that are not needed by a module and could be replaced with a forward
-declaration.  Further, Clang will only emit type info for a dynamic
-C++ class in the module that contains the vtable for the class.
-
-The B<-fstandalone-debug> option turns off these optimizations.  This
-is useful when working with 3rd-party libraries that don't come with
-debug information.  This is the default on Darwin.  Note that Clang
-will never emit type information for types that are not referenced at
-all by the program.
-
-=item B<-fexceptions>
-
-Enable generation of unwind information, this allows exceptions to be thrown
-through Clang compiled stack frames.  This is on by default in x86-64.
-
-=item B<-ftrapv>
-
-Generate code to catch integer overflow errors.  Signed integer overflow is
-undefined in C, with this flag, extra code is generated to detect this and abort
-when it happens.
-
-
-=item B<-fvisibility>
-
-This flag sets the default visibility level.
-
-=item B<-fcommon>
-
-This flag specifies that variables without initializers get common linkage.  It
-can be disabled with B<-fno-common>.
-
-=item B<-ftls-model>
-
-Set the default thread-local storage (TLS) model to use for thread-local
-variables. Valid values are: "global-dynamic", "local-dynamic", "initial-exec"
-and "local-exec". The default is "global-dynamic". The default model can be
-overridden with the tls_model attribute. The compiler will try to choose a more
-efficient model if possible.
-
-=item B<-flto> B<-emit-llvm>
-
-Generate output files in LLVM formats, suitable for link time optimization. When
-used with B<-S> this generates LLVM intermediate language assembly files,
-otherwise this generates LLVM bitcode format object files (which may be passed
-to the linker depending on the stage selection options).
-
-=cut
-
-##=item B<-fnext-runtime> B<-fobjc-nonfragile-abi> B<-fgnu-runtime>
-##These options specify which Objective-C runtime the code generator should
-##target.  FIXME: we don't want people poking these generally.
-
-=pod
-
-=back
-
-
-=head2 Driver Options
-
-=over
-
-=item B<-###>
-
-Print (but do not run) the commands to run for this compilation.
-
-=item B<--help>
-
-Display available options.
-
-=item B<-Qunused-arguments>
-
-Don't emit warning for unused driver arguments.
-
-=item B<-Wa,>I
-
-Pass the comma separated arguments in I to the assembler.
-
-=item B<-Wl,>I
-
-Pass the comma separated arguments in I to the linker.
-
-=item B<-Wp,>I
-
-Pass the comma separated arguments in I to the preprocessor.
-
-=item B<-Xanalyzer> I
-
-Pass I to the static analyzer.
-
-=item B<-Xassembler> I
-
-Pass I to the assembler.
-
-=item B<-Xlinker> I
-
-Pass I to the linker.
-
-=item B<-Xpreprocessor> I
-
-Pass I to the preprocessor.
-
-=item B<-o> I
-
-Write output to I.
-
-=item B<-print-file-name>=I
-
-Print the full library path of I.
-
-=item B<-print-libgcc-file-name>
-
-Print the library path for "libgcc.a".
-
-=item B<-print-prog-name>=I
-
-Print the full program path of I.
-
-=item B<-print-search-dirs>
-
-Print the paths used for finding libraries and programs.
-
-=item B<-save-temps>
-
-Save intermediate compilation results.
-
-=item B<-integrated-as> B<-no-integrated-as>
-
-Used to enable and disable, respectively, the use of the integrated
-assembler. Whether the integrated assembler is on by default is target
-dependent.
-
-=item B<-time>
-
-Time individual commands.
-
-=item B<-ftime-report>
-
-Print timing summary of each stage of compilation.
-
-=item B<-v>
-
-Show commands to run and use verbose output.
-
-=back
-
-
-=head2 Diagnostics Options
-
-=over
-
-=item B<-fshow-column>
-B<-fshow-source-location>
-B<-fcaret-diagnostics>
-B<-fdiagnostics-fixit-info>
-B<-fdiagnostics-parseable-fixits>
-B<-fdiagnostics-print-source-range-info>
-B<-fprint-source-range-info>
-B<-fdiagnostics-show-option>
-B<-fmessage-length>
-
-These options control how Clang prints out information about diagnostics (errors
-and warnings).  Please see the Clang User's Manual for more information.
-
-=back
-
-
-=head2 Preprocessor Options
-
-=over
-
-=item B<-D>I
-
-Adds an implicit #define into the predefines buffer which is read before the
-source file is preprocessed.
-
-=item B<-U>I
-
-Adds an implicit #undef into the predefines buffer which is read before the
-source file is preprocessed.
-
-=item B<-include> I
-
-Adds an implicit #include into the predefines buffer which is read before the
-source file is preprocessed.
-
-=item B<-I>I
-
-Add the specified directory to the search path for include files.
-
-=item B<-F>I
-
-Add the specified directory to the search path for framework include files.
-
-=item B<-nostdinc>
-
-Do not search the standard system directories or compiler builtin directories
-for include files.
-
-=item B<-nostdlibinc>
-
-Do not search the standard system directories for include files, but do search
-compiler builtin include directories.
-
-=item B<-nobuiltininc>
-
-Do not search clang's builtin directory for include files.
-
-=cut
-
-## TODO, but do we really want people using this stuff?
-#=item B<-idirafter>I
-#=item B<-iquote>I
-#=item B<-isystem>I
-#=item B<-iprefix>I
-#=item B<-iwithprefix>I
-#=item B<-iwithprefixbefore>I
-#=item B<-isysroot>
-
-=pod
-
-
-=back
-
-
-
-=cut
-
-### TODO someday.
-#=head2 Warning Control Options
-#=over
-#=back
-#=head2 Code Generation and Optimization Options
-#=over
-#=back
-#=head2 Assembler Options
-#=over
-#=back
-#=head2 Linker Options
-#=over
-#=back
-#=head2 Static Analyzer Options
-#=over
-#=back
-
-=pod
-
-
-=head1 ENVIRONMENT
-
-=over
-
-=item B, B, B
-
-These environment variables are checked, in order, for the location to
-write temporary files used during the compilation process.
-
-=item B
-
-If this environment variable is present, it is treated as a delimited
-list of paths to be added to the default system include path list. The
-delimiter is the platform dependent delimitor, as used in the I
-environment variable.
-
-Empty components in the environment variable are ignored.
-
-=item B, B, B,
-B
-
-These environment variables specify additional paths, as for CPATH,
-which are only used when processing the appropriate language.
-
-=item B
-
-If -mmacosx-version-min is unspecified, the default deployment target
-is read from this environment variable.  This option only affects darwin
-targets.
-
-=back
-
-=head1 BUGS
-
-To report bugs, please visit L.  Most bug reports should
-include preprocessed source files (use the B<-E> option) and the full output of 
-the compiler, along with information to reproduce.
-
-=head1 SEE ALSO
-
- as(1), ld(1)
-
-=head1 AUTHOR
-
-Maintained by the Clang / LLVM Team (L).
-
-=cut
diff --git a/docs/tools/manpage.css b/docs/tools/manpage.css
deleted file mode 100644
index c922564dc3c..00000000000
--- a/docs/tools/manpage.css
+++ /dev/null
@@ -1,256 +0,0 @@
-/* Based on http://www.perldoc.com/css/perldoc.css */
-
-@import url("../llvm.css");
-
-body { font-family: Arial,Helvetica; }
-
-blockquote { margin: 10pt;  }
-
-h1, a { color: #336699; }
-
-
-/*** Top menu style ****/
-.mmenuon { 
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #ff6600; font-size: 10pt;
-}
-.mmenuoff { 
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #ffffff; font-size: 10pt;
-}	  
-.cpyright {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #ffffff; font-size: xx-small;
-}
-.cpyrightText {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #ffffff; font-size: xx-small;
-}
-.sections { 
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: 11pt;
-}	 
-.dsections { 
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: 12pt;
-}	
-.slink { 
- font-family: Arial,Helvetica; font-weight: normal; text-decoration: none;
- color: #000000; font-size: 9pt;
-}	 
-
-.slink2 { font-family: Arial,Helvetica; text-decoration: none; color: #336699; }	 
-
-.maintitle { 
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: 18pt;
-}	 
-.dblArrow {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: small;
-}
-.menuSec {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: small;
-}
-
-.newstext {
- font-family: Arial,Helvetica; font-size: small;
-}
-
-.linkmenu {
- font-family: Arial,Helvetica; color: #000000; font-weight: bold;
- text-decoration: none;
-}
-
-P {
- font-family: Arial,Helvetica;
-}
-
-PRE {
-    font-size: 10pt;
-}
-.quote { 
- font-family: Times; text-decoration: none;
- color: #000000; font-size: 9pt; font-style: italic;
-}	
-.smstd { font-family: Arial,Helvetica; color: #000000; font-size: x-small; } 
-.std { font-family: Arial,Helvetica; color: #000000; } 
-.meerkatTitle { 
- font-family: sans-serif; font-size: x-small;  color: black;    }
-
-.meerkatDescription { font-family: sans-serif; font-size: 10pt; color: black }
-.meerkatCategory { 
- font-family: sans-serif; font-size: 9pt; font-weight: bold; font-style: italic; 
- color: brown; }
-.meerkatChannel { 
- font-family: sans-serif; font-size: 9pt; font-style: italic; color: brown; }
-.meerkatDate { font-family: sans-serif; font-size: xx-small; color: #336699; }
-
-.tocTitle {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #333333; font-size: 10pt;
-}
-
-.toc-item {
- font-family: Arial,Helvetica; font-weight: bold; 
- color: #336699; font-size: 10pt; text-decoration: underline;
-}
-
-.perlVersion {
- font-family: Arial,Helvetica; font-weight: bold; 
- color: #336699; font-size: 10pt; text-decoration: none;
-}
-
-.podTitle {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #000000;
-}
-
-.docTitle {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #000000; font-size: 10pt;
-}
-.dotDot {
- font-family: Arial,Helvetica; font-weight: bold; 
- color: #000000; font-size: 9pt;
-}
-
-.docSec {
- font-family: Arial,Helvetica; font-weight: normal; 
- color: #333333; font-size: 9pt;
-}
-.docVersion {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: 10pt;
-}
-
-.docSecs-on {
- font-family: Arial,Helvetica; font-weight: normal; text-decoration: none;
- color: #ff0000; font-size: 10pt;
-}
-.docSecs-off {
- font-family: Arial,Helvetica; font-weight: normal; text-decoration: none;
- color: #333333; font-size: 10pt;
-}
-
-h2 {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: medium;
-}
-h1 {
- font-family: Verdana,Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: large;
-}
-
-DL {
- font-family: Arial,Helvetica; font-weight: normal; text-decoration: none;
- color: #333333; font-size: 10pt;
-}
-
-UL > LI > A {
- font-family: Arial,Helvetica; font-weight: bold;
- color: #336699; font-size: 10pt;
-}
-
-.moduleInfo {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #333333; font-size: 11pt;
-}
-
-.moduleInfoSec {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none;
- color: #336699; font-size: 10pt;
-}
-
-.moduleInfoVal {
- font-family: Arial,Helvetica; font-weight: normal; text-decoration: underline;
- color: #000000; font-size: 10pt;
-}
-
-.cpanNavTitle {
- font-family: Arial,Helvetica; font-weight: bold; 
- color: #ffffff; font-size: 10pt;
-}
-.cpanNavLetter {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none; 
- color: #333333; font-size: 9pt;
-}
-.cpanCat {
- font-family: Arial,Helvetica; font-weight: bold; text-decoration: none; 
- color: #336699; font-size: 9pt;
-}
-
-.bttndrkblue-bkgd-top {
-	background-color: #225688;
-	background-image: url(/global/mvc_objects/images/bttndrkblue_bgtop.gif);
-}
-.bttndrkblue-bkgd-left {
-	background-color: #225688;
-	background-image: url(/global/mvc_objects/images/bttndrkblue_bgleft.gif);
-}
-.bttndrkblue-bkgd {
-	padding-top: 0px;
-	padding-bottom: 0px;
-	margin-bottom: 0px;
-	margin-top: 0px;
-	background-repeat: no-repeat;
-	background-color: #225688;
-	background-image: url(/global/mvc_objects/images/bttndrkblue_bgmiddle.gif);
-	vertical-align: top;
-}
-.bttndrkblue-bkgd-right {
-	background-color: #225688;
-	background-image: url(/global/mvc_objects/images/bttndrkblue_bgright.gif);
-}
-.bttndrkblue-bkgd-bottom {
-	background-color: #225688;
-	background-image: url(/global/mvc_objects/images/bttndrkblue_bgbottom.gif);
-}
-.bttndrkblue-text a {
-	color: #ffffff;
-	text-decoration: none;
-}
-a.bttndrkblue-text:hover {
-	color: #ffDD3C;
-	text-decoration: none;
-}
-.bg-ltblue {
-	background-color: #f0f5fa;
-} 
-
-.border-left-b {
-	background: #f0f5fa url(/i/corner-leftline.gif) repeat-y;
-} 
-
-.border-right-b {
-	background: #f0f5fa url(/i/corner-rightline.gif) repeat-y;
-} 
-
-.border-top-b {
-	background: #f0f5fa url(/i/corner-topline.gif) repeat-x;
-} 
-
-.border-bottom-b {
-	background: #f0f5fa url(/i/corner-botline.gif) repeat-x;
-} 
-
-.border-right-w {
-	background: #ffffff url(/i/corner-rightline.gif) repeat-y;
-} 
-
-.border-top-w {
-	background: #ffffff url(/i/corner-topline.gif) repeat-x;
-} 
-
-.border-bottom-w {
-	background: #ffffff url(/i/corner-botline.gif) repeat-x;
-} 
-
-.bg-white {
-	background-color: #ffffff;
-} 
-
-.border-left-w {
-	background: #ffffff url(/i/corner-leftline.gif) repeat-y;
-} 
diff --git a/emscripten-version.txt b/emscripten-version.txt
index 8b9623a5e15..73135129478 100644
--- a/emscripten-version.txt
+++ b/emscripten-version.txt
@@ -1,2 +1,2 @@
-1.35.1
+1.35.2
 
diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp
index 39443f597fa..9f8f6e3f050 100644
--- a/examples/PrintFunctionNames/PrintFunctionNames.cpp
+++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp
@@ -15,14 +15,23 @@
 #include "clang/Frontend/FrontendPluginRegistry.h"
 #include "clang/AST/AST.h"
 #include "clang/AST/ASTConsumer.h"
+#include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "clang/Sema/Sema.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
 namespace {
 
 class PrintFunctionsConsumer : public ASTConsumer {
+  CompilerInstance &Instance;
+  std::set ParsedTemplates;
+
 public:
+  PrintFunctionsConsumer(CompilerInstance &Instance,
+                         std::set ParsedTemplates)
+      : Instance(Instance), ParsedTemplates(ParsedTemplates) {}
+
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
       const Decl *D = *i;
@@ -32,13 +41,47 @@ class PrintFunctionsConsumer : public ASTConsumer {
 
     return true;
   }
+
+  void HandleTranslationUnit(ASTContext& context) override {
+    if (!Instance.getLangOpts().DelayedTemplateParsing)
+      return;
+
+    // This demonstrates how to force instantiation of some templates in
+    // -fdelayed-template-parsing mode. (Note: Doing this unconditionally for
+    // all templates is similar to not using -fdelayed-template-parsig in the
+    // first place.)
+    // The advantage of doing this in HandleTranslationUnit() is that all
+    // codegen (when using -add-plugin) is completely finished and this can't
+    // affect the compiler output.
+    struct Visitor : public RecursiveASTVisitor {
+      const std::set &ParsedTemplates;
+      Visitor(const std::set &ParsedTemplates)
+          : ParsedTemplates(ParsedTemplates) {}
+      bool VisitFunctionDecl(FunctionDecl *FD) {
+        if (FD->isLateTemplateParsed() &&
+            ParsedTemplates.count(FD->getNameAsString()))
+          LateParsedDecls.insert(FD);
+        return true;
+      }
+
+      std::set LateParsedDecls;
+    } v(ParsedTemplates);
+    v.TraverseDecl(context.getTranslationUnitDecl());
+    clang::Sema &sema = Instance.getSema();
+    for (const FunctionDecl *FD : v.LateParsedDecls) {
+      clang::LateParsedTemplate* LPT = sema.LateParsedTemplateMap.lookup(FD);
+      sema.LateTemplateParser(sema.OpaqueParser, *LPT);
+      llvm::errs() << "late-parsed-decl: \"" << FD->getNameAsString() << "\"\n";
+    }   
+  }
 };
 
 class PrintFunctionNamesAction : public PluginASTAction {
+  std::set ParsedTemplates;
 protected:
   std::unique_ptr CreateASTConsumer(CompilerInstance &CI,
                                                  llvm::StringRef) override {
-    return llvm::make_unique();
+    return llvm::make_unique(CI, ParsedTemplates);
   }
 
   bool ParseArgs(const CompilerInstance &CI,
@@ -47,12 +90,20 @@ class PrintFunctionNamesAction : public PluginASTAction {
       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
 
       // Example error handling.
+      DiagnosticsEngine &D = CI.getDiagnostics();
       if (args[i] == "-an-error") {
-        DiagnosticsEngine &D = CI.getDiagnostics();
         unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error,
                                             "invalid argument '%0'");
         D.Report(DiagID) << args[i];
         return false;
+      } else if (args[i] == "-parse-template") {
+        if (i + 1 >= e) {
+          D.Report(D.getCustomDiagID(DiagnosticsEngine::Error,
+                                     "missing -parse-template argument"));
+          return false;
+        }
+        ++i;
+        ParsedTemplates.insert(args[i]);
       }
     }
     if (!args.empty() && args[0] == "help")
diff --git a/examples/analyzer-plugin/MainCallChecker.cpp b/examples/analyzer-plugin/MainCallChecker.cpp
index 2ad8c8416a5..a6f69fd175b 100644
--- a/examples/analyzer-plugin/MainCallChecker.cpp
+++ b/examples/analyzer-plugin/MainCallChecker.cpp
@@ -37,9 +37,10 @@ void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const
     if (!BT)
       BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
 
-    BugReport *report = new BugReport(*BT, BT->getName(), N);
+    std::unique_ptr report =
+        llvm::make_unique(*BT, BT->getName(), N);
     report->addRange(Callee->getSourceRange());
-    C.emitReport(report);
+    C.emitReport(std::move(report));
   }
 }
 
diff --git a/include/clang-c/BuildSystem.h b/include/clang-c/BuildSystem.h
index 7aa01914cf2..8d323a4e6ca 100644
--- a/include/clang-c/BuildSystem.h
+++ b/include/clang-c/BuildSystem.h
@@ -73,7 +73,7 @@ clang_VirtualFileOverlay_setCaseSensitivity(CXVirtualFileOverlay,
  *
  * \param options is reserved, always pass 0.
  * \param out_buffer_ptr pointer to receive the buffer pointer, which should be
- * disposed using \c free().
+ * disposed using \c clang_free().
  * \param out_buffer_size pointer to receive the buffer size.
  * \returns 0 for success, non-zero to indicate an error.
  */
@@ -82,6 +82,14 @@ clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay, unsigned options,
                                        char **out_buffer_ptr,
                                        unsigned *out_buffer_size);
 
+/**
+ * \brief free memory allocated by libclang, such as the buffer returned by
+ * \c CXVirtualFileOverlay() or \c clang_ModuleMapDescriptor_writeToBuffer().
+ *
+ * \param buffer memory pointer to free.
+ */
+CINDEX_LINKAGE void clang_free(void *buffer);
+
 /**
  * \brief Dispose a \c CXVirtualFileOverlay object.
  */
@@ -122,7 +130,7 @@ clang_ModuleMapDescriptor_setUmbrellaHeader(CXModuleMapDescriptor,
  *
  * \param options is reserved, always pass 0.
  * \param out_buffer_ptr pointer to receive the buffer pointer, which should be
- * disposed using \c free().
+ * disposed using \c clang_free().
  * \param out_buffer_size pointer to receive the buffer size.
  * \returns 0 for success, non-zero to indicate an error.
  */
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 693eef2a798..2794965d5b9 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -2225,7 +2225,23 @@ enum CXCursorKind {
    */
   CXCursor_OMPTeamsDirective             = 253,
 
-  CXCursor_LastStmt                      = CXCursor_OMPTeamsDirective,
+  /** \brief OpenMP taskgroup directive.
+   */
+  CXCursor_OMPTaskgroupDirective         = 254,
+
+  /** \brief OpenMP cancellation point directive.
+   */
+  CXCursor_OMPCancellationPointDirective = 255,
+
+  /** \brief OpenMP cancel directive.
+   */
+  CXCursor_OMPCancelDirective            = 256,
+
+  /** \brief OpenMP target data directive.
+   */
+  CXCursor_OMPTargetDataDirective        = 257,
+
+  CXCursor_LastStmt                      = CXCursor_OMPTargetDataDirective,
 
   /**
    * \brief Cursor that represents the translation unit itself.
@@ -5628,7 +5644,7 @@ typedef enum {
  * reused after indexing is finished. Set to \c NULL if you do not require it.
  *
  * \returns 0 on success or if there were errors from which the compiler could
- * recover.  If there is a failure from which the there is no recovery, returns
+ * recover.  If there is a failure from which there is no recovery, returns
  * a non-zero \c CXErrorCode.
  *
  * The rest of the parameters are the same as #clang_parseTranslationUnit.
@@ -5659,7 +5675,7 @@ CINDEX_LINKAGE int clang_indexSourceFile(CXIndexAction,
  *
  * The parameters are the same as #clang_indexSourceFile.
  * 
- * \returns If there is a failure from which the there is no recovery, returns
+ * \returns If there is a failure from which there is no recovery, returns
  * non-zero, otherwise returns 0.
  */
 CINDEX_LINKAGE int clang_indexTranslationUnit(CXIndexAction,
@@ -5736,9 +5752,6 @@ CINDEX_LINKAGE unsigned clang_Type_visitFields(CXType T,
  * @}
  */
 
-/* Include the comment API for compatibility. This will eventually go away. */
-#include "clang-c/Documentation.h"
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/clang/ARCMigrate/ARCMT.h b/include/clang/ARCMigrate/ARCMT.h
index ad4f23c604b..74081867eeb 100644
--- a/include/clang/ARCMigrate/ARCMT.h
+++ b/include/clang/ARCMigrate/ARCMT.h
@@ -17,6 +17,7 @@
 namespace clang {
   class ASTContext;
   class DiagnosticConsumer;
+  class PCHContainerOperations;
 
 namespace arcmt {
   class MigrationPass;
@@ -37,19 +38,22 @@ namespace arcmt {
 /// the pre-migration ARC diagnostics.
 ///
 /// \returns false if no error is produced, true otherwise.
-bool checkForManualIssues(CompilerInvocation &CI,
-                          const FrontendInputFile &Input,
-                          DiagnosticConsumer *DiagClient,
-                          bool emitPremigrationARCErrors = false,
-                          StringRef plistOut = StringRef());
+bool
+checkForManualIssues(CompilerInvocation &CI, const FrontendInputFile &Input,
+                     std::shared_ptr PCHContainerOps,
+                     DiagnosticConsumer *DiagClient,
+                     bool emitPremigrationARCErrors = false,
+                     StringRef plistOut = StringRef());
 
 /// \brief Works similar to checkForManualIssues but instead of checking, it
 /// applies automatic modifications to source files to conform to ARC.
 ///
 /// \returns false if no error is produced, true otherwise.
-bool applyTransformations(CompilerInvocation &origCI,
-                          const FrontendInputFile &Input,
-                          DiagnosticConsumer *DiagClient);
+bool
+applyTransformations(CompilerInvocation &origCI,
+                     const FrontendInputFile &Input,
+                     std::shared_ptr PCHContainerOps,
+                     DiagnosticConsumer *DiagClient);
 
 /// \brief Applies automatic modifications and produces temporary files
 /// and metadata into the \p outputDir path.
@@ -62,12 +66,11 @@ bool applyTransformations(CompilerInvocation &origCI,
 /// the pre-migration ARC diagnostics.
 ///
 /// \returns false if no error is produced, true otherwise.
-bool migrateWithTemporaryFiles(CompilerInvocation &origCI,
-                               const FrontendInputFile &Input,
-                               DiagnosticConsumer *DiagClient,
-                               StringRef outputDir,
-                               bool emitPremigrationARCErrors,
-                               StringRef plistOut);
+bool migrateWithTemporaryFiles(
+    CompilerInvocation &origCI, const FrontendInputFile &Input,
+    std::shared_ptr PCHContainerOps,
+    DiagnosticConsumer *DiagClient, StringRef outputDir,
+    bool emitPremigrationARCErrors, StringRef plistOut);
 
 /// \brief Get the set of file remappings from the \p outputDir path that
 /// migrateWithTemporaryFiles produced.
@@ -93,13 +96,16 @@ std::vector getAllTransformations(LangOptions::GCMode OrigGCMode,
 
 class MigrationProcess {
   CompilerInvocation OrigCI;
+  std::shared_ptr PCHContainerOps;
   DiagnosticConsumer *DiagClient;
   FileRemapper Remapper;
 
 public:
   bool HadARCErrors;
 
-  MigrationProcess(const CompilerInvocation &CI, DiagnosticConsumer *diagClient,
+  MigrationProcess(const CompilerInvocation &CI,
+                   std::shared_ptr PCHContainerOps,
+                   DiagnosticConsumer *diagClient,
                    StringRef outputDir = StringRef());
 
   class RewriteListener {
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index 48e3451f257..c67fa67ab6b 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -236,6 +236,12 @@ class ASTContext : public RefCountedBase {
   QualType ObjCClassRedefinitionType;
   QualType ObjCSelRedefinitionType;
 
+  /// The identifier 'NSObject'.
+  IdentifierInfo *NSObjectName = nullptr;
+
+  /// The identifier 'NSCopying'.
+  IdentifierInfo *NSCopyingName = nullptr;
+
   QualType ObjCConstantStringType;
   mutable RecordDecl *CFConstantStringTypeDecl;
   
@@ -284,6 +290,11 @@ class ASTContext : public RefCountedBase {
   /// merged into.
   llvm::DenseMap MergedDecls;
 
+  /// \brief A mapping from a defining declaration to a list of modules (other
+  /// than the owning module of the declaration) that contain merged
+  /// definitions of that entity.
+  llvm::DenseMap> MergedDefModules;
+
 public:
   /// \brief A type synonym for the TemplateOrInstantiation mapping.
   typedef llvm::PointerUnion
@@ -781,6 +792,23 @@ class ASTContext : public RefCountedBase {
     MergedDecls[D] = Primary;
   }
 
+  /// \brief Note that the definition \p ND has been merged into module \p M,
+  /// and should be visible whenever \p M is visible.
+  void mergeDefinitionIntoModule(NamedDecl *ND, Module *M,
+                                 bool NotifyListeners = true);
+  /// \brief Clean up the merged definition list. Call this if you might have
+  /// added duplicates into the list.
+  void deduplicateMergedDefinitonsFor(NamedDecl *ND);
+
+  /// \brief Get the additional modules in which the definition \p Def has
+  /// been merged.
+  ArrayRef getModulesWithMergedDefinition(NamedDecl *Def) {
+    auto MergedIt = MergedDefModules.find(Def);
+    if (MergedIt == MergedDefModules.end())
+      return None;
+    return MergedIt->second;
+  }
+
   TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
 
   ExternCContextDecl *getExternCContextDecl() const;
@@ -815,9 +843,9 @@ class ASTContext : public RefCountedBase {
   mutable QualType AutoDeductTy;     // Deduction against 'auto'.
   mutable QualType AutoRRefDeductTy; // Deduction against 'auto &&'.
 
-  // Type used to help define __builtin_va_list for some targets.
-  // The type is built when constructing 'BuiltinVaListDecl'.
-  mutable QualType VaListTagTy;
+  // Decl used to help define __builtin_va_list for some targets.
+  // The decl is built when constructing 'BuiltinVaListDecl'.
+  mutable Decl *VaListTagDecl;
 
   ASTContext(LangOptions &LOpts, SourceManager &SM, IdentifierTable &idents,
              SelectorTable &sels, Builtin::Context &builtins);
@@ -1167,9 +1195,15 @@ class ASTContext : public RefCountedBase {
   QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
                                 ObjCInterfaceDecl *PrevDecl = nullptr) const;
 
+  /// Legacy interface: cannot provide type arguments or __kindof.
   QualType getObjCObjectType(QualType Base,
                              ObjCProtocolDecl * const *Protocols,
                              unsigned NumProtocols) const;
+
+  QualType getObjCObjectType(QualType Base,
+                             ArrayRef typeArgs,
+                             ArrayRef protocols,
+                             bool isKindOf) const;
   
   bool ObjCObjectAdoptsQTypeProtocols(QualType QT, ObjCInterfaceDecl *Decl);
   /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in
@@ -1329,6 +1363,24 @@ class ASTContext : public RefCountedBase {
     ObjCSelRedefinitionType = RedefType;
   }
 
+  /// Retrieve the identifier 'NSObject'.
+  IdentifierInfo *getNSObjectName() {
+    if (!NSObjectName) {
+      NSObjectName = &Idents.get("NSObject");
+    }
+
+    return NSObjectName;
+  }
+
+  /// Retrieve the identifier 'NSCopying'.
+  IdentifierInfo *getNSCopyingName() {
+    if (!NSCopyingName) {
+      NSCopyingName = &Idents.get("NSCopying");
+    }
+
+    return NSCopyingName;
+  }
+
   /// \brief Retrieve the Objective-C "instancetype" type, if already known;
   /// otherwise, returns a NULL type;
   QualType getObjCInstanceType() {
@@ -1517,7 +1569,7 @@ class ASTContext : public RefCountedBase {
   /// \brief Retrieve the C type declaration corresponding to the predefined
   /// \c __va_list_tag type used to help define the \c __builtin_va_list type
   /// for some targets.
-  QualType getVaListTagType() const;
+  Decl *getVaListTagDecl() const;
 
   /// \brief Return a type with additional \c const, \c volatile, or
   /// \c restrict qualifiers.
@@ -1642,6 +1694,9 @@ class ASTContext : public RefCountedBase {
   TypeInfo getTypeInfo(const Type *T) const;
   TypeInfo getTypeInfo(QualType T) const { return getTypeInfo(T.getTypePtr()); }
 
+  /// \brief Get default simd alignment of the specified complete type in bits.
+  unsigned getOpenMPDefaultSimdAlign(QualType T) const;
+
   /// \brief Return the size of the specified (complete) type \p T, in bits.
   uint64_t getTypeSize(QualType T) const { return getTypeInfo(T).Width; }
   uint64_t getTypeSize(const Type *T) const { return getTypeInfo(T).Width; }
@@ -1691,6 +1746,10 @@ class ASTContext : public RefCountedBase {
   /// beneficial for performance to overalign a data type.
   unsigned getPreferredTypeAlign(const Type *T) const;
 
+  /// \brief Return the default alignment for __attribute__((aligned)) on
+  /// this target, to be used if no alignment value is specified.
+  unsigned getTargetDefaultAlignForAttributeAligned(void) const;
+
   /// \brief Return the alignment in bits that should be given to a
   /// global variable with type \p T.
   unsigned getAlignOfGlobalVar(QualType T) const;
@@ -1715,7 +1774,6 @@ class ASTContext : public RefCountedBase {
   /// record (struct/union/class) \p D, which indicates its size and field
   /// position information.
   const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const;
-  const ASTRecordLayout *BuildMicrosoftASTRecordLayout(const RecordDecl *D) const;
 
   /// \brief Get or compute information about the layout of the specified
   /// Objective-C interface.
@@ -1754,6 +1812,17 @@ class ASTContext : public RefCountedBase {
   /// \param method should be the declaration from the class definition
   void setNonKeyFunction(const CXXMethodDecl *method);
 
+  /// Loading virtual member pointers using the virtual inheritance model
+  /// always results in an adjustment using the vbtable even if the index is
+  /// zero.
+  ///
+  /// This is usually OK because the first slot in the vbtable points
+  /// backwards to the top of the MDC.  However, the MDC might be reusing a
+  /// vbptr from an nv-base.  In this case, the first slot in the vbtable
+  /// points to the start of the nv-base which introduced the vbptr and *not*
+  /// the MDC.  Modify the NonVirtualBaseAdjustment to account for this.
+  CharUnits getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const;
+
   /// Get the offset of a FieldDecl or IndirectFieldDecl, in bits.
   uint64_t getFieldOffset(const ValueDecl *FD) const;
 
@@ -1828,6 +1897,36 @@ class ASTContext : public RefCountedBase {
            getCanonicalType(T2).getTypePtr();
   }
 
+  bool hasSameNullabilityTypeQualifier(QualType SubT, QualType SuperT,
+                                       bool IsParam) const {
+    auto SubTnullability = SubT->getNullability(*this);
+    auto SuperTnullability = SuperT->getNullability(*this);
+    if (SubTnullability.hasValue() == SuperTnullability.hasValue()) {
+      // Neither has nullability; return true
+      if (!SubTnullability)
+        return true;
+      // Both have nullability qualifier.
+      if (*SubTnullability == *SuperTnullability ||
+          *SubTnullability == NullabilityKind::Unspecified ||
+          *SuperTnullability == NullabilityKind::Unspecified)
+        return true;
+      
+      if (IsParam) {
+        // Ok for the superclass method parameter to be "nonnull" and the subclass
+        // method parameter to be "nullable"
+        return (*SuperTnullability == NullabilityKind::NonNull &&
+                *SubTnullability == NullabilityKind::Nullable);
+      }
+      else {
+        // For the return type, it's okay for the superclass method to specify
+        // "nullable" and the subclass method specify "nonnull"
+        return (*SuperTnullability == NullabilityKind::Nullable &&
+                *SubTnullability == NullabilityKind::NonNull);
+      }
+    }
+    return true;
+  }
+
   bool ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl,
                            const ObjCMethodDecl *MethodImp);
   
diff --git a/include/clang/AST/ASTImporter.h b/include/clang/AST/ASTImporter.h
index a335f980e6c..ee48955ca63 100644
--- a/include/clang/AST/ASTImporter.h
+++ b/include/clang/AST/ASTImporter.h
@@ -121,6 +121,11 @@ namespace clang {
     /// if an error occurred.
     Decl *Import(Decl *FromD);
 
+    /// \brief Return the copy of the given declaration in the "to" context if
+    /// it has already been imported from the "from" context.  Otherwise return
+    /// NULL.
+    Decl *GetAlreadyImportedOrNull(Decl *FromD);
+
     /// \brief Import the given declaration context from the "from"
     /// AST context into the "to" AST context.
     ///
diff --git a/include/clang/AST/ASTMutationListener.h b/include/clang/AST/ASTMutationListener.h
index d2b0a8b0c7e..f4026e95264 100644
--- a/include/clang/AST/ASTMutationListener.h
+++ b/include/clang/AST/ASTMutationListener.h
@@ -13,9 +13,8 @@
 #ifndef LLVM_CLANG_AST_ASTMUTATIONLISTENER_H
 #define LLVM_CLANG_AST_ASTMUTATIONLISTENER_H
 
-#include "clang/Basic/SourceLocation.h"
-
 namespace clang {
+  class Attr;
   class ClassTemplateDecl;
   class ClassTemplateSpecializationDecl;
   class CXXDestructorDecl;
@@ -24,12 +23,14 @@ namespace clang {
   class DeclContext;
   class FunctionDecl;
   class FunctionTemplateDecl;
+  class Module;
   class NamedDecl;
   class ObjCCategoryDecl;
   class ObjCContainerDecl;
   class ObjCInterfaceDecl;
   class ObjCPropertyDecl;
   class QualType;
+  class RecordDecl;
   class TagDecl;
   class VarDecl;
   class VarTemplateDecl;
@@ -117,8 +118,17 @@ class ASTMutationListener {
   /// \brief A definition has been made visible by being redefined locally.
   ///
   /// \param D The definition that was previously not visible.
-  virtual void RedefinedHiddenDefinition(const NamedDecl *D,
-                                         SourceLocation Loc) {}
+  /// \param M The containing module in which the definition was made visible,
+  ///        if any.
+  virtual void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {}
+  
+  /// \brief An attribute was added to a RecordDecl
+  ///
+  /// \param Attr The attribute that was added to the Record
+  ///
+  /// \param Record The RecordDecl that got a new attribute
+  virtual void AddedAttributeToRecord(const Attr *Attr, 
+                                      const RecordDecl *Record) {}
 
   // NOTE: If new methods are added they should also be added to
   // MultiplexASTMutationListener.
diff --git a/include/clang/AST/Attr.h b/include/clang/AST/Attr.h
index a854168f01d..4e282d68b74 100644
--- a/include/clang/AST/Attr.h
+++ b/include/clang/AST/Attr.h
@@ -20,6 +20,7 @@
 #include "clang/AST/Type.h"
 #include "clang/Basic/AttrKinds.h"
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/Sanitizers.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/VersionTuple.h"
 #include "llvm/ADT/SmallVector.h"
diff --git a/include/clang/AST/CXXInheritance.h b/include/clang/AST/CXXInheritance.h
index 37f6748ace9..008efb36477 100644
--- a/include/clang/AST/CXXInheritance.h
+++ b/include/clang/AST/CXXInheritance.h
@@ -162,10 +162,9 @@ class CXXBasePaths {
   
   void ComputeDeclsFound();
 
-  bool lookupInBases(ASTContext &Context, 
-                     const CXXRecordDecl *Record,
-                     CXXRecordDecl::BaseMatchesCallback *BaseMatches, 
-                     void *UserData);
+  bool lookupInBases(ASTContext &Context, const CXXRecordDecl *Record,
+                     CXXRecordDecl::BaseMatchesCallback BaseMatches);
+
 public:
   typedef std::list::iterator paths_iterator;
   typedef std::list::const_iterator const_paths_iterator;
@@ -333,12 +332,12 @@ class OverridingMethods {
 ///   struct D : B, C { };
 /// \endcode
 ///
-/// This data structure contaings a mapping from every virtual
+/// This data structure contains a mapping from every virtual
 /// function *that does not override an existing virtual function* and
 /// in every subobject where that virtual function occurs to the set
 /// of virtual functions that override it. Thus, the same virtual
 /// function \c A::f can actually occur in multiple subobjects of type
-/// \c A due to multiple inheritance, and may be overriden by
+/// \c A due to multiple inheritance, and may be overridden by
 /// different virtual functions in each, as in the following example:
 ///
 /// \code
@@ -354,7 +353,7 @@ class OverridingMethods {
 /// \c A::f but in *different* subobjects of type A. This is
 /// represented by numbering the subobjects in which the overridden
 /// and the overriding virtual member functions are located. Subobject
-/// 0 represents the virtua base class subobject of that type, while
+/// 0 represents the virtual base class subobject of that type, while
 /// subobject numbers greater than 0 refer to non-virtual base class
 /// subobjects of that type.
 class CXXFinalOverriderMap
diff --git a/include/clang/AST/CommentParser.h b/include/clang/AST/CommentParser.h
index 42bf4c989a2..fa8862899c1 100644
--- a/include/clang/AST/CommentParser.h
+++ b/include/clang/AST/CommentParser.h
@@ -75,11 +75,7 @@ class Parser {
       return;
 
     MoreLATokens.push_back(Tok);
-    for (const Token *I = &Toks.back(),
-         *B = &Toks.front();
-         I != B; --I) {
-      MoreLATokens.push_back(*I);
-    }
+    MoreLATokens.append(Toks.rbegin(), std::prev(Toks.rend()));
 
     Tok = Toks[0];
   }
diff --git a/include/clang/AST/DataRecursiveASTVisitor.h b/include/clang/AST/DataRecursiveASTVisitor.h
index d3dde6803fb..c1e98e757e1 100644
--- a/include/clang/AST/DataRecursiveASTVisitor.h
+++ b/include/clang/AST/DataRecursiveASTVisitor.h
@@ -531,10 +531,7 @@ bool RecursiveASTVisitor::TraverseStmt(Stmt *S) {
       }
     }
 
-    for (SmallVectorImpl::reverse_iterator RI = StmtsToEnqueue.rbegin(),
-                                                   RE = StmtsToEnqueue.rend();
-         RI != RE; ++RI)
-      Queue.push_back(*RI);
+    Queue.append(StmtsToEnqueue.rbegin(), StmtsToEnqueue.rend());
   }
 
   return true;
@@ -791,7 +788,7 @@ template 
 bool
 RecursiveASTVisitor::TraverseLambdaCapture(LambdaExpr *LE,
                                                     const LambdaCapture *C) {
-  if (C->isInitCapture())
+  if (LE->isInitCapture(C))
     TRY_TO(TraverseDecl(C->getCapturedVar()));
   return true;
 }
@@ -943,6 +940,9 @@ DEF_TRAVERSE_TYPE(ObjCObjectType, {
   // type is itself.
   if (T->getBaseType().getTypePtr() != T)
     TRY_TO(TraverseType(T->getBaseType()));
+  for (auto typeArg : T->getTypeArgsAsWritten()) {
+    TRY_TO(TraverseType(typeArg));
+  }
 })
 
 DEF_TRAVERSE_TYPE(ObjCObjectPointerType,
@@ -1169,6 +1169,8 @@ DEF_TRAVERSE_TYPELOC(ObjCObjectType, {
   // type is itself.
   if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr())
     TRY_TO(TraverseTypeLoc(TL.getBaseLoc()));
+  for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i)
+    TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc()));
 })
 
 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType,
@@ -1310,7 +1312,13 @@ DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement
                                            })
 
 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement
-                                    })
+  if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) {
+    for (auto typeParam : *typeParamList) {
+      TRY_TO(TraverseObjCTypeParamDecl(typeParam));
+    }
+  }
+  return true;
+})
 
 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement
                                         })
@@ -1319,7 +1327,16 @@ DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement
                                           })
 
 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement
-                                     })
+  if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) {
+    for (auto typeParam : *typeParamList) {
+      TRY_TO(TraverseObjCTypeParamDecl(typeParam));
+    }
+  }
+
+  if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) {
+    TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc()));
+  }
+})
 
 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement
                                     })
@@ -1338,6 +1355,15 @@ DEF_TRAVERSE_DECL(ObjCMethodDecl, {
   return true;
 })
 
+DEF_TRAVERSE_DECL(ObjCTypeParamDecl, {
+  if (D->hasExplicitBound()) {
+    TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
+    // We shouldn't traverse D->getTypeForDecl(); it's a result of
+    // declaring the type alias, not something that was written in the
+    // source.
+  }
+})
+
 DEF_TRAVERSE_DECL(ObjCPropertyDecl, {
   if (D->getTypeSourceInfo())
     TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc()));
@@ -1864,8 +1890,8 @@ DEF_TRAVERSE_DECL(ParmVarDecl, {
     TRY_TO(WalkUpFrom##STMT(S));                                               \
     StmtQueueAction StmtQueue(*this);                                          \
     { CODE; }                                                                  \
-    for (Stmt::child_range range = S->children(); range; ++range) {            \
-      StmtQueue.queue(*range);                                                 \
+    for (Stmt *SubStmt : S->children()) {                                      \
+      StmtQueue.queue(SubStmt);                                                \
     }                                                                          \
     return true;                                                               \
   }
@@ -2014,8 +2040,8 @@ bool RecursiveASTVisitor::TraverseInitListExpr(InitListExpr *S) {
   TRY_TO(WalkUpFromInitListExpr(S));
   StmtQueueAction StmtQueue(*this);
   // All we need are the default actions.  FIXME: use a helper function.
-  for (Stmt::child_range range = S->children(); range; ++range) {
-    StmtQueue.queue(*range);
+  for (Stmt *SubStmt : S->children()) {
+    StmtQueue.queue(SubStmt);
   }
   return true;
 }
@@ -2204,9 +2230,11 @@ DEF_TRAVERSE_STMT(CXXThisExpr, {})
 DEF_TRAVERSE_STMT(CXXThrowExpr, {})
 DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
 DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
+DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
 DEF_TRAVERSE_STMT(GNUNullExpr, {})
 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
+DEF_TRAVERSE_STMT(NoInitExpr, {})
 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {})
 DEF_TRAVERSE_STMT(ObjCEncodeExpr, {
   if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo())
@@ -2356,6 +2384,15 @@ DEF_TRAVERSE_STMT(OMPBarrierDirective,
 DEF_TRAVERSE_STMT(OMPTaskwaitDirective,
                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(OMPTaskgroupDirective,
+                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
+
+DEF_TRAVERSE_STMT(OMPCancellationPointDirective,
+                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
+
+DEF_TRAVERSE_STMT(OMPCancelDirective,
+                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
+
 DEF_TRAVERSE_STMT(OMPFlushDirective,
                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
@@ -2368,6 +2405,9 @@ DEF_TRAVERSE_STMT(OMPAtomicDirective,
 DEF_TRAVERSE_STMT(OMPTargetDirective,
                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
+DEF_TRAVERSE_STMT(OMPTargetDataDirective,
+                  { TRY_TO(TraverseOMPExecutableDirective(S)); })
+
 DEF_TRAVERSE_STMT(OMPTeamsDirective,
                   { TRY_TO(TraverseOMPExecutableDirective(S)); })
 
@@ -2435,11 +2475,13 @@ template 
 bool
 RecursiveASTVisitor::VisitOMPScheduleClause(OMPScheduleClause *C) {
   TRY_TO(TraverseStmt(C->getChunkSize()));
+  TRY_TO(TraverseStmt(C->getHelperChunkSize()));
   return true;
 }
 
 template 
-bool RecursiveASTVisitor::VisitOMPOrderedClause(OMPOrderedClause *) {
+bool RecursiveASTVisitor::VisitOMPOrderedClause(OMPOrderedClause *C) {
+  TRY_TO(TraverseStmt(C->getNumForLoops()));
   return true;
 }
 
@@ -2619,6 +2661,12 @@ bool RecursiveASTVisitor::VisitOMPFlushClause(OMPFlushClause *C) {
   return true;
 }
 
+template 
+bool RecursiveASTVisitor::VisitOMPDependClause(OMPDependClause *C) {
+  TRY_TO(VisitOMPClauseList(C));
+  return true;
+}
+
 // FIXME: look at the following tricky-seeming exprs to see if we
 // need to recurse on anything.  These are ones that have methods
 // returning decls or qualtypes or nestednamespecifier -- though I'm
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index f8e17268e6c..e06b58b37be 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -39,6 +39,7 @@ class LabelStmt;
 class MemberSpecializationInfo;
 class Module;
 class NestedNameSpecifier;
+class ParmVarDecl;
 class Stmt;
 class StringLiteral;
 class TemplateArgumentList;
@@ -82,10 +83,7 @@ class TranslationUnitDecl : public Decl, public DeclContext {
   /// translation unit, if one has been created.
   NamespaceDecl *AnonymousNamespace;
 
-  explicit TranslationUnitDecl(ASTContext &ctx)
-    : Decl(TranslationUnit, nullptr, SourceLocation()),
-      DeclContext(TranslationUnit),
-      Ctx(ctx), AnonymousNamespace(nullptr) {}
+  explicit TranslationUnitDecl(ASTContext &ctx);
 public:
   ASTContext &getASTContext() const { return Ctx; }
 
@@ -238,7 +236,11 @@ class NamedDecl : public Decl {
   bool isHidden() const { return Hidden; }
 
   /// \brief Set whether this declaration is hidden from name lookup.
-  void setHidden(bool Hide) { Hidden = Hide; }
+  void setHidden(bool Hide) {
+    assert((!Hide || isFromASTFile() || hasLocalOwningModuleStorage()) &&
+           "declaration with no owning module can't be hidden");
+    Hidden = Hide;
+  }
 
   /// \brief Determine whether this declaration is a C++ class member.
   bool isCXXClassMember() const {
@@ -750,37 +752,8 @@ class VarDecl : public DeclaratorDecl, public Redeclarable {
     unsigned SClass : 3;
     unsigned TSCSpec : 2;
     unsigned InitStyle : 2;
-
-    /// \brief Whether this variable is the exception variable in a C++ catch
-    /// or an Objective-C @catch statement.
-    unsigned ExceptionVar : 1;
-
-    /// \brief Whether this local variable could be allocated in the return
-    /// slot of its function, enabling the named return value optimization
-    /// (NRVO).
-    unsigned NRVOVariable : 1;
-
-    /// \brief Whether this variable is the for-range-declaration in a C++0x
-    /// for-range statement.
-    unsigned CXXForRangeDecl : 1;
-
-    /// \brief Whether this variable is an ARC pseudo-__strong
-    /// variable;  see isARCPseudoStrong() for details.
-    unsigned ARCPseudoStrong : 1;
-
-    /// \brief Whether this variable is (C++0x) constexpr.
-    unsigned IsConstexpr : 1;
-
-    /// \brief Whether this variable is the implicit variable for a lambda
-    /// init-capture.
-    unsigned IsInitCapture : 1;
-
-    /// \brief Whether this local extern variable's previous declaration was
-    /// declared in the same block scope. This controls whether we should merge
-    /// the type of this declaration with its previous declaration.
-    unsigned PreviousDeclInSameBlockScope : 1;
   };
-  enum { NumVarDeclBits = 14 };
+  enum { NumVarDeclBits = 7 };
 
   friend class ASTDeclReader;
   friend class StmtIteratorBase;
@@ -816,10 +789,47 @@ class VarDecl : public DeclaratorDecl, public Redeclarable {
     unsigned ParameterIndex : NumParameterIndexBits;
   };
 
+  class NonParmVarDeclBitfields {
+    friend class VarDecl;
+    friend class ASTDeclReader;
+
+    unsigned : NumVarDeclBits;
+
+    /// \brief Whether this variable is the exception variable in a C++ catch
+    /// or an Objective-C @catch statement.
+    unsigned ExceptionVar : 1;
+
+    /// \brief Whether this local variable could be allocated in the return
+    /// slot of its function, enabling the named return value optimization
+    /// (NRVO).
+    unsigned NRVOVariable : 1;
+
+    /// \brief Whether this variable is the for-range-declaration in a C++0x
+    /// for-range statement.
+    unsigned CXXForRangeDecl : 1;
+
+    /// \brief Whether this variable is an ARC pseudo-__strong
+    /// variable;  see isARCPseudoStrong() for details.
+    unsigned ARCPseudoStrong : 1;
+
+    /// \brief Whether this variable is (C++0x) constexpr.
+    unsigned IsConstexpr : 1;
+
+    /// \brief Whether this variable is the implicit variable for a lambda
+    /// init-capture.
+    unsigned IsInitCapture : 1;
+
+    /// \brief Whether this local extern variable's previous declaration was
+    /// declared in the same block scope. This controls whether we should merge
+    /// the type of this declaration with its previous declaration.
+    unsigned PreviousDeclInSameBlockScope : 1;
+  };
+
   union {
     unsigned AllBits;
     VarDeclBitfields VarDeclBits;
     ParmVarDeclBitfields ParmVarDeclBits;
+    NonParmVarDeclBitfields NonParmVarDeclBits;
   };
 
   VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
@@ -1172,9 +1182,12 @@ class VarDecl : public DeclaratorDecl, public Redeclarable {
   /// \brief Determine whether this variable is the exception variable in a
   /// C++ catch statememt or an Objective-C \@catch statement.
   bool isExceptionVariable() const {
-    return VarDeclBits.ExceptionVar;
+    return isa(this) ? false : NonParmVarDeclBits.ExceptionVar;
+  }
+  void setExceptionVariable(bool EV) {
+    assert(!isa(this));
+    NonParmVarDeclBits.ExceptionVar = EV;
   }
-  void setExceptionVariable(bool EV) { VarDeclBits.ExceptionVar = EV; }
 
   /// \brief Determine whether this local variable can be used with the named
   /// return value optimization (NRVO).
@@ -1186,36 +1199,64 @@ class VarDecl : public DeclaratorDecl, public Redeclarable {
   /// return slot when returning from the function. Within the function body,
   /// each return that returns the NRVO object will have this variable as its
   /// NRVO candidate.
-  bool isNRVOVariable() const { return VarDeclBits.NRVOVariable; }
-  void setNRVOVariable(bool NRVO) { VarDeclBits.NRVOVariable = NRVO; }
+  bool isNRVOVariable() const {
+    return isa(this) ? false : NonParmVarDeclBits.NRVOVariable;
+  }
+  void setNRVOVariable(bool NRVO) {
+    assert(!isa(this));
+    NonParmVarDeclBits.NRVOVariable = NRVO;
+  }
 
   /// \brief Determine whether this variable is the for-range-declaration in
   /// a C++0x for-range statement.
-  bool isCXXForRangeDecl() const { return VarDeclBits.CXXForRangeDecl; }
-  void setCXXForRangeDecl(bool FRD) { VarDeclBits.CXXForRangeDecl = FRD; }
+  bool isCXXForRangeDecl() const {
+    return isa(this) ? false : NonParmVarDeclBits.CXXForRangeDecl;
+  }
+  void setCXXForRangeDecl(bool FRD) {
+    assert(!isa(this));
+    NonParmVarDeclBits.CXXForRangeDecl = FRD;
+  }
 
   /// \brief Determine whether this variable is an ARC pseudo-__strong
   /// variable.  A pseudo-__strong variable has a __strong-qualified
   /// type but does not actually retain the object written into it.
   /// Generally such variables are also 'const' for safety.
-  bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; }
-  void setARCPseudoStrong(bool ps) { VarDeclBits.ARCPseudoStrong = ps; }
+  bool isARCPseudoStrong() const {
+    return isa(this) ? false : NonParmVarDeclBits.ARCPseudoStrong;
+  }
+  void setARCPseudoStrong(bool ps) {
+    assert(!isa(this));
+    NonParmVarDeclBits.ARCPseudoStrong = ps;
+  }
 
   /// Whether this variable is (C++11) constexpr.
-  bool isConstexpr() const { return VarDeclBits.IsConstexpr; }
-  void setConstexpr(bool IC) { VarDeclBits.IsConstexpr = IC; }
+  bool isConstexpr() const {
+    return isa(this) ? false : NonParmVarDeclBits.IsConstexpr;
+  }
+  void setConstexpr(bool IC) {
+    assert(!isa(this));
+    NonParmVarDeclBits.IsConstexpr = IC;
+  }
 
   /// Whether this variable is the implicit variable for a lambda init-capture.
-  bool isInitCapture() const { return VarDeclBits.IsInitCapture; }
-  void setInitCapture(bool IC) { VarDeclBits.IsInitCapture = IC; }
+  bool isInitCapture() const {
+    return isa(this) ? false : NonParmVarDeclBits.IsInitCapture;
+  }
+  void setInitCapture(bool IC) {
+    assert(!isa(this));
+    NonParmVarDeclBits.IsInitCapture = IC;
+  }
 
   /// Whether this local extern variable declaration's previous declaration
   /// was declared in the same block scope. Only correct in C++.
   bool isPreviousDeclInSameBlockScope() const {
-    return VarDeclBits.PreviousDeclInSameBlockScope;
+    return isa(this)
+               ? false
+               : NonParmVarDeclBits.PreviousDeclInSameBlockScope;
   }
   void setPreviousDeclInSameBlockScope(bool Same) {
-    VarDeclBits.PreviousDeclInSameBlockScope = Same;
+    assert(!isa(this));
+    NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same;
   }
 
   /// \brief If this variable is an instantiated static data member of a
@@ -1889,8 +1930,10 @@ class FunctionDecl : public DeclaratorDecl, public DeclContext,
 
   void setPreviousDeclaration(FunctionDecl * PrevDecl);
 
-  virtual const FunctionDecl *getCanonicalDecl() const;
   FunctionDecl *getCanonicalDecl() override;
+  const FunctionDecl *getCanonicalDecl() const {
+    return const_cast(this)->getCanonicalDecl();
+  }
 
   unsigned getBuiltinID() const;
 
@@ -2588,7 +2631,10 @@ class TypedefNameDecl : public TypeDecl, public Redeclarable {
 
   /// Retrieves the tag declaration for which this is the typedef name for
   /// linkage purposes, if any.
-  TagDecl *getAnonDeclWithTypedefName() const;
+  ///
+  /// \param AnyRedecl Look for the tag declaration in any redeclaration of
+  /// this typedef declaration.
+  TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const;
 
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h
index b3ecbf76d72..43d8366f0d5 100644
--- a/include/clang/AST/DeclBase.h
+++ b/include/clang/AST/DeclBase.h
@@ -70,8 +70,15 @@ namespace clang {
 /// Decl - This represents one declaration (or definition), e.g. a variable,
 /// typedef, function, struct, etc.
 ///
+/// Note: There are objects tacked on before the *beginning* of Decl
+/// (and its subclasses) in its Decl::operator new(). Proper alignment
+/// of all subclasses (not requiring more than DeclObjAlignment) is
+/// asserted in DeclBase.cpp.
 class Decl {
 public:
+  /// \brief Alignment guaranteed when allocating Decl and any subtypes.
+  enum { DeclObjAlignment = llvm::AlignOf::Alignment };
+
   /// \brief Lists the kind of concrete classes of Decl.
   enum Kind {
 #define DECL(DERIVED, BASE) DERIVED,
@@ -178,7 +185,12 @@ class Decl {
     OBJC_TQ_Out = 0x4,
     OBJC_TQ_Bycopy = 0x8,
     OBJC_TQ_Byref = 0x10,
-    OBJC_TQ_Oneway = 0x20
+    OBJC_TQ_Oneway = 0x20,
+
+    /// The nullability qualifier is set when the nullability of the
+    /// result or parameter was expressed via a context-sensitive
+    /// keyword.
+    OBJC_TQ_CSNullability = 0x40
   };
 
 protected:
@@ -317,7 +329,7 @@ class Decl {
     : NextInContextAndBits(), DeclCtx(DC),
       Loc(L), DeclKind(DK), InvalidDecl(0),
       HasAttrs(false), Implicit(false), Used(false), Referenced(false),
-      Access(AS_none), FromASTFile(0), Hidden(0),
+      Access(AS_none), FromASTFile(0), Hidden(DC && cast(DC)->Hidden),
       IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
       CacheValidAndLinkage(0)
   {
@@ -637,15 +649,32 @@ class Decl {
 
 private:
   Module *getOwningModuleSlow() const;
+protected:
+  bool hasLocalOwningModuleStorage() const;
 
 public:
-  Module *getOwningModule() const {
+  /// \brief Get the imported owning module, if this decl is from an imported
+  /// (non-local) module.
+  Module *getImportedOwningModule() const {
     if (!isFromASTFile())
       return nullptr;
 
     return getOwningModuleSlow();
   }
 
+  /// \brief Get the local owning module, if known. Returns nullptr if owner is
+  /// not yet known or declaration is not from a module.
+  Module *getLocalOwningModule() const {
+    if (isFromASTFile() || !Hidden)
+      return nullptr;
+    return reinterpret_cast(this)[-1];
+  }
+  void setLocalOwningModule(Module *M) {
+    assert(!isFromASTFile() && Hidden && hasLocalOwningModuleStorage() &&
+           "should not have a cached owning module");
+    reinterpret_cast(this)[-1] = M;
+  }
+
   unsigned getIdentifierNamespace() const {
     return IdentifierNamespace;
   }
diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h
index 332238fd542..87e3803a664 100644
--- a/include/clang/AST/DeclCXX.h
+++ b/include/clang/AST/DeclCXX.h
@@ -651,8 +651,8 @@ class CXXRecordDecl : public RecordDecl {
   CXXRecordDecl *getCanonicalDecl() override {
     return cast(RecordDecl::getCanonicalDecl());
   }
-  virtual const CXXRecordDecl *getCanonicalDecl() const {
-    return cast(RecordDecl::getCanonicalDecl());
+  const CXXRecordDecl *getCanonicalDecl() const {
+    return const_cast(this)->getCanonicalDecl();
   }
 
   CXXRecordDecl *getPreviousDecl() {
@@ -1392,6 +1392,10 @@ class CXXRecordDecl : public RecordDecl {
   /// \brief Returns the destructor decl for this class.
   CXXDestructorDecl *getDestructor() const;
 
+  /// \brief Returns true if the class destructor, or any implicitly invoked
+  /// destructors are marked noreturn.
+  bool isAnyDestructorNoReturn() const;
+
   /// \brief If the class is a local class [class.local], returns
   /// the enclosing function declaration.
   const FunctionDecl *isLocalClass() const {
@@ -1436,7 +1440,7 @@ class CXXRecordDecl : public RecordDecl {
   ///
   /// \returns true if this class is derived from \p Base, false otherwise.
   ///
-  /// \todo add a separate paramaeter to configure IsDerivedFrom, rather than
+  /// \todo add a separate parameter to configure IsDerivedFrom, rather than
   /// tangling input and output in \p Paths
   bool isDerivedFrom(const CXXRecordDecl *Base, CXXBasePaths &Paths) const;
 
@@ -1464,8 +1468,8 @@ class CXXRecordDecl : public RecordDecl {
   /// \param BaseDefinition the definition of the base class
   ///
   /// \returns true if this base matched the search criteria
-  typedef bool ForallBasesCallback(const CXXRecordDecl *BaseDefinition,
-                                   void *UserData);
+  typedef llvm::function_ref
+      ForallBasesCallback;
 
   /// \brief Determines if the given callback holds for all the direct
   /// or indirect base classes of this type.
@@ -1477,13 +1481,10 @@ class CXXRecordDecl : public RecordDecl {
   /// class of this type, or if \p AllowShortCircuit is true then until a call
   /// returns false.
   ///
-  /// \param UserData Passed as the second argument of every call to
-  /// \p BaseMatches.
-  ///
   /// \param AllowShortCircuit if false, forces the callback to be called
   /// for every base class, even if a dependent or non-matching base was
   /// found.
-  bool forallBases(ForallBasesCallback *BaseMatches, void *UserData,
+  bool forallBases(ForallBasesCallback BaseMatches,
                    bool AllowShortCircuit = true) const;
 
   /// \brief Function type used by lookupInBases() to determine whether a
@@ -1495,13 +1496,9 @@ class CXXRecordDecl : public RecordDecl {
   /// \param Path the current path, from the most-derived class down to the
   /// base named by the \p Specifier.
   ///
-  /// \param UserData a single pointer to user-specified data, provided to
-  /// lookupInBases().
-  ///
   /// \returns true if this base matched the search criteria, false otherwise.
-  typedef bool BaseMatchesCallback(const CXXBaseSpecifier *Specifier,
-                                   CXXBasePath &Path,
-                                   void *UserData);
+  typedef llvm::function_ref BaseMatchesCallback;
 
   /// \brief Look for entities within the base classes of this C++ class,
   /// transitively searching all base class subobjects.
@@ -1516,14 +1513,12 @@ class CXXRecordDecl : public RecordDecl {
   /// \param BaseMatches callback function used to determine whether a given
   /// base matches the user-defined search criteria.
   ///
-  /// \param UserData user data pointer that will be provided to \p BaseMatches.
-  ///
   /// \param Paths used to record the paths from this class to its base class
   /// subobjects that match the search criteria.
   ///
   /// \returns true if there exists any path from this class to a base class
   /// subobject that matches the search criteria.
-  bool lookupInBases(BaseMatchesCallback *BaseMatches, void *UserData,
+  bool lookupInBases(BaseMatchesCallback BaseMatches,
                      CXXBasePaths &Paths) const;
 
   /// \brief Base-class lookup callback that determines whether the given
@@ -1531,10 +1526,10 @@ class CXXRecordDecl : public RecordDecl {
   ///
   /// This callback can be used with \c lookupInBases() to determine whether
   /// a given derived class has is a base class subobject of a particular type.
-  /// The user data pointer should refer to the canonical CXXRecordDecl of the
+  /// The base record pointer should refer to the canonical CXXRecordDecl of the
   /// base class that we are searching for.
   static bool FindBaseClass(const CXXBaseSpecifier *Specifier,
-                            CXXBasePath &Path, void *BaseRecord);
+                            CXXBasePath &Path, const CXXRecordDecl *BaseRecord);
 
   /// \brief Base-class lookup callback that determines whether the
   /// given base class specifier refers to a specific class
@@ -1542,39 +1537,38 @@ class CXXRecordDecl : public RecordDecl {
   ///
   /// This callback can be used with \c lookupInBases() to determine
   /// whether a given derived class has is a virtual base class
-  /// subobject of a particular type.  The user data pointer should
+  /// subobject of a particular type.  The base record pointer should
   /// refer to the canonical CXXRecordDecl of the base class that we
   /// are searching for.
   static bool FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
-                                   CXXBasePath &Path, void *BaseRecord);
+                                   CXXBasePath &Path,
+                                   const CXXRecordDecl *BaseRecord);
 
   /// \brief Base-class lookup callback that determines whether there exists
   /// a tag with the given name.
   ///
   /// This callback can be used with \c lookupInBases() to find tag members
-  /// of the given name within a C++ class hierarchy. The user data pointer
-  /// is an opaque \c DeclarationName pointer.
+  /// of the given name within a C++ class hierarchy.
   static bool FindTagMember(const CXXBaseSpecifier *Specifier,
-                            CXXBasePath &Path, void *Name);
+                            CXXBasePath &Path, DeclarationName Name);
 
   /// \brief Base-class lookup callback that determines whether there exists
   /// a member with the given name.
   ///
   /// This callback can be used with \c lookupInBases() to find members
-  /// of the given name within a C++ class hierarchy. The user data pointer
-  /// is an opaque \c DeclarationName pointer.
+  /// of the given name within a C++ class hierarchy.
   static bool FindOrdinaryMember(const CXXBaseSpecifier *Specifier,
-                                 CXXBasePath &Path, void *Name);
+                                 CXXBasePath &Path, DeclarationName Name);
 
   /// \brief Base-class lookup callback that determines whether there exists
   /// a member with the given name that can be used in a nested-name-specifier.
   ///
-  /// This callback can be used with \c lookupInBases() to find membes of
+  /// This callback can be used with \c lookupInBases() to find members of
   /// the given name within a C++ class hierarchy that can occur within
   /// nested-name-specifiers.
   static bool FindNestedNameSpecifierMember(const CXXBaseSpecifier *Specifier,
                                             CXXBasePath &Path,
-                                            void *UserData);
+                                            DeclarationName Name);
 
   /// \brief Retrieve the final overriders for each virtual member
   /// function in the class hierarchy where this class is the
@@ -1781,7 +1775,7 @@ class CXXMethodDecl : public FunctionDecl {
   CXXMethodDecl *getCanonicalDecl() override {
     return cast(FunctionDecl::getCanonicalDecl());
   }
-  const CXXMethodDecl *getCanonicalDecl() const override {
+  const CXXMethodDecl *getCanonicalDecl() const {
     return const_cast(this)->getCanonicalDecl();
   }
 
@@ -2084,7 +2078,7 @@ class CXXCtorInitializer {
   /// This can only be called once for each initializer; it cannot be called
   /// on an initializer having a positive number of (implicit) array indices.
   ///
-  /// This assumes that the initialzier was written in the source code, and
+  /// This assumes that the initializer was written in the source code, and
   /// ensures that isWritten() returns true.
   void setSourceOrder(int pos) {
     assert(!IsWritten &&
@@ -2326,12 +2320,12 @@ class CXXConstructorDecl : public CXXMethodDecl {
   /// \brief Set the constructor that this inheriting constructor is based on.
   void setInheritedConstructor(const CXXConstructorDecl *BaseCtor);
 
-  const CXXConstructorDecl *getCanonicalDecl() const override {
-    return cast(FunctionDecl::getCanonicalDecl());
-  }
   CXXConstructorDecl *getCanonicalDecl() override {
     return cast(FunctionDecl::getCanonicalDecl());
   }
+  const CXXConstructorDecl *getCanonicalDecl() const {
+    return const_cast(this)->getCanonicalDecl();
+  }
 
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h
index 4a5b4f3d075..96563ee6883 100644
--- a/include/clang/AST/DeclObjC.h
+++ b/include/clang/AST/DeclObjC.h
@@ -141,7 +141,7 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
 
   // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
   /// in, inout, etc.
-  unsigned objcDeclQualifier : 6;
+  unsigned objcDeclQualifier : 7;
 
   /// \brief Indicates whether this method has a related result type.
   unsigned RelatedResultType : 1;
@@ -332,10 +332,14 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
   SourceRange getReturnTypeSourceRange() const;
 
   /// \brief Determine the type of an expression that sends a message to this
-  /// function.
-  QualType getSendResultType() const {
-    return getReturnType().getNonLValueExprType(getASTContext());
-  }
+  /// function. This replaces the type parameters with the types they would
+  /// get if the receiver was parameterless (e.g. it may replace the type
+  /// parameter with 'id').
+  QualType getSendResultType() const;
+
+  /// Determine the type of an expression that sends a message to this
+  /// function with the given receiver type.
+  QualType getSendResultType(QualType receiverType) const;
 
   TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
   void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
@@ -399,6 +403,11 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
   /// have already been created.
   void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
 
+  /// \return the type for \c self and set \arg selfIsPseudoStrong and
+  /// \arg selfIsConsumed accordingly.
+  QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
+                       bool &selfIsPseudoStrong, bool &selfIsConsumed);
+
   ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
   void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
   ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
@@ -501,6 +510,183 @@ class ObjCMethodDecl : public NamedDecl, public DeclContext {
   friend class ASTDeclWriter;
 };
 
+/// Describes the variance of a given generic parameter.
+enum class ObjCTypeParamVariance : uint8_t {
+  /// The parameter is invariant: must match exactly.
+  Invariant,
+  /// The parameter is covariant, e.g., X is a subtype of X when
+  /// the type parameter is covariant and T is a subtype of U.
+  Covariant,
+  /// The parameter is contravariant, e.g., X is a subtype of X
+  /// when the type parameter is covariant and U is a subtype of T.
+  Contravariant,
+};
+
+/// Represents the declaration of an Objective-C type parameter.
+///
+/// \code
+/// @interface NSDictionary, Value>
+/// @end
+/// \endcode
+///
+/// In the example above, both \c Key and \c Value are represented by
+/// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id,
+/// while \c Value gets an implicit bound of \c id.
+///
+/// Objective-C type parameters are typedef-names in the grammar,
+class ObjCTypeParamDecl : public TypedefNameDecl {
+  void anchor() override;
+
+  /// Index of this type parameter in the type parameter list.
+  unsigned Index : 14;
+
+  /// The variance of the type parameter.
+  unsigned Variance : 2;
+
+  /// The location of the variance, if any.
+  SourceLocation VarianceLoc;
+
+  /// The location of the ':', which will be valid when the bound was
+  /// explicitly specified.
+  SourceLocation ColonLoc;
+
+  ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc, 
+                    ObjCTypeParamVariance variance, SourceLocation varianceLoc,
+                    unsigned index,
+                    SourceLocation nameLoc, IdentifierInfo *name,
+                    SourceLocation colonLoc, TypeSourceInfo *boundInfo)
+    : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
+                      boundInfo),
+      Index(index), Variance(static_cast(variance)),
+      VarianceLoc(varianceLoc), ColonLoc(colonLoc) { }
+
+public:
+  static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
+                                   ObjCTypeParamVariance variance,
+                                   SourceLocation varianceLoc,
+                                   unsigned index,
+                                   SourceLocation nameLoc,
+                                   IdentifierInfo *name,
+                                   SourceLocation colonLoc,
+                                   TypeSourceInfo *boundInfo);
+  static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
+
+  SourceRange getSourceRange() const override LLVM_READONLY;
+
+  /// Determine the variance of this type parameter.
+  ObjCTypeParamVariance getVariance() const {
+    return static_cast(Variance);
+  }
+
+  /// Set the variance of this type parameter.
+  void setVariance(ObjCTypeParamVariance variance) {
+    Variance = static_cast(variance);
+  }
+
+  /// Retrieve the location of the variance keyword.
+  SourceLocation getVarianceLoc() const { return VarianceLoc; }
+
+  /// Retrieve the index into its type parameter list.
+  unsigned getIndex() const { return Index; }
+
+  /// Whether this type parameter has an explicitly-written type bound, e.g.,
+  /// "T : NSView".
+  bool hasExplicitBound() const { return ColonLoc.isValid(); }
+
+  /// Retrieve the location of the ':' separating the type parameter name
+  /// from the explicitly-specified bound.
+  SourceLocation getColonLoc() const { return ColonLoc; }
+
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
+  static bool classofKind(Kind K) { return K == ObjCTypeParam; }
+
+  friend class ASTDeclReader;
+  friend class ASTDeclWriter;
+};
+
+/// Stores a list of Objective-C type parameters for a parameterized class
+/// or a category/extension thereof.
+///
+/// \code
+/// @interface NSArray // stores the 
+/// @end
+/// \endcode
+class ObjCTypeParamList {
+  /// Stores the components of a SourceRange as a POD.
+  struct PODSourceRange {
+    unsigned Begin;
+    unsigned End;
+  };
+
+  union { 
+    /// Location of the left and right angle brackets.
+    PODSourceRange Brackets;
+
+    // Used only for alignment.
+    ObjCTypeParamDecl *AlignmentHack;
+  };
+
+  /// The number of parameters in the list, which are tail-allocated.
+  unsigned NumParams;
+
+  ObjCTypeParamList(SourceLocation lAngleLoc,
+                    ArrayRef typeParams,
+                    SourceLocation rAngleLoc);
+
+public:
+  /// Create a new Objective-C type parameter list.
+  static ObjCTypeParamList *create(ASTContext &ctx,
+                                   SourceLocation lAngleLoc,
+                                   ArrayRef typeParams,
+                                   SourceLocation rAngleLoc);
+
+  /// Iterate through the type parameters in the list.
+  typedef ObjCTypeParamDecl **iterator;
+
+  iterator begin() { return reinterpret_cast(this + 1); }
+
+  iterator end() { return begin() + size(); }
+
+  /// Determine the number of type parameters in this list.
+  unsigned size() const { return NumParams; }
+
+  // Iterate through the type parameters in the list.
+  typedef ObjCTypeParamDecl * const *const_iterator;
+
+  const_iterator begin() const {
+    return reinterpret_cast(this + 1);
+  }
+
+  const_iterator end() const {
+    return begin() + size();
+  }
+
+  ObjCTypeParamDecl *front() const {
+    assert(size() > 0 && "empty Objective-C type parameter list");
+    return *begin();
+  }
+
+  ObjCTypeParamDecl *back() const {
+    assert(size() > 0 && "empty Objective-C type parameter list");
+    return *(end() - 1);
+  }
+
+  SourceLocation getLAngleLoc() const {
+    return SourceLocation::getFromRawEncoding(Brackets.Begin);
+  }
+  SourceLocation getRAngleLoc() const {
+    return SourceLocation::getFromRawEncoding(Brackets.End);
+  }
+  SourceRange getSourceRange() const {
+    return SourceRange(getLAngleLoc(), getRAngleLoc());
+  }
+
+  /// Gather the default set of type arguments to be substituted for
+  /// these type parameters when dealing with an unspecialized type.
+  void gatherDefaultTypeArgs(SmallVectorImpl &typeArgs) const;
+};
+
 /// ObjCContainerDecl - Represents a container for method declarations.
 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
 /// ObjCProtocolDecl, and ObjCImplDecl.
@@ -676,9 +862,9 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
     /// declaration.
     ObjCInterfaceDecl *Definition;
     
-    /// Class's super class.
-    ObjCInterfaceDecl *SuperClass;
-
+    /// When non-null, this is always an ObjCObjectType.
+    TypeSourceInfo *SuperClassTInfo;
+    
     /// Protocols referenced in the \@interface  declaration
     ObjCProtocolList ReferencedProtocols;
 
@@ -719,16 +905,13 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
     };
     /// One of the \c InheritedDesignatedInitializersState enumeratos.
     mutable unsigned InheritedDesignatedInitializers : 2;
-
-    /// \brief The location of the superclass, if any.
-    SourceLocation SuperClassLoc;
     
     /// \brief The location of the last location in this declaration, before
     /// the properties/methods. For example, this will be the '>', '}', or 
     /// identifier, 
     SourceLocation EndLoc; 
 
-    DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(), 
+    DefinitionData() : Definition(), SuperClassTInfo(), CategoryList(), IvarList(), 
                        ExternallyCompleted(),
                        IvarListMissingImplementation(true),
                        HasDesignatedInitializers(),
@@ -736,11 +919,15 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
   };
 
   ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
-                    IdentifierInfo *Id, SourceLocation CLoc,
-                    ObjCInterfaceDecl *PrevDecl, bool IsInternal);
+                    IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
+                    SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
+                    bool IsInternal);
 
   void LoadExternalDefinition() const;
 
+  /// The type parameters associated with this class, if any.
+  ObjCTypeParamList *TypeParamList;
+
   /// \brief Contains a pointer to the data associated with this class,
   /// which will be NULL if this class has not yet been defined.
   ///
@@ -771,12 +958,33 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
   static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
                                    SourceLocation atLoc,
                                    IdentifierInfo *Id,
+                                   ObjCTypeParamList *typeParamList,
                                    ObjCInterfaceDecl *PrevDecl,
                                    SourceLocation ClassLoc = SourceLocation(),
                                    bool isInternal = false);
 
   static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
 
+  /// Retrieve the type parameters of this class.
+  ///
+  /// This function looks for a type parameter list for the given
+  /// class; if the class has been declared (with \c \@class) but not
+  /// defined (with \c \@interface), it will search for a declaration that
+  /// has type parameters, skipping any declarations that do not.
+  ObjCTypeParamList *getTypeParamList() const;
+
+  /// Set the type parameters of this class.
+  ///
+  /// This function is used by the AST importer, which must import the type
+  /// parameters after creating their DeclContext to avoid loops.
+  void setTypeParamList(ObjCTypeParamList *TPL);
+
+  /// Retrieve the type parameters written on this particular declaration of
+  /// the class.
+  ObjCTypeParamList *getTypeParamListAsWritten() const {
+    return TypeParamList;
+  }
+
   SourceRange getSourceRange() const override LLVM_READONLY {
     if (isThisDeclarationADefinition())
       return ObjCContainerDecl::getSourceRange();
@@ -994,13 +1202,8 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
     // might bring in a definition.
     // Note: a null value indicates that we don't have a definition and that
     // modules are enabled.
-    if (!Data.getOpaqueValue()) {
-      if (IdentifierInfo *II = getIdentifier()) {
-        if (II->isOutOfDate()) {
-          updateOutOfDate(*II);
-        }
-      }
-    }
+    if (!Data.getOpaqueValue())
+      getMostRecentDecl();
 
     return Data.getPointer();
   }
@@ -1023,7 +1226,16 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
   /// a forward declaration (\@class) to a definition (\@interface).
   void startDefinition();
   
-  ObjCInterfaceDecl *getSuperClass() const {
+  /// Retrieve the superclass type.
+  const ObjCObjectType *getSuperClassType() const {
+    if (TypeSourceInfo *TInfo = getSuperClassTInfo())
+      return TInfo->getType()->castAs();
+
+    return nullptr;
+  }
+
+  // Retrieve the type source information for the superclass.
+  TypeSourceInfo *getSuperClassTInfo() const {
     // FIXME: Should make sure no callers ever do this.
     if (!hasDefinition())
       return nullptr;
@@ -1031,13 +1243,15 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
     if (data().ExternallyCompleted)
       LoadExternalDefinition();
 
-    return data().SuperClass;
+    return data().SuperClassTInfo;
   }
 
-  void setSuperClass(ObjCInterfaceDecl * superCls) { 
-    data().SuperClass = 
-      (superCls && superCls->hasDefinition()) ? superCls->getDefinition() 
-                                              : superCls; 
+  // Retrieve the declaration for the superclass of this class, which
+  // does not include any type arguments that apply to the superclass.
+  ObjCInterfaceDecl *getSuperClass() const;
+
+  void setSuperClass(TypeSourceInfo *superClass) { 
+    data().SuperClassTInfo = superClass;
   }
 
   /// \brief Iterator that walks over the list of categories, filtering out
@@ -1329,8 +1543,8 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
                           
   void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
 
-  void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
-  SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
+  /// Retrieve the starting location of the superclass.
+  SourceLocation getSuperClassLoc() const;
 
   /// isImplicitInterfaceDecl - check that this is an implicitly declared
   /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
@@ -1438,6 +1652,10 @@ class ObjCIvarDecl : public FieldDecl {
   void setSynthesize(bool synth) { Synthesized = synth; }
   bool getSynthesize() const { return Synthesized; }
 
+  /// Retrieve the type of this instance variable when viewed as a member of a
+  /// specific object type.
+  QualType getUsageType(QualType objectType) const;
+
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   static bool classofKind(Kind K) { return K == ObjCIvar; }
@@ -1628,13 +1846,8 @@ class ObjCProtocolDecl : public ObjCContainerDecl,
     // might bring in a definition.
     // Note: a null value indicates that we don't have a definition and that
     // modules are enabled.
-    if (!Data.getOpaqueValue()) {
-      if (IdentifierInfo *II = getIdentifier()) {
-        if (II->isOutOfDate()) {
-          updateOutOfDate(*II);
-        }
-      }
-    }
+    if (!Data.getOpaqueValue())
+      getMostRecentDecl();
 
     return Data.getPointer();
   }
@@ -1719,6 +1932,9 @@ class ObjCCategoryDecl : public ObjCContainerDecl {
   /// Interface belonging to this category
   ObjCInterfaceDecl *ClassInterface;
 
+  /// The type parameters associated with this category, if any.
+  ObjCTypeParamList *TypeParamList;
+
   /// referenced protocols in this category.
   ObjCProtocolList ReferencedProtocols;
 
@@ -1736,13 +1952,9 @@ class ObjCCategoryDecl : public ObjCContainerDecl {
   ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
                    SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
                    IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
+                   ObjCTypeParamList *typeParamList,
                    SourceLocation IvarLBraceLoc=SourceLocation(),
-                   SourceLocation IvarRBraceLoc=SourceLocation())
-    : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
-      ClassInterface(IDecl), NextClassCategory(nullptr),
-      CategoryNameLoc(CategoryNameLoc),
-      IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
-  }
+                   SourceLocation IvarRBraceLoc=SourceLocation());
 
 public:
 
@@ -1752,6 +1964,7 @@ class ObjCCategoryDecl : public ObjCContainerDecl {
                                   SourceLocation CategoryNameLoc,
                                   IdentifierInfo *Id,
                                   ObjCInterfaceDecl *IDecl,
+                                  ObjCTypeParamList *typeParamList,
                                   SourceLocation IvarLBraceLoc=SourceLocation(),
                                   SourceLocation IvarRBraceLoc=SourceLocation());
   static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
@@ -1759,6 +1972,17 @@ class ObjCCategoryDecl : public ObjCContainerDecl {
   ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
   const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
 
+  /// Retrieve the type parameter list associated with this category or
+  /// extension.
+  ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
+
+  /// Set the type parameters of this category.
+  ///
+  /// This function is used by the AST importer, which must import the type
+  /// parameters after creating their DeclContext to avoid loops.
+  void setTypeParamList(ObjCTypeParamList *TPL);
+
+
   ObjCCategoryImplDecl *getImplementation() const;
   void setImplementation(ObjCCategoryImplDecl *ImplD);
 
@@ -2203,13 +2427,17 @@ class ObjCPropertyDecl : public NamedDecl {
     OBJC_PR_atomic    = 0x100,
     OBJC_PR_weak      = 0x200,
     OBJC_PR_strong    = 0x400,
-    OBJC_PR_unsafe_unretained = 0x800
+    OBJC_PR_unsafe_unretained = 0x800,
+    /// Indicates that the nullability of the type was spelled with a
+    /// property attribute rather than a type qualifier.
+    OBJC_PR_nullability = 0x1000,
+    OBJC_PR_null_resettable = 0x2000
     // Adding a property should change NumPropertyAttrsBits
   };
 
   enum {
     /// \brief Number of bits fitting all the property attributes.
-    NumPropertyAttrsBits = 12
+    NumPropertyAttrsBits = 14
   };
 
   enum SetterKind { Assign, Retain, Copy, Weak };
@@ -2217,7 +2445,8 @@ class ObjCPropertyDecl : public NamedDecl {
 private:
   SourceLocation AtLoc;   // location of \@property
   SourceLocation LParenLoc; // location of '(' starting attribute list or null.
-  TypeSourceInfo *DeclType;
+  QualType DeclType;
+  TypeSourceInfo *DeclTypeSourceInfo;
   unsigned PropertyAttributes : NumPropertyAttrsBits;
   unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
   // \@required/\@optional
@@ -2232,12 +2461,13 @@ class ObjCPropertyDecl : public NamedDecl {
 
   ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
                    SourceLocation AtLocation,  SourceLocation LParenLocation,
-                   TypeSourceInfo *T)
+                   QualType T, TypeSourceInfo *TSI,
+                   PropertyControl propControl)
     : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), 
-      LParenLoc(LParenLocation), DeclType(T),
+      LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
       PropertyAttributes(OBJC_PR_noattr),
       PropertyAttributesAsWritten(OBJC_PR_noattr),
-      PropertyImplementation(None),
+      PropertyImplementation(propControl),
       GetterName(Selector()),
       SetterName(Selector()),
       GetterMethodDecl(nullptr), SetterMethodDecl(nullptr),
@@ -2248,7 +2478,8 @@ class ObjCPropertyDecl : public NamedDecl {
                                   SourceLocation L,
                                   IdentifierInfo *Id, SourceLocation AtLocation,
                                   SourceLocation LParenLocation,
-                                  TypeSourceInfo *T,
+                                  QualType T,
+                                  TypeSourceInfo *TSI,
                                   PropertyControl propControl = None);
   
   static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
@@ -2259,9 +2490,18 @@ class ObjCPropertyDecl : public NamedDecl {
   SourceLocation getLParenLoc() const { return LParenLoc; }
   void setLParenLoc(SourceLocation L) { LParenLoc = L; }
 
-  TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
-  QualType getType() const { return DeclType->getType(); }
-  void setType(TypeSourceInfo *T) { DeclType = T; }
+  TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
+
+  QualType getType() const { return DeclType; }
+
+  void setType(QualType T, TypeSourceInfo *TSI) { 
+    DeclType = T;
+    DeclTypeSourceInfo = TSI; 
+  }
+
+  /// Retrieve the type when this property is used with a specific base object
+  /// type.
+  QualType getUsageType(QualType objectType) const;
 
   PropertyAttributeKind getPropertyAttributes() const {
     return PropertyAttributeKind(PropertyAttributes);
diff --git a/include/clang/AST/DeclTemplate.h b/include/clang/AST/DeclTemplate.h
index 90cfb204917..44b907f7494 100644
--- a/include/clang/AST/DeclTemplate.h
+++ b/include/clang/AST/DeclTemplate.h
@@ -43,7 +43,7 @@ typedef llvm::PointerUnion3
+class DefaultArgStorage {
+  /// Storage for both the value *and* another parameter from which we inherit
+  /// the default argument. This is used when multiple default arguments for a
+  /// parameter are merged together from different modules.
+  struct Chain {
+    ParmDecl *PrevDeclWithDefaultArg;
+    ArgType Value;
+  };
+  static_assert(sizeof(Chain) == sizeof(void *) * 2,
+                "non-pointer argument type?");
+
+  llvm::PointerUnion3 ValueOrInherited;
+
+  static ParmDecl *getParmOwningDefaultArg(ParmDecl *Parm) {
+    const DefaultArgStorage &Storage = Parm->getDefaultArgStorage();
+    if (auto *Prev = Storage.ValueOrInherited.template dyn_cast())
+      Parm = Prev;
+    assert(!Parm->getDefaultArgStorage()
+                .ValueOrInherited.template is() &&
+           "should only be one level of indirection");
+    return Parm;
+  }
+
+public:
+  DefaultArgStorage() : ValueOrInherited(ArgType()) {}
+
+  /// Determine whether there is a default argument for this parameter.
+  bool isSet() const { return !ValueOrInherited.isNull(); }
+  /// Determine whether the default argument for this parameter was inherited
+  /// from a previous declaration of the same entity.
+  bool isInherited() const { return ValueOrInherited.template is(); }
+  /// Get the default argument's value. This does not consider whether the
+  /// default argument is visible.
+  ArgType get() const {
+    const DefaultArgStorage *Storage = this;
+    if (auto *Prev = ValueOrInherited.template dyn_cast())
+      Storage = &Prev->getDefaultArgStorage();
+    if (auto *C = Storage->ValueOrInherited.template dyn_cast())
+      return C->Value;
+    return Storage->ValueOrInherited.template get();
+  }
+  /// Get the parameter from which we inherit the default argument, if any.
+  /// This is the parameter on which the default argument was actually written.
+  const ParmDecl *getInheritedFrom() const {
+    if (auto *D = ValueOrInherited.template dyn_cast())
+      return D;
+    if (auto *C = ValueOrInherited.template dyn_cast())
+      return C->PrevDeclWithDefaultArg;
+    return nullptr;
+  }
+  /// Set the default argument.
+  void set(ArgType Arg) {
+    assert(!isSet() && "default argument already set");
+    ValueOrInherited = Arg;
+  }
+  /// Set that the default argument was inherited from another parameter.
+  void setInherited(const ASTContext &C, ParmDecl *InheritedFrom) {
+    assert(!isInherited() && "default argument already inherited");
+    InheritedFrom = getParmOwningDefaultArg(InheritedFrom);
+    if (!isSet())
+      ValueOrInherited = InheritedFrom;
+    else
+      ValueOrInherited = new (allocateDefaultArgStorageChain(C))
+          Chain{InheritedFrom, ValueOrInherited.template get()};
+  }
+  /// Remove the default argument, even if it was inherited.
+  void clear() {
+    ValueOrInherited = ArgType();
+  }
+};
+
 //===----------------------------------------------------------------------===//
 // Kinds of Templates
 //===----------------------------------------------------------------------===//
@@ -460,27 +542,20 @@ class MemberSpecializationInfo {
 ///     friend void foo<>(T);
 ///   };
 /// \endcode
-class DependentFunctionTemplateSpecializationInfo {
-  struct CA {
-    /// The number of potential template candidates.
-    unsigned NumTemplates;
+class LLVM_ALIGNAS(/*alignof(uint64_t)*/ 8)
+    DependentFunctionTemplateSpecializationInfo {
+  /// The number of potential template candidates.
+  unsigned NumTemplates;
 
-    /// The number of template arguments.
-    unsigned NumArgs;
-  };
-
-  union {
-    // Force sizeof to be a multiple of sizeof(void*) so that the
-    // trailing data is aligned.
-    void *Aligner;
-    struct CA d;
-  };
+  /// The number of template arguments.
+  unsigned NumArgs;
 
   /// The locations of the left and right angle brackets.
   SourceRange AngleLocs;
 
   FunctionTemplateDecl * const *getTemplates() const {
-    return reinterpret_cast(this+1);
+    return reinterpret_cast(
+        &getTemplateArgs()[NumArgs]);
   }
 
 public:
@@ -490,9 +565,7 @@ class DependentFunctionTemplateSpecializationInfo {
 
   /// \brief Returns the number of function templates that this might
   /// be a specialization of.
-  unsigned getNumTemplates() const {
-    return d.NumTemplates;
-  }
+  unsigned getNumTemplates() const { return NumTemplates; }
 
   /// \brief Returns the i'th template candidate.
   FunctionTemplateDecl *getTemplate(unsigned I) const {
@@ -502,14 +575,11 @@ class DependentFunctionTemplateSpecializationInfo {
 
   /// \brief Returns the explicit template arguments that were given.
   const TemplateArgumentLoc *getTemplateArgs() const {
-    return reinterpret_cast(
-                                            &getTemplates()[getNumTemplates()]);
+    return reinterpret_cast(this + 1);
   }
 
   /// \brief Returns the number of explicit template arguments that were given.
-  unsigned getNumTemplateArgs() const {
-    return d.NumArgs;
-  }
+  unsigned getNumTemplateArgs() const { return NumArgs; }
 
   /// \brief Returns the nth template argument.
   const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
@@ -942,18 +1012,16 @@ class TemplateTypeParmDecl : public TypeDecl {
   /// If false, it was declared with the 'class' keyword.
   bool Typename : 1;
 
-  /// \brief Whether this template type parameter inherited its
-  /// default argument.
-  bool InheritedDefault : 1;
-
   /// \brief The default template argument, if any.
-  TypeSourceInfo *DefaultArgument;
+  typedef DefaultArgStorage
+      DefArgStorage;
+  DefArgStorage DefaultArgument;
 
   TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
                        SourceLocation IdLoc, IdentifierInfo *Id,
                        bool Typename)
     : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
-      InheritedDefault(false), DefaultArgument() { }
+      DefaultArgument() { }
 
   /// Sema creates these on the stack during auto type deduction.
   friend class Sema;
@@ -974,35 +1042,45 @@ class TemplateTypeParmDecl : public TypeDecl {
   /// If not, it was declared with the 'class' keyword.
   bool wasDeclaredWithTypename() const { return Typename; }
 
+  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
+
   /// \brief Determine whether this template parameter has a default
   /// argument.
-  bool hasDefaultArgument() const { return DefaultArgument != nullptr; }
+  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
 
   /// \brief Retrieve the default argument, if any.
-  QualType getDefaultArgument() const { return DefaultArgument->getType(); }
+  QualType getDefaultArgument() const {
+    return DefaultArgument.get()->getType();
+  }
 
   /// \brief Retrieves the default argument's source information, if any.
-  TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
+  TypeSourceInfo *getDefaultArgumentInfo() const {
+    return DefaultArgument.get();
+  }
 
   /// \brief Retrieves the location of the default argument declaration.
   SourceLocation getDefaultArgumentLoc() const;
 
   /// \brief Determines whether the default argument was inherited
   /// from a previous declaration of this template.
-  bool defaultArgumentWasInherited() const { return InheritedDefault; }
+  bool defaultArgumentWasInherited() const {
+    return DefaultArgument.isInherited();
+  }
 
-  /// \brief Set the default argument for this template parameter, and
-  /// whether that default argument was inherited from another
-  /// declaration.
-  void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
-    DefaultArgument = DefArg;
-    InheritedDefault = Inherited;
+  /// \brief Set the default argument for this template parameter.
+  void setDefaultArgument(TypeSourceInfo *DefArg) {
+    DefaultArgument.set(DefArg);
+  }
+  /// \brief Set that this default argument was inherited from another
+  /// parameter.
+  void setInheritedDefaultArgument(const ASTContext &C,
+                                   TemplateTypeParmDecl *Prev) {
+    DefaultArgument.setInherited(C, Prev);
   }
 
   /// \brief Removes the default argument of this template parameter.
   void removeDefaultArgument() {
-    DefaultArgument = nullptr;
-    InheritedDefault = false;
+    DefaultArgument.clear();
   }
 
   /// \brief Set whether this template type parameter was declared with
@@ -1034,7 +1112,8 @@ class NonTypeTemplateParmDecl
   : public DeclaratorDecl, protected TemplateParmPosition {
   /// \brief The default template argument, if any, and whether or not
   /// it was inherited.
-  llvm::PointerIntPair DefaultArgumentAndInherited;
+  typedef DefaultArgStorage DefArgStorage;
+  DefArgStorage DefaultArgument;
 
   // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
   // down here to save memory.
@@ -1055,9 +1134,8 @@ class NonTypeTemplateParmDecl
                           IdentifierInfo *Id, QualType T,
                           bool ParameterPack, TypeSourceInfo *TInfo)
     : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
-      TemplateParmPosition(D, P), DefaultArgumentAndInherited(nullptr, false),
-      ParameterPack(ParameterPack), ExpandedParameterPack(false),
-      NumExpandedTypes(0)
+      TemplateParmPosition(D, P), ParameterPack(ParameterPack),
+      ExpandedParameterPack(false), NumExpandedTypes(0)
   { }
 
   NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
@@ -1097,16 +1175,14 @@ class NonTypeTemplateParmDecl
 
   SourceRange getSourceRange() const override LLVM_READONLY;
 
+  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
+
   /// \brief Determine whether this template parameter has a default
   /// argument.
-  bool hasDefaultArgument() const {
-    return DefaultArgumentAndInherited.getPointer() != nullptr;
-  }
+  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
 
   /// \brief Retrieve the default argument, if any.
-  Expr *getDefaultArgument() const {
-    return DefaultArgumentAndInherited.getPointer();
-  }
+  Expr *getDefaultArgument() const { return DefaultArgument.get(); }
 
   /// \brief Retrieve the location of the default argument, if any.
   SourceLocation getDefaultArgumentLoc() const;
@@ -1114,22 +1190,20 @@ class NonTypeTemplateParmDecl
   /// \brief Determines whether the default argument was inherited
   /// from a previous declaration of this template.
   bool defaultArgumentWasInherited() const {
-    return DefaultArgumentAndInherited.getInt();
+    return DefaultArgument.isInherited();
   }
 
   /// \brief Set the default argument for this template parameter, and
   /// whether that default argument was inherited from another
   /// declaration.
-  void setDefaultArgument(Expr *DefArg, bool Inherited) {
-    DefaultArgumentAndInherited.setPointer(DefArg);
-    DefaultArgumentAndInherited.setInt(Inherited);
+  void setDefaultArgument(Expr *DefArg) { DefaultArgument.set(DefArg); }
+  void setInheritedDefaultArgument(const ASTContext &C,
+                                   NonTypeTemplateParmDecl *Parm) {
+    DefaultArgument.setInherited(C, Parm);
   }
 
   /// \brief Removes the default argument of this template parameter.
-  void removeDefaultArgument() {
-    DefaultArgumentAndInherited.setPointer(nullptr);
-    DefaultArgumentAndInherited.setInt(false);
-  }
+  void removeDefaultArgument() { DefaultArgument.clear(); }
 
   /// \brief Whether this parameter is a non-type template parameter pack.
   ///
@@ -1217,10 +1291,10 @@ class TemplateTemplateParmDecl : public TemplateDecl,
 {
   void anchor() override;
 
-  /// DefaultArgument - The default template argument, if any.
-  TemplateArgumentLoc DefaultArgument;
-  /// Whether or not the default argument was inherited.
-  bool DefaultArgumentWasInherited;
+  /// \brief The default template argument, if any.
+  typedef DefaultArgStorage
+      DefArgStorage;
+  DefArgStorage DefaultArgument;
 
   /// \brief Whether this parameter is a parameter pack.
   bool ParameterPack;
@@ -1237,8 +1311,7 @@ class TemplateTemplateParmDecl : public TemplateDecl,
                            unsigned D, unsigned P, bool ParameterPack,
                            IdentifierInfo *Id, TemplateParameterList *Params)
     : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
-      TemplateParmPosition(D, P), DefaultArgument(),
-      DefaultArgumentWasInherited(false), ParameterPack(ParameterPack),
+      TemplateParmPosition(D, P), ParameterPack(ParameterPack),
       ExpandedParameterPack(false), NumExpandedParams(0)
     { }
 
@@ -1322,15 +1395,16 @@ class TemplateTemplateParmDecl : public TemplateDecl,
     return reinterpret_cast(this + 1)[I];
   }
 
+  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
+
   /// \brief Determine whether this template parameter has a default
   /// argument.
-  bool hasDefaultArgument() const {
-    return !DefaultArgument.getArgument().isNull();
-  }
+  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
 
   /// \brief Retrieve the default argument, if any.
   const TemplateArgumentLoc &getDefaultArgument() const {
-    return DefaultArgument;
+    static const TemplateArgumentLoc None;
+    return DefaultArgument.isSet() ? *DefaultArgument.get() : None;
   }
 
   /// \brief Retrieve the location of the default argument, if any.
@@ -1339,22 +1413,21 @@ class TemplateTemplateParmDecl : public TemplateDecl,
   /// \brief Determines whether the default argument was inherited
   /// from a previous declaration of this template.
   bool defaultArgumentWasInherited() const {
-    return DefaultArgumentWasInherited;
+    return DefaultArgument.isInherited();
   }
 
   /// \brief Set the default argument for this template parameter, and
   /// whether that default argument was inherited from another
   /// declaration.
-  void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) {
-    DefaultArgument = DefArg;
-    DefaultArgumentWasInherited = Inherited;
+  void setDefaultArgument(const ASTContext &C,
+                          const TemplateArgumentLoc &DefArg);
+  void setInheritedDefaultArgument(const ASTContext &C,
+                                   TemplateTemplateParmDecl *Prev) {
+    DefaultArgument.setInherited(C, Prev);
   }
 
   /// \brief Removes the default argument of this template parameter.
-  void removeDefaultArgument() {
-    DefaultArgument = TemplateArgumentLoc();
-    DefaultArgumentWasInherited = false;
-  }
+  void removeDefaultArgument() { DefaultArgument.clear(); }
 
   SourceRange getSourceRange() const override LLVM_READONLY {
     SourceLocation End = getLocation();
diff --git a/include/clang/AST/EvaluatedExprVisitor.h b/include/clang/AST/EvaluatedExprVisitor.h
index 59de104b83f..aad7726f848 100644
--- a/include/clang/AST/EvaluatedExprVisitor.h
+++ b/include/clang/AST/EvaluatedExprVisitor.h
@@ -26,29 +26,32 @@ class ASTContext;
   
 /// \brief Given a potentially-evaluated expression, this visitor visits all
 /// of its potentially-evaluated subexpressions, recursively.
-template
-class EvaluatedExprVisitor : public StmtVisitor {
-  ASTContext &Context;
-  
+template