Clang 17, the latest major version of Clang as of October 2023, has full support for all published C++ standards up to C++17, implements most features of C++20, and has initial support for the upcoming C++23 standard.[13] Since v16.0.0, Clang compiles C++ using the GNU++17 dialect by default, which includes features from the C++17 standard and conforming GNU extensions.[14]
Background
In 2005, Apple Inc. made extensive use of LLVM in several commercial products,[15] including the iOS SDK and Xcode 3.1. An OpenGL code compiler for OS X that converts OpenGL calls into more fundamental calls for graphics processing units (GPU) that do not support certain features, was one of the first uses of LLVM. This enabled Apple to support OpenGL on computers using Intel GMA chipsets, increasing performance on those machines.[16]
The LLVM project originally intended to use GCC's front end. The GCC source code, however, is large and somewhat cumbersome; as one long-time GCC developer put it referring to LLVM, "Trying to make the hippo dance is not really a lot of fun".[17] Besides, Apple software uses Objective-C, which is a low priority for GCC developers. As such, GCC does not integrate smoothly into Apple's integrated development environment (IDE).[18] Finally, GCC's license agreement, the GNU General Public License (GPL) version 3, requires developers who distribute extensions or modified versions of GCC to make their source code available, but LLVM's permissive software license doesn't require this.[4][5]
For these reasons, Apple developed Clang, a new compiler front end which supports C, Objective-C and C++.[18] In July 2007, the project received the approval for becoming open-source.[19]
Design
Clang works in tandem with LLVM.[20] The combination of Clang and LLVM provides most of the toolchain for replacing the GCC stack. One of Clang's main goals is to provide a library-based architecture,[21] so that the compiler could interoperate with other tools that interact with source code, such as integrated development environments (IDE). In contrast, GCC works in a compile-link-debug workflow; integrating it with other tools is not always easy. For instance, GCC uses a step called fold that is key to the overall compile process, which has the side effect of translating the code tree into a form that looks unlike the original source code. If an error is found during or after the fold step, it can be difficult to translate that back into one location in the original source. Besides, vendors using the GCC stack within IDEs must use separate tools to index the code, to provide features like syntax highlighting and intelligent code completion.
Clang retains more information during the compiling process than GCC, and preserves the overall form of the original code, making it easier to map errors back into the original source. Clang's error reports are more detailed, specific, and machine-readable, so IDEs can index the compiler's output. Modular design of the compiler can offer source code indexing, syntax checking, and other features normally associated with rapid application development systems. The parse tree is also more suitable for supporting automated code refactoring, as it directly represents the original source code.
Clang compiles only C-like languages, such as C, C++, Objective-C, and Objective-C++. In many cases, Clang can replace GCC as needed, with no other effects on the toolchain as a whole.[citation needed] It supports most of the commonly used GCC options. A Fortran project, Flang was in-progress in 2022. However, for other languages, such as Ada, LLVM remains dependent on GCC or another compiler front end.
Flang - Fortran
The Flang project by Nvidia and The Portland Group adds Fortran support.[22] Flang is LLVM's Fortran frontend. It is often referred to as "LLVM Flang" to differentiate itself from "Classic Flang" – these are two separate and independent Fortran compilers. "LLVM Flang" is under active development. Development versions of Flang were in progress as of October 2023[update] and could be downloaded from the LLVM Project.[23]
Performance and GCC compatibility
Clang is compatible with GCC.[10] Its command-line interface shares many of GCC's flags and options. Clang implements many GNU language extensions and compiler intrinsics, some of which are purely for compatibility. For example, even though Clang implements atomic intrinsics which correspond exactly with C11 atomics, it also implements GCC's __sync_* intrinsics for compatibility with GCC and the C++ Standard Library (libstdc++). Clang also maintains application binary interface (ABI) compatibility with GCC-generated object code. In practice, Clang is a drop-in replacement for GCC.[24]
Clang's developers aim to reduce memory footprint and increase compiling speed compared to other compilers, such as GCC. In October 2007, they report that Clang compiled the Carbon libraries more than twice as fast as GCC, while using about one-sixth GCC's memory and disk space.[25] By 2011, Clang seemed to retain this advantage in compiler performance.[26][27] As of mid-2014, Clang still consistently compiles faster than GCC in a mixed compile time and program performance benchmark.[28] However, by 2019, Clang is significantly slower at compiling the Linux Kernel than GCC while remaining slightly faster at compiling LLVM.[29]
While Clang has historically been faster than GCC at compiling, the output quality has lagged behind. As of 2014, performance of Clang-compiled programs lagged behind performance of the GCC-compiled program, sometimes by large factors (up to 5.5x),[28] replicating earlier reports of slower performance.[26] Both compilers have evolved to increase their performance since then, with the gap narrowing:
Comparisons in November 2016 between GCC 4.8.2 versus clang 3.4, on a large harness of test files shows that GCC outperforms clang by approximately 17% on well-optimized source code. Test results are code-specific, and unoptimized C source code can reverse such differences. The two compilers thus seem broadly comparable.[30][unreliable source]
Comparisons in 2019 on Intel Ice Lake has shown that programs generated by Clang 10 has achieved 96% of the performance of GCC 10 over 41 different benchmarks (while winning 22 and losing 19 out of them).[29]
In 2023, another comparison conducted four years later revealed that programs compiled using Clang now match the performance of those compiled with GCC. On average, Clang 16 surpasses GCC 13 by 6%.[31]
Interface
libclang provides a C interface, providing a relatively small API. Exposed functionality includes: parsing source code into an AST, loading ASTs, traversing the AST, associating source locations with elements within the AST.
Status history
This table presents only significant steps and releases in Clang history.
Date
Highlights
11 July 2007
Clang front-end released under open-source licence
Clang 1.0 released, with LLVM 2.6 for the first time.
December 2009
Code generation for C and Objective-C reach production quality. Support for C++ and Objective-C++ still incomplete. Clang C++ can parse GCC 4.2 libstdc++ and generate working code for non-trivial programs,[20] and can compile itself.[36]
Clang 3.5 can rebuild 94.3% of the Debian archive. The percentage of failures has dropped by 1.2% per release since January 2013, mainly due to increased compatibility with GCC flags.[51]
^"LLVM 2.1 Release Notes". llvm.org. LLVM Developer Group. September 27, 2007. Archived from the original on November 17, 2016. Retrieved April 30, 2018.
^"LLVM 19.1.4". November 19, 2024. Retrieved November 19, 2024.
^"Releases". llvm – llvm-project repo. LLVM Developer Group. August 27, 2021. Archived from the original on September 12, 2021. Retrieved September 12, 2021 – via GitHub.com.
^ abc"LICENSE.TXT", llvm.org, LLVM Developer Group, archived from the original on November 11, 2020, retrieved September 24, 2019
^ abc"LLVM Developer Policy", llvm.org, LLVM Developer Group, § Copyright, License, and Patents, archived from the original on November 13, 2012, retrieved September 12, 2021
^"Clang Language Extensions". Archived from the original on February 24, 2014. Retrieved November 8, 2017. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions.
^"Getting Involved with the Clang Project", clang.llvm.org, LLVM Developer Group, archived from the original on April 27, 2018, retrieved September 18, 2012, Clang is a subproject of the LLVM Project, but has its own mailing lists because the communities have people with different interests.
^Zadeck, Kenneth (November 19, 2005). "Re: LLVM/GCC Integration Proposal". GCC development (Mailing list). Archived from the original on March 10, 2016. Retrieved July 25, 2016.
^Lattner, Chris (July 11, 2007). "New LLVM C front-end: "clang"". cfe-dev (Mailing list). Archived from the original on March 26, 2020. Retrieved March 26, 2020.
^ abcSimonis, Volker (February 10, 2011). "Compiling the HotSpot VM with Clang". Archived from the original on February 18, 2011. Retrieved February 13, 2011. While the overall GCC compatibility is excellent and the compile times are impressive, the performance of the generated code is still lacking behind a recent GCC version.
^"Benchmarking LLVM & Clang Against GCC 4.5". Phoronix. April 21, 2010. Archived from the original on November 2, 2016. Retrieved February 13, 2011. Binaries from LLVM-GCC and Clang both struggled to compete with GCC 4.5.0 in the timed HMMer benchmark of a Pfam database search. LLVM-GCC and Clang were about 23% slower(...)Though LLVM / Clang isn't the performance champion at this point, both components continue to be under very active development and there will hopefully be more news to report in the coming months