blob: 90283d36e9ee8749598867fe87d363999d45134c [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
Chris Lattner40ae32f2007-12-10 05:06:15 +000020 to avoid controversy where possible. Also, since software is infinitely
21 mutable, so focus on architectural issues that are impractical to fix
22 without a major rewrite, instead of talking about little details that
23 can be fixed with a reasonable amount of effort.</p>
Chris Lattner83109672007-12-10 01:44:24 +000024
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 specific goals.</p>
29
30 <p>Please email cfe-dev if you think we should add another compiler to this
31 list or if you think some characterization is unfair here.</p>
32
33 <!--=====================================================================-->
34 <h2><a name="gcc">Clang vs GCC (GNU Compiler Collection)</a></h2>
35 <!--=====================================================================-->
36
Chris Lattner40ae32f2007-12-10 05:06:15 +000037 <p>Pro's of GCC vs clang:</p>
Chris Lattner83109672007-12-10 01:44:24 +000038
39 <ul>
40 <li>GCC supports languages that clang does not aim to, such as Java, Ada,
41 FORTRAN, etc.</li>
42 <li>GCC front-ends are very mature and already support C/C++/ObjC and all
43 the variants we are interested in. clang's support for C++ in
44 particular is nowhere near what GCC supports.</li>
45 <li>GCC is popular and widely adopted.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000046 <li>GCC does not require a C++ compiler to build it.</li>
Chris Lattner83109672007-12-10 01:44:24 +000047 </ul>
48
Chris Lattner40ae32f2007-12-10 05:06:15 +000049 <p>Pro's of clang vs GCC:</p>
Chris Lattner83109672007-12-10 01:44:24 +000050
51 <ul>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000052 <li>The Clang ASTs and design are intended to be easily understandable to
53 anyone who is familiar with the languages involved and who have a basic
54 understanding of how a compiler works. GCC has a very old codebase
55 which presents a steep learning curve to new developers.</li>
56 <li>Clang is designed as an API from its inception, allowing it to be reused
57 by source analysis tools, refactoring, IDEs (etc) as well as for code
58 generation. GCC is built as a monolithic static compiler, which makes
59 it extremely difficult to use as an API and integrate into other tools.
60 Further, its historic design and <a
Chris Lattner83109672007-12-10 01:44:24 +000061 href="http://gcc.gnu.org/ml/gcc/2007-11/msg00460.html">current</a>
Chris Lattnerff11fa32007-12-10 02:05:32 +000062 <a href="http://gcc.gnu.org/ml/gcc/2004-12/msg00888.html">policy</a>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000063 makes it difficult to decouple the front-end from the rest of the
64 compiler. </li>
Chris Lattner83109672007-12-10 01:44:24 +000065 <li>Various GCC design decisions make it very difficult to reuse: its build
66 system is difficult to modify, you can't link multiple targets into one
67 binary, you can't link multiple front-ends into one binary, it uses a
68 custom garbage collector, uses global variables extensively, is not
69 reentrant or multi-threadable, etc. Clang has none of these problems.
70 </li>
Chris Lattner42f956b2007-12-10 02:24:44 +000071 <li>For every token, clang tracks information about where it was written and
Chris Lattner40ae32f2007-12-10 05:06:15 +000072 where it was ultimately expanded into if it was involved in a macro.
Chris Lattner42f956b2007-12-10 02:24:44 +000073 GCC does not track information about macro instantiations when parsing
Chris Lattner40ae32f2007-12-10 05:06:15 +000074 source code. This makes it very difficult for source rewriting tools
75 (e.g. for refactoring) to work in the presence of (even simple)
76 macros.</li>
Chris Lattner42f956b2007-12-10 02:24:44 +000077 <li>Clang does not implicitly simplify code as it parses it like GCC does.
Chris Lattner40ae32f2007-12-10 05:06:15 +000078 Doing so causes many problems for source analysis tools: as one simple
Chris Lattner42f956b2007-12-10 02:24:44 +000079 example, if you write "x-x" in your source code, the GCC AST will
80 contain "0", with no mention of 'x'. This is extremely bad for a
81 refactoring tool that wants to rename 'x'.</li>
Chris Lattner83109672007-12-10 01:44:24 +000082 <li>GCC does not have a way to serialize the AST of a file out to disk and
83 read it back into another program. Its PCH mechanism is architecturally
Chris Lattner6c9a70d2007-12-10 02:18:15 +000084 only able to read the dump back into the exact same executable as the
85 one that produced it.</li>
86 <li>Clang is <a href="features.html#performance">much faster and uses far
87 less memory</a> than GCC.</li>
88 <li>Clang aims to provide extremely clear and concise diagnostics (error and
89 warning messages), and includes support for <a
90 href="features.html#expressivediags">expressive diagnostics</a>. GCC's
91 warnings are acceptable, but are often confusing and it does not support
92 expressive diagnostics. Clang also preserves typedefs in diagnostics
93 consistently.</li>
Chris Lattnerff11fa32007-12-10 02:05:32 +000094 <li>GCC is licensed under the GPL license. clang uses a BSD license, which
95 allows it to be used by projects that do not themselves want to be
96 GPL.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +000097 <li>Clang inherits a number of features from its use of LLVM as a backend,
98 including support for a bytecode representation for intermediate code,
99 pluggable optimizers, link-time optimization support, Just-In-Time
100 compilation, etc.</li>
Chris Lattner83109672007-12-10 01:44:24 +0000101 </ul>
102
103 <!--=====================================================================-->
104 <h2><a name="elsa">Clang vs Elsa (Elkhound-based C++ Parser)</a></h2>
105 <!--=====================================================================-->
106
Chris Lattner40ae32f2007-12-10 05:06:15 +0000107 <p>Pro's of Elsa vs clang:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000108
109 <ul>
110 <li>Elsa's support for C++ is far beyond what clang provides. If you need
111 C++ support in the next year, Elsa is a great way to get it. That said,
112 Elsa is missing important support for templates and other pieces: for
113 example, it is not capable of compiling the GCC STL headers from any
114 version newer than GCC 3.4.</li>
Chris Lattner40ae32f2007-12-10 05:06:15 +0000115 <li>Elsa's parser and AST is designed to be easily extensible by adding
116 grammar rules. Clang has a very simple and easily hackable parser,
117 but requires you to write C++ code to do it.</li>
Chris Lattner83109672007-12-10 01:44:24 +0000118 </ul>
119
Chris Lattner40ae32f2007-12-10 05:06:15 +0000120 <p>Pro's of clang vs Elsa:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000121
122 <ul>
123 <li>The Elsa community is extremely small and major development work seems
124 to have ceased in 2005, though it continues to be used by other projects
125 (e.g. Oink). Clang has a vibrant community including developers that
126 are paid to work on it full time.</li>
127 <li>Elsa is not built as a stack of reusable libraries like clang is. It is
128 very difficult to use part of elsa without the whole front-end. For
129 example, you cannot use Elsa to parse C/ObjC code without building an
130 AST. You can do this in Clang and it is much faster than building an
131 AST.</li>
132 <li>Elsa does not have an integrated preprocessor, which makes it extremely
133 difficult to accurately map from a source location in the AST back to
Chris Lattner40ae32f2007-12-10 05:06:15 +0000134 its original position before preprocessing. Like GCC, it does not keep
Chris Lattner83109672007-12-10 01:44:24 +0000135 track of macro expansions.</li>
136 <li>Elsa is slower and uses more memory than GCC, which requires far more
137 space and time than clang.</li>
138 <li>Elsa only does partial semantic analysis. It is intended to work on
139 code that is already validated by GCC, so it does not do many semantic
140 checks required by the languages it implements.</li>
141 <li>Elsa does not support Objective-C.</li>
142 <li>Elsa does not support native code generation.</li>
143 </ul>
144
145
146 <!--=====================================================================-->
147 <h2><a name="pcc">Clang vs PCC (Portable C Compiler)</a></h2>
148 <!--=====================================================================-->
149
Chris Lattner40ae32f2007-12-10 05:06:15 +0000150 <p>Pro's of PCC vs clang:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000151
152 <ul>
153 <li>The PCC source base is very small and builds quickly with just a C
154 compiler.</li>
155 </ul>
156
Chris Lattner40ae32f2007-12-10 05:06:15 +0000157 <p>Pro's of clang vs PCC:</p>
Chris Lattner83109672007-12-10 01:44:24 +0000158
159 <ul>
160 <li>PCC dates from the 1970's and has been dormant for most of that time.
161 The clang + llvm community are very active.</li>
Chris Lattner6c9a70d2007-12-10 02:18:15 +0000162 <li>PCC doesn't support C99, Objective-C, and doesn't aim to support
163 C++.</li>
Chris Lattner83109672007-12-10 01:44:24 +0000164 <li>PCC's code generation is very limited compared to LLVM, it produces very
165 inefficient code and does not support many important targets.</li>
Chris Lattner40ae32f2007-12-10 05:06:15 +0000166 <li>Like Elsa, PCC's does not have an integrated preprocessor, making it
167 extremely difficult to use it for source analysis tools.</li>
Chris Lattner83109672007-12-10 01:44:24 +0000168 </div>
169</body>
170</html>