blob: 21c6df09309032602e8c69788224ff7cc409cde4 [file] [log] [blame]
Chris Lattner83109672007-12-10 01:44:24 +00001<!-- Material used from: HTML 4.01 specs: http://www.w3.org/TR/html401/ -->
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
3 "http://www.w3.org/TR/html4/strict.dtd">
4<html>
5<head>
6 <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
7 <title>Comparing clang to other compilers</title>
8 <link type="text/css" rel="stylesheet" href="menu.css" />
9 <link type="text/css" rel="stylesheet" href="content.css" />
10</head>
11<body>
12 <!--#include virtual="menu.html.incl"-->
13 <div id="content">
14 <h1>Clang vs Other 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 technical points
20 to avoid controversy where possible. Also, software is infinitely
21 mutable, so we avoid mentioning anything that would be easy to fix.</p>
22
23 <p>The goal of this list is to describe how differences in goals lead to
24 different strengths and weaknesses, not to make some compiler look bad.
25 This will hopefully help you to evaluate whether using clang is a good
26 idea for your specific goals.</p>
27
28 <p>Please email cfe-dev if you think we should add another compiler to this
29 list or if you think some characterization is unfair here.</p>
30
31 <!--=====================================================================-->
32 <h2><a name="gcc">Clang vs GCC (GNU Compiler Collection)</a></h2>
33 <!--=====================================================================-->
34
35 <p>Pros of GCC vs clang:</p>
36
37 <ul>
38 <li>GCC supports languages that clang does not aim to, such as Java, Ada,
39 FORTRAN, etc.</li>
40 <li>GCC front-ends are very mature and already support C/C++/ObjC and all
41 the variants we are interested in. clang's support for C++ in
42 particular is nowhere near what GCC supports.</li>
43 <li>GCC is popular and widely adopted.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000044 <li>GCC does not require a C++ compiler to build it.</li>
Chris Lattner83109672007-12-10 01:44:24 +000045 </ul>
46
Chris Lattnerb0c4c632007-12-10 01:52:24 +000047 <p>Pros of clang vs GCC:</p>
Chris Lattner83109672007-12-10 01:44:24 +000048
49 <ul>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000050 <li>The Clang ASTs and design are intended to be easily understandable to
51 anyone who is familiar with the languages involved and who have a basic
52 understanding of how a compiler works. GCC has a very old codebase
53 which presents a steep learning curve to new developers.</li>
54 <li>Clang is designed as an API from its inception, allowing it to be reused
55 by source analysis tools, refactoring, IDEs (etc) as well as for code
56 generation. GCC is built as a monolithic static compiler, which makes
57 it extremely difficult to use as an API and integrate into other tools.
58 Further, its historic design and <a
Chris Lattner83109672007-12-10 01:44:24 +000059 href="http://gcc.gnu.org/ml/gcc/2007-11/msg00460.html">current</a>
Chris Lattnerff11fa32007-12-10 02:05:32 +000060 <a href="http://gcc.gnu.org/ml/gcc/2004-12/msg00888.html">policy</a>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000061 makes it difficult to decouple the front-end from the rest of the
62 compiler. </li>
Chris Lattner83109672007-12-10 01:44:24 +000063 <li>Various GCC design decisions make it very difficult to reuse: its build
64 system is difficult to modify, you can't link multiple targets into one
65 binary, you can't link multiple front-ends into one binary, it uses a
66 custom garbage collector, uses global variables extensively, is not
67 reentrant or multi-threadable, etc. Clang has none of these problems.
68 </li>
Chris Lattner42f956b2007-12-10 02:24:44 +000069 <li>For every token, clang tracks information about where it was written and
70 where it was ultimately expanded into if was involved in a macro.
71 GCC does not track information about macro instantiations when parsing
72 source code. This makes it very difficult for static analysis and
73 refactoring tools to work in the presence of (even simple) macros.</li>
74 <li>Clang does not implicitly simplify code as it parses it like GCC does.
75 This causes many problems for source analysis tools: as one simple
76 example, if you write "x-x" in your source code, the GCC AST will
77 contain "0", with no mention of 'x'. This is extremely bad for a
78 refactoring tool that wants to rename 'x'.</li>
Chris Lattner83109672007-12-10 01:44:24 +000079 <li>GCC does not have a way to serialize the AST of a file out to disk and
80 read it back into another program. Its PCH mechanism is architecturally
Chris Lattner6c9a70d2007-12-10 02:18:15 +000081 only able to read the dump back into the exact same executable as the
82 one that produced it.</li>
83 <li>Clang is <a href="features.html#performance">much faster and uses far
84 less memory</a> than GCC.</li>
85 <li>Clang aims to provide extremely clear and concise diagnostics (error and
86 warning messages), and includes support for <a
87 href="features.html#expressivediags">expressive diagnostics</a>. GCC's
88 warnings are acceptable, but are often confusing and it does not support
89 expressive diagnostics. Clang also preserves typedefs in diagnostics
90 consistently.</li>
Chris Lattnerff11fa32007-12-10 02:05:32 +000091 <li>GCC is licensed under the GPL license. clang uses a BSD license, which
92 allows it to be used by projects that do not themselves want to be
93 GPL.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000094 <li>Clang inherits a number of features from its use of LLVM as a backend,
95 including support for a bytecode representation for intermediate code,
96 pluggable optimizers, link-time optimization support, Just-In-Time
97 compilation, etc.</li>
Chris Lattner83109672007-12-10 01:44:24 +000098 </ul>
99
100 <!--=====================================================================-->
101 <h2><a name="elsa">Clang vs Elsa (Elkhound-based C++ Parser)</a></h2>
102 <!--=====================================================================-->
103
104 <p>Pros of Elsa vs clang:</p>
105
106 <ul>
107 <li>Elsa's support for C++ is far beyond what clang provides. If you need
108 C++ support in the next year, Elsa is a great way to get it. That said,
109 Elsa is missing important support for templates and other pieces: for
110 example, it is not capable of compiling the GCC STL headers from any
111 version newer than GCC 3.4.</li>
112 <li>Elsa's parser and AST is designed to be easily composable by adding
113 grammar rules. Clang has a very simple and easily extensible parser,
114 but requires you to write C++ code to extend it.</li>
115 </ul>
116
Chris Lattnerb0c4c632007-12-10 01:52:24 +0000117 <p>Pros of clang vs Elsa:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000118
119 <ul>
120 <li>The Elsa community is extremely small and major development work seems
121 to have ceased in 2005, though it continues to be used by other projects
122 (e.g. Oink). Clang has a vibrant community including developers that
123 are paid to work on it full time.</li>
124 <li>Elsa is not built as a stack of reusable libraries like clang is. It is
125 very difficult to use part of elsa without the whole front-end. For
126 example, you cannot use Elsa to parse C/ObjC code without building an
127 AST. You can do this in Clang and it is much faster than building an
128 AST.</li>
129 <li>Elsa does not have an integrated preprocessor, which makes it extremely
130 difficult to accurately map from a source location in the AST back to
131 its original position before preprocessing. Likewise, it does not keep
132 track of macro expansions.</li>
133 <li>Elsa is slower and uses more memory than GCC, which requires far more
134 space and time than clang.</li>
135 <li>Elsa only does partial semantic analysis. It is intended to work on
136 code that is already validated by GCC, so it does not do many semantic
137 checks required by the languages it implements.</li>
138 <li>Elsa does not support Objective-C.</li>
139 <li>Elsa does not support native code generation.</li>
140 </ul>
141
142
143 <!--=====================================================================-->
144 <h2><a name="pcc">Clang vs PCC (Portable C Compiler)</a></h2>
145 <!--=====================================================================-->
146
147 <p>Pros of PCC vs clang:</p>
148
149 <ul>
150 <li>The PCC source base is very small and builds quickly with just a C
151 compiler.</li>
152 </ul>
153
Chris Lattnerb0c4c632007-12-10 01:52:24 +0000154 <p>Pros of clang vs PCC:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000155
156 <ul>
157 <li>PCC dates from the 1970's and has been dormant for most of that time.
158 The clang + llvm community are very active.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +0000159 <li>PCC doesn't support C99, Objective-C, and doesn't aim to support
160 C++.</li>
Chris Lattner83109672007-12-10 01:44:24 +0000161 <li>PCC's code generation is very limited compared to LLVM, it produces very
162 inefficient code and does not support many important targets.</li>
163 <li>PCC's does not have an integrated preprocessor, so it is extremely
164 difficult to use it for source analysis tools.</li>
165 </div>
166</body>
167</html>