blob: 81fefab2adfff407f34d9bcb95754430dcb5be81 [file] [log] [blame]
Ted Kremenek17a295d2008-06-11 06:19:49 +00001<html>
2<head>
Chris Lattner86920d32007-07-31 05:42:17 +00003<title>"clang" CFE Internals Manual</title>
Ted Kremenek17a295d2008-06-11 06:19:49 +00004<link type="text/css" rel="stylesheet" href="../menu.css" />
5<link type="text/css" rel="stylesheet" href="../content.css" />
6</head>
7<body>
8
9<!--#include virtual="../menu.html.incl"-->
10
11<div id="content">
Chris Lattner86920d32007-07-31 05:42:17 +000012
13<h1>"clang" CFE Internals Manual</h1>
14
15<ul>
16<li><a href="#intro">Introduction</a></li>
17<li><a href="#libsystem">LLVM System and Support Libraries</a></li>
18<li><a href="#libbasic">The clang 'Basic' Library</a>
19 <ul>
20 <li><a href="#SourceLocation">The SourceLocation and SourceManager
21 classes</a></li>
22 </ul>
23</li>
24<li><a href="#liblex">The Lexer and Preprocessor Library</a>
25 <ul>
26 <li><a href="#Token">The Token class</a></li>
27 <li><a href="#Lexer">The Lexer class</a></li>
Chris Lattner79281252008-03-09 02:27:26 +000028 <li><a href="#TokenLexer">The TokenLexer class</a></li>
Chris Lattner86920d32007-07-31 05:42:17 +000029 <li><a href="#MultipleIncludeOpt">The MultipleIncludeOpt class</a></li>
30 </ul>
31</li>
32<li><a href="#libparse">The Parser Library</a>
33 <ul>
34 </ul>
35</li>
36<li><a href="#libast">The AST Library</a>
37 <ul>
38 <li><a href="#Type">The Type class and its subclasses</a></li>
39 <li><a href="#QualType">The QualType class</a></li>
Ted Kremenek8bc05712007-10-10 23:01:43 +000040 <li><a href="#CFG">The CFG class</a></li>
Chris Lattner86920d32007-07-31 05:42:17 +000041 </ul>
42</li>
43</ul>
44
45
46<!-- ======================================================================= -->
47<h2 id="intro">Introduction</h2>
48<!-- ======================================================================= -->
49
50<p>This document describes some of the more important APIs and internal design
51decisions made in the clang C front-end. The purpose of this document is to
52both capture some of this high level information and also describe some of the
53design decisions behind it. This is meant for people interested in hacking on
54clang, not for end-users. The description below is categorized by
55libraries, and does not describe any of the clients of the libraries.</p>
56
57<!-- ======================================================================= -->
58<h2 id="libsystem">LLVM System and Support Libraries</h2>
59<!-- ======================================================================= -->
60
61<p>The LLVM libsystem library provides the basic clang system abstraction layer,
62which is used for file system access. The LLVM libsupport library provides many
63underlying libraries and <a
64href="http://llvm.org/docs/ProgrammersManual.html">data-structures</a>,
65 including command line option
66processing and various containers.</p>
67
68<!-- ======================================================================= -->
69<h2 id="libbasic">The clang 'Basic' Library</h2>
70<!-- ======================================================================= -->
71
72<p>This library certainly needs a better name. The 'basic' library contains a
73number of low-level utilities for tracking and manipulating source buffers,
74locations within the source buffers, diagnostics, tokens, target abstraction,
75and information about the subset of the language being compiled for.</p>
76
77<p>Part of this infrastructure is specific to C (such as the TargetInfo class),
78other parts could be reused for other non-C-based languages (SourceLocation,
79SourceManager, Diagnostics, FileManager). When and if there is future demand
80we can figure out if it makes sense to introduce a new library, move the general
81classes somewhere else, or introduce some other solution.</p>
82
83<p>We describe the roles of these classes in order of their dependencies.</p>
84
85<!-- ======================================================================= -->
86<h3 id="SourceLocation">The SourceLocation and SourceManager classes</h3>
87<!-- ======================================================================= -->
88
89<p>Strangely enough, the SourceLocation class represents a location within the
90source code of the program. Important design points include:</p>
91
92<ol>
93<li>sizeof(SourceLocation) must be extremely small, as these are embedded into
94 many AST nodes and are passed around often. Currently it is 32 bits.</li>
95<li>SourceLocation must be a simple value object that can be efficiently
96 copied.</li>
97<li>We should be able to represent a source location for any byte of any input
98 file. This includes in the middle of tokens, in whitespace, in trigraphs,
99 etc.</li>
100<li>A SourceLocation must encode the current #include stack that was active when
101 the location was processed. For example, if the location corresponds to a
102 token, it should contain the set of #includes active when the token was
103 lexed. This allows us to print the #include stack for a diagnostic.</li>
104<li>SourceLocation must be able to describe macro expansions, capturing both
105 the ultimate instantiation point and the source of the original character
106 data.</li>
107</ol>
108
109<p>In practice, the SourceLocation works together with the SourceManager class
110to encode two pieces of information about a location: it's physical location
111and it's virtual location. For most tokens, these will be the same. However,
112for a macro expansion (or tokens that came from a _Pragma directive) these will
113describe the location of the characters corresponding to the token and the
114location where the token was used (i.e. the macro instantiation point or the
115location of the _Pragma itself).</p>
116
117<p>For efficiency, we only track one level of macro instantions: if a token was
118produced by multiple instantiations, we only track the source and ultimate
119destination. Though we could track the intermediate instantiation points, this
120would require extra bookkeeping and no known client would benefit substantially
121from this.</p>
122
123<p>The clang front-end inherently depends on the location of a token being
124tracked correctly. If it is ever incorrect, the front-end may get confused and
125die. The reason for this is that the notion of the 'spelling' of a Token in
126clang depends on being able to find the original input characters for the token.
127This concept maps directly to the "physical" location for the token.</p>
128
129<!-- ======================================================================= -->
130<h2 id="liblex">The Lexer and Preprocessor Library</h2>
131<!-- ======================================================================= -->
132
133<p>The Lexer library contains several tightly-connected classes that are involved
134with the nasty process of lexing and preprocessing C source code. The main
135interface to this library for outside clients is the large <a
136href="#Preprocessor">Preprocessor</a> class.
137It contains the various pieces of state that are required to coherently read
138tokens out of a translation unit.</p>
139
140<p>The core interface to the Preprocessor object (once it is set up) is the
141Preprocessor::Lex method, which returns the next <a href="#Token">Token</a> from
142the preprocessor stream. There are two types of token providers that the
143preprocessor is capable of reading from: a buffer lexer (provided by the <a
144href="#Lexer">Lexer</a> class) and a buffered token stream (provided by the <a
Chris Lattner79281252008-03-09 02:27:26 +0000145href="#TokenLexer">TokenLexer</a> class).
Chris Lattner86920d32007-07-31 05:42:17 +0000146
147
148<!-- ======================================================================= -->
149<h3 id="Token">The Token class</h3>
150<!-- ======================================================================= -->
151
152<p>The Token class is used to represent a single lexed token. Tokens are
153intended to be used by the lexer/preprocess and parser libraries, but are not
154intended to live beyond them (for example, they should not live in the ASTs).<p>
155
156<p>Tokens most often live on the stack (or some other location that is efficient
157to access) as the parser is running, but occasionally do get buffered up. For
158example, macro definitions are stored as a series of tokens, and the C++
159front-end will eventually need to buffer tokens up for tentative parsing and
160various pieces of look-ahead. As such, the size of a Token matter. On a 32-bit
161system, sizeof(Token) is currently 16 bytes.</p>
162
163<p>Tokens contain the following information:</p>
164
165<ul>
166<li><b>A SourceLocation</b> - This indicates the location of the start of the
167token.</li>
168
169<li><b>A length</b> - This stores the length of the token as stored in the
170SourceBuffer. For tokens that include them, this length includes trigraphs and
171escaped newlines which are ignored by later phases of the compiler. By pointing
172into the original source buffer, it is always possible to get the original
173spelling of a token completely accurately.</li>
174
175<li><b>IdentifierInfo</b> - If a token takes the form of an identifier, and if
176identifier lookup was enabled when the token was lexed (e.g. the lexer was not
177reading in 'raw' mode) this contains a pointer to the unique hash value for the
178identifier. Because the lookup happens before keyword identification, this
179field is set even for language keywords like 'for'.</li>
180
181<li><b>TokenKind</b> - This indicates the kind of token as classified by the
182lexer. This includes things like <tt>tok::starequal</tt> (for the "*="
183operator), <tt>tok::ampamp</tt> for the "&amp;&amp;" token, and keyword values
184(e.g. <tt>tok::kw_for</tt>) for identifiers that correspond to keywords. Note
185that some tokens can be spelled multiple ways. For example, C++ supports
186"operator keywords", where things like "and" are treated exactly like the
187"&amp;&amp;" operator. In these cases, the kind value is set to
188<tt>tok::ampamp</tt>, which is good for the parser, which doesn't have to
189consider both forms. For something that cares about which form is used (e.g.
190the preprocessor 'stringize' operator) the spelling indicates the original
191form.</li>
192
193<li><b>Flags</b> - There are currently four flags tracked by the
194lexer/preprocessor system on a per-token basis:
195
196 <ol>
197 <li><b>StartOfLine</b> - This was the first token that occurred on its input
198 source line.</li>
199 <li><b>LeadingSpace</b> - There was a space character either immediately
200 before the token or transitively before the token as it was expanded
201 through a macro. The definition of this flag is very closely defined by
202 the stringizing requirements of the preprocessor.</li>
203 <li><b>DisableExpand</b> - This flag is used internally to the preprocessor to
204 represent identifier tokens which have macro expansion disabled. This
205 prevents them from being considered as candidates for macro expansion ever
206 in the future.</li>
207 <li><b>NeedsCleaning</b> - This flag is set if the original spelling for the
208 token includes a trigraph or escaped newline. Since this is uncommon,
209 many pieces of code can fast-path on tokens that did not need cleaning.
210 </p>
211 </ol>
212</li>
213</ul>
214
215<p>One interesting (and somewhat unusual) aspect of tokens is that they don't
216contain any semantic information about the lexed value. For example, if the
217token was a pp-number token, we do not represent the value of the number that
218was lexed (this is left for later pieces of code to decide). Additionally, the
219lexer library has no notion of typedef names vs variable names: both are
220returned as identifiers, and the parser is left to decide whether a specific
221identifier is a typedef or a variable (tracking this requires scope information
222among other things).</p>
223
224<!-- ======================================================================= -->
225<h3 id="Lexer">The Lexer class</h3>
226<!-- ======================================================================= -->
227
228<p>The Lexer class provides the mechanics of lexing tokens out of a source
229buffer and deciding what they mean. The Lexer is complicated by the fact that
230it operates on raw buffers that have not had spelling eliminated (this is a
231necessity to get decent performance), but this is countered with careful coding
232as well as standard performance techniques (for example, the comment handling
233code is vectorized on X86 and PowerPC hosts).</p>
234
235<p>The lexer has a couple of interesting modal features:</p>
236
237<ul>
238<li>The lexer can operate in 'raw' mode. This mode has several features that
239 make it possible to quickly lex the file (e.g. it stops identifier lookup,
240 doesn't specially handle preprocessor tokens, handles EOF differently, etc).
241 This mode is used for lexing within an "<tt>#if 0</tt>" block, for
242 example.</li>
243<li>The lexer can capture and return comments as tokens. This is required to
244 support the -C preprocessor mode, which passes comments through, and is
245 used by the diagnostic checker to identifier expect-error annotations.</li>
246<li>The lexer can be in ParsingFilename mode, which happens when preprocessing
Chris Lattner84386242007-09-16 19:25:23 +0000247 after reading a #include directive. This mode changes the parsing of '&lt;'
Chris Lattner86920d32007-07-31 05:42:17 +0000248 to return an "angled string" instead of a bunch of tokens for each thing
249 within the filename.</li>
250<li>When parsing a preprocessor directive (after "<tt>#</tt>") the
251 ParsingPreprocessorDirective mode is entered. This changes the parser to
252 return EOM at a newline.</li>
253<li>The Lexer uses a LangOptions object to know whether trigraphs are enabled,
254 whether C++ or ObjC keywords are recognized, etc.</li>
255</ul>
256
257<p>In addition to these modes, the lexer keeps track of a couple of other
258 features that are local to a lexed buffer, which change as the buffer is
259 lexed:</p>
260
261<ul>
262<li>The Lexer uses BufferPtr to keep track of the current character being
263 lexed.</li>
264<li>The Lexer uses IsAtStartOfLine to keep track of whether the next lexed token
265 will start with its "start of line" bit set.</li>
266<li>The Lexer keeps track of the current #if directives that are active (which
267 can be nested).</li>
268<li>The Lexer keeps track of an <a href="#MultipleIncludeOpt">
269 MultipleIncludeOpt</a> object, which is used to
270 detect whether the buffer uses the standard "<tt>#ifndef XX</tt> /
271 <tt>#define XX</tt>" idiom to prevent multiple inclusion. If a buffer does,
272 subsequent includes can be ignored if the XX macro is defined.</li>
273</ul>
274
275<!-- ======================================================================= -->
Chris Lattner79281252008-03-09 02:27:26 +0000276<h3 id="TokenLexer">The TokenLexer class</h3>
Chris Lattner86920d32007-07-31 05:42:17 +0000277<!-- ======================================================================= -->
278
Chris Lattner79281252008-03-09 02:27:26 +0000279<p>The TokenLexer class is a token provider that returns tokens from a list
Chris Lattner86920d32007-07-31 05:42:17 +0000280of tokens that came from somewhere else. It typically used for two things: 1)
281returning tokens from a macro definition as it is being expanded 2) returning
282tokens from an arbitrary buffer of tokens. The later use is used by _Pragma and
283will most likely be used to handle unbounded look-ahead for the C++ parser.</p>
284
285<!-- ======================================================================= -->
286<h3 id="MultipleIncludeOpt">The MultipleIncludeOpt class</h3>
287<!-- ======================================================================= -->
288
289<p>The MultipleIncludeOpt class implements a really simple little state machine
290that is used to detect the standard "<tt>#ifndef XX</tt> / <tt>#define XX</tt>"
291idiom that people typically use to prevent multiple inclusion of headers. If a
292buffer uses this idiom and is subsequently #include'd, the preprocessor can
293simply check to see whether the guarding condition is defined or not. If so,
294the preprocessor can completely ignore the include of the header.</p>
295
296
297
298<!-- ======================================================================= -->
299<h2 id="libparse">The Parser Library</h2>
300<!-- ======================================================================= -->
301
302<!-- ======================================================================= -->
303<h2 id="libast">The AST Library</h2>
304<!-- ======================================================================= -->
305
306<!-- ======================================================================= -->
307<h3 id="Type">The Type class and its subclasses</h3>
308<!-- ======================================================================= -->
309
310<p>The Type class (and its subclasses) are an important part of the AST. Types
311are accessed through the ASTContext class, which implicitly creates and uniques
312them as they are needed. Types have a couple of non-obvious features: 1) they
313do not capture type qualifiers like const or volatile (See
314<a href="#QualType">QualType</a>), and 2) they implicitly capture typedef
Chris Lattner8a2bc622007-07-31 06:37:39 +0000315information. Once created, types are immutable (unlike decls).</p>
Chris Lattner86920d32007-07-31 05:42:17 +0000316
317<p>Typedefs in C make semantic analysis a bit more complex than it would
318be without them. The issue is that we want to capture typedef information
319and represent it in the AST perfectly, but the semantics of operations need to
320"see through" typedefs. For example, consider this code:</p>
321
322<code>
323void func() {<br>
Bill Wendling30d17752007-10-06 01:56:01 +0000324&nbsp;&nbsp;typedef int foo;<br>
325&nbsp;&nbsp;foo X, *Y;<br>
326&nbsp;&nbsp;typedef foo* bar;<br>
327&nbsp;&nbsp;bar Z;<br>
328&nbsp;&nbsp;*X; <i>// error</i><br>
329&nbsp;&nbsp;**Y; <i>// error</i><br>
330&nbsp;&nbsp;**Z; <i>// error</i><br>
Chris Lattner86920d32007-07-31 05:42:17 +0000331}<br>
332</code>
333
334<p>The code above is illegal, and thus we expect there to be diagnostics emitted
335on the annotated lines. In this example, we expect to get:</p>
336
337<pre>
Chris Lattner8a2bc622007-07-31 06:37:39 +0000338<b>test.c:6:1: error: indirection requires pointer operand ('foo' invalid)</b>
Chris Lattner86920d32007-07-31 05:42:17 +0000339*X; // error
340<font color="blue">^~</font>
Chris Lattner8a2bc622007-07-31 06:37:39 +0000341<b>test.c:7:1: error: indirection requires pointer operand ('foo' invalid)</b>
Chris Lattner86920d32007-07-31 05:42:17 +0000342**Y; // error
343<font color="blue">^~~</font>
Chris Lattner8a2bc622007-07-31 06:37:39 +0000344<b>test.c:8:1: error: indirection requires pointer operand ('foo' invalid)</b>
345**Z; // error
346<font color="blue">^~~</font>
Chris Lattner86920d32007-07-31 05:42:17 +0000347</pre>
348
349<p>While this example is somewhat silly, it illustrates the point: we want to
350retain typedef information where possible, so that we can emit errors about
351"<tt>std::string</tt>" instead of "<tt>std::basic_string&lt;char, std:...</tt>".
352Doing this requires properly keeping typedef information (for example, the type
353of "X" is "foo", not "int"), and requires properly propagating it through the
Chris Lattner8a2bc622007-07-31 06:37:39 +0000354various operators (for example, the type of *Y is "foo", not "int"). In order
355to retain this information, the type of these expressions is an instance of the
356TypedefType class, which indicates that the type of these expressions is a
357typedef for foo.
358</p>
Chris Lattner86920d32007-07-31 05:42:17 +0000359
Chris Lattner8a2bc622007-07-31 06:37:39 +0000360<p>Representing types like this is great for diagnostics, because the
361user-specified type is always immediately available. There are two problems
362with this: first, various semantic checks need to make judgements about the
Chris Lattner33fc68a2007-07-31 18:54:50 +0000363<em>actual structure</em> of a type, ignoring typdefs. Second, we need an
364efficient way to query whether two types are structurally identical to each
365other, ignoring typedefs. The solution to both of these problems is the idea of
Chris Lattner8a2bc622007-07-31 06:37:39 +0000366canonical types.</p>
Chris Lattner86920d32007-07-31 05:42:17 +0000367
Chris Lattner8a2bc622007-07-31 06:37:39 +0000368<h4>Canonical Types</h4>
Chris Lattner86920d32007-07-31 05:42:17 +0000369
Chris Lattner8a2bc622007-07-31 06:37:39 +0000370<p>Every instance of the Type class contains a canonical type pointer. For
371simple types with no typedefs involved (e.g. "<tt>int</tt>", "<tt>int*</tt>",
372"<tt>int**</tt>"), the type just points to itself. For types that have a
373typedef somewhere in their structure (e.g. "<tt>foo</tt>", "<tt>foo*</tt>",
374"<tt>foo**</tt>", "<tt>bar</tt>"), the canonical type pointer points to their
375structurally equivalent type without any typedefs (e.g. "<tt>int</tt>",
376"<tt>int*</tt>", "<tt>int**</tt>", and "<tt>int*</tt>" respectively).</p>
Chris Lattner86920d32007-07-31 05:42:17 +0000377
Chris Lattner8a2bc622007-07-31 06:37:39 +0000378<p>This design provides a constant time operation (dereferencing the canonical
379type pointer) that gives us access to the structure of types. For example,
380we can trivially tell that "bar" and "foo*" are the same type by dereferencing
381their canonical type pointers and doing a pointer comparison (they both point
382to the single "<tt>int*</tt>" type).</p>
383
384<p>Canonical types and typedef types bring up some complexities that must be
385carefully managed. Specifically, the "isa/cast/dyncast" operators generally
386shouldn't be used in code that is inspecting the AST. For example, when type
387checking the indirection operator (unary '*' on a pointer), the type checker
388must verify that the operand has a pointer type. It would not be correct to
389check that with "<tt>isa&lt;PointerType&gt;(SubExpr-&gt;getType())</tt>",
390because this predicate would fail if the subexpression had a typedef type.</p>
391
392<p>The solution to this problem are a set of helper methods on Type, used to
393check their properties. In this case, it would be correct to use
394"<tt>SubExpr-&gt;getType()-&gt;isPointerType()</tt>" to do the check. This
395predicate will return true if the <em>canonical type is a pointer</em>, which is
396true any time the type is structurally a pointer type. The only hard part here
397is remembering not to use the <tt>isa/cast/dyncast</tt> operations.</p>
398
399<p>The second problem we face is how to get access to the pointer type once we
400know it exists. To continue the example, the result type of the indirection
401operator is the pointee type of the subexpression. In order to determine the
402type, we need to get the instance of PointerType that best captures the typedef
403information in the program. If the type of the expression is literally a
404PointerType, we can return that, otherwise we have to dig through the
405typedefs to find the pointer type. For example, if the subexpression had type
406"<tt>foo*</tt>", we could return that type as the result. If the subexpression
407had type "<tt>bar</tt>", we want to return "<tt>foo*</tt>" (note that we do
408<em>not</em> want "<tt>int*</tt>"). In order to provide all of this, Type has
Chris Lattner11406c12007-07-31 16:50:51 +0000409a getAsPointerType() method that checks whether the type is structurally a
Chris Lattner8a2bc622007-07-31 06:37:39 +0000410PointerType and, if so, returns the best one. If not, it returns a null
411pointer.</p>
412
413<p>This structure is somewhat mystical, but after meditating on it, it will
414make sense to you :).</p>
Chris Lattner86920d32007-07-31 05:42:17 +0000415
416<!-- ======================================================================= -->
417<h3 id="QualType">The QualType class</h3>
418<!-- ======================================================================= -->
419
420<p>The QualType class is designed as a trivial value class that is small,
421passed by-value and is efficient to query. The idea of QualType is that it
422stores the type qualifiers (const, volatile, restrict) separately from the types
423themselves: QualType is conceptually a pair of "Type*" and bits for the type
424qualifiers.</p>
425
426<p>By storing the type qualifiers as bits in the conceptual pair, it is
427extremely efficient to get the set of qualifiers on a QualType (just return the
428field of the pair), add a type qualifier (which is a trivial constant-time
429operation that sets a bit), and remove one or more type qualifiers (just return
430a QualType with the bitfield set to empty).</p>
431
432<p>Further, because the bits are stored outside of the type itself, we do not
433need to create duplicates of types with different sets of qualifiers (i.e. there
434is only a single heap allocated "int" type: "const int" and "volatile const int"
435both point to the same heap allocated "int" type). This reduces the heap size
436used to represent bits and also means we do not have to consider qualifiers when
437uniquing types (<a href="#Type">Type</a> does not even contain qualifiers).</p>
438
439<p>In practice, on hosts where it is safe, the 3 type qualifiers are stored in
440the low bit of the pointer to the Type object. This means that QualType is
441exactly the same size as a pointer, and this works fine on any system where
442malloc'd objects are at least 8 byte aligned.</p>
Ted Kremenek8bc05712007-10-10 23:01:43 +0000443
444<!-- ======================================================================= -->
445<h3 id="CFG">The <tt>CFG</tt> class</h3>
446<!-- ======================================================================= -->
447
448<p>The <tt>CFG</tt> class is designed to represent a source-level
449control-flow graph for a single statement (<tt>Stmt*</tt>). Typically
450instances of <tt>CFG</tt> are constructed for function bodies (usually
451an instance of <tt>CompoundStmt</tt>), but can also be instantiated to
452represent the control-flow of any class that subclasses <tt>Stmt</tt>,
453which includes simple expressions. Control-flow graphs are especially
454useful for performing
455<a href="http://en.wikipedia.org/wiki/Data_flow_analysis#Sensitivities">flow-
456or path-sensitive</a> program analyses on a given function.</p>
457
458<h4>Basic Blocks</h4>
459
460<p>Concretely, an instance of <tt>CFG</tt> is a collection of basic
461blocks. Each basic block is an instance of <tt>CFGBlock</tt>, which
462simply contains an ordered sequence of <tt>Stmt*</tt> (each referring
463to statements in the AST). The ordering of statements within a block
464indicates unconditional flow of control from one statement to the
465next. <a href="#ConditionalControlFlow">Conditional control-flow</a>
466is represented using edges between basic blocks. The statements
467within a given <tt>CFGBlock</tt> can be traversed using
468the <tt>CFGBlock::*iterator</tt> interface.</p>
469
470<p>
Ted Kremenek18e17e72007-10-18 22:50:52 +0000471A <tt>CFG</tt> object owns the instances of <tt>CFGBlock</tt> within
Ted Kremenek8bc05712007-10-10 23:01:43 +0000472the control-flow graph it represents. Each <tt>CFGBlock</tt> within a
473CFG is also uniquely numbered (accessible
474via <tt>CFGBlock::getBlockID()</tt>). Currently the number is
475based on the ordering the blocks were created, but no assumptions
476should be made on how <tt>CFGBlock</tt>s are numbered other than their
477numbers are unique and that they are numbered from 0..N-1 (where N is
478the number of basic blocks in the CFG).</p>
479
480<h4>Entry and Exit Blocks</h4>
481
482Each instance of <tt>CFG</tt> contains two special blocks:
483an <i>entry</i> block (accessible via <tt>CFG::getEntry()</tt>), which
484has no incoming edges, and an <i>exit</i> block (accessible
485via <tt>CFG::getExit()</tt>), which has no outgoing edges. Neither
486block contains any statements, and they serve the role of providing a
487clear entrance and exit for a body of code such as a function body.
488The presence of these empty blocks greatly simplifies the
489implementation of many analyses built on top of CFGs.
490
491<h4 id ="ConditionalControlFlow">Conditional Control-Flow</h4>
492
493<p>Conditional control-flow (such as those induced by if-statements
494and loops) is represented as edges between <tt>CFGBlock</tt>s.
495Because different C language constructs can induce control-flow,
496each <tt>CFGBlock</tt> also records an extra <tt>Stmt*</tt> that
497represents the <i>terminator</i> of the block. A terminator is simply
498the statement that caused the control-flow, and is used to identify
499the nature of the conditional control-flow between blocks. For
500example, in the case of an if-statement, the terminator refers to
501the <tt>IfStmt</tt> object in the AST that represented the given
502branch.</p>
503
504<p>To illustrate, consider the following code example:</p>
505
506<code>
507int foo(int x) {<br>
508&nbsp;&nbsp;x = x + 1;<br>
509<br>
510&nbsp;&nbsp;if (x > 2) x++;<br>
511&nbsp;&nbsp;else {<br>
512&nbsp;&nbsp;&nbsp;&nbsp;x += 2;<br>
513&nbsp;&nbsp;&nbsp;&nbsp;x *= 2;<br>
514&nbsp;&nbsp;}<br>
515<br>
516&nbsp;&nbsp;return x;<br>
517}
518</code>
519
520<p>After invoking the parser+semantic analyzer on this code fragment,
521the AST of the body of <tt>foo</tt> is referenced by a
522single <tt>Stmt*</tt>. We can then construct an instance
523of <tt>CFG</tt> representing the control-flow graph of this function
524body by single call to a static class method:</p>
525
526<code>
527&nbsp;&nbsp;Stmt* FooBody = ...<br>
528&nbsp;&nbsp;CFG* FooCFG = <b>CFG::buildCFG</b>(FooBody);
529</code>
530
531<p>It is the responsibility of the caller of <tt>CFG::buildCFG</tt>
532to <tt>delete</tt> the returned <tt>CFG*</tt> when the CFG is no
533longer needed.</p>
534
535<p>Along with providing an interface to iterate over
536its <tt>CFGBlock</tt>s, the <tt>CFG</tt> class also provides methods
537that are useful for debugging and visualizing CFGs. For example, the
538method
539<tt>CFG::dump()</tt> dumps a pretty-printed version of the CFG to
540standard error. This is especially useful when one is using a
541debugger such as gdb. For example, here is the output
542of <tt>FooCFG->dump()</tt>:</p>
543
544<code>
545&nbsp;[ B5 (ENTRY) ]<br>
546&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (0):<br>
547&nbsp;&nbsp;&nbsp;&nbsp;Successors (1): B4<br>
548<br>
549&nbsp;[ B4 ]<br>
550&nbsp;&nbsp;&nbsp;&nbsp;1: x = x + 1<br>
551&nbsp;&nbsp;&nbsp;&nbsp;2: (x > 2)<br>
552&nbsp;&nbsp;&nbsp;&nbsp;<b>T: if [B4.2]</b><br>
553&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (1): B5<br>
554&nbsp;&nbsp;&nbsp;&nbsp;Successors (2): B3 B2<br>
555<br>
556&nbsp;[ B3 ]<br>
557&nbsp;&nbsp;&nbsp;&nbsp;1: x++<br>
558&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (1): B4<br>
559&nbsp;&nbsp;&nbsp;&nbsp;Successors (1): B1<br>
560<br>
561&nbsp;[ B2 ]<br>
562&nbsp;&nbsp;&nbsp;&nbsp;1: x += 2<br>
563&nbsp;&nbsp;&nbsp;&nbsp;2: x *= 2<br>
564&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (1): B4<br>
565&nbsp;&nbsp;&nbsp;&nbsp;Successors (1): B1<br>
566<br>
567&nbsp;[ B1 ]<br>
568&nbsp;&nbsp;&nbsp;&nbsp;1: return x;<br>
569&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (2): B2 B3<br>
570&nbsp;&nbsp;&nbsp;&nbsp;Successors (1): B0<br>
571<br>
572&nbsp;[ B0 (EXIT) ]<br>
573&nbsp;&nbsp;&nbsp;&nbsp;Predecessors (1): B1<br>
574&nbsp;&nbsp;&nbsp;&nbsp;Successors (0):
575</code>
576
577<p>For each block, the pretty-printed output displays for each block
578the number of <i>predecessor</i> blocks (blocks that have outgoing
579control-flow to the given block) and <i>successor</i> blocks (blocks
580that have control-flow that have incoming control-flow from the given
581block). We can also clearly see the special entry and exit blocks at
582the beginning and end of the pretty-printed output. For the entry
583block (block B5), the number of predecessor blocks is 0, while for the
584exit block (block B0) the number of successor blocks is 0.</p>
585
586<p>The most interesting block here is B4, whose outgoing control-flow
587represents the branching caused by the sole if-statement
588in <tt>foo</tt>. Of particular interest is the second statement in
589the block, <b><tt>(x > 2)</tt></b>, and the terminator, printed
590as <b><tt>if [B4.2]</tt></b>. The second statement represents the
591evaluation of the condition of the if-statement, which occurs before
592the actual branching of control-flow. Within the <tt>CFGBlock</tt>
593for B4, the <tt>Stmt*</tt> for the second statement refers to the
594actual expression in the AST for <b><tt>(x > 2)</tt></b>. Thus
595pointers to subclasses of <tt>Expr</tt> can appear in the list of
596statements in a block, and not just subclasses of <tt>Stmt</tt> that
597refer to proper C statements.</p>
598
599<p>The terminator of block B4 is a pointer to the <tt>IfStmt</tt>
600object in the AST. The pretty-printer outputs <b><tt>if
601[B4.2]</tt></b> because the condition expression of the if-statement
602has an actual place in the basic block, and thus the terminator is
603essentially
604<i>referring</i> to the expression that is the second statement of
605block B4 (i.e., B4.2). In this manner, conditions for control-flow
606(which also includes conditions for loops and switch statements) are
607hoisted into the actual basic block.</p>
608
Ted Kremenek98f19b62007-10-10 23:22:00 +0000609<!--
Ted Kremenek8bc05712007-10-10 23:01:43 +0000610<h4>Implicit Control-Flow</h4>
Ted Kremenek98f19b62007-10-10 23:22:00 +0000611-->
Ted Kremenek8bc05712007-10-10 23:01:43 +0000612
613<!--
614<p>A key design principle of the <tt>CFG</tt> class was to not require
615any transformations to the AST in order to represent control-flow.
616Thus the <tt>CFG</tt> does not perform any "lowering" of the
617statements in an AST: loops are not transformed into guarded gotos,
618short-circuit operations are not converted to a set of if-statements,
619and so on.</p>
620-->
Ted Kremenek17a295d2008-06-11 06:19:49 +0000621
622</div>
623</body>
624</html>