Software:Clang

From HandWiki
Short description: Compiler front-end
Clang
Clang 13.0.1 version information screenshot.png
Clang 13.0.1
Original author(s)Chris Lattner
Developer(s)LLVM Developer Group
Initial releaseSeptember 26, 2007; 16 years ago (2007-09-26)[1]
Written inC++
Operating systemUnix-like
PlatformAArch64, ARMv7, IA-32, x86-64, ppc64le[2]
TypeCompiler front end
LicenseApache License 2.0 with LLVM Exceptions[3][4]
Websiteclang.llvm.org

Clang (/ˈklæŋ/)[5] is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP,[6] OpenCL, RenderScript, CUDA, SYCL, and HIP[7] frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.[8][9] It includes a static analyzer, and several code analysis tools.[10]

Clang operates in tandem with the LLVM compiler back end and has been a subproject of LLVM 2.6 and later.[11] As with LLVM, it is free and open-source software under the Apache License 2.0 software license.[3][4] 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.[12] 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.[13]

Background

Starting in 2005, Apple Inc. began extensive use of LLVM in several commercial products,[14] including the iOS SDK and Xcode 3.1. One of the first uses of LLVM was 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. This allowed Apple to support OpenGL on computers using Intel GMA chipsets, increasing performance on those machines.[15]

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".[16] 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).[17] 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.[3][4]

In the end, Apple chose to develop Clang, a new compiler front end that supports C, Objective-C and C++.[17] In July 2007, the project received the approval for becoming open-source.[18]

Design

Clang works in tandem with LLVM.[19] 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,[20] 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.[21] 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) and could be downloaded from the LLVM Project.[22]

Performance and GCC compatibility

Clang compiling htop

Clang is compatible with GCC.[9] 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.[23]

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.[24] By 2011, Clang seemed to retain this advantage in compiler performance.[25][26] As of mid-2014, Clang still consistently compiles faster than GCC in a mixed compile time and program performance benchmark.[27] However, by 2019, Clang is significantly slower at compiling the Linux Kernel than GCC while remaining slightly faster at compiling LLVM.[28]

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),[27] replicating earlier reports of slower performance.[25] 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.[29][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).[28]

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.[30][31]
16 March 2009 Clang/LLVM can compile a working DragonFly BSD kernel.[32][33]
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,[19] and can compile itself.[34]
2 February 2010 Clang self-hosting.[35]
20 May 2010 Clang latest version built the Boost C++ libraries successfully, and passed nearly all tests.[36]
10 June 2010 Clang/LLVM becomes integral part of FreeBSD, but default compiler is still GCC.[37]
25 October 2010 Clang/LLVM can compile a working modified Linux kernel.[38]
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.[39][12]
10 February 2011 Clang can compile a working HotSpot Java virtual machine.[25]
19 January 2012 Clang becomes an optional component in NetBSD cross-platform build system, but GCC is still default.[40]
29 February 2012 Clang 3.0 can rebuild 91.2% of the Debian archive.[41]
29 February 2012 Clang becomes default compiler in MINIX 3[42]
12 May 2012 Clang/LLVM announced to replace GCC in FreeBSD.[43]
5 November 2012 Clang becomes default compiler in FreeBSD 10.x on amd64/i386.[44]
18 February 2013 Clang/LLVM can compile a working modified Android Linux Kernel for Nexus 7.[45][46]
19 April 2013 Clang is C++11 feature complete.[47]
6 November 2013 Clang is C++14 feature complete.[48]
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.[49]
October 2016 Clang becomes default compiler for Android[50] (and later only compiler supported by Android NDK[51]).
13 March 2017 Clang 4.0.0 released
26 July 2017 Clang becomes default compiler in OpenBSD 6.2 on amd64/i386.[52]
7 September 2017 Clang 5.0.0 released
19 January 2018 Clang becomes default compiler in OpenBSD 6.3 on arm.[53]
5 March 2018 Clang is now used to build Google Chrome for Windows.[54]
8 March 2018 Clang 6.0.0 released
5 September 2018 Clang is now used to build Firefox for Windows.[55]
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.[56]
19 September 2019 Clang 9.0.0 released with official RISC-V target support.[57]
29 February 2020 Clang becomes the only C compiler in the FreeBSD base system, with the removal of GCC.[58]
24 March 2020 Clang 10.0.0 released
2 April 2020 Clang becomes default compiler in OpenBSD 6.7 on powerpc.[59]
12 October 2020 Clang 11.0.0 released
21 December 2020 Clang becomes default compiler in OpenBSD 6.9 on mips64el.[60]
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

References

  1. "LLVM 2.1 Release Notes". LLVM Developer Group. 27 September 2007. https://www.llvm.org/releases/2.1/docs/ReleaseNotes.html. 
  2. "Releases". llvm / llvm-project repo. LLVM Developer Group. 27 August 2021. https://github.com/llvm/llvm-project/releases. 
  3. 3.0 3.1 3.2 LICENSE.TXT, LLVM Developer Group, https://releases.llvm.org/9.0.0/LICENSE.TXT, retrieved 2019-09-24 
  4. 4.0 4.1 4.2 LLVM Developer Policy, LLVM Developer Group, § Copyright, License, and Patents, https://llvm.org/docs/DeveloperPolicy.html#copyright-license-and-patents, retrieved September 12, 2021 
  5. Christopher, Eric (3 July 2008). "simply wonder pronunciation of Clang". LLVMdev (Mailing list). Retrieved 2015-09-22.
  6. "OpenMP Support". May 22, 2015. http://blog.llvm.org/2015/05/openmp-support_22.html. 
  7. "C++ Heterogeneous-Compute Interface for Portability". https://github.com/ROCm-Developer-Tools/HIP. 
  8. "Clang Language Extensions". https://clang.llvm.org/docs/LanguageExtensions.html. "In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions." 
  9. 9.0 9.1 Clang - Features and Goals: GCC Compatibility, 15 April 2013, https://clang.llvm.org/features.html#gcccompat, retrieved April 30, 2018 
  10. "Clang Static Analyzer". LLVM. https://clang-analyzer.llvm.org/. 
  11. Getting Involved with the Clang Project, LLVM Developer Group, https://clang.llvm.org/get_involved.html, retrieved 2012-09-18, "Clang is a subproject of the LLVM Project, but has its own mailing lists because the communities have people with different interests." 
  12. 12.0 12.1 "C++ and C++'0x Support in Clang". LLVM. https://clang.llvm.org/cxx_status.html. 
  13. "Clang 17.0.1 Release Notes". LLVM. https://releases.llvm.org/17.0.1/tools/clang/docs/ReleaseNotes.html. 
  14. Treat, Adam (19 February 2005). "mkspecs and patches for LLVM compile of Qt4". Qt4-preview-feedback (Mailing list). Archived from the original on 4 October 2011.
  15. Lattner, Chris (25 May 2007). "LLVM for OpenGL and other stuff" (Slides). LLVM Developers' Meeting. https://llvm.org/devmtg/2007-05/10-Lattner-OpenGL.pdf. Retrieved April 30, 2018. 
  16. Zadeck, Kenneth (19 November 2005). "Re: LLVM/GCC Integration Proposal". GCC development (Mailing list). Archived from the original on March 10, 2016. Retrieved July 25, 2016.
  17. 17.0 17.1 Naroff, Steve (25 May 2007). "New LLVM C Front-end" (Slides). LLVM Developers' Meeting. https://llvm.org/devmtg/2007-05/09-Naroff-CFE.pdf. Retrieved April 30, 2018. 
  18. Lattner, Chris (11 July 2007). "New LLVM C front-end: "clang"". cfe-dev (Mailing list). Archived from the original on March 26, 2020. Retrieved March 26, 2020.
  19. 19.0 19.1 Clang team, clang: a C language family frontend for LLVM
  20. "Clang: Utility and Applications: Library Based Architecture". https://clang.llvm.org/features.html#libraryarch. 
  21. "FLANG: NVIDIA Brings Fortran To LLVM". https://phoronix.com/scan.php?page=news_item&px=LLVM-NVIDIA-Fortran-Flang. 
  22. "Welcome to Flang's documentation — the Flang Compiler". https://flang.llvm.org/docs/. 
  23. Clang can often be used as a drop-in replacement for GCC, May 2020, https://clang.llvm.org/get_started.html#driver, retrieved May 9, 2020 
  24. Clang - Features and Goals: Fast compiles and Low Memory Use, October 2007, https://clang.llvm.org/features.html, retrieved April 30, 2018 
  25. 25.0 25.1 25.2 Simonis, Volker (10 February 2011). "Compiling the HotSpot VM with Clang". https://weblogs.java.net/blog/simonis/archive/2011/02/10/compiling-hotspot-vm-clang. "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. " 
  26. "Benchmarking LLVM & Clang Against GCC 4.5". Phoronix. 21 April 2010. https://www.phoronix.com/scan.php?page=article&item=gcc_llvm_clang&num=6. "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" 
  27. 27.0 27.1 "GCC 4.9 VS. LLVM Clang 3.5 Linux Compiler Benchmarks". OpenBenchmarking.org. 14 April 2014. https://openbenchmarking.org/result/1404144-KH-CLANG359076. 
  28. 28.0 28.1 Michael Larabel (Dec 23, 2019). "LLVM Clang Achieves ~96% The Performance Of GCC On Intel Ice Lake". https://www.phoronix.com/scan.php?page=news_item&px=GCC-LLVM-Clang-Icelake-Tests. 
  29. "Clang vs GCC - which produces better binaries?". https://stackoverflow.com/questions/3187414/clang-vs-gcc-which-produces-better-binaries#15043814. 
  30. Divacky, Roman (February 25, 2009). "[Announce] clang/llvm can compile booting FreeBSD kernel on i386/amd64". https://lists.freebsd.org/pipermail/freebsd-current/2009-February/003743.html. 
  31. Building FreeBSD with Clang, Wiki.freebsd.org, 2012-08-24, https://wiki.freebsd.org/BuildingFreeBSDWithClang, retrieved 2012-09-18 
  32. Hornung, Alex. "llvm/clang once more". https://leaf.dragonflybsd.org/mailarchive/kernel/2009-03/msg00067.html. 
  33. Clang, DragonFly BSD, https://www.dragonflybsd.org/docs/developer/clang/, retrieved 2012-09-18 
  34. "Clang can compile LLVM and Clang". LLVM Project Blog. December 24, 2009. http://blog.llvm.org/2009/12/clang-builds-llvm.html. 
  35. "Clang Successfully Self-Hosts". LLVM Project Blog. February 4, 2010. http://blog.llvm.org/2010/02/clang-successfully-self-hosts.html. 
  36. Gregor, Doug (May 20, 2010). "Clang++ Builds Boost!". LLVM Project Blog. http://blog.llvm.org/2010/05/clang-builds-boost.html. 
  37. Davis, Brad. "FreeBSD Status Reports April–June, 2010". https://www.freebsd.org/news/status/report-2010-04-2010-06.html#Clang-Replacing-GCC-in-the-Base-System. 
  38. Clang builds a working Linux Kernel (Boots to RL5 with SMP, networking and X, self hosts), Lists.cs.uiuc.edu, http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-October/011711.html, retrieved 2012-09-18 
  39. Gregor, Douglas (26 January 2011). "New C++0x feature support in Clang" (Mailing list). Archived from the original on 30 January 2011. Retrieved 29 January 2011.
  40. Sonnenberger, Jörg (2012-01-19). "Status of NetBSD and LLVM". https://www.sonnenberger.org/2012/01/19/status-netbsd-and-llvm/. 
  41. Ledru, Sylvestre. "Rebuild of the Debian archive with clang". https://sylvestre.ledru.info/blog/2012/02/29/rebuild_of_the_debian_archive_with_clang. 
  42. "Official Minix 3 website: News". http://www.minix3.org/news/. 
  43. Gerzo, Daniel (12 May 2012). "FreeBSD Quarterly Status Report January–March, 2012" (Mailing list). Archived from the original on February 18, 2018. Retrieved 14 May 2012.
  44. Davis, Brooks (5 November 2012). "Heads Up: Clang now the default on x86" (Mailing list). Archived from the original on April 30, 2018. Retrieved 7 November 2012.
  45. Webster, Behan (18 February 2013). "LLVMLinux: Compiling Android with LLVM". https://events.linuxfoundation.org/images/stories/slides/abs2013_webster.pdf. 
  46. Tinti, Vinicius (17 March 2013). "LLVMLinux: Nexus 7". https://llvm.linuxfoundation.org/index.php/Nexus_7. 
  47. Du Toit, Stefanus. "Clang is C++11 feature complete as of *just now*!". https://twitter.com/sdt_intel/status/325289093170864129. 
  48. "[llvm-project Revision 194194"]. https://llvm.org/viewvc/llvm-project?view=revision&revision=194194. 
  49. Ledru, Sylvestre. "Rebuild of Debian using Clang 3.5.0". https://sylvestre.ledru.info/blog/2014/09/11/rebuild-of-debian-using-clang-3-5. 
  50. "NDK Revision History | Android Developers" (in en). https://developer.android.com/ndk/downloads/revision_history.html. 
  51. "NDK Revision History | Android NDK | Android Developers" (in en). https://developer.android.com/ndk/downloads/revision_history. 
  52. Nagy, Robert (26 July 2017). "switch the default compiler on amd64 and i386 to clang" (Mailing list). Archived from the original on October 25, 2018. Retrieved 30 September 2017.
  53. Kettenis, Mark (19 January 2018). "Switch the default compiler on armv7 to clang" (Mailing list). Archived from the original on April 8, 2022. Retrieved 27 August 2020.
  54. "Clang is now used to build Chrome for Windows". March 5, 2018. http://blog.llvm.org/2018/03/clang-is-now-used-to-build-chrome-for.html. 
  55. "Firefox Beta 63.0beta, See All New Features, Updates and Fixes" (in en). https://www.mozilla.org/en-US/firefox/63.0beta/releasenotes/. 
  56. Hankala, Visa (1 July 2019). "Switch the default compiler on octeon to clang" (Mailing list). Archived from the original on April 8, 2022. Retrieved 27 August 2020.
  57. "LLVM 9 Releases With Official RISC-V Target Support, Asm Goto, Clang 9, And More". 26 September 2019. https://riscv.org/2019/09/llvm-9-releases-with-official-risc-v-target-support-asm-goto-clang-9-and-more-vincy-davis-packt-pub/. 
  58. "FreeBSD svn commit r358454". February 29, 2020. https://lists.freebsd.org/pipermail/svn-src-all/2020-February/194446.html. 
  59. Kettenis, Mark (2 April 2020). "Switch the default compiler on powerpc to clang" (Mailing list). Archived from the original on April 8, 2022. Retrieved 27 August 2020.
  60. Hankala, Visa (21 December 2020). "Make clang the default compiler on loongson" (Mailing list). Archived from the original on January 13, 2021. Retrieved 1 January 2021.

External links