blob: 4bca65dc40d312cbc078c735a4171d1aae086296 [file] [log] [blame]
Cedric Venet3d658642009-02-14 20:20:19 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<!-- Material used from: HTML 4.01 specs: http://www.w3.org/TR/html401/ -->
4<html>
5<head>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +00006 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
Cedric Venet3d658642009-02-14 20:20:19 +00007 <title>Comparing clang to other open source compilers</title>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +00008 <link type="text/css" rel="stylesheet" href="menu.css">
9 <link type="text/css" rel="stylesheet" href="content.css">
Cedric Venet3d658642009-02-14 20:20:19 +000010</head>
11<body>
12 <!--#include virtual="menu.html.incl"-->
13 <div id="content">
14 <h1>Clang vs Other Open Source Compilers</h1>
15
16 <p>Building an entirely new compiler front-end is a big task, and it isn't
17 always clear to people why we decided to do this. Here we compare clang
18 and its goals to other open source compiler front-ends that are
19 available. We restrict the discussion to very specific objective points
20 to avoid controversy where possible. Also, software is infinitely
21 mutable, so we don't talk about little details that can be fixed with
22 a reasonable amount of effort: we'll talk about issues that are
23 difficult to fix for architectural or political reasons.</p>
24
25 <p>The goal of this list is to describe how differences in goals lead to
26 different strengths and weaknesses, not to make some compiler look bad.
27 This will hopefully help you to evaluate whether using clang is a good
28 idea for your personal goals. Because we don't know specifically what
29 <em>you</em> want to do, we describe the features of these compilers in
30 terms of <em>our</em> goals: if you are only interested in static
31 analysis, you may not care that something lacks codegen support, for
32 example.</p>
33
Stephen Hines6bcf27b2014-05-29 04:14:42 -070034 <p>Please email <a href="get_involved.html">cfe-dev</a> if you think we should add another compiler to this
Cedric Venet3d658642009-02-14 20:20:19 +000035 list or if you think some characterization is unfair here.</p>
36
37 <ul>
38 <li><a href="#gcc">Clang vs GCC</a> (GNU Compiler Collection)</li>
39 <li><a href="#elsa">Clang vs Elsa</a> (Elkhound-based C++ Parser)</li>
40 <li><a href="#pcc">Clang vs PCC</a> (Portable C Compiler)</li>
41 </ul>
42
43
44 <!--=====================================================================-->
45 <h2><a name="gcc">Clang vs GCC (GNU Compiler Collection)</a></h2>
46 <!--=====================================================================-->
47
48 <p>Pro's of GCC vs clang:</p>
49
50 <ul>
51 <li>GCC supports languages that clang does not aim to, such as Java, Ada,
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052 FORTRAN, Go, etc.</li>
Cedric Venet3d658642009-02-14 20:20:19 +000053 <li>GCC supports more targets than LLVM.</li>
Stephen Hines651f13c2014-04-23 16:59:28 -070054 <li>GCC supports many language extensions, some of which are not implemented
55 by Clang. For instance, in C mode, GCC supports
56 <a href="http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html">nested
57 functions</a> and has an
58 <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37428">undocumented
59 extension allowing VLAs in structs</a>.
Cedric Venet3d658642009-02-14 20:20:19 +000060 </ul>
Stephen Hines651f13c2014-04-23 16:59:28 -070061
Cedric Venet3d658642009-02-14 20:20:19 +000062 <p>Pro's of clang vs GCC:</p>
63
64 <ul>
65 <li>The Clang ASTs and design are intended to be <a
66 href="features.html#simplecode">easily understandable</a> by
67 anyone who is familiar with the languages involved and who has a basic
68 understanding of how a compiler works. GCC has a very old codebase
69 which presents a steep learning curve to new developers.</li>
70 <li>Clang is designed as an API from its inception, allowing it to be reused
71 by source analysis tools, refactoring, IDEs (etc) as well as for code
72 generation. GCC is built as a monolithic static compiler, which makes
73 it extremely difficult to use as an API and integrate into other tools.
74 Further, its historic design and <a
75 href="http://gcc.gnu.org/ml/gcc/2007-11/msg00460.html">current</a>
76 <a href="http://gcc.gnu.org/ml/gcc/2004-12/msg00888.html">policy</a>
77 makes it difficult to decouple the front-end from the rest of the
78 compiler. </li>
79 <li>Various GCC design decisions make it very difficult to reuse: its build
80 system is difficult to modify, you can't link multiple targets into one
81 binary, you can't link multiple front-ends into one binary, it uses a
82 custom garbage collector, uses global variables extensively, is not
83 reentrant or multi-threadable, etc. Clang has none of these problems.
84 </li>
Cedric Venet3d658642009-02-14 20:20:19 +000085 <li>Clang does not implicitly simplify code as it parses it like GCC does.
86 Doing so causes many problems for source analysis tools: as one simple
87 example, if you write "x-x" in your source code, the GCC AST will
88 contain "0", with no mention of 'x'. This is extremely bad for a
89 refactoring tool that wants to rename 'x'.</li>
90 <li>Clang can serialize its AST out to disk and read it back into another
91 program, which is useful for whole program analysis. GCC does not have
92 this. GCC's PCH mechanism (which is just a dump of the compiler
93 memory image) is related, but is architecturally only
94 able to read the dump back into the exact same executable as the one
95 that produced it (it is not a structured format).</li>
96 <li>Clang is <a href="features.html#performance">much faster and uses far
97 less memory</a> than GCC.</li>
98 <li>Clang aims to provide extremely clear and concise diagnostics (error and
99 warning messages), and includes support for <a
Chris Lattner7eee9f72009-03-19 18:57:33 +0000100 href="diagnostics.html">expressive diagnostics</a>. GCC's warnings are
101 sometimes acceptable, but are often confusing and it does not support
Cedric Venet3d658642009-02-14 20:20:19 +0000102 expressive diagnostics. Clang also preserves typedefs in diagnostics
Chris Lattner7eee9f72009-03-19 18:57:33 +0000103 consistently, showing macro expansions and many other features.</li>
Rafael Espindola04359432012-08-14 21:58:29 +0000104 <li>GCC is licensed under the GPL license. <a href="features.html#license">
105 clang uses a BSD license,</a> which allows it to be embedded in
106 software that is not GPL-licensed.</li>
Cedric Venet3d658642009-02-14 20:20:19 +0000107 <li>Clang inherits a number of features from its use of LLVM as a backend,
108 including support for a bytecode representation for intermediate code,
109 pluggable optimizers, link-time optimization support, Just-In-Time
110 compilation, ability to link in multiple code generators, etc.</li>
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700111 <li><a href="compatibility.html#cxx">Clang's support for C++</a> is more
Stephen Hines651f13c2014-04-23 16:59:28 -0700112 compliant than GCC's in many ways.</li>
113 <li>Clang supports
114 <a href="http://clang.llvm.org/docs/LanguageExtensions.html">many language
115 extensions</a>, some of which are not implemented by GCC. For instance,
116 Clang provides attributes for checking thread safety and extended vector
117 types.</li>
Cedric Venet3d658642009-02-14 20:20:19 +0000118 </ul>
119
120 <!--=====================================================================-->
121 <h2><a name="elsa">Clang vs Elsa (Elkhound-based C++ Parser)</a></h2>
122 <!--=====================================================================-->
123
124 <p>Pro's of Elsa vs clang:</p>
125
126 <ul>
Cedric Venet3d658642009-02-14 20:20:19 +0000127 <li>Elsa's parser and AST is designed to be easily extensible by adding
128 grammar rules. Clang has a very simple and easily hackable parser,
129 but requires you to write C++ code to do it.</li>
130 </ul>
131
132 <p>Pro's of clang vs Elsa:</p>
133
134 <ul>
Chris Lattner9858e8f2011-05-13 05:29:16 +0000135 <li>Clang's C and C++ support is far more mature and practically useful than
David Blaikie1e2b3d72011-10-14 07:58:10 +0000136 Elsa's, and includes many C++'11 features.</li>
Cedric Venet3d658642009-02-14 20:20:19 +0000137 <li>The Elsa community is extremely small and major development work seems
Chris Lattnerb76c2322009-06-26 04:10:17 +0000138 to have ceased in 2005. Work continued to be used by other small
139 projects (e.g. Oink), but Oink is apparently dead now too. Clang has a
140 vibrant community including developers that
Cedric Venet3d658642009-02-14 20:20:19 +0000141 are paid to work on it full time. In practice this means that you can
142 file bugs against Clang and they will often be fixed for you. If you
143 use Elsa, you are (mostly) on your own for bug fixes and feature
144 enhancements.</li>
145 <li>Elsa is not built as a stack of reusable libraries like clang is. It is
146 very difficult to use part of Elsa without the whole front-end. For
147 example, you cannot use Elsa to parse C/ObjC code without building an
148 AST. You can do this in Clang and it is much faster than building an
149 AST.</li>
150 <li>Elsa does not have an integrated preprocessor, which makes it extremely
151 difficult to accurately map from a source location in the AST back to
152 its original position before preprocessing. Like GCC, it does not keep
153 track of macro expansions.</li>
154 <li>Elsa is even slower and uses more memory than GCC, which itself requires
155 far more space and time than clang.</li>
156 <li>Elsa only does partial semantic analysis. It is intended to work on
157 code that is already validated by GCC, so it does not do many semantic
158 checks required by the languages it implements.</li>
159 <li>Elsa does not support Objective-C.</li>
160 <li>Elsa does not support native code generation.</li>
161 </ul>
162
Cedric Venet3d658642009-02-14 20:20:19 +0000163
164 <!--=====================================================================-->
165 <h2><a name="pcc">Clang vs PCC (Portable C Compiler)</a></h2>
166 <!--=====================================================================-->
167
168 <p>Pro's of PCC vs clang:</p>
169
170 <ul>
171 <li>The PCC source base is very small and builds quickly with just a C
172 compiler.</li>
173 </ul>
174
175 <p>Pro's of clang vs PCC:</p>
176
177 <ul>
178 <li>PCC dates from the 1970's and has been dormant for most of that time.
179 The clang + llvm communities are very active.</li>
Daniel Dunbar5a7cb842009-10-17 03:28:37 +0000180 <li>PCC doesn't support Objective-C or C++ and doesn't aim to support
181 C++.</li>
Cedric Venet3d658642009-02-14 20:20:19 +0000182 <li>PCC's code generation is very limited compared to LLVM. It produces very
183 inefficient code and does not support many important targets.</li>
184 <li>Like Elsa, PCC's does not have an integrated preprocessor, making it
185 extremely difficult to use it for source analysis tools.</li>
Benjamin Kramer665a8dc2012-01-15 15:26:07 +0000186 </ul>
Cedric Venet3d658642009-02-14 20:20:19 +0000187 </div>
188</body>
189</html>