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