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