1*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 2*9880d681SAndroid Build Coastguard Worker// C Language Family Front-end 3*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 4*9880d681SAndroid Build Coastguard Worker Chris Lattner 5*9880d681SAndroid Build Coastguard Worker 6*9880d681SAndroid Build Coastguard WorkerI. Introduction: 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard Worker clang: noun 9*9880d681SAndroid Build Coastguard Worker 1. A loud, resonant, metallic sound. 10*9880d681SAndroid Build Coastguard Worker 2. The strident call of a crane or goose. 11*9880d681SAndroid Build Coastguard Worker 3. C-language family front-end toolkit. 12*9880d681SAndroid Build Coastguard Worker 13*9880d681SAndroid Build Coastguard Worker The world needs better compiler tools, tools which are built as libraries. This 14*9880d681SAndroid Build Coastguard Worker design point allows reuse of the tools in new and novel ways. However, building 15*9880d681SAndroid Build Coastguard Worker the tools as libraries isn't enough: they must have clean APIs, be as 16*9880d681SAndroid Build Coastguard Worker decoupled from each other as possible, and be easy to modify/extend. This 17*9880d681SAndroid Build Coastguard Worker requires clean layering, decent design, and avoiding tying the libraries to a 18*9880d681SAndroid Build Coastguard Worker specific use. Oh yeah, did I mention that we want the resultant libraries to 19*9880d681SAndroid Build Coastguard Worker be as fast as possible? :) 20*9880d681SAndroid Build Coastguard Worker 21*9880d681SAndroid Build Coastguard Worker This front-end is built as a component of the LLVM toolkit that can be used 22*9880d681SAndroid Build Coastguard Worker with the LLVM backend or independently of it. In this spirit, the API has been 23*9880d681SAndroid Build Coastguard Worker carefully designed as the following components: 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard Worker libsupport - Basic support library, reused from LLVM. 26*9880d681SAndroid Build Coastguard Worker 27*9880d681SAndroid Build Coastguard Worker libsystem - System abstraction library, reused from LLVM. 28*9880d681SAndroid Build Coastguard Worker 29*9880d681SAndroid Build Coastguard Worker libbasic - Diagnostics, SourceLocations, SourceBuffer abstraction, 30*9880d681SAndroid Build Coastguard Worker file system caching for input source files. This depends on 31*9880d681SAndroid Build Coastguard Worker libsupport and libsystem. 32*9880d681SAndroid Build Coastguard Worker 33*9880d681SAndroid Build Coastguard Worker libast - Provides classes to represent the C AST, the C type system, 34*9880d681SAndroid Build Coastguard Worker builtin functions, and various helpers for analyzing and 35*9880d681SAndroid Build Coastguard Worker manipulating the AST (visitors, pretty printers, etc). This 36*9880d681SAndroid Build Coastguard Worker library depends on libbasic. 37*9880d681SAndroid Build Coastguard Worker 38*9880d681SAndroid Build Coastguard Worker 39*9880d681SAndroid Build Coastguard Worker liblex - C/C++/ObjC lexing and preprocessing, identifier hash table, 40*9880d681SAndroid Build Coastguard Worker pragma handling, tokens, and macros. This depends on libbasic. 41*9880d681SAndroid Build Coastguard Worker 42*9880d681SAndroid Build Coastguard Worker libparse - C (for now) parsing and local semantic analysis. This library 43*9880d681SAndroid Build Coastguard Worker invokes coarse-grained 'Actions' provided by the client to do 44*9880d681SAndroid Build Coastguard Worker stuff (e.g. libsema builds ASTs). This depends on liblex. 45*9880d681SAndroid Build Coastguard Worker 46*9880d681SAndroid Build Coastguard Worker libsema - Provides a set of parser actions to build a standardized AST 47*9880d681SAndroid Build Coastguard Worker for programs. AST's are 'streamed' out a top-level declaration 48*9880d681SAndroid Build Coastguard Worker at a time, allowing clients to use decl-at-a-time processing, 49*9880d681SAndroid Build Coastguard Worker build up entire translation units, or even build 'whole 50*9880d681SAndroid Build Coastguard Worker program' ASTs depending on how they use the APIs. This depends 51*9880d681SAndroid Build Coastguard Worker on libast and libparse. 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard Worker librewrite - Fast, scalable rewriting of source code. This operates on 54*9880d681SAndroid Build Coastguard Worker the raw syntactic text of source code, allowing a client 55*9880d681SAndroid Build Coastguard Worker to insert and delete text in very large source files using 56*9880d681SAndroid Build Coastguard Worker the same source location information embedded in ASTs. This 57*9880d681SAndroid Build Coastguard Worker is intended to be a low-level API that is useful for 58*9880d681SAndroid Build Coastguard Worker higher-level clients and libraries such as code refactoring. 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard Worker libanalysis - Source-level dataflow analysis useful for performing analyses 61*9880d681SAndroid Build Coastguard Worker such as computing live variables. It also includes a 62*9880d681SAndroid Build Coastguard Worker path-sensitive "graph-reachability" engine for writing 63*9880d681SAndroid Build Coastguard Worker analyses that reason about different possible paths of 64*9880d681SAndroid Build Coastguard Worker execution through source code. This is currently being 65*9880d681SAndroid Build Coastguard Worker employed to write a set of checks for finding bugs in software. 66*9880d681SAndroid Build Coastguard Worker 67*9880d681SAndroid Build Coastguard Worker libcodegen - Lower the AST to LLVM IR for optimization & codegen. Depends 68*9880d681SAndroid Build Coastguard Worker on libast. 69*9880d681SAndroid Build Coastguard Worker 70*9880d681SAndroid Build Coastguard Worker clang - An example driver, client of the libraries at various levels. 71*9880d681SAndroid Build Coastguard Worker This depends on all these libraries, and on LLVM VMCore. 72*9880d681SAndroid Build Coastguard Worker 73*9880d681SAndroid Build Coastguard Worker This front-end has been intentionally built as a DAG of libraries, making it 74*9880d681SAndroid Build Coastguard Worker easy to reuse individual parts or replace pieces if desired. For example, to 75*9880d681SAndroid Build Coastguard Worker build a preprocessor, you take the Basic and Lexer libraries. If you want an 76*9880d681SAndroid Build Coastguard Worker indexer, you take those plus the Parser library and provide some actions for 77*9880d681SAndroid Build Coastguard Worker indexing. If you want a refactoring, static analysis, or source-to-source 78*9880d681SAndroid Build Coastguard Worker compiler tool, it makes sense to take those plus the AST building and semantic 79*9880d681SAndroid Build Coastguard Worker analyzer library. Finally, if you want to use this with the LLVM backend, 80*9880d681SAndroid Build Coastguard Worker you'd take these components plus the AST to LLVM lowering code. 81*9880d681SAndroid Build Coastguard Worker 82*9880d681SAndroid Build Coastguard Worker In the future I hope this toolkit will grow to include new and interesting 83*9880d681SAndroid Build Coastguard Worker components, including a C++ front-end, ObjC support, and a whole lot of other 84*9880d681SAndroid Build Coastguard Worker things. 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard Worker Finally, it should be pointed out that the goal here is to build something that 87*9880d681SAndroid Build Coastguard Worker is high-quality and industrial-strength: all the obnoxious features of the C 88*9880d681SAndroid Build Coastguard Worker family must be correctly supported (trigraphs, preprocessor arcana, K&R-style 89*9880d681SAndroid Build Coastguard Worker prototypes, GCC/MS extensions, etc). It cannot be used if it is not 'real'. 90*9880d681SAndroid Build Coastguard Worker 91*9880d681SAndroid Build Coastguard Worker 92*9880d681SAndroid Build Coastguard WorkerII. Usage of clang driver: 93*9880d681SAndroid Build Coastguard Worker 94*9880d681SAndroid Build Coastguard Worker * Basic Command-Line Options: 95*9880d681SAndroid Build Coastguard Worker - Help: clang --help 96*9880d681SAndroid Build Coastguard Worker - Standard GCC options accepted: -E, -I*, -i*, -pedantic, -std=c90, etc. 97*9880d681SAndroid Build Coastguard Worker - To make diagnostics more gcc-like: -fno-caret-diagnostics -fno-show-column 98*9880d681SAndroid Build Coastguard Worker - Enable metric printing: -stats 99*9880d681SAndroid Build Coastguard Worker 100*9880d681SAndroid Build Coastguard Worker * -fsyntax-only is currently the default mode. 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker * -E mode works the same way as GCC. 103*9880d681SAndroid Build Coastguard Worker 104*9880d681SAndroid Build Coastguard Worker * -Eonly mode does all preprocessing, but does not print the output, 105*9880d681SAndroid Build Coastguard Worker useful for timing the preprocessor. 106*9880d681SAndroid Build Coastguard Worker 107*9880d681SAndroid Build Coastguard Worker * -fsyntax-only is currently partially implemented, lacking some 108*9880d681SAndroid Build Coastguard Worker semantic analysis (some errors and warnings are not produced). 109*9880d681SAndroid Build Coastguard Worker 110*9880d681SAndroid Build Coastguard Worker * -parse-noop parses code without building an AST. This is useful 111*9880d681SAndroid Build Coastguard Worker for timing the cost of the parser without including AST building 112*9880d681SAndroid Build Coastguard Worker time. 113*9880d681SAndroid Build Coastguard Worker 114*9880d681SAndroid Build Coastguard Worker * -parse-ast builds ASTs, but doesn't print them. This is most 115*9880d681SAndroid Build Coastguard Worker useful for timing AST building vs -parse-noop. 116*9880d681SAndroid Build Coastguard Worker 117*9880d681SAndroid Build Coastguard Worker * -parse-ast-print pretty prints most expression and statements nodes. 118*9880d681SAndroid Build Coastguard Worker 119*9880d681SAndroid Build Coastguard Worker * -parse-ast-check checks that diagnostic messages that are expected 120*9880d681SAndroid Build Coastguard Worker are reported and that those which are reported are expected. 121*9880d681SAndroid Build Coastguard Worker 122*9880d681SAndroid Build Coastguard Worker * -dump-cfg builds ASTs and then CFGs. CFGs are then pretty-printed. 123*9880d681SAndroid Build Coastguard Worker 124*9880d681SAndroid Build Coastguard Worker * -view-cfg builds ASTs and then CFGs. CFGs are then visualized by 125*9880d681SAndroid Build Coastguard Worker invoking Graphviz. 126*9880d681SAndroid Build Coastguard Worker 127*9880d681SAndroid Build Coastguard Worker For more information on getting Graphviz to work with clang/LLVM, 128*9880d681SAndroid Build Coastguard Worker see: http://llvm.org/docs/ProgrammersManual.html#ViewGraph 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard Worker 131*9880d681SAndroid Build Coastguard WorkerIII. Current advantages over GCC: 132*9880d681SAndroid Build Coastguard Worker 133*9880d681SAndroid Build Coastguard Worker * Column numbers are fully tracked (no 256 col limit, no GCC-style pruning). 134*9880d681SAndroid Build Coastguard Worker * All diagnostics have column numbers, includes 'caret diagnostics', and they 135*9880d681SAndroid Build Coastguard Worker highlight regions of interesting code (e.g. the LHS and RHS of a binop). 136*9880d681SAndroid Build Coastguard Worker * Full diagnostic customization by client (can format diagnostics however they 137*9880d681SAndroid Build Coastguard Worker like, e.g. in an IDE or refactoring tool) through DiagnosticClient interface. 138*9880d681SAndroid Build Coastguard Worker * Built as a framework, can be reused by multiple tools. 139*9880d681SAndroid Build Coastguard Worker * All languages supported linked into same library (no cc1,cc1obj, ...). 140*9880d681SAndroid Build Coastguard Worker * mmap's code in read-only, does not dirty the pages like GCC (mem footprint). 141*9880d681SAndroid Build Coastguard Worker * LLVM License, can be linked into non-GPL projects. 142*9880d681SAndroid Build Coastguard Worker * Full diagnostic control, per diagnostic. Diagnostics are identified by ID. 143*9880d681SAndroid Build Coastguard Worker * Significantly faster than GCC at semantic analysis, parsing, preprocessing 144*9880d681SAndroid Build Coastguard Worker and lexing. 145*9880d681SAndroid Build Coastguard Worker * Defers exposing platform-specific stuff to as late as possible, tracks use of 146*9880d681SAndroid Build Coastguard Worker platform-specific features (e.g. #ifdef PPC) to allow 'portable bytecodes'. 147*9880d681SAndroid Build Coastguard Worker * The lexer doesn't rely on the "lexer hack": it has no notion of scope and 148*9880d681SAndroid Build Coastguard Worker does not categorize identifiers as types or variables -- this is up to the 149*9880d681SAndroid Build Coastguard Worker parser to decide. 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard WorkerPotential Future Features: 152*9880d681SAndroid Build Coastguard Worker 153*9880d681SAndroid Build Coastguard Worker * Fine grained diag control within the source (#pragma enable/disable warning). 154*9880d681SAndroid Build Coastguard Worker * Better token tracking within macros? (Token came from this line, which is 155*9880d681SAndroid Build Coastguard Worker a macro argument instantiated here, recursively instantiated here). 156*9880d681SAndroid Build Coastguard Worker * Fast #import with a module system. 157*9880d681SAndroid Build Coastguard Worker * Dependency tracking: change to header file doesn't recompile every function 158*9880d681SAndroid Build Coastguard Worker that texually depends on it: recompile only those functions that need it. 159*9880d681SAndroid Build Coastguard Worker This is aka 'incremental parsing'. 160*9880d681SAndroid Build Coastguard Worker 161*9880d681SAndroid Build Coastguard Worker 162*9880d681SAndroid Build Coastguard WorkerIV. Missing Functionality / Improvements 163*9880d681SAndroid Build Coastguard Worker 164*9880d681SAndroid Build Coastguard WorkerLexer: 165*9880d681SAndroid Build Coastguard Worker * Source character mapping. GCC supports ASCII and UTF-8. 166*9880d681SAndroid Build Coastguard Worker See GCC options: -ftarget-charset and -ftarget-wide-charset. 167*9880d681SAndroid Build Coastguard Worker * Universal character support. Experimental in GCC, enabled with 168*9880d681SAndroid Build Coastguard Worker -fextended-identifiers. 169*9880d681SAndroid Build Coastguard Worker * -fpreprocessed mode. 170*9880d681SAndroid Build Coastguard Worker 171*9880d681SAndroid Build Coastguard WorkerPreprocessor: 172*9880d681SAndroid Build Coastguard Worker * #assert/#unassert 173*9880d681SAndroid Build Coastguard Worker * MSExtension: "L#param" stringizes to a wide string literal. 174*9880d681SAndroid Build Coastguard Worker * Add support for -M* 175*9880d681SAndroid Build Coastguard Worker 176*9880d681SAndroid Build Coastguard WorkerTraditional Preprocessor: 177*9880d681SAndroid Build Coastguard Worker * Currently, we have none. :) 178*9880d681SAndroid Build Coastguard Worker 179