Clang#Design
{{Short description|Compiler front-end}}
{{About|the compiler|other uses|Clang (disambiguation)}}
{{Not to be confused with|C (programming language)}}
{{Use mdy dates|date=January 2021}}
{{Infobox software
| name = Clang
| logo =
| screenshot = Clang 13.0.1 version information screenshot.png
| caption = Clang 13.0.1
| author = Chris Lattner
| developer = LLVM Developer Group
| released = {{Start date and age|2007|09|26}}{{Cite web|url=https://www.llvm.org/releases/2.1/docs/ReleaseNotes.html|title=LLVM 2.1 Release Notes|website=llvm.org|publisher=LLVM Developer Group|date=27 September 2007|access-date=April 30, 2018|archive-date=November 17, 2016|archive-url=https://web.archive.org/web/20161117042311/http://llvm.org/releases/2.1/docs/ReleaseNotes.html|url-status=live}}
| latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}}
| latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}}
| latest preview version = {{wikidata|property|preferred|references|edit|Q864915|P348|P548=Q1072356}}
| latest preview date = {{wikidata|qualifier|preferred|single|Q864915|P348|P548=Q1072356|P577}}
| programming language = C++
| operating system = Unix-like
| platform = AArch64, ARMv7, IA-32, x86-64, ppc64le{{Cite web |url=https://github.com/llvm/llvm-project/releases |title=Releases |work=llvm – llvm-project repo |publisher=LLVM Developer Group |via=GitHub.com |date=27 August 2021 |access-date=September 12, 2021 |archive-date=September 12, 2021 |archive-url=https://web.archive.org/web/20210912214255/https://github.com/llvm/llvm-project/releases |url-status=live}}
| language =
| genre = Compiler front end
| license = Apache 2.0 with LLVM Exceptions
| website = {{URL|https://clang.llvm.org}}
}}
Clang ({{IPAc-en|ˈ|k|l|æ|ŋ}}){{Cite mailing list |url=https://lists.llvm.org/pipermail/llvm-dev/2008-July/015629.html |title=simply wonder pronunciation of Clang |date=3 July 2008 |access-date=2015-09-22 |mailing-list=LLVMdev |last=Christopher |first=Eric}} is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, and the software frameworks OpenMP,{{cite web |title=OpenMP Support |url=http://blog.llvm.org/2015/05/openmp-support_22.html |website=LLVM Project Blog |date=May 22, 2015 |access-date=28 March 2016 |archive-date=March 26, 2016 |archive-url=https://web.archive.org/web/20160326042624/http://blog.llvm.org/2015/05/openmp-support_22.html |url-status=live}} OpenCL, RenderScript, CUDA, SYCL, and HIP.{{cite web |title=C++ Heterogeneous-Compute Interface for Portability |website=GitHub |url=https://github.com/ROCm-Developer-Tools/HIP |access-date=18 February 2020 |archive-date=June 27, 2020 |archive-url=https://web.archive.org/web/20200627204139/https://github.com/ROCm-Developer-Tools/HIP |url-status=live}} It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compiling flags and unofficial language extensions.{{cite web |url=https://clang.llvm.org/docs/LanguageExtensions.html |title=Clang Language Extensions |access-date=2017-11-08 |quote=In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. |archive-date=February 24, 2014 |archive-url=https://web.archive.org/web/20140224002333/http://clang.llvm.org/docs/LanguageExtensions.html |url-status=live}} It includes a static analyzer, and several code analysis tools.{{cite web |url=https://clang-analyzer.llvm.org/ |title=Clang Static Analyzer |publisher=LLVM |access-date=3 September 2009 |archive-date=April 30, 2018 |archive-url=https://web.archive.org/web/20180430013035/http://clang-analyzer.llvm.org/ |url-status=live}}
Clang operates in tandem with the LLVM compiler back end and has been a subproject of LLVM 2.6 and later.{{citation |url=https://clang.llvm.org/get_involved.html |title=Getting Involved with the Clang Project |website=clang.llvm.org |publisher=LLVM Developer Group |access-date=2012-09-18 |quote=Clang is a subproject of the LLVM Project, but has its own mailing lists because the communities have people with different interests. |archive-date=April 27, 2018 |archive-url=https://web.archive.org/web/20180427112311/http://clang.llvm.org/get_involved.html |url-status=live}} As with LLVM, it is free and open-source software under the Apache 2.0 software license.{{citation |url=https://releases.llvm.org/9.0.0/LICENSE.TXT |title=LICENSE.TXT |website=llvm.org |publisher=LLVM Developer Group |access-date=2019-09-24 |archive-date=November 11, 2020 |archive-url=https://web.archive.org/web/20201111220059/https://releases.llvm.org/9.0.0/LICENSE.TXT |url-status=live}}{{Citation |url=https://llvm.org/docs/DeveloperPolicy.html#copyright-license-and-patents |title=LLVM Developer Policy |website=llvm.org |publisher=LLVM Developer Group |at=§ Copyright, License, and Patents |access-date=September 12, 2021 |archive-date=November 13, 2012 |archive-url=https://web.archive.org/web/20121113204817/https://www.llvm.org/docs/DeveloperPolicy.html#copyright-license-and-patents |url-status=live}} 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 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.{{cite web |title=Clang 17.0.1 Release Notes |url=https://releases.llvm.org/17.0.1/tools/clang/docs/ReleaseNotes.html|archive-url= |archive-date= |access-date=October 25, 2023 |publisher=LLVM}}
Background
In 2005, Apple Inc. made extensive use of LLVM in several commercial products,{{cite mailing list|first=Adam|last=Treat|date=19 February 2005|url=http://lists.trolltech.com/qt4-preview-feedback/2005-02/msg00691.html|title=mkspecs and patches for LLVM compile of Qt4|mailing-list=Qt4-preview-feedback|url-status=dead|archive-url=https://web.archive.org/web/20111004073001/http://lists.trolltech.com/qt4-preview-feedback/2005-02/msg00691.html|archive-date=4 October 2011}} 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.{{cite conference
|first=Chris
|last=Lattner
|date=25 May 2007
|title=LLVM for OpenGL and other stuff
|format=Slides
|conference=LLVM Developers' Meeting
|conference-url=https://llvm.org/devmtg/2007-05/
|url=https://llvm.org/devmtg/2007-05/10-Lattner-OpenGL.pdf
|access-date=April 30, 2018
|archive-date=May 8, 2016
|archive-url=https://web.archive.org/web/20160508213227/http://llvm.org/devmtg/2007-05/10-Lattner-OpenGL.pdf
|url-status=live
}}
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".{{cite mailing list
| first=Kenneth
| last=Zadeck
| date=19 November 2005
| url=https://gcc.gnu.org/ml/gcc/2005-11/msg00918.html
| title=Re: LLVM/GCC Integration Proposal
| mailing-list=GCC development
| access-date=July 25, 2016
| archive-date=March 10, 2016
| archive-url=https://web.archive.org/web/20160310023249/https://gcc.gnu.org/ml/gcc/2005-11/msg00918.html
| url-status=live
}} 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).{{cite conference
| first=Steve
| last=Naroff
| date=25 May 2007
| title=New LLVM C Front-end
| format=Slides
| url=https://llvm.org/devmtg/2007-05/09-Naroff-CFE.pdf
| conference=LLVM Developers' Meeting
| conference-url=https://llvm.org/devmtg/2007-05/
| access-date=April 30, 2018
| archive-date=April 3, 2019
| archive-url=https://web.archive.org/web/20190403123249/http://llvm.org/devmtg/2007-05/09-Naroff-CFE.pdf
| url-status=live
}} 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.{{cite mailing list
| first=Chris
| last=Lattner
| date=11 July 2007
| url=https://lists.llvm.org/pipermail/cfe-dev/2007-July/000000.html
| title=New LLVM C front-end: "clang"
| mailing-list=cfe-dev
| access-date=March 26, 2020
| archive-date=March 26, 2020
| archive-url=https://web.archive.org/web/20200326194539/https://lists.llvm.org/pipermail/cfe-dev/2007-July/000000.html
| url-status=live
}}
Design
Clang works in tandem with LLVM.Clang team, [https://clang.llvm.org/ clang: a C language family frontend for LLVM] {{Webarchive|url=https://web.archive.org/web/20171112232029/http://clang.llvm.org/ |date=November 12, 2017 }} 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,{{Cite web |url=https://clang.llvm.org/features.html#libraryarch |title=Clang: Utility and Applications: Library Based Architecture |access-date=April 30, 2018 |archive-date=April 27, 2018 |archive-url=https://web.archive.org/web/20180427121756/http://clang.llvm.org/features.html#libraryarch |url-status=live}} 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|date=June 2008}} 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.{{Cite web |url=https://phoronix.com/scan.php?page=news_item&px=LLVM-NVIDIA-Fortran-Flang |title=FLANG: NVIDIA Brings Fortran To LLVM |access-date=May 18, 2017 |archive-date=May 20, 2017 |archive-url=https://web.archive.org/web/20170520182339/http://www.phoronix.com/scan.php?page=news_item&px=LLVM-NVIDIA-Fortran-Flang |url-status=live}} 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|2023|10|lc=y}} and could be downloaded from the LLVM Project.{{Cite web |url=https://flang.llvm.org/docs/ |title=Welcome to Flang's documentation — the Flang Compiler}}
Performance and GCC compatibility
File:Clang 12.0.1 compiling htop.png]]
Clang is compatible with GCC.{{citation |url=https://clang.llvm.org/features.html#gcccompat |title=Clang - Features and Goals: GCC Compatibility |date=15 April 2013 |access-date=April 30, 2018 |archive-date=April 27, 2018 |archive-url=https://web.archive.org/web/20180427121756/http://clang.llvm.org/features.html#gcccompat |url-status=live }} 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.{{citation |url=https://clang.llvm.org/get_started.html#driver |title=Clang can often be used as a drop-in replacement for GCC |date=May 2020 |access-date=May 9, 2020 |archive-date=May 8, 2020 |archive-url=https://web.archive.org/web/20200508185500/http://clang.llvm.org/get_started.html#driver |url-status=live}}
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.{{citation |url=https://clang.llvm.org/features.html |title=Clang - Features and Goals: Fast compiles and Low Memory Use |date=October 2007 |access-date=April 30, 2018 |archive-date=April 27, 2018 |archive-url=https://web.archive.org/web/20180427121756/http://clang.llvm.org/features.html |url-status=live }} By 2011, Clang seemed to retain this advantage in compiler performance.{{cite web|url=https://weblogs.java.net/blog/simonis/archive/2011/02/10/compiling-hotspot-vm-clang |first=Volker |last=Simonis |title=Compiling the HotSpot VM with Clang |date=10 February 2011 |quote=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. |access-date=13 February 2011 |url-status=dead |archive-url=https://web.archive.org/web/20110218013535/http://weblogs.java.net/blog/simonis/archive/2011/02/10/compiling-hotspot-vm-clang |archive-date=18 February 2011 }}{{cite web
| url=https://www.phoronix.com/scan.php?page=article&item=gcc_llvm_clang&num=6
| publisher=Phoronix
| title=Benchmarking LLVM & Clang Against GCC 4.5
| date=21 April 2010
| access-date=13 February 2011
| quote=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
| archive-date=November 2, 2016
| archive-url=https://web.archive.org/web/20161102222609/http://www.phoronix.com/scan.php?page=article&item=gcc_llvm_clang&num=6
| url-status=live
}} As of mid-2014, Clang still consistently compiles faster than GCC in a mixed compile time and program performance benchmark.{{cite web
| url=https://openbenchmarking.org/result/1404144-KH-CLANG359076
| publisher=OpenBenchmarking.org
| title=GCC 4.9 VS. LLVM Clang 3.5 Linux Compiler Benchmarks
| date=14 April 2014
| access-date=25 June 2014
| archive-date=October 23, 2017
| archive-url=https://web.archive.org/web/20171023202436/http://openbenchmarking.org/result/1404144-KH-CLANG359076
| url-status=live
}} 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.{{Cite web|url=https://stackoverflow.com/questions/3187414/clang-vs-gcc-which-produces-better-binaries#15043814|title=Clang vs GCC - which produces better binaries?|website=stackoverflow.com|access-date=2018-10-01|archive-date=June 1, 2017|archive-url=https://web.archive.org/web/20170601085417/https://stackoverflow.com/questions/3187414/clang-vs-gcc-which-produces-better-binaries#15043814|url-status=live}}{{Unreliable source?|certain=y|date=October 2018}}
- 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).{{cite web|url=https://www.phoronix.com/scan.php?page=news_item&px=GCC-LLVM-Clang-Icelake-Tests|title=LLVM Clang Achieves ~96% The Performance Of GCC On Intel Ice Lake|author=Michael Larabel|date=Dec 23, 2019|access-date=January 14, 2021|archive-date=January 15, 2021|archive-url=https://web.archive.org/web/20210115185420/https://www.phoronix.com/scan.php?page=news_item&px=GCC-LLVM-Clang-Icelake-Tests|url-status=live}}
- Another comparison conducted in 2023 revealed that programs compiled using Clang now match the performance of those compiled with GCC. On average, Clang 16 surpasses GCC 13 by 6%.{{cite web|url=https://www.phoronix.com/review/gcc13-clang16-raptorlake/5|title=LLVM Clang 16 vs. GCC 13 Compiler Performance On Intel Raptor Lake|author=Michael Larabel|date=May 11, 2023|access-date=April 24, 2024}}
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.
See also
{{Portal|Free and open-source software}}
References
{{Reflist}}