Clang Explained
Clang [3] is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP,[4] OpenCL, RenderScript, CUDA, SYCL, and HIP[5] frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.[6] It includes a static analyzer, and several code analysis tools.[7]
Clang operates in tandem with the LLVM compiler back end and has been a subproject of LLVM 2.6 and later. As with LLVM, it is free and open-source software under the Apache License 2.0 software license. Its contributors include Apple, Microsoft, Google, ARM, Sony, Intel, and AMD.
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. 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.[8]
Background
In 2005, Apple Inc. made extensive use of LLVM in several commercial products,[9] 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.[10]
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".[11] 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).[12] 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.
For these reasons, Apple developed Clang, a new compiler front end which supports C, Objective-C and C++. In July 2007, the project received the approval for becoming open-source.[13]
Design
Clang works in tandem with LLVM.[14] 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,[15] 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. 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.[16] 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 and could be downloaded from the LLVM Project.[17]
Performance and GCC compatibility
Clang is compatible with GCC. 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 libstdc++. Clang also maintains ABI compatibility with GCC-generated object code. In practice, Clang is a drop-in replacement for GCC.
Clang's developers aim to reduce memory footprint and increase compilation speed compared to competing 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. By 2011, Clang seemed to retain this advantage in compiler performance.[18] [19] As of mid-2014, Clang still consistently compiles faster than GCC in a mixed compile time and program performance benchmark.[20] However, by 2019, Clang is significantly slower at compiling the Linux Kernel than GCC while remaining slightly faster at compiling LLVM.
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), replicating earlier reports of slower performance. 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.[21]
- 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).[22]
- 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%.[23]
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 |
25 February 2009 | Clang/LLVM can compile a working FreeBSD kernel.[24] |
16 March 2009 | Clang/LLVM can compile a working DragonFly BSD kernel.[25] |
23 October 2009 | 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, and can compile itself.[26] |
2 February 2010 | Clang self-hosting.[27] |
20 May 2010 | Clang latest version built the Boost C++ libraries successfully, and passed nearly all tests.[28] |
10 June 2010 | Clang/LLVM becomes integral part of FreeBSD, but default compiler is still GCC.[29] |
25 October 2010 | Clang/LLVM can compile a working modified Linux kernel. |
January 2011 | Preliminary work completed to support the draft C++0x standard, with a few of the draft's new features supported in Clang development version.[30] [31] |
10 February 2011 | Clang can compile a working HotSpot Java virtual machine. |
19 January 2012 | Clang becomes an optional component in NetBSD cross-platform build system, but GCC is still default.[32] |
29 February 2012 | Clang 3.0 can rebuild 91.2% of the Debian archive.[33] |
29 February 2012 | Clang becomes default compiler in MINIX 3[34] |
12 May 2012 | Clang/LLVM announced to replace GCC in FreeBSD.[35] |
5 November 2012 | Clang becomes default compiler in FreeBSD 10.x on amd64/i386.[36] |
18 February 2013 | Clang/LLVM can compile a working modified Android Linux Kernel for Nexus 7.[37] [38] |
19 April 2013 | Clang is C++11 feature complete.[39] |
6 November 2013 | Clang is C++14 feature complete.[40] |
11 September 2014 | 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.[41] |
October 2016 | Clang becomes default compiler for Android[42] (and later only compiler supported by Android NDK[43]). |
13 March 2017 | Clang 4.0.0 released |
26 July 2017 | Clang becomes default compiler in OpenBSD 6.2 on amd64/i386.[44] |
7 September 2017 | Clang 5.0.0 released |
19 January 2018 | Clang becomes default compiler in OpenBSD 6.3 on arm.[45] |
5 March 2018 | Clang is now used to build Google Chrome for Windows.[46] |
8 March 2018 | Clang 6.0.0 released |
5 September 2018 | Clang is now used to build Firefox for Windows.[47] |
19 September 2018 | Clang 7.0.0 released |
20 March 2019 | Clang 8.0.0 released |
1 July 2019 | Clang becomes default compiler in OpenBSD 6.6 on mips64.[48] |
19 September 2019 | Clang 9.0.0 released with official RISC-V target support.[49] |
29 February 2020 | Clang becomes the only C compiler in the FreeBSD base system, with the removal of GCC.[50] |
24 March 2020 | Clang 10.0.0 released |
2 April 2020 | Clang becomes default compiler in OpenBSD 6.7 on powerpc.[51] |
12 October 2020 | Clang 11.0.0 released |
21 December 2020 | Clang becomes default compiler in OpenBSD 6.9 on mips64el.[52] |
14 April 2021 | Clang 12.0.0 released |
4 October 2021 | Clang 13.0.0 released |
25 March 2022 | Clang 14.0.0 released |
6 September 2022 | Clang 15.0.0 released |
17 March 2023 | Clang 16.0.0 released |
9 September 2023 | Clang 17.0.1 released | |
See also
Notes and References
- Web site: LLVM 2.1 Release Notes. llvm.org. LLVM Developer Group. 27 September 2007. April 30, 2018. November 17, 2016. https://web.archive.org/web/20161117042311/http://llvm.org/releases/2.1/docs/ReleaseNotes.html. live.
- Web site: Releases . llvm / llvm-project repo . LLVM Developer Group . . 27 August 2021 . September 12, 2021 . September 12, 2021 . https://web.archive.org/web/20210912214255/https://github.com/llvm/llvm-project/releases . live .
- simply wonder pronunciation of Clang . 3 July 2008 . 2015-09-22 . LLVMdev . Christopher . Eric.
- Web site: OpenMP Support . LLVM Project Blog . May 22, 2015 . 28 March 2016 . March 26, 2016 . https://web.archive.org/web/20160326042624/http://blog.llvm.org/2015/05/openmp-support_22.html . live .
- Web site: C++ Heterogeneous-Compute Interface for Portability . . 18 February 2020 . June 27, 2020 . https://web.archive.org/web/20200627204139/https://github.com/ROCm-Developer-Tools/HIP . live .
- Web site: Clang Language Extensions . 2017-11-08 . In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. . February 24, 2014 . https://web.archive.org/web/20140224002333/http://clang.llvm.org/docs/LanguageExtensions.html . live .
- Web site: Clang Static Analyzer . LLVM . 3 September 2009 . April 30, 2018 . https://web.archive.org/web/20180430013035/http://clang-analyzer.llvm.org/ . live .
- Web site: Clang 17.0.1 Release Notes . October 25, 2023 . LLVM.
- Adam. Treat. 19 February 2005. mkspecs and patches for LLVM compile of Qt4. Qt4-preview-feedback. dead. https://web.archive.org/web/20111004073001/http://lists.trolltech.com/qt4-preview-feedback/2005-02/msg00691.html. 4 October 2011.
- Chris . Lattner . 25 May 2007 . LLVM for OpenGL and other stuff . Slides . LLVM Developers' Meeting . https://llvm.org/devmtg/2007-05/ . April 30, 2018 . May 8, 2016 . https://web.archive.org/web/20160508213227/http://llvm.org/devmtg/2007-05/10-Lattner-OpenGL.pdf . live .
- Kenneth . Zadeck . 19 November 2005 . Re: LLVM/GCC Integration Proposal . GCC development . July 25, 2016 . March 10, 2016 . https://web.archive.org/web/20160310023249/https://gcc.gnu.org/ml/gcc/2005-11/msg00918.html . live .
- Steve . Naroff . 25 May 2007 . New LLVM C Front-end . Slides . LLVM Developers' Meeting . https://llvm.org/devmtg/2007-05/ . April 30, 2018 . April 3, 2019 . https://web.archive.org/web/20190403123249/http://llvm.org/devmtg/2007-05/09-Naroff-CFE.pdf . live .
- Chris . Lattner . 11 July 2007 . New LLVM C front-end: "clang" . cfe-dev . March 26, 2020 . March 26, 2020 . https://web.archive.org/web/20200326194539/https://lists.llvm.org/pipermail/cfe-dev/2007-July/000000.html . live .
- Clang team, clang: a C language family frontend for LLVM
- Web site: Clang: Utility and Applications: Library Based Architecture . April 30, 2018 . April 27, 2018 . https://web.archive.org/web/20180427121756/http://clang.llvm.org/features.html#libraryarch . live .
- Web site: FLANG: NVIDIA Brings Fortran To LLVM . May 18, 2017 . May 20, 2017 . https://web.archive.org/web/20170520182339/http://www.phoronix.com/scan.php?page=news_item&px=LLVM-NVIDIA-Fortran-Flang . live .
- Web site: Welcome to Flang's documentation — the Flang Compiler .
- Web site: Compiling the HotSpot VM with Clang . Volker . Simonis . 10 February 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. . 13 February 2011 . dead . https://web.archive.org/web/20110218013535/http://weblogs.java.net/blog/simonis/archive/2011/02/10/compiling-hotspot-vm-clang . 18 February 2011 .
- Web site: Benchmarking LLVM & Clang Against GCC 4.5. Phoronix. 21 April 2010. 13 February 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. November 2, 2016. https://web.archive.org/web/20161102222609/http://www.phoronix.com/scan.php?page=article&item=gcc_llvm_clang&num=6. live.
- Web site: GCC 4.9 VS. LLVM Clang 3.5 Linux Compiler Benchmarks. OpenBenchmarking.org. 14 April 2014. 25 June 2014. October 23, 2017. https://web.archive.org/web/20171023202436/http://openbenchmarking.org/result/1404144-KH-CLANG359076. live.
- Web site: Clang vs GCC - which produces better binaries?. stackoverflow.com. 2018-10-01. June 1, 2017. https://web.archive.org/web/20170601085417/https://stackoverflow.com/questions/3187414/clang-vs-gcc-which-produces-better-binaries#15043814. live.
- Web site: LLVM Clang Achieves ~96% The Performance Of GCC On Intel Ice Lake. Michael Larabel. Dec 23, 2019. January 14, 2021. January 15, 2021. https://web.archive.org/web/20210115185420/https://www.phoronix.com/scan.php?page=news_item&px=GCC-LLVM-Clang-Icelake-Tests. live.
- Web site: LLVM Clang 16 vs. GCC 13 Compiler Performance On Intel Raptor Lake. Michael Larabel. May 11, 2023. April 24, 2024.
- Web site: Roman . Divacky . [Announce] clang/llvm can compile booting FreeBSD kernel on i386/amd64 . February 25, 2009 . April 30, 2018 . May 1, 2018 . https://web.archive.org/web/20180501004409/https://lists.freebsd.org/pipermail/freebsd-current/2009-February/003743.html . live .
- Web site: Alex . Hornung . llvm/clang once more . April 30, 2018 . April 30, 2018 . https://web.archive.org/web/20180430182720/https://leaf.dragonflybsd.org/mailarchive/kernel/2009-03/msg00067.html . live .
- Web site: LLVM Project Blog . Clang can compile LLVM and Clang . December 24, 2009 . December 31, 2009 . April 26, 2012 . https://web.archive.org/web/20120426000005/http://blog.llvm.org/2009/12/clang-builds-llvm.html . live .
- Web site: LLVM Project Blog . Clang Successfully Self-Hosts . February 4, 2010 . February 5, 2010 . April 26, 2012 . https://web.archive.org/web/20120426000008/http://blog.llvm.org/2010/02/clang-successfully-self-hosts.html . live .
- Web site: Clang++ Builds Boost! . Doug . Gregor . May 20, 2010 . LLVM Project Blog . May 21, 2010 . April 26, 2012 . https://web.archive.org/web/20120426000011/http://blog.llvm.org/2010/05/clang-builds-boost.html . live .
- Web site: Brad . Davis . FreeBSD Status Reports April–June, 2010 . April 30, 2018 . June 24, 2018 . https://web.archive.org/web/20180624151136/https://www.freebsd.org/news/status/report-2010-04-2010-06.html#Clang-Replacing-GCC-in-the-Base-System . live .
- New C++0x feature support in Clang . Douglas . Gregor . 26 January 2011 . 29 January 2011 . dead . https://web.archive.org/web/20110130065354/http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-January/013013.html . 30 January 2011 .
- Web site: LLVM . C++ and C++'0x Support in Clang . March 12, 2023 . July 4, 2013 . https://web.archive.org/web/20130704124639/http://clang.llvm.org/cxx_status.html . live .
- Web site: Status of NetBSD and LLVM . 2014-02-26 . Sonnenberger . Jörg . 2012-01-19 . https://web.archive.org/web/20160921050033/http://www.sonnenberger.org/2012/01/19/status-netbsd-and-llvm/ . 21 September 2016 . dead .
- Web site: Sylvestre . Ledru . Rebuild of the Debian archive with clang . April 30, 2018 . May 1, 2018 . https://web.archive.org/web/20180501004947/http://sylvestre.ledru.info/blog/2012/02/29/rebuild_of_the_debian_archive_with_clang . live .
- Web site: Official Minix 3 website: News . December 7, 2012 . December 17, 2009 . https://web.archive.org/web/20091217134619/http://www.minix3.org/news/ . live .
- FreeBSD Quarterly Status Report January–March, 2012 . Daniel . Gerzo . 12 May 2012 . 14 May 2012 . February 18, 2018 . https://web.archive.org/web/20180218091158/https://lists.freebsd.org/pipermail/freebsd-stable/2012-May/067486.html . live .
- Heads Up: Clang now the default on x86 . Brooks . Davis . 5 November 2012 . 7 November 2012 . April 30, 2018 . https://web.archive.org/web/20180430183152/https://lists.freebsd.org/pipermail/freebsd-current/2012-November/037610.html . live .
- Web site: LLVMLinux: Compiling Android with LLVM . https://web.archive.org/web/20140512210100/http://events.linuxfoundation.org/images/stories/slides/abs2013_webster.pdf . dead . May 12, 2014 . Behan . Webster . 18 February 2013 . 11 May 2013 .
- Web site: LLVMLinux: Nexus 7 . https://web.archive.org/web/20140611050105/http://llvm.linuxfoundation.org/index.php/Nexus_7 . dead . June 11, 2014 . Vinicius . Tinti . 17 March 2013 . 11 May 2013 .
- Web site: Stefanus . Du Toit . Clang is C++11 feature complete as of *just now*! . April 20, 2013 . September 24, 2019 . https://web.archive.org/web/20190924204137/https://twitter.com/stefanusdutoit/status/325289093170864129 . live .
- Web site: [llvm-project] Revision 194194 ]. April 30, 2018 . November 18, 2018 . https://web.archive.org/web/20181118095338/http://llvm.org/viewvc/llvm-project?view=revision&revision=194194 . dead .
- Web site: Sylvestre . Ledru . Rebuild of Debian using Clang 3.5.0 . April 30, 2018 . May 1, 2018 . https://web.archive.org/web/20180501093222/http://sylvestre.ledru.info/blog/2014/09/11/rebuild-of-debian-using-clang-3-5 . live .
- Web site: NDK Revision History Android Developers. developer.android.com. en. 2018-03-06. March 6, 2018. https://web.archive.org/web/20180306202823/https://developer.android.com/ndk/downloads/revision_history.html. live.
- Web site: NDK Revision History Android NDK Android Developers. Android Developers. en. 2018-04-30. April 5, 2019. https://web.archive.org/web/20190405155532/https://developer.android.com/ndk/downloads/revision_history. live.
- switch the default compiler on amd64 and i386 to clang . Robert . Nagy . 26 July 2017 . 30 September 2017 . October 25, 2018 . https://web.archive.org/web/20181025144820/https://marc.info/?l=openbsd-cvs&m=150109829003860&w=2 . live .
- Switch the default compiler on armv7 to clang. . Mark . Kettenis . 19 January 2018 . 27 August 2020 . April 8, 2022 . https://web.archive.org/web/20220408000151/https://marc.info/?l=openbsd-cvs&m=151637866226630&w=2 . live .
- Web site: Clang is now used to build Chrome for Windows. blog.llvm.org. March 5, 2018. 2018-04-07. March 5, 2018. https://web.archive.org/web/20180305220009/http://blog.llvm.org/2018/03/clang-is-now-used-to-build-chrome-for.html. live.
- Web site: Firefox Beta 63.0beta, See All New Features, Updates and Fixes. www.mozilla.org. en. 2020-03-18. April 19, 2021. https://web.archive.org/web/20210419121201/https://www.mozilla.org/en-US/firefox/63.0beta/releasenotes/. live.
- Switch the default compiler on octeon to clang. . Visa . Hankala . 1 July 2019 . 27 August 2020 . April 8, 2022 . https://web.archive.org/web/20220408000151/https://marc.info/?l=openbsd-cvs&m=156199443031362&w=2 . live .
- Web site: LLVM 9 Releases With Official RISC-V Target Support, Asm Goto, Clang 9, And More. riscv.org. 2019-09-26. 26 September 2019. December 28, 2019. https://web.archive.org/web/20191228001225/https://riscv.org/2019/09/llvm-9-releases-with-official-risc-v-target-support-asm-goto-clang-9-and-more-vincy-davis-packt-pub/. live.
- Web site: FreeBSD svn commit r358454. February 29, 2020 . March 7, 2020. March 10, 2020. https://web.archive.org/web/20200310155542/https://lists.freebsd.org/pipermail/svn-src-all/2020-February/194446.html. live.
- Switch the default compiler on powerpc to clang. . Mark . Kettenis . 2 April 2020 . 27 August 2020 . April 8, 2022 . https://web.archive.org/web/20220408000153/https://marc.info/?l=openbsd-cvs&m=158585034211462&w=2 . live .
- Make clang the default compiler on loongson. . Visa . Hankala . 21 December 2020 . 1 January 2021 . January 13, 2021 . https://web.archive.org/web/20210113033833/https://marc.info/?l=openbsd-cvs&m=160861154515848&w=2 . live .