Ted Kremenek | 17a295d | 2008-06-11 06:19:49 +0000 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 3 | <title>"Clang" CFE Internals Manual</title> |
Ted Kremenek | 17a295d | 2008-06-11 06:19:49 +0000 | [diff] [blame] | 4 | <link type="text/css" rel="stylesheet" href="../menu.css" /> |
| 5 | <link type="text/css" rel="stylesheet" href="../content.css" /> |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 6 | <style type="text/css"> |
| 7 | td { |
| 8 | vertical-align: top; |
| 9 | } |
| 10 | </style> |
Ted Kremenek | 17a295d | 2008-06-11 06:19:49 +0000 | [diff] [blame] | 11 | </head> |
| 12 | <body> |
| 13 | |
| 14 | <!--#include virtual="../menu.html.incl"--> |
| 15 | |
| 16 | <div id="content"> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 18 | <h1>"Clang" CFE Internals Manual</h1> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 19 | |
| 20 | <ul> |
| 21 | <li><a href="#intro">Introduction</a></li> |
| 22 | <li><a href="#libsystem">LLVM System and Support Libraries</a></li> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 23 | <li><a href="#libbasic">The Clang 'Basic' Library</a> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 24 | <ul> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 25 | <li><a href="#Diagnostics">The Diagnostics Subsystem</a></li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 26 | <li><a href="#SourceLocation">The SourceLocation and SourceManager |
| 27 | classes</a></li> |
Douglas Gregor | 715c92a | 2010-10-27 16:02:28 +0000 | [diff] [blame^] | 28 | <li><a href="#SourceRange">SourceRange and CharSourceRange</a></li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 29 | </ul> |
| 30 | </li> |
Daniel Dunbar | 27d9e9f | 2009-03-30 06:50:01 +0000 | [diff] [blame] | 31 | <li><a href="#libdriver">The Driver Library</a> |
| 32 | <ul> |
| 33 | </ul> |
| 34 | </li> |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 35 | <li><a href="#pch">Precompiled Headers</a> |
Daniel Dunbar | 27d9e9f | 2009-03-30 06:50:01 +0000 | [diff] [blame] | 36 | <li><a href="#libfrontend">The Frontend Library</a> |
| 37 | <ul> |
| 38 | </ul> |
| 39 | </li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 40 | <li><a href="#liblex">The Lexer and Preprocessor Library</a> |
| 41 | <ul> |
| 42 | <li><a href="#Token">The Token class</a></li> |
| 43 | <li><a href="#Lexer">The Lexer class</a></li> |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 44 | <li><a href="#AnnotationToken">Annotation Tokens</a></li> |
Chris Lattner | 7928125 | 2008-03-09 02:27:26 +0000 | [diff] [blame] | 45 | <li><a href="#TokenLexer">The TokenLexer class</a></li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 46 | <li><a href="#MultipleIncludeOpt">The MultipleIncludeOpt class</a></li> |
| 47 | </ul> |
| 48 | </li> |
| 49 | <li><a href="#libparse">The Parser Library</a> |
| 50 | <ul> |
| 51 | </ul> |
| 52 | </li> |
| 53 | <li><a href="#libast">The AST Library</a> |
| 54 | <ul> |
| 55 | <li><a href="#Type">The Type class and its subclasses</a></li> |
| 56 | <li><a href="#QualType">The QualType class</a></li> |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 57 | <li><a href="#DeclarationName">Declaration names</a></li> |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 58 | <li><a href="#DeclContext">Declaration contexts</a> |
| 59 | <ul> |
| 60 | <li><a href="#Redeclarations">Redeclarations and Overloads</a></li> |
| 61 | <li><a href="#LexicalAndSemanticContexts">Lexical and Semantic |
| 62 | Contexts</a></li> |
| 63 | <li><a href="#TransparentContexts">Transparent Declaration Contexts</a></li> |
| 64 | <li><a href="#MultiDeclContext">Multiply-Defined Declaration Contexts</a></li> |
| 65 | </ul> |
| 66 | </li> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 67 | <li><a href="#CFG">The CFG class</a></li> |
Chris Lattner | 7bad199 | 2008-11-16 21:48:07 +0000 | [diff] [blame] | 68 | <li><a href="#Constants">Constant Folding in the Clang AST</a></li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 69 | </ul> |
| 70 | </li> |
Argyrios Kyrtzidis | 7240d77 | 2009-07-10 03:41:36 +0000 | [diff] [blame] | 71 | <li><a href="libIndex.html">The Index Library</a></li> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 72 | </ul> |
| 73 | |
| 74 | |
| 75 | <!-- ======================================================================= --> |
| 76 | <h2 id="intro">Introduction</h2> |
| 77 | <!-- ======================================================================= --> |
| 78 | |
| 79 | <p>This document describes some of the more important APIs and internal design |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 80 | decisions made in the Clang C front-end. The purpose of this document is to |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 81 | both capture some of this high level information and also describe some of the |
| 82 | design decisions behind it. This is meant for people interested in hacking on |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 83 | Clang, not for end-users. The description below is categorized by |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 84 | libraries, and does not describe any of the clients of the libraries.</p> |
| 85 | |
| 86 | <!-- ======================================================================= --> |
| 87 | <h2 id="libsystem">LLVM System and Support Libraries</h2> |
| 88 | <!-- ======================================================================= --> |
| 89 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 90 | <p>The LLVM libsystem library provides the basic Clang system abstraction layer, |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 91 | which is used for file system access. The LLVM libsupport library provides many |
| 92 | underlying libraries and <a |
| 93 | href="http://llvm.org/docs/ProgrammersManual.html">data-structures</a>, |
| 94 | including command line option |
| 95 | processing and various containers.</p> |
| 96 | |
| 97 | <!-- ======================================================================= --> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 98 | <h2 id="libbasic">The Clang 'Basic' Library</h2> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 99 | <!-- ======================================================================= --> |
| 100 | |
| 101 | <p>This library certainly needs a better name. The 'basic' library contains a |
| 102 | number of low-level utilities for tracking and manipulating source buffers, |
| 103 | locations within the source buffers, diagnostics, tokens, target abstraction, |
| 104 | and information about the subset of the language being compiled for.</p> |
| 105 | |
| 106 | <p>Part of this infrastructure is specific to C (such as the TargetInfo class), |
| 107 | other parts could be reused for other non-C-based languages (SourceLocation, |
| 108 | SourceManager, Diagnostics, FileManager). When and if there is future demand |
| 109 | we can figure out if it makes sense to introduce a new library, move the general |
| 110 | classes somewhere else, or introduce some other solution.</p> |
| 111 | |
| 112 | <p>We describe the roles of these classes in order of their dependencies.</p> |
| 113 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 114 | |
| 115 | <!-- ======================================================================= --> |
| 116 | <h3 id="Diagnostics">The Diagnostics Subsystem</h3> |
| 117 | <!-- ======================================================================= --> |
| 118 | |
| 119 | <p>The Clang Diagnostics subsystem is an important part of how the compiler |
| 120 | communicates with the human. Diagnostics are the warnings and errors produced |
| 121 | when the code is incorrect or dubious. In Clang, each diagnostic produced has |
Sebastian Redl | 9bc2a99 | 2010-07-07 23:42:27 +0000 | [diff] [blame] | 122 | (at the minimum) a unique ID, an English translation associated with it, a <a |
| 123 | href="#SourceLocation">SourceLocation</a> to "put the caret", and a severity (e.g. |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 124 | <tt>WARNING</tt> or <tt>ERROR</tt>). They can also optionally include a number |
| 125 | of arguments to the dianostic (which fill in "%0"'s in the string) as well as a |
| 126 | number of source ranges that related to the diagnostic.</p> |
| 127 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 128 | <p>In this section, we'll be giving examples produced by the Clang command line |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 129 | driver, but diagnostics can be <a href="#DiagnosticClient">rendered in many |
| 130 | different ways</a> depending on how the DiagnosticClient interface is |
Sebastian Redl | 9bc2a99 | 2010-07-07 23:42:27 +0000 | [diff] [blame] | 131 | implemented. A representative example of a diagnostic is:</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 132 | |
| 133 | <pre> |
| 134 | t.c:38:15: error: invalid operands to binary expression ('int *' and '_Complex float') |
| 135 | <font color="darkgreen">P = (P-42) + Gamma*4;</font> |
| 136 | <font color="blue">~~~~~~ ^ ~~~~~~~</font> |
| 137 | </pre> |
| 138 | |
| 139 | <p>In this example, you can see the English translation, the severity (error), |
| 140 | you can see the source location (the caret ("^") and file/line/column info), |
| 141 | the source ranges "~~~~", arguments to the diagnostic ("int*" and "_Complex |
| 142 | float"). You'll have to believe me that there is a unique ID backing the |
| 143 | diagnostic :).</p> |
| 144 | |
| 145 | <p>Getting all of this to happen has several steps and involves many moving |
| 146 | pieces, this section describes them and talks about best practices when adding |
| 147 | a new diagnostic.</p> |
| 148 | |
Chris Lattner | 4c50b69 | 2010-05-01 17:35:19 +0000 | [diff] [blame] | 149 | <!-- ============================== --> |
| 150 | <h4>The Diagnostic*Kinds.def files</h4> |
| 151 | <!-- ============================== --> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 4c50b69 | 2010-05-01 17:35:19 +0000 | [diff] [blame] | 153 | <p>Diagnostics are created by adding an entry to one of the <tt> |
| 154 | clang/Basic/Diagnostic*Kinds.def</tt> files, depending on what library will |
| 155 | be using it. This file encodes the unique ID of the |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 156 | diagnostic (as an enum, the first argument), the severity of the diagnostic |
| 157 | (second argument) and the English translation + format string.</p> |
| 158 | |
| 159 | <p>There is little sanity with the naming of the unique ID's right now. Some |
| 160 | start with err_, warn_, ext_ to encode the severity into the name. Since the |
| 161 | enum is referenced in the C++ code that produces the diagnostic, it is somewhat |
| 162 | useful for it to be reasonably short.</p> |
| 163 | |
| 164 | <p>The severity of the diagnostic comes from the set {<tt>NOTE</tt>, |
| 165 | <tt>WARNING</tt>, <tt>EXTENSION</tt>, <tt>EXTWARN</tt>, <tt>ERROR</tt>}. The |
| 166 | <tt>ERROR</tt> severity is used for diagnostics indicating the program is never |
| 167 | acceptable under any circumstances. When an error is emitted, the AST for the |
| 168 | input code may not be fully built. The <tt>EXTENSION</tt> and <tt>EXTWARN</tt> |
| 169 | severities are used for extensions to the language that Clang accepts. This |
| 170 | means that Clang fully understands and can represent them in the AST, but we |
| 171 | produce diagnostics to tell the user their code is non-portable. The difference |
| 172 | is that the former are ignored by default, and the later warn by default. The |
| 173 | <tt>WARNING</tt> severity is used for constructs that are valid in the currently |
| 174 | selected source language but that are dubious in some way. The <tt>NOTE</tt> |
Daniel Dunbar | 426b863 | 2009-02-17 15:49:03 +0000 | [diff] [blame] | 175 | level is used to staple more information onto previous diagnostics.</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 176 | |
| 177 | <p>These <em>severities</em> are mapped into a smaller set (the |
| 178 | Diagnostic::Level enum, {<tt>Ignored</tt>, <tt>Note</tt>, <tt>Warning</tt>, |
Chris Lattner | 0aad297 | 2009-02-05 22:49:08 +0000 | [diff] [blame] | 179 | <tt>Error</tt>, <tt>Fatal</tt> }) of output <em>levels</em> by the diagnostics |
Chris Lattner | a180fdd | 2009-02-17 07:07:29 +0000 | [diff] [blame] | 180 | subsystem based on various configuration options. Clang internally supports a |
| 181 | fully fine grained mapping mechanism that allows you to map almost any |
| 182 | diagnostic to the output level that you want. The only diagnostics that cannot |
| 183 | be mapped are <tt>NOTE</tt>s, which always follow the severity of the previously |
| 184 | emitted diagnostic and <tt>ERROR</tt>s, which can only be mapped to |
| 185 | <tt>Fatal</tt> (it is not possible to turn an error into a warning, |
| 186 | for example).</p> |
| 187 | |
| 188 | <p>Diagnostic mappings are used in many ways. For example, if the user |
| 189 | specifies <tt>-pedantic</tt>, <tt>EXTENSION</tt> maps to <tt>Warning</tt>, if |
| 190 | they specify <tt>-pedantic-errors</tt>, it turns into <tt>Error</tt>. This is |
| 191 | used to implement options like <tt>-Wunused_macros</tt>, <tt>-Wundef</tt> etc. |
| 192 | </p> |
| 193 | |
| 194 | <p> |
| 195 | Mapping to <tt>Fatal</tt> should only be used for diagnostics that are |
| 196 | considered so severe that error recovery won't be able to recover sensibly from |
| 197 | them (thus spewing a ton of bogus errors). One example of this class of error |
| 198 | are failure to #include a file. |
| 199 | </p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 200 | |
| 201 | <!-- ================= --> |
| 202 | <h4>The Format String</h4> |
| 203 | <!-- ================= --> |
| 204 | |
| 205 | <p>The format string for the diagnostic is very simple, but it has some power. |
| 206 | It takes the form of a string in English with markers that indicate where and |
| 207 | how arguments to the diagnostic are inserted and formatted. For example, here |
| 208 | are some simple format strings:</p> |
| 209 | |
| 210 | <pre> |
| 211 | "binary integer literals are an extension" |
| 212 | "format string contains '\\0' within the string body" |
| 213 | "more '<b>%%</b>' conversions than data arguments" |
Chris Lattner | 545b368 | 2008-11-23 20:27:13 +0000 | [diff] [blame] | 214 | "invalid operands to binary expression (<b>%0</b> and <b>%1</b>)" |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 215 | "overloaded '<b>%0</b>' must be a <b>%select{unary|binary|unary or binary}2</b> operator" |
| 216 | " (has <b>%1</b> parameter<b>%s1</b>)" |
| 217 | </pre> |
| 218 | |
| 219 | <p>These examples show some important points of format strings. You can use any |
| 220 | plain ASCII character in the diagnostic string except "%" without a problem, |
| 221 | but these are C strings, so you have to use and be aware of all the C escape |
| 222 | sequences (as in the second example). If you want to produce a "%" in the |
| 223 | output, use the "%%" escape sequence, like the third diagnostic. Finally, |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 224 | Clang uses the "%...[digit]" sequences to specify where and how arguments to |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 225 | the diagnostic are formatted.</p> |
| 226 | |
| 227 | <p>Arguments to the diagnostic are numbered according to how they are specified |
| 228 | by the C++ code that <a href="#producingdiag">produces them</a>, and are |
| 229 | referenced by <tt>%0</tt> .. <tt>%9</tt>. If you have more than 10 arguments |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 230 | to your diagnostic, you are doing something wrong :). Unlike printf, there |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 231 | is no requirement that arguments to the diagnostic end up in the output in |
| 232 | the same order as they are specified, you could have a format string with |
| 233 | <tt>"%1 %0"</tt> that swaps them, for example. The text in between the |
| 234 | percent and digit are formatting instructions. If there are no instructions, |
| 235 | the argument is just turned into a string and substituted in.</p> |
| 236 | |
| 237 | <p>Here are some "best practices" for writing the English format string:</p> |
| 238 | |
| 239 | <ul> |
| 240 | <li>Keep the string short. It should ideally fit in the 80 column limit of the |
| 241 | <tt>DiagnosticKinds.def</tt> file. This avoids the diagnostic wrapping when |
| 242 | printed, and forces you to think about the important point you are conveying |
| 243 | with the diagnostic.</li> |
| 244 | <li>Take advantage of location information. The user will be able to see the |
| 245 | line and location of the caret, so you don't need to tell them that the |
| 246 | problem is with the 4th argument to the function: just point to it.</li> |
| 247 | <li>Do not capitalize the diagnostic string, and do not end it with a |
| 248 | period.</li> |
| 249 | <li>If you need to quote something in the diagnostic string, use single |
| 250 | quotes.</li> |
| 251 | </ul> |
| 252 | |
| 253 | <p>Diagnostics should never take random English strings as arguments: you |
| 254 | shouldn't use <tt>"you have a problem with %0"</tt> and pass in things like |
| 255 | <tt>"your argument"</tt> or <tt>"your return value"</tt> as arguments. Doing |
| 256 | this prevents <a href="translation">translating</a> the Clang diagnostics to |
| 257 | other languages (because they'll get random English words in their otherwise |
| 258 | localized diagnostic). The exceptions to this are C/C++ language keywords |
| 259 | (e.g. auto, const, mutable, etc) and C/C++ operators (<tt>/=</tt>). Note |
| 260 | that things like "pointer" and "reference" are not keywords. On the other |
| 261 | hand, you <em>can</em> include anything that comes from the user's source code, |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 262 | including variable names, types, labels, etc. The 'select' format can be |
| 263 | used to achieve this sort of thing in a localizable way, see below.</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 264 | |
| 265 | <!-- ==================================== --> |
| 266 | <h4>Formatting a Diagnostic Argument</a></h4> |
| 267 | <!-- ==================================== --> |
| 268 | |
| 269 | <p>Arguments to diagnostics are fully typed internally, and come from a couple |
| 270 | different classes: integers, types, names, and random strings. Depending on |
| 271 | the class of the argument, it can be optionally formatted in different ways. |
| 272 | This gives the DiagnosticClient information about what the argument means |
| 273 | without requiring it to use a specific presentation (consider this MVC for |
| 274 | Clang :).</p> |
| 275 | |
| 276 | <p>Here are the different diagnostic argument formats currently supported by |
| 277 | Clang:</p> |
| 278 | |
| 279 | <table> |
| 280 | <tr><td colspan="2"><b>"s" format</b></td></tr> |
| 281 | <tr><td>Example:</td><td><tt>"requires %1 parameter%s1"</tt></td></tr> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 282 | <tr><td>Class:</td><td>Integers</td></tr> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 283 | <tr><td>Description:</td><td>This is a simple formatter for integers that is |
| 284 | useful when producing English diagnostics. When the integer is 1, it prints |
| 285 | as nothing. When the integer is not 1, it prints as "s". This allows some |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 286 | simple grammatical forms to be to be handled correctly, and eliminates the |
| 287 | need to use gross things like <tt>"requires %1 parameter(s)"</tt>.</td></tr> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 288 | |
| 289 | <tr><td colspan="2"><b>"select" format</b></td></tr> |
| 290 | <tr><td>Example:</td><td><tt>"must be a %select{unary|binary|unary or binary}2 |
| 291 | operator"</tt></td></tr> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 292 | <tr><td>Class:</td><td>Integers</td></tr> |
John McCall | 3a47e23 | 2010-01-14 19:12:17 +0000 | [diff] [blame] | 293 | <tr><td>Description:</td><td><p>This format specifier is used to merge multiple |
Chris Lattner | cc54334 | 2008-11-22 23:50:47 +0000 | [diff] [blame] | 294 | related diagnostics together into one common one, without requiring the |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 295 | difference to be specified as an English string argument. Instead of |
Chris Lattner | cc54334 | 2008-11-22 23:50:47 +0000 | [diff] [blame] | 296 | specifying the string, the diagnostic gets an integer argument and the |
| 297 | format string selects the numbered option. In this case, the "%2" value |
| 298 | must be an integer in the range [0..2]. If it is 0, it prints 'unary', if |
| 299 | it is 1 it prints 'binary' if it is 2, it prints 'unary or binary'. This |
| 300 | allows other language translations to substitute reasonable words (or entire |
| 301 | phrases) based on the semantics of the diagnostic instead of having to do |
John McCall | 3a47e23 | 2010-01-14 19:12:17 +0000 | [diff] [blame] | 302 | things textually.</p> |
| 303 | <p>The selected string does undergo formatting.</p></td></tr> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 304 | |
| 305 | <tr><td colspan="2"><b>"plural" format</b></td></tr> |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 306 | <tr><td>Example:</td><td><tt>"you have %1 %plural{1:mouse|:mice}1 connected to |
| 307 | your computer"</tt></td></tr> |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 308 | <tr><td>Class:</td><td>Integers</td></tr> |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 309 | <tr><td>Description:</td><td><p>This is a formatter for complex plural forms. |
| 310 | It is designed to handle even the requirements of languages with very |
| 311 | complex plural forms, as many Baltic languages have. The argument consists |
| 312 | of a series of expression/form pairs, separated by ':', where the first form |
| 313 | whose expression evaluates to true is the result of the modifier.</p> |
| 314 | <p>An expression can be empty, in which case it is always true. See the |
| 315 | example at the top. Otherwise, it is a series of one or more numeric |
| 316 | conditions, separated by ','. If any condition matches, the expression |
| 317 | matches. Each numeric condition can take one of three forms.</p> |
| 318 | <ul> |
| 319 | <li>number: A simple decimal number matches if the argument is the same |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 320 | as the number. Example: <tt>"%plural{1:mouse|:mice}4"</tt></li> |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 321 | <li>range: A range in square brackets matches if the argument is within |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 322 | the range. Then range is inclusive on both ends. Example: |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 323 | <tt>"%plural{0:none|1:one|[2,5]:some|:many}2"</tt></li> |
| 324 | <li>modulo: A modulo operator is followed by a number, and |
| 325 | equals sign and either a number or a range. The tests are the |
| 326 | same as for plain |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 327 | numbers and ranges, but the argument is taken modulo the number first. |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 328 | Example: <tt>"%plural{%100=0:even hundred|%100=[1,50]:lower half|:everything |
| 329 | else}1"</tt></li> |
Sebastian Redl | 6816856 | 2008-11-22 22:16:45 +0000 | [diff] [blame] | 330 | </ul> |
| 331 | <p>The parser is very unforgiving. A syntax error, even whitespace, will |
| 332 | abort, as will a failure to match the argument against any |
| 333 | expression.</p></td></tr> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 334 | |
John McCall | 3a47e23 | 2010-01-14 19:12:17 +0000 | [diff] [blame] | 335 | <tr><td colspan="2"><b>"ordinal" format</b></td></tr> |
| 336 | <tr><td>Example:</td><td><tt>"ambiguity in %ordinal0 argument"</tt></td></tr> |
| 337 | <tr><td>Class:</td><td>Integers</td></tr> |
| 338 | <tr><td>Description:</td><td><p>This is a formatter which represents the |
| 339 | argument number as an ordinal: the value <tt>1</tt> becomes <tt>1st</tt>, |
| 340 | <tt>3</tt> becomes <tt>3rd</tt>, and so on. Values less than <tt>1</tt> |
| 341 | are not supported.</p> |
| 342 | <p>This formatter is currently hard-coded to use English ordinals.</p></td></tr> |
| 343 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 344 | <tr><td colspan="2"><b>"objcclass" format</b></td></tr> |
| 345 | <tr><td>Example:</td><td><tt>"method %objcclass0 not found"</tt></td></tr> |
| 346 | <tr><td>Class:</td><td>DeclarationName</td></tr> |
| 347 | <tr><td>Description:</td><td><p>This is a simple formatter that indicates the |
| 348 | DeclarationName corresponds to an Objective-C class method selector. As |
| 349 | such, it prints the selector with a leading '+'.</p></td></tr> |
| 350 | |
| 351 | <tr><td colspan="2"><b>"objcinstance" format</b></td></tr> |
| 352 | <tr><td>Example:</td><td><tt>"method %objcinstance0 not found"</tt></td></tr> |
| 353 | <tr><td>Class:</td><td>DeclarationName</td></tr> |
| 354 | <tr><td>Description:</td><td><p>This is a simple formatter that indicates the |
| 355 | DeclarationName corresponds to an Objective-C instance method selector. As |
| 356 | such, it prints the selector with a leading '-'.</p></td></tr> |
| 357 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 358 | <tr><td colspan="2"><b>"q" format</b></td></tr> |
| 359 | <tr><td>Example:</td><td><tt>"candidate found by name lookup is %q0"</tt></td></tr> |
| 360 | <tr><td>Class:</td><td>NamedDecl*</td></tr> |
| 361 | <tr><td>Description</td><td><p>This formatter indicates that the fully-qualified name of the declaration should be printed, e.g., "std::vector" rather than "vector".</p></td></tr> |
| 362 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 363 | </table> |
| 364 | |
Chris Lattner | cc54334 | 2008-11-22 23:50:47 +0000 | [diff] [blame] | 365 | <p>It is really easy to add format specifiers to the Clang diagnostics system, |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 366 | but they should be discussed before they are added. If you are creating a lot |
| 367 | of repetitive diagnostics and/or have an idea for a useful formatter, please |
| 368 | bring it up on the cfe-dev mailing list.</p> |
Chris Lattner | cc54334 | 2008-11-22 23:50:47 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 370 | <!-- ===================================================== --> |
| 371 | <h4><a name="#producingdiag">Producing the Diagnostic</a></h4> |
| 372 | <!-- ===================================================== --> |
| 373 | |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 374 | <p>Now that you've created the diagnostic in the DiagnosticKinds.def file, you |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 375 | need to write the code that detects the condition in question and emits the |
| 376 | new diagnostic. Various components of Clang (e.g. the preprocessor, Sema, |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 377 | etc) provide a helper function named "Diag". It creates a diagnostic and |
| 378 | accepts the arguments, ranges, and other information that goes along with |
| 379 | it.</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 381 | <p>For example, the binary expression error comes from code like this:</p> |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 382 | |
| 383 | <pre> |
| 384 | if (various things that are bad) |
| 385 | Diag(Loc, diag::err_typecheck_invalid_operands) |
| 386 | << lex->getType() << rex->getType() |
| 387 | << lex->getSourceRange() << rex->getSourceRange(); |
| 388 | </pre> |
| 389 | |
| 390 | <p>This shows that use of the Diag method: they take a location (a <a |
| 391 | href="#SourceLocation">SourceLocation</a> object) and a diagnostic enum value |
| 392 | (which matches the name from DiagnosticKinds.def). If the diagnostic takes |
| 393 | arguments, they are specified with the << operator: the first argument |
| 394 | becomes %0, the second becomes %1, etc. The diagnostic interface allows you to |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 395 | specify arguments of many different types, including <tt>int</tt> and |
| 396 | <tt>unsigned</tt> for integer arguments, <tt>const char*</tt> and |
| 397 | <tt>std::string</tt> for string arguments, <tt>DeclarationName</tt> and |
| 398 | <tt>const IdentifierInfo*</tt> for names, <tt>QualType</tt> for types, etc. |
| 399 | SourceRanges are also specified with the << operator, but do not have a |
| 400 | specific ordering requirement.</p> |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 401 | |
| 402 | <p>As you can see, adding and producing a diagnostic is pretty straightforward. |
| 403 | The hard part is deciding exactly what you need to say to help the user, picking |
| 404 | a suitable wording, and providing the information needed to format it correctly. |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 405 | The good news is that the call site that issues a diagnostic should be |
| 406 | completely independent of how the diagnostic is formatted and in what language |
| 407 | it is rendered. |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 408 | </p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 410 | <!-- ==================================================== --> |
| 411 | <h4 id="code-modification-hints">Code Modification Hints</h4> |
| 412 | <!-- ==================================================== --> |
| 413 | |
| 414 | <p>In some cases, the front end emits diagnostics when it is clear |
| 415 | that some small change to the source code would fix the problem. For |
| 416 | example, a missing semicolon at the end of a statement or a use of |
Chris Lattner | 34c0533 | 2009-02-27 19:31:12 +0000 | [diff] [blame] | 417 | deprecated syntax that is easily rewritten into a more modern form. |
| 418 | Clang tries very hard to emit the diagnostic and recover gracefully |
| 419 | in these and other cases.</p> |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 34c0533 | 2009-02-27 19:31:12 +0000 | [diff] [blame] | 421 | <p>However, for these cases where the fix is obvious, the diagnostic |
| 422 | can be annotated with a code |
| 423 | modification "hint" that describes how to change the code referenced |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 424 | by the diagnostic to fix the problem. For example, it might add the |
| 425 | missing semicolon at the end of the statement or rewrite the use of a |
| 426 | deprecated construct into something more palatable. Here is one such |
| 427 | example C++ front end, where we warn about the right-shift operator |
| 428 | changing meaning from C++98 to C++0x:</p> |
| 429 | |
| 430 | <pre> |
| 431 | test.cpp:3:7: warning: use of right-shift operator ('>>') in template argument will require parentheses in C++0x |
| 432 | A<100 >> 2> *a; |
| 433 | ^ |
| 434 | ( ) |
| 435 | </pre> |
| 436 | |
| 437 | <p>Here, the code modification hint is suggesting that parentheses be |
| 438 | added, and showing exactly where those parentheses would be inserted |
| 439 | into the source code. The code modification hints themselves describe |
| 440 | what changes to make to the source code in an abstract manner, which |
| 441 | the text diagnostic printer renders as a line of "insertions" below |
| 442 | the caret line. <a href="#DiagnosticClient">Other diagnostic |
| 443 | clients</a> might choose to render the code differently (e.g., as |
| 444 | markup inline) or even give the user the ability to automatically fix |
| 445 | the problem.</p> |
| 446 | |
| 447 | <p>All code modification hints are described by the |
| 448 | <code>CodeModificationHint</code> class, instances of which should be |
| 449 | attached to the diagnostic using the << operator in the same way |
| 450 | that highlighted source ranges and arguments are passed to the |
| 451 | diagnostic. Code modification hints can be created with one of three |
| 452 | constructors:</p> |
| 453 | |
| 454 | <dl> |
| 455 | <dt><code>CodeModificationHint::CreateInsertion(Loc, Code)</code></dt> |
| 456 | <dd>Specifies that the given <code>Code</code> (a string) should be inserted |
| 457 | before the source location <code>Loc</code>.</dd> |
| 458 | |
| 459 | <dt><code>CodeModificationHint::CreateRemoval(Range)</code></dt> |
| 460 | <dd>Specifies that the code in the given source <code>Range</code> |
| 461 | should be removed.</dd> |
| 462 | |
| 463 | <dt><code>CodeModificationHint::CreateReplacement(Range, Code)</code></dt> |
| 464 | <dd>Specifies that the code in the given source <code>Range</code> |
| 465 | should be removed, and replaced with the given <code>Code</code> string.</dd> |
| 466 | </dl> |
| 467 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 468 | <!-- ============================================================= --> |
| 469 | <h4><a name="DiagnosticClient">The DiagnosticClient Interface</a></h4> |
| 470 | <!-- ============================================================= --> |
| 471 | |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 472 | <p>Once code generates a diagnostic with all of the arguments and the rest of |
| 473 | the relevant information, Clang needs to know what to do with it. As previously |
| 474 | mentioned, the diagnostic machinery goes through some filtering to map a |
| 475 | severity onto a diagnostic level, then (assuming the diagnostic is not mapped to |
| 476 | "<tt>Ignore</tt>") it invokes an object that implements the DiagnosticClient |
| 477 | interface with the information.</p> |
| 478 | |
| 479 | <p>It is possible to implement this interface in many different ways. For |
| 480 | example, the normal Clang DiagnosticClient (named 'TextDiagnosticPrinter') turns |
| 481 | the arguments into strings (according to the various formatting rules), prints |
| 482 | out the file/line/column information and the string, then prints out the line of |
| 483 | code, the source ranges, and the caret. However, this behavior isn't required. |
| 484 | </p> |
| 485 | |
| 486 | <p>Another implementation of the DiagnosticClient interface is the |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 487 | 'TextDiagnosticBuffer' class, which is used when Clang is in -verify mode. |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 488 | Instead of formatting and printing out the diagnostics, this implementation just |
| 489 | captures and remembers the diagnostics as they fly by. Then -verify compares |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 490 | the list of produced diagnostics to the list of expected ones. If they disagree, |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 491 | it prints out its own output. |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 492 | </p> |
| 493 | |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 494 | <p>There are many other possible implementations of this interface, and this is |
| 495 | why we prefer diagnostics to pass down rich structured information in arguments. |
| 496 | For example, an HTML output might want declaration names be linkified to where |
| 497 | they come from in the source. Another example is that a GUI might let you click |
| 498 | on typedefs to expand them. This application would want to pass significantly |
| 499 | more information about types through to the GUI than a simple flat string. The |
| 500 | interface allows this to happen.</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 501 | |
| 502 | <!-- ====================================================== --> |
| 503 | <h4><a name="translation">Adding Translations to Clang</a></h4> |
| 504 | <!-- ====================================================== --> |
| 505 | |
Chris Lattner | 627b705 | 2008-11-23 00:28:33 +0000 | [diff] [blame] | 506 | <p>Not possible yet! Diagnostic strings should be written in UTF-8, the client |
Chris Lattner | fd408ea | 2008-11-23 00:42:53 +0000 | [diff] [blame] | 507 | can translate to the relevant code page if needed. Each translation completely |
| 508 | replaces the format string for the diagnostic.</p> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 509 | |
| 510 | |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 511 | <!-- ======================================================================= --> |
| 512 | <h3 id="SourceLocation">The SourceLocation and SourceManager classes</h3> |
| 513 | <!-- ======================================================================= --> |
| 514 | |
| 515 | <p>Strangely enough, the SourceLocation class represents a location within the |
| 516 | source code of the program. Important design points include:</p> |
| 517 | |
| 518 | <ol> |
| 519 | <li>sizeof(SourceLocation) must be extremely small, as these are embedded into |
| 520 | many AST nodes and are passed around often. Currently it is 32 bits.</li> |
| 521 | <li>SourceLocation must be a simple value object that can be efficiently |
| 522 | copied.</li> |
| 523 | <li>We should be able to represent a source location for any byte of any input |
| 524 | file. This includes in the middle of tokens, in whitespace, in trigraphs, |
| 525 | etc.</li> |
| 526 | <li>A SourceLocation must encode the current #include stack that was active when |
| 527 | the location was processed. For example, if the location corresponds to a |
| 528 | token, it should contain the set of #includes active when the token was |
| 529 | lexed. This allows us to print the #include stack for a diagnostic.</li> |
| 530 | <li>SourceLocation must be able to describe macro expansions, capturing both |
| 531 | the ultimate instantiation point and the source of the original character |
| 532 | data.</li> |
| 533 | </ol> |
| 534 | |
| 535 | <p>In practice, the SourceLocation works together with the SourceManager class |
Nick Lewycky | 77561e5 | 2010-05-26 21:48:10 +0000 | [diff] [blame] | 536 | to encode two pieces of information about a location: its spelling location |
| 537 | and its instantiation location. For most tokens, these will be the same. |
| 538 | However, for a macro expansion (or tokens that came from a _Pragma directive) |
| 539 | these will describe the location of the characters corresponding to the token |
| 540 | and the location where the token was used (i.e. the macro instantiation point |
| 541 | or the location of the _Pragma itself).</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 543 | <p>The Clang front-end inherently depends on the location of a token being |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 544 | tracked correctly. If it is ever incorrect, the front-end may get confused and |
| 545 | die. The reason for this is that the notion of the 'spelling' of a Token in |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 546 | Clang depends on being able to find the original input characters for the token. |
Chris Lattner | 18376dd | 2009-01-16 07:00:50 +0000 | [diff] [blame] | 547 | This concept maps directly to the "spelling location" for the token.</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 548 | |
Douglas Gregor | 715c92a | 2010-10-27 16:02:28 +0000 | [diff] [blame^] | 549 | |
| 550 | <!-- ======================================================================= --> |
| 551 | <h3 id="SourceRange">SourceRange and CharSourceRange</h3> |
| 552 | <!-- ======================================================================= --> |
| 553 | <!-- mostly taken from |
| 554 | http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-August/010595.html --> |
| 555 | |
| 556 | <p>Clang represents most source ranges by [first, last], where first and last |
| 557 | each point to the beginning of their respective tokens. For example |
| 558 | consider the SourceRange of the following statement:</p> |
| 559 | <pre> |
| 560 | x = foo + bar; |
| 561 | ^first ^last |
| 562 | </pre> |
| 563 | |
| 564 | <p>To map from this representation to a character-based |
| 565 | representation, the 'last' location needs to be adjusted to point to |
| 566 | (or past) the end of that token with either |
| 567 | <code>Lexer::MeasureTokenLength()</code> or |
| 568 | <code>Preprocessor::getLocForEndOfToken()</code>. For the rare cases |
| 569 | where character-level source ranges information is needed we use |
| 570 | the <code>CharSourceRange</code> class.</p> |
| 571 | |
| 572 | |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 573 | <!-- ======================================================================= --> |
Daniel Dunbar | 27d9e9f | 2009-03-30 06:50:01 +0000 | [diff] [blame] | 574 | <h2 id="libdriver">The Driver Library</h2> |
| 575 | <!-- ======================================================================= --> |
| 576 | |
Ted Kremenek | cfa8d57 | 2009-04-09 18:08:18 +0000 | [diff] [blame] | 577 | <p>The clang Driver and library are documented <a |
| 578 | href="DriverInternals.html">here<a>.<p> |
| 579 | |
| 580 | <!-- ======================================================================= --> |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 581 | <h2 id="pch">Precompiled Headers</h2> |
Ted Kremenek | cfa8d57 | 2009-04-09 18:08:18 +0000 | [diff] [blame] | 582 | <!-- ======================================================================= --> |
| 583 | |
Douglas Gregor | 32110df | 2009-05-20 00:16:32 +0000 | [diff] [blame] | 584 | <p>Clang supports two implementations of precompiled headers. The |
| 585 | default implementation, precompiled headers (<a |
| 586 | href="PCHInternals.html">PCH</a>) uses a serialized representation |
| 587 | of Clang's internal data structures, encoded with the <a |
| 588 | href="http://llvm.org/docs/BitCodeFormat.html">LLVM bitstream |
| 589 | format</a>. Pretokenized headers (<a |
| 590 | href="PTHInternals.html">PTH</a>), on the other hand, contain a |
| 591 | serialized representation of the tokens encountered when |
| 592 | preprocessing a header (and anything that header includes).</p> |
| 593 | |
Daniel Dunbar | 27d9e9f | 2009-03-30 06:50:01 +0000 | [diff] [blame] | 594 | |
| 595 | <!-- ======================================================================= --> |
| 596 | <h2 id="libfrontend">The Frontend Library</h2> |
| 597 | <!-- ======================================================================= --> |
| 598 | |
| 599 | <p>The Frontend library contains functionality useful for building |
| 600 | tools on top of the clang libraries, for example several methods for |
| 601 | outputting diagnostics.</p> |
| 602 | |
| 603 | <!-- ======================================================================= --> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 604 | <h2 id="liblex">The Lexer and Preprocessor Library</h2> |
| 605 | <!-- ======================================================================= --> |
| 606 | |
| 607 | <p>The Lexer library contains several tightly-connected classes that are involved |
| 608 | with the nasty process of lexing and preprocessing C source code. The main |
| 609 | interface to this library for outside clients is the large <a |
| 610 | href="#Preprocessor">Preprocessor</a> class. |
| 611 | It contains the various pieces of state that are required to coherently read |
| 612 | tokens out of a translation unit.</p> |
| 613 | |
| 614 | <p>The core interface to the Preprocessor object (once it is set up) is the |
| 615 | Preprocessor::Lex method, which returns the next <a href="#Token">Token</a> from |
| 616 | the preprocessor stream. There are two types of token providers that the |
| 617 | preprocessor is capable of reading from: a buffer lexer (provided by the <a |
| 618 | href="#Lexer">Lexer</a> class) and a buffered token stream (provided by the <a |
Chris Lattner | 7928125 | 2008-03-09 02:27:26 +0000 | [diff] [blame] | 619 | href="#TokenLexer">TokenLexer</a> class). |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 620 | |
| 621 | |
| 622 | <!-- ======================================================================= --> |
| 623 | <h3 id="Token">The Token class</h3> |
| 624 | <!-- ======================================================================= --> |
| 625 | |
| 626 | <p>The Token class is used to represent a single lexed token. Tokens are |
| 627 | intended to be used by the lexer/preprocess and parser libraries, but are not |
| 628 | intended to live beyond them (for example, they should not live in the ASTs).<p> |
| 629 | |
| 630 | <p>Tokens most often live on the stack (or some other location that is efficient |
| 631 | to access) as the parser is running, but occasionally do get buffered up. For |
| 632 | example, macro definitions are stored as a series of tokens, and the C++ |
Chris Lattner | 3fcbb89 | 2008-11-23 08:32:53 +0000 | [diff] [blame] | 633 | front-end periodically needs to buffer tokens up for tentative parsing and |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 634 | various pieces of look-ahead. As such, the size of a Token matter. On a 32-bit |
| 635 | system, sizeof(Token) is currently 16 bytes.</p> |
| 636 | |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 637 | <p>Tokens occur in two forms: "<a href="#AnnotationToken">Annotation |
| 638 | Tokens</a>" and normal tokens. Normal tokens are those returned by the lexer, |
| 639 | annotation tokens represent semantic information and are produced by the parser, |
| 640 | replacing normal tokens in the token stream. Normal tokens contain the |
| 641 | following information:</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 642 | |
| 643 | <ul> |
| 644 | <li><b>A SourceLocation</b> - This indicates the location of the start of the |
| 645 | token.</li> |
| 646 | |
| 647 | <li><b>A length</b> - This stores the length of the token as stored in the |
| 648 | SourceBuffer. For tokens that include them, this length includes trigraphs and |
| 649 | escaped newlines which are ignored by later phases of the compiler. By pointing |
| 650 | into the original source buffer, it is always possible to get the original |
| 651 | spelling of a token completely accurately.</li> |
| 652 | |
| 653 | <li><b>IdentifierInfo</b> - If a token takes the form of an identifier, and if |
| 654 | identifier lookup was enabled when the token was lexed (e.g. the lexer was not |
| 655 | reading in 'raw' mode) this contains a pointer to the unique hash value for the |
| 656 | identifier. Because the lookup happens before keyword identification, this |
| 657 | field is set even for language keywords like 'for'.</li> |
| 658 | |
| 659 | <li><b>TokenKind</b> - This indicates the kind of token as classified by the |
| 660 | lexer. This includes things like <tt>tok::starequal</tt> (for the "*=" |
| 661 | operator), <tt>tok::ampamp</tt> for the "&&" token, and keyword values |
| 662 | (e.g. <tt>tok::kw_for</tt>) for identifiers that correspond to keywords. Note |
| 663 | that some tokens can be spelled multiple ways. For example, C++ supports |
| 664 | "operator keywords", where things like "and" are treated exactly like the |
| 665 | "&&" operator. In these cases, the kind value is set to |
| 666 | <tt>tok::ampamp</tt>, which is good for the parser, which doesn't have to |
| 667 | consider both forms. For something that cares about which form is used (e.g. |
| 668 | the preprocessor 'stringize' operator) the spelling indicates the original |
| 669 | form.</li> |
| 670 | |
| 671 | <li><b>Flags</b> - There are currently four flags tracked by the |
| 672 | lexer/preprocessor system on a per-token basis: |
| 673 | |
| 674 | <ol> |
| 675 | <li><b>StartOfLine</b> - This was the first token that occurred on its input |
| 676 | source line.</li> |
| 677 | <li><b>LeadingSpace</b> - There was a space character either immediately |
| 678 | before the token or transitively before the token as it was expanded |
| 679 | through a macro. The definition of this flag is very closely defined by |
| 680 | the stringizing requirements of the preprocessor.</li> |
| 681 | <li><b>DisableExpand</b> - This flag is used internally to the preprocessor to |
| 682 | represent identifier tokens which have macro expansion disabled. This |
| 683 | prevents them from being considered as candidates for macro expansion ever |
| 684 | in the future.</li> |
| 685 | <li><b>NeedsCleaning</b> - This flag is set if the original spelling for the |
| 686 | token includes a trigraph or escaped newline. Since this is uncommon, |
| 687 | many pieces of code can fast-path on tokens that did not need cleaning. |
| 688 | </p> |
| 689 | </ol> |
| 690 | </li> |
| 691 | </ul> |
| 692 | |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 693 | <p>One interesting (and somewhat unusual) aspect of normal tokens is that they |
| 694 | don't contain any semantic information about the lexed value. For example, if |
| 695 | the token was a pp-number token, we do not represent the value of the number |
| 696 | that was lexed (this is left for later pieces of code to decide). Additionally, |
| 697 | the lexer library has no notion of typedef names vs variable names: both are |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 698 | returned as identifiers, and the parser is left to decide whether a specific |
| 699 | identifier is a typedef or a variable (tracking this requires scope information |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 700 | among other things). The parser can do this translation by replacing tokens |
| 701 | returned by the preprocessor with "Annotation Tokens".</p> |
| 702 | |
| 703 | <!-- ======================================================================= --> |
| 704 | <h3 id="AnnotationToken">Annotation Tokens</h3> |
| 705 | <!-- ======================================================================= --> |
| 706 | |
| 707 | <p>Annotation Tokens are tokens that are synthesized by the parser and injected |
| 708 | into the preprocessor's token stream (replacing existing tokens) to record |
| 709 | semantic information found by the parser. For example, if "foo" is found to be |
| 710 | a typedef, the "foo" <tt>tok::identifier</tt> token is replaced with an |
| 711 | <tt>tok::annot_typename</tt>. This is useful for a couple of reasons: 1) this |
| 712 | makes it easy to handle qualified type names (e.g. "foo::bar::baz<42>::t") |
| 713 | in C++ as a single "token" in the parser. 2) if the parser backtracks, the |
| 714 | reparse does not need to redo semantic analysis to determine whether a token |
| 715 | sequence is a variable, type, template, etc.</p> |
| 716 | |
| 717 | <p>Annotation Tokens are created by the parser and reinjected into the parser's |
| 718 | token stream (when backtracking is enabled). Because they can only exist in |
| 719 | tokens that the preprocessor-proper is done with, it doesn't need to keep around |
| 720 | flags like "start of line" that the preprocessor uses to do its job. |
| 721 | Additionally, an annotation token may "cover" a sequence of preprocessor tokens |
| 722 | (e.g. <tt>a::b::c</tt> is five preprocessor tokens). As such, the valid fields |
| 723 | of an annotation token are different than the fields for a normal token (but |
| 724 | they are multiplexed into the normal Token fields):</p> |
| 725 | |
| 726 | <ul> |
| 727 | <li><b>SourceLocation "Location"</b> - The SourceLocation for the annotation |
| 728 | token indicates the first token replaced by the annotation token. In the example |
| 729 | above, it would be the location of the "a" identifier.</li> |
| 730 | |
| 731 | <li><b>SourceLocation "AnnotationEndLoc"</b> - This holds the location of the |
| 732 | last token replaced with the annotation token. In the example above, it would |
| 733 | be the location of the "c" identifier.</li> |
| 734 | |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 735 | <li><b>void* "AnnotationValue"</b> - This contains an opaque object |
| 736 | that the parser gets from Sema. The parser merely preserves the |
| 737 | information for Sema to later interpret based on the annotation token |
| 738 | kind.</li> |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 739 | |
| 740 | <li><b>TokenKind "Kind"</b> - This indicates the kind of Annotation token this |
| 741 | is. See below for the different valid kinds.</li> |
| 742 | </ul> |
| 743 | |
| 744 | <p>Annotation tokens currently come in three kinds:</p> |
| 745 | |
| 746 | <ol> |
| 747 | <li><b>tok::annot_typename</b>: This annotation token represents a |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 748 | resolved typename token that is potentially qualified. The |
| 749 | AnnotationValue field contains the <tt>QualType</tt> returned by |
| 750 | Sema::getTypeName(), possibly with source location information |
| 751 | attached.</li> |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 752 | |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 753 | <li><b>tok::annot_cxxscope</b>: This annotation token represents a C++ |
| 754 | scope specifier, such as "A::B::". This corresponds to the grammar |
| 755 | productions "::" and ":: [opt] nested-name-specifier". The |
| 756 | AnnotationValue pointer is a <tt>NestedNameSpecifier*</tt> returned by |
| 757 | the Sema::ActOnCXXGlobalScopeSpecifier and |
| 758 | Sema::ActOnCXXNestedNameSpecifier callbacks.</li> |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 760 | <li><b>tok::annot_template_id</b>: This annotation token represents a |
| 761 | C++ template-id such as "foo<int, 4>", where "foo" is the name |
| 762 | of a template. The AnnotationValue pointer is a pointer to a malloc'd |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 763 | TemplateIdAnnotation object. Depending on the context, a parsed |
| 764 | template-id that names a type might become a typename annotation token |
| 765 | (if all we care about is the named type, e.g., because it occurs in a |
| 766 | type specifier) or might remain a template-id token (if we want to |
| 767 | retain more source location information or produce a new type, e.g., |
| 768 | in a declaration of a class template specialization). template-id |
| 769 | annotation tokens that refer to a type can be "upgraded" to typename |
| 770 | annotation tokens by the parser.</li> |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 771 | |
| 772 | </ol> |
| 773 | |
Cedric Venet | da76b28 | 2009-01-06 16:22:54 +0000 | [diff] [blame] | 774 | <p>As mentioned above, annotation tokens are not returned by the preprocessor, |
Chris Lattner | 3932fe0 | 2009-01-06 06:02:08 +0000 | [diff] [blame] | 775 | they are formed on demand by the parser. This means that the parser has to be |
| 776 | aware of cases where an annotation could occur and form it where appropriate. |
| 777 | This is somewhat similar to how the parser handles Translation Phase 6 of C99: |
| 778 | String Concatenation (see C99 5.1.1.2). In the case of string concatenation, |
| 779 | the preprocessor just returns distinct tok::string_literal and |
| 780 | tok::wide_string_literal tokens and the parser eats a sequence of them wherever |
| 781 | the grammar indicates that a string literal can occur.</p> |
| 782 | |
| 783 | <p>In order to do this, whenever the parser expects a tok::identifier or |
| 784 | tok::coloncolon, it should call the TryAnnotateTypeOrScopeToken or |
| 785 | TryAnnotateCXXScopeToken methods to form the annotation token. These methods |
| 786 | will maximally form the specified annotation tokens and replace the current |
| 787 | token with them, if applicable. If the current tokens is not valid for an |
| 788 | annotation token, it will remain an identifier or :: token.</p> |
| 789 | |
| 790 | |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 791 | |
| 792 | <!-- ======================================================================= --> |
| 793 | <h3 id="Lexer">The Lexer class</h3> |
| 794 | <!-- ======================================================================= --> |
| 795 | |
| 796 | <p>The Lexer class provides the mechanics of lexing tokens out of a source |
| 797 | buffer and deciding what they mean. The Lexer is complicated by the fact that |
| 798 | it operates on raw buffers that have not had spelling eliminated (this is a |
| 799 | necessity to get decent performance), but this is countered with careful coding |
| 800 | as well as standard performance techniques (for example, the comment handling |
| 801 | code is vectorized on X86 and PowerPC hosts).</p> |
| 802 | |
| 803 | <p>The lexer has a couple of interesting modal features:</p> |
| 804 | |
| 805 | <ul> |
| 806 | <li>The lexer can operate in 'raw' mode. This mode has several features that |
| 807 | make it possible to quickly lex the file (e.g. it stops identifier lookup, |
| 808 | doesn't specially handle preprocessor tokens, handles EOF differently, etc). |
| 809 | This mode is used for lexing within an "<tt>#if 0</tt>" block, for |
| 810 | example.</li> |
| 811 | <li>The lexer can capture and return comments as tokens. This is required to |
| 812 | support the -C preprocessor mode, which passes comments through, and is |
| 813 | used by the diagnostic checker to identifier expect-error annotations.</li> |
| 814 | <li>The lexer can be in ParsingFilename mode, which happens when preprocessing |
Chris Lattner | 8438624 | 2007-09-16 19:25:23 +0000 | [diff] [blame] | 815 | after reading a #include directive. This mode changes the parsing of '<' |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 816 | to return an "angled string" instead of a bunch of tokens for each thing |
| 817 | within the filename.</li> |
| 818 | <li>When parsing a preprocessor directive (after "<tt>#</tt>") the |
| 819 | ParsingPreprocessorDirective mode is entered. This changes the parser to |
| 820 | return EOM at a newline.</li> |
| 821 | <li>The Lexer uses a LangOptions object to know whether trigraphs are enabled, |
| 822 | whether C++ or ObjC keywords are recognized, etc.</li> |
| 823 | </ul> |
| 824 | |
| 825 | <p>In addition to these modes, the lexer keeps track of a couple of other |
| 826 | features that are local to a lexed buffer, which change as the buffer is |
| 827 | lexed:</p> |
| 828 | |
| 829 | <ul> |
| 830 | <li>The Lexer uses BufferPtr to keep track of the current character being |
| 831 | lexed.</li> |
| 832 | <li>The Lexer uses IsAtStartOfLine to keep track of whether the next lexed token |
| 833 | will start with its "start of line" bit set.</li> |
| 834 | <li>The Lexer keeps track of the current #if directives that are active (which |
| 835 | can be nested).</li> |
| 836 | <li>The Lexer keeps track of an <a href="#MultipleIncludeOpt"> |
| 837 | MultipleIncludeOpt</a> object, which is used to |
| 838 | detect whether the buffer uses the standard "<tt>#ifndef XX</tt> / |
| 839 | <tt>#define XX</tt>" idiom to prevent multiple inclusion. If a buffer does, |
| 840 | subsequent includes can be ignored if the XX macro is defined.</li> |
| 841 | </ul> |
| 842 | |
| 843 | <!-- ======================================================================= --> |
Chris Lattner | 7928125 | 2008-03-09 02:27:26 +0000 | [diff] [blame] | 844 | <h3 id="TokenLexer">The TokenLexer class</h3> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 845 | <!-- ======================================================================= --> |
| 846 | |
Chris Lattner | 7928125 | 2008-03-09 02:27:26 +0000 | [diff] [blame] | 847 | <p>The TokenLexer class is a token provider that returns tokens from a list |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 848 | of tokens that came from somewhere else. It typically used for two things: 1) |
| 849 | returning tokens from a macro definition as it is being expanded 2) returning |
| 850 | tokens from an arbitrary buffer of tokens. The later use is used by _Pragma and |
| 851 | will most likely be used to handle unbounded look-ahead for the C++ parser.</p> |
| 852 | |
| 853 | <!-- ======================================================================= --> |
| 854 | <h3 id="MultipleIncludeOpt">The MultipleIncludeOpt class</h3> |
| 855 | <!-- ======================================================================= --> |
| 856 | |
| 857 | <p>The MultipleIncludeOpt class implements a really simple little state machine |
| 858 | that is used to detect the standard "<tt>#ifndef XX</tt> / <tt>#define XX</tt>" |
| 859 | idiom that people typically use to prevent multiple inclusion of headers. If a |
| 860 | buffer uses this idiom and is subsequently #include'd, the preprocessor can |
| 861 | simply check to see whether the guarding condition is defined or not. If so, |
| 862 | the preprocessor can completely ignore the include of the header.</p> |
| 863 | |
| 864 | |
| 865 | |
| 866 | <!-- ======================================================================= --> |
| 867 | <h2 id="libparse">The Parser Library</h2> |
| 868 | <!-- ======================================================================= --> |
| 869 | |
| 870 | <!-- ======================================================================= --> |
| 871 | <h2 id="libast">The AST Library</h2> |
| 872 | <!-- ======================================================================= --> |
| 873 | |
| 874 | <!-- ======================================================================= --> |
| 875 | <h3 id="Type">The Type class and its subclasses</h3> |
| 876 | <!-- ======================================================================= --> |
| 877 | |
| 878 | <p>The Type class (and its subclasses) are an important part of the AST. Types |
| 879 | are accessed through the ASTContext class, which implicitly creates and uniques |
| 880 | them as they are needed. Types have a couple of non-obvious features: 1) they |
| 881 | do not capture type qualifiers like const or volatile (See |
| 882 | <a href="#QualType">QualType</a>), and 2) they implicitly capture typedef |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 883 | information. Once created, types are immutable (unlike decls).</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 884 | |
| 885 | <p>Typedefs in C make semantic analysis a bit more complex than it would |
| 886 | be without them. The issue is that we want to capture typedef information |
| 887 | and represent it in the AST perfectly, but the semantics of operations need to |
| 888 | "see through" typedefs. For example, consider this code:</p> |
| 889 | |
| 890 | <code> |
| 891 | void func() {<br> |
Bill Wendling | 30d1775 | 2007-10-06 01:56:01 +0000 | [diff] [blame] | 892 | typedef int foo;<br> |
| 893 | foo X, *Y;<br> |
| 894 | typedef foo* bar;<br> |
| 895 | bar Z;<br> |
| 896 | *X; <i>// error</i><br> |
| 897 | **Y; <i>// error</i><br> |
| 898 | **Z; <i>// error</i><br> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 899 | }<br> |
| 900 | </code> |
| 901 | |
| 902 | <p>The code above is illegal, and thus we expect there to be diagnostics emitted |
| 903 | on the annotated lines. In this example, we expect to get:</p> |
| 904 | |
| 905 | <pre> |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 906 | <b>test.c:6:1: error: indirection requires pointer operand ('foo' invalid)</b> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 907 | *X; // error |
| 908 | <font color="blue">^~</font> |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 909 | <b>test.c:7:1: error: indirection requires pointer operand ('foo' invalid)</b> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 910 | **Y; // error |
| 911 | <font color="blue">^~~</font> |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 912 | <b>test.c:8:1: error: indirection requires pointer operand ('foo' invalid)</b> |
| 913 | **Z; // error |
| 914 | <font color="blue">^~~</font> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 915 | </pre> |
| 916 | |
| 917 | <p>While this example is somewhat silly, it illustrates the point: we want to |
| 918 | retain typedef information where possible, so that we can emit errors about |
| 919 | "<tt>std::string</tt>" instead of "<tt>std::basic_string<char, std:...</tt>". |
| 920 | Doing this requires properly keeping typedef information (for example, the type |
| 921 | of "X" is "foo", not "int"), and requires properly propagating it through the |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 922 | various operators (for example, the type of *Y is "foo", not "int"). In order |
| 923 | to retain this information, the type of these expressions is an instance of the |
| 924 | TypedefType class, which indicates that the type of these expressions is a |
| 925 | typedef for foo. |
| 926 | </p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 928 | <p>Representing types like this is great for diagnostics, because the |
| 929 | user-specified type is always immediately available. There are two problems |
| 930 | with this: first, various semantic checks need to make judgements about the |
Chris Lattner | 33fc68a | 2007-07-31 18:54:50 +0000 | [diff] [blame] | 931 | <em>actual structure</em> of a type, ignoring typdefs. Second, we need an |
| 932 | efficient way to query whether two types are structurally identical to each |
| 933 | other, ignoring typedefs. The solution to both of these problems is the idea of |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 934 | canonical types.</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 935 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 936 | <!-- =============== --> |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 937 | <h4>Canonical Types</h4> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 938 | <!-- =============== --> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 940 | <p>Every instance of the Type class contains a canonical type pointer. For |
| 941 | simple types with no typedefs involved (e.g. "<tt>int</tt>", "<tt>int*</tt>", |
| 942 | "<tt>int**</tt>"), the type just points to itself. For types that have a |
| 943 | typedef somewhere in their structure (e.g. "<tt>foo</tt>", "<tt>foo*</tt>", |
| 944 | "<tt>foo**</tt>", "<tt>bar</tt>"), the canonical type pointer points to their |
| 945 | structurally equivalent type without any typedefs (e.g. "<tt>int</tt>", |
| 946 | "<tt>int*</tt>", "<tt>int**</tt>", and "<tt>int*</tt>" respectively).</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 948 | <p>This design provides a constant time operation (dereferencing the canonical |
| 949 | type pointer) that gives us access to the structure of types. For example, |
| 950 | we can trivially tell that "bar" and "foo*" are the same type by dereferencing |
| 951 | their canonical type pointers and doing a pointer comparison (they both point |
| 952 | to the single "<tt>int*</tt>" type).</p> |
| 953 | |
| 954 | <p>Canonical types and typedef types bring up some complexities that must be |
| 955 | carefully managed. Specifically, the "isa/cast/dyncast" operators generally |
| 956 | shouldn't be used in code that is inspecting the AST. For example, when type |
| 957 | checking the indirection operator (unary '*' on a pointer), the type checker |
| 958 | must verify that the operand has a pointer type. It would not be correct to |
| 959 | check that with "<tt>isa<PointerType>(SubExpr->getType())</tt>", |
| 960 | because this predicate would fail if the subexpression had a typedef type.</p> |
| 961 | |
| 962 | <p>The solution to this problem are a set of helper methods on Type, used to |
| 963 | check their properties. In this case, it would be correct to use |
| 964 | "<tt>SubExpr->getType()->isPointerType()</tt>" to do the check. This |
| 965 | predicate will return true if the <em>canonical type is a pointer</em>, which is |
| 966 | true any time the type is structurally a pointer type. The only hard part here |
| 967 | is remembering not to use the <tt>isa/cast/dyncast</tt> operations.</p> |
| 968 | |
| 969 | <p>The second problem we face is how to get access to the pointer type once we |
| 970 | know it exists. To continue the example, the result type of the indirection |
| 971 | operator is the pointee type of the subexpression. In order to determine the |
| 972 | type, we need to get the instance of PointerType that best captures the typedef |
| 973 | information in the program. If the type of the expression is literally a |
| 974 | PointerType, we can return that, otherwise we have to dig through the |
| 975 | typedefs to find the pointer type. For example, if the subexpression had type |
| 976 | "<tt>foo*</tt>", we could return that type as the result. If the subexpression |
| 977 | had type "<tt>bar</tt>", we want to return "<tt>foo*</tt>" (note that we do |
| 978 | <em>not</em> want "<tt>int*</tt>"). In order to provide all of this, Type has |
Chris Lattner | 11406c1 | 2007-07-31 16:50:51 +0000 | [diff] [blame] | 979 | a getAsPointerType() method that checks whether the type is structurally a |
Chris Lattner | 8a2bc62 | 2007-07-31 06:37:39 +0000 | [diff] [blame] | 980 | PointerType and, if so, returns the best one. If not, it returns a null |
| 981 | pointer.</p> |
| 982 | |
| 983 | <p>This structure is somewhat mystical, but after meditating on it, it will |
| 984 | make sense to you :).</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 985 | |
| 986 | <!-- ======================================================================= --> |
| 987 | <h3 id="QualType">The QualType class</h3> |
| 988 | <!-- ======================================================================= --> |
| 989 | |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 990 | <p>The QualType class is designed as a trivial value class that is |
| 991 | small, passed by-value and is efficient to query. The idea of |
| 992 | QualType is that it stores the type qualifiers (const, volatile, |
| 993 | restrict, plus some extended qualifiers required by language |
| 994 | extensions) separately from the types themselves. QualType is |
| 995 | conceptually a pair of "Type*" and the bits for these type qualifiers.</p> |
Chris Lattner | 86920d3 | 2007-07-31 05:42:17 +0000 | [diff] [blame] | 996 | |
| 997 | <p>By storing the type qualifiers as bits in the conceptual pair, it is |
| 998 | extremely efficient to get the set of qualifiers on a QualType (just return the |
| 999 | field of the pair), add a type qualifier (which is a trivial constant-time |
| 1000 | operation that sets a bit), and remove one or more type qualifiers (just return |
| 1001 | a QualType with the bitfield set to empty).</p> |
| 1002 | |
| 1003 | <p>Further, because the bits are stored outside of the type itself, we do not |
| 1004 | need to create duplicates of types with different sets of qualifiers (i.e. there |
| 1005 | is only a single heap allocated "int" type: "const int" and "volatile const int" |
| 1006 | both point to the same heap allocated "int" type). This reduces the heap size |
| 1007 | used to represent bits and also means we do not have to consider qualifiers when |
| 1008 | uniquing types (<a href="#Type">Type</a> does not even contain qualifiers).</p> |
| 1009 | |
John McCall | 027ac44 | 2010-09-03 05:07:55 +0000 | [diff] [blame] | 1010 | <p>In practice, the two most common type qualifiers (const and |
| 1011 | restrict) are stored in the low bits of the pointer to the Type |
| 1012 | object, together with a flag indicating whether extended qualifiers |
| 1013 | are present (which must be heap-allocated). This means that QualType |
| 1014 | is exactly the same size as a pointer.</p> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1015 | |
| 1016 | <!-- ======================================================================= --> |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1017 | <h3 id="DeclarationName">Declaration names</h3> |
| 1018 | <!-- ======================================================================= --> |
| 1019 | |
| 1020 | <p>The <tt>DeclarationName</tt> class represents the name of a |
| 1021 | declaration in Clang. Declarations in the C family of languages can |
Chris Lattner | 3fcbb89 | 2008-11-23 08:32:53 +0000 | [diff] [blame] | 1022 | take several different forms. Most declarations are named by |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1023 | simple identifiers, e.g., "<code>f</code>" and "<code>x</code>" in |
| 1024 | the function declaration <code>f(int x)</code>. In C++, declaration |
| 1025 | names can also name class constructors ("<code>Class</code>" |
| 1026 | in <code>struct Class { Class(); }</code>), class destructors |
| 1027 | ("<code>~Class</code>"), overloaded operator names ("operator+"), |
| 1028 | and conversion functions ("<code>operator void const *</code>"). In |
| 1029 | Objective-C, declaration names can refer to the names of Objective-C |
| 1030 | methods, which involve the method name and the parameters, |
Chris Lattner | 3fcbb89 | 2008-11-23 08:32:53 +0000 | [diff] [blame] | 1031 | collectively called a <i>selector</i>, e.g., |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1032 | "<code>setWidth:height:</code>". Since all of these kinds of |
Chris Lattner | 3fcbb89 | 2008-11-23 08:32:53 +0000 | [diff] [blame] | 1033 | entities - variables, functions, Objective-C methods, C++ |
| 1034 | constructors, destructors, and operators - are represented as |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1035 | subclasses of Clang's common <code>NamedDecl</code> |
| 1036 | class, <code>DeclarationName</code> is designed to efficiently |
| 1037 | represent any kind of name.</p> |
| 1038 | |
| 1039 | <p>Given |
| 1040 | a <code>DeclarationName</code> <code>N</code>, <code>N.getNameKind()</code> |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 1041 | will produce a value that describes what kind of name <code>N</code> |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1042 | stores. There are 8 options (all of the names are inside |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1043 | the <code>DeclarationName</code> class)</p> |
| 1044 | <dl> |
| 1045 | <dt>Identifier</dt> |
| 1046 | <dd>The name is a simple |
| 1047 | identifier. Use <code>N.getAsIdentifierInfo()</code> to retrieve the |
| 1048 | corresponding <code>IdentifierInfo*</code> pointing to the actual |
| 1049 | identifier. Note that C++ overloaded operators (e.g., |
| 1050 | "<code>operator+</code>") are represented as special kinds of |
| 1051 | identifiers. Use <code>IdentifierInfo</code>'s <code>getOverloadedOperatorID</code> |
| 1052 | function to determine whether an identifier is an overloaded |
| 1053 | operator name.</dd> |
| 1054 | |
| 1055 | <dt>ObjCZeroArgSelector, ObjCOneArgSelector, |
| 1056 | ObjCMultiArgSelector</dt> |
| 1057 | <dd>The name is an Objective-C selector, which can be retrieved as a |
| 1058 | <code>Selector</code> instance |
| 1059 | via <code>N.getObjCSelector()</code>. The three possible name |
| 1060 | kinds for Objective-C reflect an optimization within |
| 1061 | the <code>DeclarationName</code> class: both zero- and |
| 1062 | one-argument selectors are stored as a |
| 1063 | masked <code>IdentifierInfo</code> pointer, and therefore require |
| 1064 | very little space, since zero- and one-argument selectors are far |
| 1065 | more common than multi-argument selectors (which use a different |
| 1066 | structure).</dd> |
| 1067 | |
| 1068 | <dt>CXXConstructorName</dt> |
| 1069 | <dd>The name is a C++ constructor |
| 1070 | name. Use <code>N.getCXXNameType()</code> to retrieve |
| 1071 | the <a href="#QualType">type</a> that this constructor is meant to |
| 1072 | construct. The type is always the canonical type, since all |
| 1073 | constructors for a given type have the same name.</dd> |
| 1074 | |
| 1075 | <dt>CXXDestructorName</dt> |
| 1076 | <dd>The name is a C++ destructor |
| 1077 | name. Use <code>N.getCXXNameType()</code> to retrieve |
| 1078 | the <a href="#QualType">type</a> whose destructor is being |
| 1079 | named. This type is always a canonical type.</dd> |
| 1080 | |
| 1081 | <dt>CXXConversionFunctionName</dt> |
| 1082 | <dd>The name is a C++ conversion function. Conversion functions are |
| 1083 | named according to the type they convert to, e.g., "<code>operator void |
| 1084 | const *</code>". Use <code>N.getCXXNameType()</code> to retrieve |
| 1085 | the type that this conversion function converts to. This type is |
| 1086 | always a canonical type.</dd> |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1087 | |
| 1088 | <dt>CXXOperatorName</dt> |
| 1089 | <dd>The name is a C++ overloaded operator name. Overloaded operators |
| 1090 | are named according to their spelling, e.g., |
| 1091 | "<code>operator+</code>" or "<code>operator new |
| 1092 | []</code>". Use <code>N.getCXXOverloadedOperator()</code> to |
| 1093 | retrieve the overloaded operator (a value of |
| 1094 | type <code>OverloadedOperatorKind</code>).</dd> |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1095 | </dl> |
| 1096 | |
| 1097 | <p><code>DeclarationName</code>s are cheap to create, copy, and |
| 1098 | compare. They require only a single pointer's worth of storage in |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1099 | the common cases (identifiers, zero- |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1100 | and one-argument Objective-C selectors) and use dense, uniqued |
| 1101 | storage for the other kinds of |
| 1102 | names. Two <code>DeclarationName</code>s can be compared for |
| 1103 | equality (<code>==</code>, <code>!=</code>) using a simple bitwise |
| 1104 | comparison, can be ordered |
| 1105 | with <code><</code>, <code>></code>, <code><=</code>, |
| 1106 | and <code>>=</code> (which provide a lexicographical ordering for |
| 1107 | normal identifiers but an unspecified ordering for other kinds of |
| 1108 | names), and can be placed into LLVM <code>DenseMap</code>s |
| 1109 | and <code>DenseSet</code>s.</p> |
| 1110 | |
| 1111 | <p><code>DeclarationName</code> instances can be created in different |
| 1112 | ways depending on what kind of name the instance will store. Normal |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1113 | identifiers (<code>IdentifierInfo</code> pointers) and Objective-C selectors |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1114 | (<code>Selector</code>) can be implicitly converted |
| 1115 | to <code>DeclarationName</code>s. Names for C++ constructors, |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1116 | destructors, conversion functions, and overloaded operators can be retrieved from |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1117 | the <code>DeclarationNameTable</code>, an instance of which is |
| 1118 | available as <code>ASTContext::DeclarationNames</code>. The member |
| 1119 | functions <code>getCXXConstructorName</code>, <code>getCXXDestructorName</code>, |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1120 | <code>getCXXConversionFunctionName</code>, and <code>getCXXOperatorName</code>, respectively, |
| 1121 | return <code>DeclarationName</code> instances for the four kinds of |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1122 | C++ special function names.</p> |
| 1123 | |
| 1124 | <!-- ======================================================================= --> |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1125 | <h3 id="DeclContext">Declaration contexts</h3> |
| 1126 | <!-- ======================================================================= --> |
| 1127 | <p>Every declaration in a program exists within some <i>declaration |
| 1128 | context</i>, such as a translation unit, namespace, class, or |
| 1129 | function. Declaration contexts in Clang are represented by |
| 1130 | the <code>DeclContext</code> class, from which the various |
| 1131 | declaration-context AST nodes |
| 1132 | (<code>TranslationUnitDecl</code>, <code>NamespaceDecl</code>, <code>RecordDecl</code>, <code>FunctionDecl</code>, |
| 1133 | etc.) will derive. The <code>DeclContext</code> class provides |
| 1134 | several facilities common to each declaration context:</p> |
| 1135 | <dl> |
| 1136 | <dt>Source-centric vs. Semantics-centric View of Declarations</dt> |
| 1137 | <dd><code>DeclContext</code> provides two views of the declarations |
| 1138 | stored within a declaration context. The source-centric view |
| 1139 | accurately represents the program source code as written, including |
| 1140 | multiple declarations of entities where present (see the |
| 1141 | section <a href="#Redeclarations">Redeclarations and |
| 1142 | Overloads</a>), while the semantics-centric view represents the |
| 1143 | program semantics. The two views are kept synchronized by semantic |
| 1144 | analysis while the ASTs are being constructed.</dd> |
| 1145 | |
| 1146 | <dt>Storage of declarations within that context</dt> |
| 1147 | <dd>Every declaration context can contain some number of |
| 1148 | declarations. For example, a C++ class (represented |
| 1149 | by <code>RecordDecl</code>) contains various member functions, |
| 1150 | fields, nested types, and so on. All of these declarations will be |
| 1151 | stored within the <code>DeclContext</code>, and one can iterate |
| 1152 | over the declarations via |
| 1153 | [<code>DeclContext::decls_begin()</code>, |
| 1154 | <code>DeclContext::decls_end()</code>). This mechanism provides |
| 1155 | the source-centric view of declarations in the context.</dd> |
| 1156 | |
| 1157 | <dt>Lookup of declarations within that context</dt> |
| 1158 | <dd>The <code>DeclContext</code> structure provides efficient name |
| 1159 | lookup for names within that declaration context. For example, |
| 1160 | if <code>N</code> is a namespace we can look for the |
| 1161 | name <code>N::f</code> |
| 1162 | using <code>DeclContext::lookup</code>. The lookup itself is |
| 1163 | based on a lazily-constructed array (for declaration contexts |
| 1164 | with a small number of declarations) or hash table (for |
| 1165 | declaration contexts with more declarations). The lookup |
| 1166 | operation provides the semantics-centric view of the declarations |
| 1167 | in the context.</dd> |
| 1168 | |
| 1169 | <dt>Ownership of declarations</dt> |
| 1170 | <dd>The <code>DeclContext</code> owns all of the declarations that |
| 1171 | were declared within its declaration context, and is responsible |
| 1172 | for the management of their memory as well as their |
| 1173 | (de-)serialization.</dd> |
| 1174 | </dl> |
| 1175 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1176 | <p>All declarations are stored within a declaration context, and one |
| 1177 | can query |
| 1178 | information about the context in which each declaration lives. One |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1179 | can retrieve the <code>DeclContext</code> that contains a |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1180 | particular <code>Decl</code> |
| 1181 | using <code>Decl::getDeclContext</code>. However, see the |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1182 | section <a href="#LexicalAndSemanticContexts">Lexical and Semantic |
| 1183 | Contexts</a> for more information about how to interpret this |
| 1184 | context information.</p> |
| 1185 | |
| 1186 | <h4 id="Redeclarations">Redeclarations and Overloads</h4> |
| 1187 | <p>Within a translation unit, it is common for an entity to be |
| 1188 | declared several times. For example, we might declare a function "f" |
| 1189 | and then later re-declare it as part of an inlined definition:</p> |
| 1190 | |
| 1191 | <pre> |
| 1192 | void f(int x, int y, int z = 1); |
| 1193 | |
| 1194 | inline void f(int x, int y, int z) { /* ... */ } |
| 1195 | </pre> |
| 1196 | |
| 1197 | <p>The representation of "f" differs in the source-centric and |
| 1198 | semantics-centric views of a declaration context. In the |
| 1199 | source-centric view, all redeclarations will be present, in the |
| 1200 | order they occurred in the source code, making |
| 1201 | this view suitable for clients that wish to see the structure of |
| 1202 | the source code. In the semantics-centric view, only the most recent "f" |
| 1203 | will be found by the lookup, since it effectively replaces the first |
| 1204 | declaration of "f".</p> |
| 1205 | |
| 1206 | <p>In the semantics-centric view, overloading of functions is |
| 1207 | represented explicitly. For example, given two declarations of a |
| 1208 | function "g" that are overloaded, e.g.,</p> |
| 1209 | <pre> |
| 1210 | void g(); |
| 1211 | void g(int); |
| 1212 | </pre> |
| 1213 | <p>the <code>DeclContext::lookup</code> operation will return |
| 1214 | an <code>OverloadedFunctionDecl</code> that contains both |
| 1215 | declarations of "g". Clients that perform semantic analysis on a |
| 1216 | program that is not concerned with the actual source code will |
| 1217 | primarily use this semantics-centric view.</p> |
| 1218 | |
| 1219 | <h4 id="LexicalAndSemanticContexts">Lexical and Semantic Contexts</h4> |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1220 | <p>Each declaration has two potentially different |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1221 | declaration contexts: a <i>lexical</i> context, which corresponds to |
| 1222 | the source-centric view of the declaration context, and |
| 1223 | a <i>semantic</i> context, which corresponds to the |
| 1224 | semantics-centric view. The lexical context is accessible |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1225 | via <code>Decl::getLexicalDeclContext</code> while the |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1226 | semantic context is accessible |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1227 | via <code>Decl::getDeclContext</code>, both of which return |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1228 | <code>DeclContext</code> pointers. For most declarations, the two |
| 1229 | contexts are identical. For example:</p> |
| 1230 | |
| 1231 | <pre> |
| 1232 | class X { |
| 1233 | public: |
| 1234 | void f(int x); |
| 1235 | }; |
| 1236 | </pre> |
| 1237 | |
| 1238 | <p>Here, the semantic and lexical contexts of <code>X::f</code> are |
| 1239 | the <code>DeclContext</code> associated with the |
| 1240 | class <code>X</code> (itself stored as a <code>RecordDecl</code> AST |
| 1241 | node). However, we can now define <code>X::f</code> out-of-line:</p> |
| 1242 | |
| 1243 | <pre> |
| 1244 | void X::f(int x = 17) { /* ... */ } |
| 1245 | </pre> |
| 1246 | |
| 1247 | <p>This definition of has different lexical and semantic |
| 1248 | contexts. The lexical context corresponds to the declaration |
| 1249 | context in which the actual declaration occurred in the source |
| 1250 | code, e.g., the translation unit containing <code>X</code>. Thus, |
| 1251 | this declaration of <code>X::f</code> can be found by traversing |
| 1252 | the declarations provided by |
| 1253 | [<code>decls_begin()</code>, <code>decls_end()</code>) in the |
| 1254 | translation unit.</p> |
| 1255 | |
| 1256 | <p>The semantic context of <code>X::f</code> corresponds to the |
| 1257 | class <code>X</code>, since this member function is (semantically) a |
| 1258 | member of <code>X</code>. Lookup of the name <code>f</code> into |
| 1259 | the <code>DeclContext</code> associated with <code>X</code> will |
| 1260 | then return the definition of <code>X::f</code> (including |
| 1261 | information about the default argument).</p> |
| 1262 | |
| 1263 | <h4 id="TransparentContexts">Transparent Declaration Contexts</h4> |
| 1264 | <p>In C and C++, there are several contexts in which names that are |
| 1265 | logically declared inside another declaration will actually "leak" |
| 1266 | out into the enclosing scope from the perspective of name |
| 1267 | lookup. The most obvious instance of this behavior is in |
| 1268 | enumeration types, e.g.,</p> |
| 1269 | <pre> |
| 1270 | enum Color { |
| 1271 | Red, |
| 1272 | Green, |
| 1273 | Blue |
| 1274 | }; |
| 1275 | </pre> |
| 1276 | |
| 1277 | <p>Here, <code>Color</code> is an enumeration, which is a declaration |
| 1278 | context that contains the |
| 1279 | enumerators <code>Red</code>, <code>Green</code>, |
| 1280 | and <code>Blue</code>. Thus, traversing the list of declarations |
| 1281 | contained in the enumeration <code>Color</code> will |
| 1282 | yield <code>Red</code>, <code>Green</code>, |
| 1283 | and <code>Blue</code>. However, outside of the scope |
| 1284 | of <code>Color</code> one can name the enumerator <code>Red</code> |
| 1285 | without qualifying the name, e.g.,</p> |
| 1286 | |
| 1287 | <pre> |
| 1288 | Color c = Red; |
| 1289 | </pre> |
| 1290 | |
| 1291 | <p>There are other entities in C++ that provide similar behavior. For |
| 1292 | example, linkage specifications that use curly braces:</p> |
| 1293 | |
| 1294 | <pre> |
| 1295 | extern "C" { |
| 1296 | void f(int); |
| 1297 | void g(int); |
| 1298 | } |
| 1299 | // f and g are visible here |
| 1300 | </pre> |
| 1301 | |
| 1302 | <p>For source-level accuracy, we treat the linkage specification and |
| 1303 | enumeration type as a |
| 1304 | declaration context in which its enclosed declarations ("Red", |
| 1305 | "Green", and "Blue"; "f" and "g") |
| 1306 | are declared. However, these declarations are visible outside of the |
| 1307 | scope of the declaration context.</p> |
| 1308 | |
| 1309 | <p>These language features (and several others, described below) have |
| 1310 | roughly the same set of |
| 1311 | requirements: declarations are declared within a particular lexical |
| 1312 | context, but the declarations are also found via name lookup in |
| 1313 | scopes enclosing the declaration itself. This feature is implemented |
| 1314 | via <i>transparent</i> declaration contexts |
| 1315 | (see <code>DeclContext::isTransparentContext()</code>), whose |
| 1316 | declarations are visible in the nearest enclosing non-transparent |
| 1317 | declaration context. This means that the lexical context of the |
| 1318 | declaration (e.g., an enumerator) will be the |
| 1319 | transparent <code>DeclContext</code> itself, as will the semantic |
| 1320 | context, but the declaration will be visible in every outer context |
| 1321 | up to and including the first non-transparent declaration context (since |
| 1322 | transparent declaration contexts can be nested).</p> |
| 1323 | |
| 1324 | <p>The transparent <code>DeclContexts</code> are:</p> |
| 1325 | <ul> |
| 1326 | <li>Enumerations (but not C++0x "scoped enumerations"): |
| 1327 | <pre> |
| 1328 | enum Color { |
| 1329 | Red, |
| 1330 | Green, |
| 1331 | Blue |
| 1332 | }; |
| 1333 | // Red, Green, and Blue are in scope |
| 1334 | </pre></li> |
| 1335 | <li>C++ linkage specifications: |
| 1336 | <pre> |
| 1337 | extern "C" { |
| 1338 | void f(int); |
| 1339 | void g(int); |
| 1340 | } |
| 1341 | // f and g are in scope |
| 1342 | </pre></li> |
| 1343 | <li>Anonymous unions and structs: |
| 1344 | <pre> |
| 1345 | struct LookupTable { |
| 1346 | bool IsVector; |
| 1347 | union { |
| 1348 | std::vector<Item> *Vector; |
| 1349 | std::set<Item> *Set; |
| 1350 | }; |
| 1351 | }; |
| 1352 | |
| 1353 | LookupTable LT; |
| 1354 | LT.Vector = 0; // Okay: finds Vector inside the unnamed union |
| 1355 | </pre> |
| 1356 | </li> |
| 1357 | <li>C++0x inline namespaces: |
| 1358 | <pre> |
| 1359 | namespace mylib { |
| 1360 | inline namespace debug { |
| 1361 | class X; |
| 1362 | } |
| 1363 | } |
| 1364 | mylib::X *xp; // okay: mylib::X refers to mylib::debug::X |
| 1365 | </pre> |
| 1366 | </li> |
| 1367 | </ul> |
| 1368 | |
| 1369 | |
| 1370 | <h4 id="MultiDeclContext">Multiply-Defined Declaration Contexts</h4> |
| 1371 | <p>C++ namespaces have the interesting--and, so far, unique--property that |
| 1372 | the namespace can be defined multiple times, and the declarations |
| 1373 | provided by each namespace definition are effectively merged (from |
| 1374 | the semantic point of view). For example, the following two code |
| 1375 | snippets are semantically indistinguishable:</p> |
| 1376 | <pre> |
| 1377 | // Snippet #1: |
| 1378 | namespace N { |
| 1379 | void f(); |
| 1380 | } |
| 1381 | namespace N { |
| 1382 | void f(int); |
| 1383 | } |
| 1384 | |
| 1385 | // Snippet #2: |
| 1386 | namespace N { |
| 1387 | void f(); |
| 1388 | void f(int); |
| 1389 | } |
| 1390 | </pre> |
| 1391 | |
| 1392 | <p>In Clang's representation, the source-centric view of declaration |
| 1393 | contexts will actually have two separate <code>NamespaceDecl</code> |
| 1394 | nodes in Snippet #1, each of which is a declaration context that |
| 1395 | contains a single declaration of "f". However, the semantics-centric |
| 1396 | view provided by name lookup into the namespace <code>N</code> for |
| 1397 | "f" will return an <code>OverloadedFunctionDecl</code> that contains |
| 1398 | both declarations of "f".</p> |
| 1399 | |
| 1400 | <p><code>DeclContext</code> manages multiply-defined declaration |
| 1401 | contexts internally. The |
| 1402 | function <code>DeclContext::getPrimaryContext</code> retrieves the |
| 1403 | "primary" context for a given <code>DeclContext</code> instance, |
| 1404 | which is the <code>DeclContext</code> responsible for maintaining |
| 1405 | the lookup table used for the semantics-centric view. Given the |
| 1406 | primary context, one can follow the chain |
| 1407 | of <code>DeclContext</code> nodes that define additional |
| 1408 | declarations via <code>DeclContext::getNextContext</code>. Note that |
| 1409 | these functions are used internally within the lookup and insertion |
| 1410 | methods of the <code>DeclContext</code>, so the vast majority of |
| 1411 | clients can ignore them.</p> |
| 1412 | |
| 1413 | <!-- ======================================================================= --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1414 | <h3 id="CFG">The <tt>CFG</tt> class</h3> |
| 1415 | <!-- ======================================================================= --> |
| 1416 | |
| 1417 | <p>The <tt>CFG</tt> class is designed to represent a source-level |
| 1418 | control-flow graph for a single statement (<tt>Stmt*</tt>). Typically |
| 1419 | instances of <tt>CFG</tt> are constructed for function bodies (usually |
| 1420 | an instance of <tt>CompoundStmt</tt>), but can also be instantiated to |
| 1421 | represent the control-flow of any class that subclasses <tt>Stmt</tt>, |
| 1422 | which includes simple expressions. Control-flow graphs are especially |
| 1423 | useful for performing |
| 1424 | <a href="http://en.wikipedia.org/wiki/Data_flow_analysis#Sensitivities">flow- |
| 1425 | or path-sensitive</a> program analyses on a given function.</p> |
| 1426 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1427 | <!-- ============ --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1428 | <h4>Basic Blocks</h4> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1429 | <!-- ============ --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1430 | |
| 1431 | <p>Concretely, an instance of <tt>CFG</tt> is a collection of basic |
| 1432 | blocks. Each basic block is an instance of <tt>CFGBlock</tt>, which |
| 1433 | simply contains an ordered sequence of <tt>Stmt*</tt> (each referring |
| 1434 | to statements in the AST). The ordering of statements within a block |
| 1435 | indicates unconditional flow of control from one statement to the |
| 1436 | next. <a href="#ConditionalControlFlow">Conditional control-flow</a> |
| 1437 | is represented using edges between basic blocks. The statements |
| 1438 | within a given <tt>CFGBlock</tt> can be traversed using |
| 1439 | the <tt>CFGBlock::*iterator</tt> interface.</p> |
| 1440 | |
| 1441 | <p> |
Ted Kremenek | 18e17e7 | 2007-10-18 22:50:52 +0000 | [diff] [blame] | 1442 | A <tt>CFG</tt> object owns the instances of <tt>CFGBlock</tt> within |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1443 | the control-flow graph it represents. Each <tt>CFGBlock</tt> within a |
| 1444 | CFG is also uniquely numbered (accessible |
| 1445 | via <tt>CFGBlock::getBlockID()</tt>). Currently the number is |
| 1446 | based on the ordering the blocks were created, but no assumptions |
| 1447 | should be made on how <tt>CFGBlock</tt>s are numbered other than their |
| 1448 | numbers are unique and that they are numbered from 0..N-1 (where N is |
| 1449 | the number of basic blocks in the CFG).</p> |
| 1450 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1451 | <!-- ===================== --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1452 | <h4>Entry and Exit Blocks</h4> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1453 | <!-- ===================== --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1454 | |
| 1455 | Each instance of <tt>CFG</tt> contains two special blocks: |
| 1456 | an <i>entry</i> block (accessible via <tt>CFG::getEntry()</tt>), which |
| 1457 | has no incoming edges, and an <i>exit</i> block (accessible |
| 1458 | via <tt>CFG::getExit()</tt>), which has no outgoing edges. Neither |
| 1459 | block contains any statements, and they serve the role of providing a |
| 1460 | clear entrance and exit for a body of code such as a function body. |
| 1461 | The presence of these empty blocks greatly simplifies the |
| 1462 | implementation of many analyses built on top of CFGs. |
| 1463 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1464 | <!-- ===================================================== --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1465 | <h4 id ="ConditionalControlFlow">Conditional Control-Flow</h4> |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1466 | <!-- ===================================================== --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1467 | |
| 1468 | <p>Conditional control-flow (such as those induced by if-statements |
| 1469 | and loops) is represented as edges between <tt>CFGBlock</tt>s. |
| 1470 | Because different C language constructs can induce control-flow, |
| 1471 | each <tt>CFGBlock</tt> also records an extra <tt>Stmt*</tt> that |
| 1472 | represents the <i>terminator</i> of the block. A terminator is simply |
| 1473 | the statement that caused the control-flow, and is used to identify |
| 1474 | the nature of the conditional control-flow between blocks. For |
| 1475 | example, in the case of an if-statement, the terminator refers to |
| 1476 | the <tt>IfStmt</tt> object in the AST that represented the given |
| 1477 | branch.</p> |
| 1478 | |
| 1479 | <p>To illustrate, consider the following code example:</p> |
| 1480 | |
| 1481 | <code> |
| 1482 | int foo(int x) {<br> |
| 1483 | x = x + 1;<br> |
| 1484 | <br> |
| 1485 | if (x > 2) x++;<br> |
| 1486 | else {<br> |
| 1487 | x += 2;<br> |
| 1488 | x *= 2;<br> |
| 1489 | }<br> |
| 1490 | <br> |
| 1491 | return x;<br> |
| 1492 | } |
| 1493 | </code> |
| 1494 | |
| 1495 | <p>After invoking the parser+semantic analyzer on this code fragment, |
| 1496 | the AST of the body of <tt>foo</tt> is referenced by a |
| 1497 | single <tt>Stmt*</tt>. We can then construct an instance |
| 1498 | of <tt>CFG</tt> representing the control-flow graph of this function |
| 1499 | body by single call to a static class method:</p> |
| 1500 | |
| 1501 | <code> |
| 1502 | Stmt* FooBody = ...<br> |
| 1503 | CFG* FooCFG = <b>CFG::buildCFG</b>(FooBody); |
| 1504 | </code> |
| 1505 | |
| 1506 | <p>It is the responsibility of the caller of <tt>CFG::buildCFG</tt> |
| 1507 | to <tt>delete</tt> the returned <tt>CFG*</tt> when the CFG is no |
| 1508 | longer needed.</p> |
| 1509 | |
| 1510 | <p>Along with providing an interface to iterate over |
| 1511 | its <tt>CFGBlock</tt>s, the <tt>CFG</tt> class also provides methods |
| 1512 | that are useful for debugging and visualizing CFGs. For example, the |
| 1513 | method |
| 1514 | <tt>CFG::dump()</tt> dumps a pretty-printed version of the CFG to |
| 1515 | standard error. This is especially useful when one is using a |
| 1516 | debugger such as gdb. For example, here is the output |
| 1517 | of <tt>FooCFG->dump()</tt>:</p> |
| 1518 | |
| 1519 | <code> |
| 1520 | [ B5 (ENTRY) ]<br> |
| 1521 | Predecessors (0):<br> |
| 1522 | Successors (1): B4<br> |
| 1523 | <br> |
| 1524 | [ B4 ]<br> |
| 1525 | 1: x = x + 1<br> |
| 1526 | 2: (x > 2)<br> |
| 1527 | <b>T: if [B4.2]</b><br> |
| 1528 | Predecessors (1): B5<br> |
| 1529 | Successors (2): B3 B2<br> |
| 1530 | <br> |
| 1531 | [ B3 ]<br> |
| 1532 | 1: x++<br> |
| 1533 | Predecessors (1): B4<br> |
| 1534 | Successors (1): B1<br> |
| 1535 | <br> |
| 1536 | [ B2 ]<br> |
| 1537 | 1: x += 2<br> |
| 1538 | 2: x *= 2<br> |
| 1539 | Predecessors (1): B4<br> |
| 1540 | Successors (1): B1<br> |
| 1541 | <br> |
| 1542 | [ B1 ]<br> |
| 1543 | 1: return x;<br> |
| 1544 | Predecessors (2): B2 B3<br> |
| 1545 | Successors (1): B0<br> |
| 1546 | <br> |
| 1547 | [ B0 (EXIT) ]<br> |
| 1548 | Predecessors (1): B1<br> |
| 1549 | Successors (0): |
| 1550 | </code> |
| 1551 | |
| 1552 | <p>For each block, the pretty-printed output displays for each block |
| 1553 | the number of <i>predecessor</i> blocks (blocks that have outgoing |
| 1554 | control-flow to the given block) and <i>successor</i> blocks (blocks |
| 1555 | that have control-flow that have incoming control-flow from the given |
| 1556 | block). We can also clearly see the special entry and exit blocks at |
| 1557 | the beginning and end of the pretty-printed output. For the entry |
| 1558 | block (block B5), the number of predecessor blocks is 0, while for the |
| 1559 | exit block (block B0) the number of successor blocks is 0.</p> |
| 1560 | |
| 1561 | <p>The most interesting block here is B4, whose outgoing control-flow |
| 1562 | represents the branching caused by the sole if-statement |
| 1563 | in <tt>foo</tt>. Of particular interest is the second statement in |
| 1564 | the block, <b><tt>(x > 2)</tt></b>, and the terminator, printed |
| 1565 | as <b><tt>if [B4.2]</tt></b>. The second statement represents the |
| 1566 | evaluation of the condition of the if-statement, which occurs before |
| 1567 | the actual branching of control-flow. Within the <tt>CFGBlock</tt> |
| 1568 | for B4, the <tt>Stmt*</tt> for the second statement refers to the |
| 1569 | actual expression in the AST for <b><tt>(x > 2)</tt></b>. Thus |
| 1570 | pointers to subclasses of <tt>Expr</tt> can appear in the list of |
| 1571 | statements in a block, and not just subclasses of <tt>Stmt</tt> that |
| 1572 | refer to proper C statements.</p> |
| 1573 | |
| 1574 | <p>The terminator of block B4 is a pointer to the <tt>IfStmt</tt> |
| 1575 | object in the AST. The pretty-printer outputs <b><tt>if |
| 1576 | [B4.2]</tt></b> because the condition expression of the if-statement |
| 1577 | has an actual place in the basic block, and thus the terminator is |
| 1578 | essentially |
| 1579 | <i>referring</i> to the expression that is the second statement of |
| 1580 | block B4 (i.e., B4.2). In this manner, conditions for control-flow |
| 1581 | (which also includes conditions for loops and switch statements) are |
| 1582 | hoisted into the actual basic block.</p> |
| 1583 | |
Chris Lattner | 62fd278 | 2008-11-22 21:41:31 +0000 | [diff] [blame] | 1584 | <!-- ===================== --> |
| 1585 | <!-- <h4>Implicit Control-Flow</h4> --> |
| 1586 | <!-- ===================== --> |
Ted Kremenek | 8bc0571 | 2007-10-10 23:01:43 +0000 | [diff] [blame] | 1587 | |
| 1588 | <!-- |
| 1589 | <p>A key design principle of the <tt>CFG</tt> class was to not require |
| 1590 | any transformations to the AST in order to represent control-flow. |
| 1591 | Thus the <tt>CFG</tt> does not perform any "lowering" of the |
| 1592 | statements in an AST: loops are not transformed into guarded gotos, |
| 1593 | short-circuit operations are not converted to a set of if-statements, |
| 1594 | and so on.</p> |
| 1595 | --> |
Ted Kremenek | 17a295d | 2008-06-11 06:19:49 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 7bad199 | 2008-11-16 21:48:07 +0000 | [diff] [blame] | 1597 | |
| 1598 | <!-- ======================================================================= --> |
| 1599 | <h3 id="Constants">Constant Folding in the Clang AST</h3> |
| 1600 | <!-- ======================================================================= --> |
| 1601 | |
| 1602 | <p>There are several places where constants and constant folding matter a lot to |
| 1603 | the Clang front-end. First, in general, we prefer the AST to retain the source |
| 1604 | code as close to how the user wrote it as possible. This means that if they |
| 1605 | wrote "5+4", we want to keep the addition and two constants in the AST, we don't |
| 1606 | want to fold to "9". This means that constant folding in various ways turns |
| 1607 | into a tree walk that needs to handle the various cases.</p> |
| 1608 | |
| 1609 | <p>However, there are places in both C and C++ that require constants to be |
| 1610 | folded. For example, the C standard defines what an "integer constant |
| 1611 | expression" (i-c-e) is with very precise and specific requirements. The |
| 1612 | language then requires i-c-e's in a lot of places (for example, the size of a |
| 1613 | bitfield, the value for a case statement, etc). For these, we have to be able |
| 1614 | to constant fold the constants, to do semantic checks (e.g. verify bitfield size |
| 1615 | is non-negative and that case statements aren't duplicated). We aim for Clang |
| 1616 | to be very pedantic about this, diagnosing cases when the code does not use an |
| 1617 | i-c-e where one is required, but accepting the code unless running with |
| 1618 | <tt>-pedantic-errors</tt>.</p> |
| 1619 | |
| 1620 | <p>Things get a little bit more tricky when it comes to compatibility with |
| 1621 | real-world source code. Specifically, GCC has historically accepted a huge |
| 1622 | superset of expressions as i-c-e's, and a lot of real world code depends on this |
| 1623 | unfortuate accident of history (including, e.g., the glibc system headers). GCC |
| 1624 | accepts anything its "fold" optimizer is capable of reducing to an integer |
| 1625 | constant, which means that the definition of what it accepts changes as its |
| 1626 | optimizer does. One example is that GCC accepts things like "case X-X:" even |
| 1627 | when X is a variable, because it can fold this to 0.</p> |
| 1628 | |
| 1629 | <p>Another issue are how constants interact with the extensions we support, such |
| 1630 | as __builtin_constant_p, __builtin_inf, __extension__ and many others. C99 |
| 1631 | obviously does not specify the semantics of any of these extensions, and the |
| 1632 | definition of i-c-e does not include them. However, these extensions are often |
| 1633 | used in real code, and we have to have a way to reason about them.</p> |
| 1634 | |
| 1635 | <p>Finally, this is not just a problem for semantic analysis. The code |
| 1636 | generator and other clients have to be able to fold constants (e.g. to |
| 1637 | initialize global variables) and has to handle a superset of what C99 allows. |
| 1638 | Further, these clients can benefit from extended information. For example, we |
| 1639 | know that "foo()||1" always evaluates to true, but we can't replace the |
| 1640 | expression with true because it has side effects.</p> |
| 1641 | |
| 1642 | <!-- ======================= --> |
| 1643 | <h4>Implementation Approach</h4> |
| 1644 | <!-- ======================= --> |
| 1645 | |
| 1646 | <p>After trying several different approaches, we've finally converged on a |
| 1647 | design (Note, at the time of this writing, not all of this has been implemented, |
| 1648 | consider this a design goal!). Our basic approach is to define a single |
| 1649 | recursive method evaluation method (<tt>Expr::Evaluate</tt>), which is |
| 1650 | implemented in <tt>AST/ExprConstant.cpp</tt>. Given an expression with 'scalar' |
| 1651 | type (integer, fp, complex, or pointer) this method returns the following |
| 1652 | information:</p> |
| 1653 | |
| 1654 | <ul> |
| 1655 | <li>Whether the expression is an integer constant expression, a general |
| 1656 | constant that was folded but has no side effects, a general constant that |
| 1657 | was folded but that does have side effects, or an uncomputable/unfoldable |
| 1658 | value. |
| 1659 | </li> |
| 1660 | <li>If the expression was computable in any way, this method returns the APValue |
| 1661 | for the result of the expression.</li> |
| 1662 | <li>If the expression is not evaluatable at all, this method returns |
| 1663 | information on one of the problems with the expression. This includes a |
| 1664 | SourceLocation for where the problem is, and a diagnostic ID that explains |
| 1665 | the problem. The diagnostic should be have ERROR type.</li> |
| 1666 | <li>If the expression is not an integer constant expression, this method returns |
| 1667 | information on one of the problems with the expression. This includes a |
| 1668 | SourceLocation for where the problem is, and a diagnostic ID that explains |
| 1669 | the problem. The diagnostic should be have EXTENSION type.</li> |
| 1670 | </ul> |
| 1671 | |
| 1672 | <p>This information gives various clients the flexibility that they want, and we |
| 1673 | will eventually have some helper methods for various extensions. For example, |
| 1674 | Sema should have a <tt>Sema::VerifyIntegerConstantExpression</tt> method, which |
| 1675 | calls Evaluate on the expression. If the expression is not foldable, the error |
| 1676 | is emitted, and it would return true. If the expression is not an i-c-e, the |
| 1677 | EXTENSION diagnostic is emitted. Finally it would return false to indicate that |
| 1678 | the AST is ok.</p> |
| 1679 | |
| 1680 | <p>Other clients can use the information in other ways, for example, codegen can |
| 1681 | just use expressions that are foldable in any way.</p> |
| 1682 | |
| 1683 | <!-- ========== --> |
| 1684 | <h4>Extensions</h4> |
| 1685 | <!-- ========== --> |
| 1686 | |
Chris Lattner | 552de0a | 2008-11-23 08:16:56 +0000 | [diff] [blame] | 1687 | <p>This section describes how some of the various extensions Clang supports |
Chris Lattner | 7bad199 | 2008-11-16 21:48:07 +0000 | [diff] [blame] | 1688 | interacts with constant evaluation:</p> |
| 1689 | |
| 1690 | <ul> |
| 1691 | <li><b><tt>__extension__</tt></b>: The expression form of this extension causes |
| 1692 | any evaluatable subexpression to be accepted as an integer constant |
| 1693 | expression.</li> |
| 1694 | <li><b><tt>__builtin_constant_p</tt></b>: This returns true (as a integer |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1695 | constant expression) if the operand is any evaluatable constant. As a |
| 1696 | special case, if <tt>__builtin_constant_p</tt> is the (potentially |
| 1697 | parenthesized) condition of a conditional operator expression ("?:"), only |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1698 | the true side of the conditional operator is considered, and it is evaluated |
| 1699 | with full constant folding.</li> |
Chris Lattner | 7bad199 | 2008-11-16 21:48:07 +0000 | [diff] [blame] | 1700 | <li><b><tt>__builtin_choose_expr</tt></b>: The condition is required to be an |
| 1701 | integer constant expression, but we accept any constant as an "extension of |
| 1702 | an extension". This only evaluates one operand depending on which way the |
| 1703 | condition evaluates.</li> |
| 1704 | <li><b><tt>__builtin_classify_type</tt></b>: This always returns an integer |
| 1705 | constant expression.</li> |
| 1706 | <li><b><tt>__builtin_inf,nan,..</tt></b>: These are treated just like a |
| 1707 | floating-point literal.</li> |
| 1708 | <li><b><tt>__builtin_abs,copysign,..</tt></b>: These are constant folded as |
| 1709 | general constant expressions.</li> |
Douglas Gregor | eb661ed | 2010-09-11 18:08:34 +0000 | [diff] [blame] | 1710 | <li><b><tt>__builtin_strlen</tt></b> and <b><tt>strlen</tt></b>: These are constant folded as integer constant expressions if the argument is a string literal.</li> |
Chris Lattner | 7bad199 | 2008-11-16 21:48:07 +0000 | [diff] [blame] | 1711 | </ul> |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
Ted Kremenek | 17a295d | 2008-06-11 06:19:49 +0000 | [diff] [blame] | 1716 | </div> |
| 1717 | </body> |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1718 | </html> |