Clang

What is Clang?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code.

Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP. Clang uses the LLVM compiler as its back end and it has been included in the release of the LLVM since the LLVM 2.6.Clang 还用作 OpenMP、OpenCL、RenderScript、CUDA 和 HIP 等框架的编译器。

Clang 也被构建为 GCC 命令的直接替代品。在设计上,Clang 编译器的构造与 GCC 非常相似,以确保最大程度地提高可移植性。然而,两者之间的区别在于,GCC 默认为 gnu89,而 Clang 默认为 gnu99。

Clang is also built to be a drop-in replacement for GCC command. In its design, the Clang compiler has been constructed to work very similarly to GCC to ensure that portability is maximized. However, the difference between the two is that while GCC defaults to gnu89, Clang defaults to gnu99.Clang 也被构建为 GCC 命令的直接替代品。在设计上,Clang 编译器的构造与 GCC 非常相似,以确保最大程度地提高可移植性。然而,两者之间的区别在于,GCC 默认为 gnu89,而 Clang 默认为 gnu99。

How does Clang work? How to use Clang?

The Clang Compiler has been designed to work just like any other Compiler. Clang works in three different stages.

The first stage is the front end that is used for parsing source code. It checks the code for errors and builds a language-specific Abstract Syntax Tree (AST) to work as its input code.它检查代码中的错误并构建特定于语言的抽象语法树 (AST) 作为其输入代码。

The second stage is the optimizer that is used for optimizing the AST that was generated by the frontend.第二阶段是用于优化前端生成的 AST 的优化器。

The third and final stage is the back end. This is responsible for generating the final code to be executed by the machine which can depend on the target.第三阶段也是最后一阶段是后端。它负责生成要由机器执行的最终代码,该代码可以依赖于目标

The History of Clang + Latest version

The original contributor of Clang is Apple. Apple developers originally made use of GCC as a front-end for LLVM but it caused certain problems for developers that were working with LLVM. This was mostly because the GCC code was large and it doesn’t integrate into Apple’s IDE (Integrated development environment).Clang 的原始贡献者是 Apple。Apple 开发人员最初使用 GCC 作为 LLVM 的前端,但这给使用 LLVM 的开发人员带来了一些问题。这主要是因为 GCC 代码很大,并且无法集成到 Apple 的 IDE(集成开发环境)中。

In addition to this, the LLVM has a license which was not compatible with GCC’s license. During this time, Apple software made heavy use of Objective-C, which has a low priority for GCC developers as front-end. This was why Apple developed the Clang Project and made it open source in July 2007.除此之外,LLVM 的许可证与 GCC 的许可证不兼容。在此期间,Apple 软件大量使用 Objective-C,而 Objective-C 作为前端对于 GCC 开发人员来说优先级较低。这就是 Apple 开发 Clang 项目并于 2007 年 7 月将其开源的原因。

The latest version of Clang is the Clang 12. As of November 2020, it has been built with full support for all published C++ standards and partially supports the upcoming C++20 standards.
Clang 的最新版本是 Clang 12。截至 2020 年 11 月,它已完全支持所有已发布的 C++ 标准,并部分支持即将推出的 C++20 标准。

Clang examples

The first version of clang was Clang 1.0 and it was released with LLVM 2.6. The next version was Clang C++ which was able to parse GCC 4.2 libstdc, compile itself and function as a working code generator for non-trivial operations.clang 的第一个版本是 Clang 1.0,它与 LLVM 2.6 一起发布。下一个版本是 Clang C++,它能够解析 GCC 4.2 libstdc、自行编译并充当非平凡操作的工作代码生成器

From Clang 3.0 down to the latest version – Clang 12.0.0, the compiler has evolved with a ton of meaningful features.
从 Clang 3.0 到最新版本 - Clang 12.0.0,编译器已经发展出大量有意义的功能。
It has become more scalable, extremely light and very fast. It is designed to keep more data in the course of the compiling process when compared to GCC (learn about what is GCC). Also, it preserves the form of the main code.
它变得更具可扩展性、极其轻量且非常快速。与 GCC 相比,它旨在在编译过程中保留更多数据(了解什么是 GCC)。此外,它保留了主代码的形式。
The reason for this is that it will be easy to map errors back into the main source. Clang provides error reports that are specific and detailed and it is also machine-readable. This way IDEs get to index the compiler’s output while compiling.
这样做的原因是将错误映射回主源将很容易。Clang 提供具体而详细的错误报告,并且也是机器可读的。这样,IDE 就可以在编译时索引编译器的输出。

Clang features / Clang attributes

According to performance comparisons carried out by Alibabatech, the results show that Clang’s front-end is significantly faster than GCC in the compilation and it uses less memory. This gives Clang an edge especially for making large projects.
根据阿里巴巴科技进行的性能比较,结果显示 Clang 的前端在编译过程中明显快于 GCC,并且占用的内存更少。这让 Clang 尤其在制作大型项目时具有优势。
Clang compiler generates diagnostics (error and warning messages) that are more useful and understandable.

Also, Clang uses a Library based Architecture as the front-end is separated into distinct libraries that can suit various needs. This makes it easy to time and estimates the cost of every stack layer.

此外,Clang 使用基于库的架构,因为前端被分成不同的库,可以满足各种需求。这使得计时和估算每个堆栈层的成本变得容易。
Clang’s library-based process makes it easy for fresh developers to join because all they need is to get the basics and not the entire picture.
Clang 基于库的流程让新开发人员可以轻松加入,因为他们需要的只是掌握基础知识,而不是全貌。
Clang Supports Diverse Clients as the primary goal is to aid incremental compilation. This gives it a better connection to the GUI of IDE. Great care was taken to build and design clang to give it a firm foundation for a new set of C++, Objective C and C development tools.
Clang 支持多种客户端,因为主要目标是帮助增量编译。这使其与 IDE 的 GUI 有了更好的连接。
Clang supports a wide range of language extensions as they are compatible with Microsoft, GCC, and other well-known compilers to enhance functionality via specific features of clang.
Clang 支持广泛的语言扩展,因为它们与 Microsoft、GCC 和其他知名编译器兼容,可通过 clang 的特定功能增强功能。
Clang Compilers are also able to integrate with IDEs which gives the user more visibility over the entire project. Clang uses the BSD license of the LLVM for commercial projects as it was designed not to be used as a separate compiler alone.
Clang 编译器还能够与 IDE 集成,让用户更清楚地了解整个项目。Clang 使用 LLVM 的 BSD 许可证进行商业项目,因为它的设计目的并非单独用作单独的编译器。
It was designed as a library embedded within a third-party app. The BSD license encourages several contributors to go through and improve the source code which enhances it at the end of the day.
它被设计为嵌入第三方应用程序中的库。BSD 许可证鼓励多位贡献者仔细研究并改进源代码,最终增强其功能。

To learn more about Clang features, visit this GCC vs Clang blog post.

要了解有关 Clang 功能的更多信息,请访问此 GCC vs Clang 博客文章。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果Autosar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值