Release 3.8 the Clang Team
Total Page:16
File Type:pdf, Size:1020Kb
Clang Documentation Release 3.8 The Clang Team Aug 19, 2017 Contents 1 Introduction 3 2 What’s New in Clang 3.8? 5 3 Additional Information 13 4 Using Clang as a Compiler 15 5 Using Clang as a Library 257 6 Using Clang Tools 283 7 Design Documents 299 8 Indices and tables 347 i ii Clang Documentation, Release 3.8 • Introduction • What’s New in Clang 3.8? – Improvements to Clang’s diagnostics – New Compiler Flags – Alignment – C Language Changes in Clang – OpenCL C Language Changes in Clang – OpenMP Support in Clang – CUDA Support in Clang – Internal API Changes – AST Matchers – Static Analyzer – Clang-tidy • Additional Information Written by the LLVM Team Contents 1 Clang Documentation, Release 3.8 2 Contents CHAPTER 1 Introduction This document contains the release notes for the Clang C/C++/Objective-C 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 documentation. All LLVM releases may be downloaded from the LLVM releases web site. For more information about Clang or LLVM, including information about the latest release, please check out the main please see the Clang Web Site or the LLVM Web Site. 3 Clang Documentation, Release 3.8 4 Chapter 1. Introduction CHAPTER 2 What’s New in Clang 3.8? Some of the major new features and improvements to Clang are listed here. Generic improvements to Clang as a whole or to its underlying infrastructure are described first, followed by language-specific sections with improvements to Clang’s support for those languages. Improvements to Clang’s diagnostics Clang’s diagnostics are constantly being improved to catch more issues, explain them more clearly, and provide more accurate source information about them. The improvements since the 3.7 release include: • -Wmicrosoft has been split into many targeted flags, so that projects can choose to enable only a subset of these warnings. -Wno-microsoft still disables all these warnings, and -Wmicrosoft still enables them all. New Compiler Flags Clang can “tune” DWARF debugging information to suit one of several different debuggers. This fine-tuning can mean omitting DWARF features that the debugger does not need or use, or including DWARF extensions specific to the debugger. Clang supports tuning for three debuggers, as follows. • -ggdb is equivalent to -g plus tuning for the GDB debugger. For compatibility with GCC, Clang allows this option to be followed by a single digit from 0 to 3 indicating the debugging information “level.” For example, -ggdb1 is equivalent to -ggdb -g1. • -glldb is equivalent to -g plus tuning for the LLDB debugger. • -gsce is equivalent to -g plus tuning for the Sony Computer Entertainment debugger. Specifying -g without a tuning option will use a target-dependent default. The new -fstrict-vtable-pointers flag enables better devirtualization support (experimental). 5 Clang Documentation, Release 3.8 Alignment Clang has gotten better at passing down strict type alignment information to LLVM, and several targets have gotten better at taking advantage of that information. Dereferencing a pointer that is not adequately aligned for its type is undefined behavior. It may crash on target architectures that strictly enforce alignment, but even on architectures that do not, frequent use of unaligned pointers may hurt the performance of the generated code. If you find yourself fixing a bug involving an inadequately aligned pointer, you have several options. The best option, when practical, is to increase the alignment of the memory. For example, this array is not guaranteed to be sufficiently aligned to store a pointer value: char buffer[sizeof(const char*)]; Writing a pointer directly into it violates C’s alignment rules: ((const char**) buffer)[0]="Hello, world! \n"; But you can use alignment attributes to increase the required alignment: __attribute__((aligned(__alignof__(const char*)))) char buffer[sizeof(const char*)]; When that’s not practical, you can instead reduce the alignment requirements of the pointer. If the pointer is to a struct that represents that layout of a serialized structure, consider making that struct packed; this will remove any implicit internal padding that the compiler might add to the struct and reduce its alignment requirement to 1. struct file_header { uint16_t magic_number; uint16_t format_version; uint16_t num_entries; } __attribute__((packed)); You may also override the default alignment assumptions of a pointer by using a typedef with explicit alignment: typedef const char *unaligned_char_ptr __attribute__((aligned(1))); ((unaligned_char_ptr*) buffer)[0]="Hello, world! \n"; The final option is to copy the memory into something that is properly aligned. Be aware, however, that Clang will assume that pointers are properly aligned for their type when you pass them to a library function like memcpy. For example, this code will assume that the source and destination pointers are both properly aligned for an int: void copy_int_array(int *dest, const int *src, size_t num) { memcpy(dest, src, num * sizeof(int)); } You may explicitly disable this assumption by casting the argument to a less-aligned pointer type: void copy_unaligned_int_array(int *dest, const int *src, size_t num) { memcpy((char*) dest, (const char*) src, num * sizeof(int)); } Clang promises not to look through the explicit cast when inferring the alignment of this memcpy. 6 Chapter 2. What’s New in Clang 3.8? Clang Documentation, Release 3.8 C Language Changes in Clang Better support for __builtin_object_size Clang 3.8 has expanded support for the __builtin_object_size intrinsic. Specifically, __builtin_object_size will now fail less often when you’re trying to get the size of a subobject. Additionally, the pass_object_size attribute was added, which allows __builtin_object_size to successfully report the size of function parameters, without requiring that the function be inlined. overloadable attribute relaxations Previously, functions marked overloadable in C would strictly use C++’s type conversion rules, so the following code would not compile: void foo(char *bar, char *baz) __attribute__((overloadable)); void foo(char *bar) __attribute__((overloadable)); void callFoo() { int a; foo(&a); } Now, Clang is able to selectively use C’s type conversion rules during overload resolution in C, which allows the above example to compile (albeit potentially with a warning about an implicit conversion from int* to char*). OpenCL C Language Changes in Clang Several OpenCL 2.0 features have been added, including: • Command-line option -std=CL2.0. • Generic address space (__generic) along with new conversion rules between different address spaces and default address space deduction. • Support for program scope variables with __global address space. • Pipe specifier was added (although no pipe functions are supported yet). • Atomic types: atomic_int, atomic_uint, atomic_long, atomic_ulong, atomic_float, atomic_double, atomic_flag, atomic_intptr_t, atomic_uintptr_t, atomic_size_t, atomic_ptrdiff_t and their usage with C11 style builtin functions. • Image types: image2d_depth_t, image2d_array_depth_t, image2d_msaa_t, image2d_array_msaa_t, image2d_msaa_depth_t, image2d_array_msaa_depth_t. • Other types (for pipes and device side enqueue): clk_event_t, queue_t, ndrange_t, reserve_id_t. Several additional features/bugfixes have been added to the previous standards: • A set of floating point arithmetic relaxation flags: -cl-no-signed-zeros, -cl-unsafe-math-optimizations, -cl-finite-math-only, -cl-fast-relaxed-math. • Added ^^ to the list of reserved operations. • Improved vector support and diagnostics. • Improved diagnostics for function pointers. 2.4. C Language Changes in Clang 7 Clang Documentation, Release 3.8 OpenMP Support in Clang OpenMP 3.1 is fully supported and is enabled by default with -fopenmp which now uses the Clang OpenMP library instead of the GCC OpenMP library. The runtime can be built in-tree. In addition to OpenMP 3.1, several important elements of the OpenMP 4.0/4.5 are supported as well. We continue to aim to complete OpenMP 4.5 • map clause • task dependencies • num_teams clause • thread_limit clause • target and target data directive • target directive with implicit data mapping • target enter data and target exit data directive • Array sections [2.4, Array Sections]. • Directive name modifiers for if clause [2.12, if Clause]. • linear clause can be used in loop-based directives [2.7.2, loop Construct]. • simdlen clause [2.8, SIMD Construct]. • hint clause [2.13.2, critical Construct]. • Parsing/semantic analysis of all non-device directives introduced in OpenMP 4.5. The codegen for OpenMP constructs was significantly improved allowing us to produce much more stable and fast code. Full test cases of IR are also implemented. CUDA Support in Clang Clang has experimental support for end-to-end CUDA compilation now: • The driver now detects CUDA installation, creates host and device compilation pipelines, links device-side code with appropriate CUDA bitcode and produces single object file with host and GPU code. • Implemented target attribute-based function overloading which allows Clang to compile CUDA sources without splitting them into separate host/device TUs. Internal API Changes 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