blob: d469d6315ecb8be4704b52f524a8f68e069ceed7 [file] [log] [blame]
Chris Lattnerd80c43c2001-07-09 03:27:08 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html><head><title>A Few Coding Standards</title></head>
3<body bgcolor=white>
4
5<table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
6<tr><td>&nbsp; <font size=+5 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>A Few Coding Standards</b></font></td>
7</tr></table>
8
9<ol>
10 <li><a href="#introduction">Introduction</a>
Chris Lattner7ae36bb2001-07-23 20:40:41 +000011 <li><a href="#mechanicalissues">Mechanical Source Issues</a>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000012 <ol>
13 <li><a href="#sourceformating">Source Code Formatting</a>
14 <ol>
15 <li><a href="#scf_commenting">Commenting</a>
16 <li><a href="#scf_commentformat">Comment Formatting</a>
Chris Lattner2e5cbc22003-08-07 21:45:47 +000017 <li><a href="#scf_includes">#include Style</a>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000018 <li><a href="#scf_codewidth">Source Code Width</a>
19 <li><a href="#scf_spacestabs">Use Spaces Instead of Tabs</a>
20 <li><a href="#scf_indentation">Indent Code Consistently</a>
21 </ol>
22 <li><a href="#compilerissues">Compiler Issues</a>
23 <ol>
24 <li><a href="#ci_warningerrors">Treat Compiler Warnings Like Errors</a>
25 <li><a href="#ci_cpp_features">Which C++ features can I use?</a>
26 <li><a href="#ci_portable_code">Write Portable Code</a>
27 </ol>
28 </ol>
29 <li><a href="#styleissues">Style Issues</a>
30 <ol>
31 <li><a href="#macro">The High Level Issues</a>
32 <ol>
33 <li><a href="#hl_module">A Public Header File <b>is</b> a Module</a>
34 <li><a href="#hl_dontinclude">#include as Little as Possible</a>
35 <li><a href="#hl_privateheaders">Keep "internal" Headers Private</a>
36 </ol>
37 <li><a href="#micro">The Low Level Issues</a>
38 <ol>
39 <li><a href="#hl_assert">Assert Liberally</a>
40 <li><a href="#hl_preincrement">Prefer Preincrement</a>
Chris Lattner13c46592002-01-20 19:01:26 +000041 <li><a href="#hl_avoidendl">Avoid endl</a>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000042 <li><a href="#hl_exploitcpp">Exploit C++ to its Fullest</a>
43 </ol>
44 <li><a href="#iterators">Writing Iterators</a>
45 </ol>
46 <li><a href="#seealso">See Also</a>
47</ol><p>
48
49
50<!-- *********************************************************************** -->
51</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
52<a name="introduction">Introduction
53</b></font></td></tr></table><ul>
54<!-- *********************************************************************** -->
55
Misha Brukman1b36d672003-10-06 19:26:00 +000056This document attempts to describe a few coding standards that are being used in
57the LLVM source tree. Although no coding standards should be regarded as
58absolute requirements to be followed in all instances, coding standards can be
59useful.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000060
Misha Brukman1b36d672003-10-06 19:26:00 +000061This document intentionally does not prescribe fixed standards for religious
62issues such as brace placement and space usage. For issues like this, follow
63the golden rule:
Chris Lattnerd80c43c2001-07-09 03:27:08 +000064
65<a name="goldenrule">
Misha Brukman1b36d672003-10-06 19:26:00 +000066<blockquote><b>If you are adding a significant body of source to a project, feel
67free to use whatever style you are most comfortable with. If you are extending,
68enhancing, or bug fixing already implemented code, use the style that is already
69being used so that the source is uniform and easy to follow.</b></blockquote>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000070
Misha Brukman1b36d672003-10-06 19:26:00 +000071The ultimate goal of these guidelines is the increase readability and
72maintainability of our common source base. If you have suggestions for topics to
73be included, please mail them to <a href="mailto:sabre@nondot.org">Chris</a>.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000074
75
76<!-- *********************************************************************** -->
77</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
78<a name="mechanicalissues">Mechanical Source Issues
79</b></font></td></tr></table><ul>
80<!-- *********************************************************************** -->
81
82<!-- ======================================================================= -->
83</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
84<a name="sourceformating">Source Code Formatting
85</b></font></td></tr></table><ul>
86
87
88<!-- _______________________________________________________________________ -->
89</ul><a name="scf_commenting"><h4><hr size=0>Commenting</h4><ul>
90
Misha Brukman1b36d672003-10-06 19:26:00 +000091Comments are one critical part of readability and maintainability. Everyone
92knows they should comment, so should you. :) Although we all should probably
93comment our code more than we do, there are a few very critical places that
94documentation is very useful:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +000095
96<ol>
97<h4><li>File Headers</h4>
Misha Brukman1b36d672003-10-06 19:26:00 +000098Every source file should have a header on it that describes the basic purpose of
99the file. If a file does not have a header, it should not be checked into CVS.
100Most source trees will probably have a standard file header format. The
101standard format for the LLVM source tree looks like this:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000102
103<pre>
Chris Lattnerc49fcd12003-10-13 14:58:11 +0000104//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000105//
106// This file contains the declaration of the Instruction class, which is the
107// base class for all of the VM instructions.
108//
109//===----------------------------------------------------------------------===//
110</pre>
111
Misha Brukman1b36d672003-10-06 19:26:00 +0000112A few things to note about this particular format. The "<tt>-*- C++ -*-</tt>"
113string on the first line is there to tell Emacs that the source file is a C++
114file, not a C file (Emacs assumes .h files are C files by default [Note that tag
115this is not necessary in .cpp files]). The name of the file is also on the
116first line, along with a very short description of the purpose of the file.
117This is important when printing out code and flipping though lots of pages.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000118
Misha Brukman1b36d672003-10-06 19:26:00 +0000119The main body of the description does not have to be very long in most cases.
120Here it's only two lines. If an algorithm is being implemented or something
121tricky is going on, a reference to the paper where it is published should be
122included, as well as any notes or "gotchas" in the code to watch out for.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000123
124
125<h4><li>Class overviews</h4>
126
Misha Brukman1b36d672003-10-06 19:26:00 +0000127Classes are one fundemental part of a good object oriented design. As such, a
128class definition should have a comment block that explains what the class is
129used for... if it's not obvious. If it's so completely obvious your grandma
130could figure it out, it's probably safe to leave it out. Naming classes
131something sane goes a long ways towards avoiding writing documentation. :)<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000132
133
134<h4><li>Method information</h4>
135
Misha Brukman1b36d672003-10-06 19:26:00 +0000136Methods defined in a class (as well as any global functions) should also be
137documented properly. A quick note about what it does any a description of the
138borderline behaviour is all that is necessary here (unless something
139particularly tricky or insideous is going on). The hope is that people can
140figure out how to use your interfaces without reading the code itself... that is
141the goal metric.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000142
Misha Brukman1b36d672003-10-06 19:26:00 +0000143Good things to talk about here are what happens when something unexpected
144happens: does the method return null? Abort? Format your hard disk?<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000145</ol>
146
147
148<!-- _______________________________________________________________________ -->
149</ul><a name="scf_commentformat"><h4><hr size=0>Comment Formatting</h4><ul>
150
Misha Brukman1b36d672003-10-06 19:26:00 +0000151In general, prefer C++ style (<tt>//</tt>) comments. They take less space,
152require less typing, don't have nesting problems, etc. There are a few cases
153when it is useful to use C style (<tt>/* */</tt>) comments however:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000154
155<ol>
Misha Brukman1b36d672003-10-06 19:26:00 +0000156<li>When writing a C code: Obviously if you are writing C code, use C style
157comments. :)
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000158<li>When writing a header file that may be #included by a C source file.
Misha Brukman1b36d672003-10-06 19:26:00 +0000159<li>When writing a source file that is used by a tool that only accepts C style
160comments.
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000161</ol><p>
162
Misha Brukman1b36d672003-10-06 19:26:00 +0000163To comment out a large block of code, use <tt>#if 0</tt> and <tt>#endif</tt>.
164These nest properly and are better behaved in general than C style comments.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000165
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000166<!-- _______________________________________________________________________ -->
167</ul><a name="scf_includes"><h4><hr size=0>#include Style</h4><ul>
168
169Immediately after the <a href="#scf_commenting">header file comment</a> (and
170include guards if working on a header file), the <a
171href="hl_dontinclude">minimal</a> list of #includes required by the file should
172be listed. We prefer these #includes to be listed in this order:<p>
173
174<ol>
175<li><a href="#mmheader">Main Module header</a>
176<li><a href="#hl_privateheaders">Local/Private Headers</a>
177<li>llvm/*
178<li>llvm/Analysis/*
179<li>llvm/Assembly/*
180<li>llvm/Bytecode/*
181<li>llvm/CodeGen/*
182<li>...
183<li>Support/*
184<li>Config/*
185<li>System #includes
186</ol>
187
188... and each catagory should be sorted by name.<p>
189
190<a name="mmheader">The "Main Module Header" file applies to .cpp file which
191implement an interface defined by a .h file. This #include should always be
192included <b>first</b> regardless of where it lives on the file system. By
193including a header file first in the .cpp files that implement the interfaces,
194we ensure that the header does not have any hidden dependencies which are not
195explicitly #included in the header, but should be. It is also a form of
196documentation in the .cpp file to indicate where the interfaces it implements
197are defined.<p>
198
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000199
200<!-- _______________________________________________________________________ -->
201</ul><a name="scf_codewidth"><h4><hr size=0>Source Code Width</h4><ul>
202
203Write your code to fit within 80 columns of text. This helps those of us who like to print out code and look at your code in an xterm without resizing it.
204
205
206<!-- _______________________________________________________________________ -->
207</ul><a name="scf_spacestabs"><h4><hr size=0>Use Spaces Instead of Tabs</h4><ul>
208
Misha Brukman1b36d672003-10-06 19:26:00 +0000209In all cases, prefer spaces to tabs in source files. People have different
210prefered indentation levels, and different styles of indentation that they
211like... this is fine. What isn't is that different editors/viewers expand tabs
212out to different tab stops. This can cause your code to look completely
213unreadable, and it is not worth dealing with.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000214
Misha Brukman1b36d672003-10-06 19:26:00 +0000215As always, follow the <a href="#goldenrule">Golden Rule</a> above: follow the
216style of existing code if your are modifying and extending it. If you like four
217spaces of indentation, <b>DO NOT</b> do that in the middle of a chunk of code
218with two spaces of indentation. Also, do not reindent a whole source file: it
219makes for incredible diffs that are absolutely worthless.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000220
221
222<!-- _______________________________________________________________________ -->
223</ul><a name="scf_indentation"><h4><hr size=0>Indent Code Consistently</h4><ul>
224
Misha Brukman1b36d672003-10-06 19:26:00 +0000225Okay, your first year of programming you were told that indentation is
226important. If you didn't believe and internalize this then, now is the time.
227Just do it.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000228
229
230
231
232<!-- ======================================================================= -->
233</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
234<a name="compilerissues">Compiler Issues
235</b></font></td></tr></table><ul>
236
237
238<!-- _______________________________________________________________________ -->
239</ul><a name="ci_warningerrors"><h4><hr size=0>Treat Compiler Warnings Like Errors</h4><ul>
240
Misha Brukman1b36d672003-10-06 19:26:00 +0000241If your code has compiler warnings in it, something is wrong: you aren't casting
242values correctly, your have "questionable" constructs in your code, or you are
243doing something legitimately wrong. Compiler warnings can cover up legitimate
244errors in output and make dealing with a translation unit difficult.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000245
Misha Brukman1b36d672003-10-06 19:26:00 +0000246It is not possible to prevent all warnings from all compilers, nor is it
247desirable. Instead, pick a standard compiler (like <tt>gcc</tt>) that provides
248a good thorough set of warnings, and stick to them. At least in the case of
249<tt>gcc</tt>, it is possible to work around any spurious errors by changing the
250syntax of the code slightly. For example, an warning that annoys me occurs when
251I write code like this:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000252
253<pre>
254 if (V = getValue()) {
255 ..
256 }
257</pre><p>
258
Misha Brukman1b36d672003-10-06 19:26:00 +0000259<tt>gcc</tt> will warn me that I probably want to use the <tt>==</tt> operator,
260and that I probably mistyped it. In most cases, I haven't, and I really don't
261want the spurious errors. To fix this particular problem, I rewrite the code
262like this:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000263
264<pre>
265 if ((V = getValue())) {
266 ..
267 }
268</pre><p>
269
Misha Brukman2d10d752003-07-28 21:57:18 +0000270...which shuts <tt>gcc</tt> up. Any <tt>gcc</tt> warning that annoys you can be
271fixed by massaging the code appropriately.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000272
Misha Brukman2d10d752003-07-28 21:57:18 +0000273These are the <tt>gcc</tt> warnings that I prefer to enable: <tt>-Wall -Winline
274-W -Wwrite-strings -Wno-unused</tt><p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000275
276
277<!-- _______________________________________________________________________ -->
278</ul><a name="ci_cpp_features"><h4><hr size=0>Which C++ features can I use?</h4><ul>
279
Misha Brukman2d10d752003-07-28 21:57:18 +0000280Compilers are finally catching up to the C++ standard. Most compilers implement
281most features, so you can use just about any features that you would like. In
282the LLVM source tree, I have chosen to not use these features:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000283
284<ol>
Misha Brukman2d10d752003-07-28 21:57:18 +0000285<li>Exceptions: Exceptions are very useful for error reporting and handling
286exceptional conditions. I do not use them in LLVM because they do have an
287associated performance impact (by restricting restructuring of code), and parts
288of LLVM are designed for performance critical purposes.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000289
Misha Brukman2d10d752003-07-28 21:57:18 +0000290Just like most of the rules in this document, this isn't a hard and fast
291requirement. Exceptions are used in the Parser, because it simplifies error
292reporting <b>significantly</b>, and the LLVM parser is not at all in the
293critical path.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000294
Misha Brukman2d10d752003-07-28 21:57:18 +0000295<li>RTTI: RTTI has a large cost in terms of executable size, and compilers are
296not yet very good at stomping out "dead" class information blocks. Because of
297this, typeinfo and dynamic cast are not used.
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000298</ol><p>
299
Misha Brukman2d10d752003-07-28 21:57:18 +0000300Other features, such as templates (without partial specialization) can be used
301freely. The general goal is to have clear, consise, performant code... if a
302technique assists with that then use it.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000303
304
305<!-- _______________________________________________________________________ -->
306</ul><a name="ci_portable_code"><h4><hr size=0>Write Portable Code</h4><ul>
307
Misha Brukman2d10d752003-07-28 21:57:18 +0000308In almost all cases, it is possible and within reason to write completely
309portable code. If there are cases where it isn't possible to write portable
310code, isolate it behind a well defined (and well documented) interface.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000311
Misha Brukman2d10d752003-07-28 21:57:18 +0000312In practice, this means that you shouldn't assume much about the host compiler,
313including its support for "high tech" features like partial specialization of
314templates. In fact, Visual C++ 6 could be an important target for our work in
315the future, and we don't want to have to rewrite all of our code to support
316it.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000317
318
319
320<!-- *********************************************************************** -->
321</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
322<a name="styleissues">Style Issues
323</b></font></td></tr></table><ul>
324<!-- *********************************************************************** -->
325
326
327<!-- ======================================================================= -->
328</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
329<a name="macro">The High Level Issues
330</b></font></td></tr></table><ul>
331
332
333<!-- _______________________________________________________________________ -->
334</ul><a name="hl_module"><h4><hr size=0>A Public Header File <b>is</b> a Module</h4><ul>
335
Misha Brukman2d10d752003-07-28 21:57:18 +0000336C++ doesn't do too well in the modularity department. There is no real
337encapsulation or data hiding (unless you use expensive protocol classes), but it
338is what we have to work with. When you write a public header file (in the LLVM
339source tree, they live in the top level "include" directory), you are defining a
340module of functionality.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000341
Misha Brukman2d10d752003-07-28 21:57:18 +0000342Ideally, modules should be completely independent of each other, and their
343header files should only include the absolute minimum number of headers
344possible. A module is not just a class, a function, or a namespace: <a
345href="http://www.cuj.com/articles/2000/0002/0002c/0002c.htm">it's a collection
346of these</a> that defines an interface. This interface may be several
347functions, classes or data structures, but the important issue is how they work
348together.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000349
Misha Brukman2d10d752003-07-28 21:57:18 +0000350In general, a module should be implemented with one or more <tt>.cpp</tt> files.
351Each of these <tt>.cpp</tt> files should include the header that defines their
352interface first. This ensure that all of the dependences of the module header
353have been properly added to the module header itself, and are not implicit.
354System headers should be included after user headers for a translation unit.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000355
356
357<!-- _______________________________________________________________________ -->
358</ul><a name="hl_dontinclude"><h4><hr size=0>#include as Little as Possible</h4><ul>
359
Misha Brukman2d10d752003-07-28 21:57:18 +0000360<tt>#include</tt> hurts compile time performance. Don't do it unless you have
361to, especially in header files.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000362
Misha Brukman2d10d752003-07-28 21:57:18 +0000363But wait, sometimes you need to have the definition of a class to use it, or to
364inherit from it. In these cases go ahead and #include that header file. Be
365aware however that there are many cases where you don't need to have the full
366definition of a class. If you are using a pointer or reference to a class, you
367don't need the header file. If you are simply returning a class instance from a
368prototyped function or method, you don't need it. In fact, for most cases, you
369simply don't need the definition of a class... and not <tt>#include</tt>'ing
370speeds up compilation.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000371
Misha Brukman2d10d752003-07-28 21:57:18 +0000372It is easy to try to go too overboard on this recommendation, however. You
373<b>must</b> include all of the header files that you are using, either directly
374or indirectly (through another header file). To make sure that you don't
375accidently forget to include a header file in your module header, make sure to
376include your module header <b>first</b> in the implementation file (as mentioned
377above). This way there won't be any hidden dependencies that you'll find out
378about later...<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000379
380
381<!-- _______________________________________________________________________ -->
382</ul><a name="hl_privateheaders"><h4><hr size=0>Keep "internal" Headers Private</h4><ul>
383
Misha Brukman2d10d752003-07-28 21:57:18 +0000384Many modules have a complex implementation that causes them to use more than one
385implementation (<tt>.cpp</tt>) file. It is often tempting to put the internal
386communication interface (helper classes, extra functions, etc) in the public
387module header file. Don't do this. :)<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000388
Misha Brukman2d10d752003-07-28 21:57:18 +0000389If you really need to do something like this, put a private header file in the
390same directory as the source files, and include it locally. This ensures that
391your private interface remains private and undisturbed by outsiders.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000392
Misha Brukman2d10d752003-07-28 21:57:18 +0000393Note however, that it's okay to put extra implementation methods a public class
394itself... just make them private (or protected), and all is well.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000395
396
397<!-- ======================================================================= -->
398</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
399<a name="micro">The Low Level Issues
400</b></font></td></tr></table><ul>
401
402
403<!-- _______________________________________________________________________ -->
404</ul><a name="hl_assert"><h4><hr size=0>Assert Liberally</h4><ul>
405
Misha Brukman2d10d752003-07-28 21:57:18 +0000406Use the "<tt>assert</tt>" function to its fullest. Check all of your
407preconditions and assumptions, you never know when a bug (not neccesarily even
408yours) might be caught early by an assertion, which reduces debugging time
409dramatically. The "<tt>&lt;cassert&gt;</tt>" header file is probably already
410included by the header files you are using, so it doesn't cost anything to use
411it.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000412
Misha Brukman2d10d752003-07-28 21:57:18 +0000413To further assist with debugging, make sure to put some kind of error message in
414the assertion statement (which is printed if the assertion is tripped). This
415helps the poor debugging make sense of why an assertion is being made and
416enforced, and hopefully what to do about it. Here is one complete example:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000417
418<pre>
419 inline Value *getOperand(unsigned i) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000420 assert(i &lt; Operands.size() && "getOperand() out of range!");
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000421 return Operands[i];
422 }
423</pre>
424
425Here are some examples:
426
427<pre>
Chris Lattnerb00c5822001-10-02 03:41:24 +0000428 assert(Ty-&gt;isPointerType() && "Can't allocate a non pointer type!");
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000429
430 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
431
Chris Lattnerb00c5822001-10-02 03:41:24 +0000432 assert(idx &lt; getNumSuccessors() && "Successor # out of range!");
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000433
434 assert(V1.getType() == V2.getType() && "Constant types must be identical!");
435
Chris Lattnerb00c5822001-10-02 03:41:24 +0000436 assert(isa&lt;PHINode&gt;(Succ-&gt;front()) && "Only works on PHId BBs!");
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000437</pre><p>
438
439You get the idea...<p>
440
441
442<!-- _______________________________________________________________________ -->
443</ul><a name="hl_preincrement"><h4><hr size=0>Prefer Preincrement</h4><ul>
444
Misha Brukman2d10d752003-07-28 21:57:18 +0000445Hard fast rule: Preincrement (++X) may be no slower than postincrement (X++) and
446could very well be a lot faster than it. Use preincrementation whenever
447possible.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000448
Misha Brukman2d10d752003-07-28 21:57:18 +0000449The semantics of postincrement include making a copy of the value being
450incremented, returning it, and then preincrementing the "work value". For
451primitive types, this isn't a big deal... but for iterators, it can be a huge
452issue (for example, some iterators contains stack and set objects in them...
453copying an iterator could invoke the copy ctor's of these as well). In general,
454get in the habit of always using preincrement, and you won't have a problem.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000455
456
457<!-- _______________________________________________________________________ -->
Chris Lattner13c46592002-01-20 19:01:26 +0000458</ul><a name="hl_avoidendl"><h4><hr size=0>Avoid endl</h4><ul>
459
Misha Brukman2d10d752003-07-28 21:57:18 +0000460The <tt>endl</tt> modifier, when used with iostreams outputs a newline to the
461output stream specified. In addition to doing this, however, it also flushes
462the output stream. In other words, these are equivalent:<p>
Chris Lattner13c46592002-01-20 19:01:26 +0000463
464<pre>
465 cout << endl;
466 cout << "\n" << flush;
467</pre>
468
469Most of the time, you probably have no reason to flush the output stream, so it's better to use a literal <tt>"\n"</tt>.<p>
470
471
472<!-- _______________________________________________________________________ -->
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000473</ul><a name="hl_exploitcpp"><h4><hr size=0>Exploit C++ to its Fullest</h4><ul>
474
Misha Brukman2d10d752003-07-28 21:57:18 +0000475C++ is a powerful language. With a firm grasp on its capabilities, you can make
476write effective, consise, readable and maintainable code all at the same time.
477By staying consistent, you reduce the amount of special cases that need to be
478remembered. Reducing the total number of lines of code you write is a good way
479to avoid documentation, and avoid giving bugs a place to hide.<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000480
Misha Brukman2d10d752003-07-28 21:57:18 +0000481For these reasons, come to know and love the contents of your local
482&lt;algorithm&gt; header file. Know about &lt;functional&gt; and what it can do
483for you. C++ is just a tool that wants you to master it. :)<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000484
485
486
487<!-- ======================================================================= -->
488</ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td>&nbsp;</td><td width="100%">&nbsp; <font color="#EEEEFF" face="Georgia,Palatino"><b>
489<a name="iterators">Writing Iterators
490</b></font></td></tr></table><ul>
491
Misha Brukman1b36d672003-10-06 19:26:00 +0000492Here's a pretty good summary of how to write your own data structure iterators
493in a way that is compatible with the STL, and with a lot of other code out there
494(slightly edited by Chris):<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000495
496<pre>
497From: Ross Smith <ross.s@ihug.co.nz>
498Newsgroups: comp.lang.c++.moderated
499Subject: Writing iterators (was: Re: Non-template functions that take iterators)
500Date: 28 Jun 2001 12:07:10 -0400
501
502Andre Majorel wrote:
503> Any pointers handy on "writing STL-compatible iterators for
504> dummies ?"
505
506I'll give it a try...
507
508The usual situation requiring user-defined iterators is that you have
509a type that bears some resemblance to an STL container, and you want
510to provide iterators so it can be used with STL algorithms. You need
511to ask three questions:
512
513First, is this simply a wrapper for an underlying collection of
514objects that's held somewhere as a real STL container, or is it a
515"virtual container" for which iteration is (under the hood) more
516complicated than simply incrementing some underlying iterator (or
517pointer or index or whatever)? In the former case you can frequently
518get away with making your container's iterators simply typedefs for
519those of the underlying container; your begin() function would call
520member_container.begin(), and so on.
521
522Second, do you only need read-only iterators, or do you need separate
523read-only (const) and read-write (non-const) iterators?
524
525Third, which kind of iterator (input, output, forward, bidirectional,
526or random access) is appropriate? If you're familiar with the
527properties of the iterator types (if not, visit
528<a href="http://www.sgi.com/tech/stl/">http://www.sgi.com/tech/stl/</a>), the appropriate choice should be
529obvious from the semantics of the container.
530
531I'll start with forward iterators, as the simplest case that's likely
532to come up in normal code. Input and output iterators have some odd
533properties and rarely need to be implemented in user code; I'll leave
534them out of discussion. Bidirectional and random access iterators are
535covered below.
536
537The exact behaviour of a forward iterator is spelled out in the
538Standard in terms of a set of expressions with specified behaviour,
539rather than a set of member functions, which leaves some leeway in how
540you actually implement it. Typically it looks something like this
541(I'll start with the const-iterator-only situation):
542
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000543 #include &lt;iterator&gt;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000544
545 class container {
546 public:
547 typedef something_or_other value_type;
548 class const_iterator:
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000549 public std::iterator&lt;std::forward_iterator_tag, value_type&gt; {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000550 friend class container;
551 public:
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000552 const value_type&amp; operator*() const;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000553 const value_type* operator->() const;
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000554 const_iterator&amp; operator++();
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000555 const_iterator operator++(int);
556 friend bool operator==(const_iterator lhs,
557 const_iterator rhs);
558 friend bool operator!=(const_iterator lhs,
559 const_iterator rhs);
560 private:
561 //...
562 };
563 //...
564 };
565
566An iterator should always be derived from an instantiation of the
567std::iterator template. The iterator's life cycle functions
568(constructors, destructor, and assignment operator) aren't declared
569here; in most cases the compiler-generated ones are sufficient. The
570container needs to be a friend of the iterator so that the container's
571begin() and end() functions can fill in the iterator's private members
572with the appropriate values.
573
574<i>[Chris's Note: I prefer to not make my iterators friends. Instead, two
575ctor's are provided for the iterator class: one to start at the end of the
576container, and one at the beginning. Typically this is done by providing
577two constructors with different signatures.]</i>
578
579There are normally only three member functions that need nontrivial
580implementations; the rest are just boilerplate.
581
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000582 const container::value_type&amp;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000583 container::const_iterator::operator*() const {
584 // find the element and return a reference to it
585 }
586
587 const container::value_type*
588 container::const_iterator::operator->() const {
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000589 return &amp;**this;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000590 }
591
592If there's an underlying real container, operator*() can just return a
593reference to the appropriate element. If there's no actual container
594and the elements need to be generated on the fly -- what I think of as
595a "virtual container" -- things get a bit more complicated; you'll
596probably need to give the iterator a value_type member object, and
597fill it in when you need to. This might be done as part of the
598increment operator (below), or if the operation is nontrivial, you
599might choose the "lazy" approach and only generate the actual value
600when one of the dereferencing operators is called.
601
602The operator->() function is just boilerplate around a call to
603operator*().
604
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000605 container::const_iterator&amp;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000606 container::const_iterator::operator++() {
607 // the incrementing logic goes here
608 return *this;
609 }
610
611 container::const_iterator
612 container::const_iterator::operator++(int) {
613 const_iterator old(*this);
614 ++*this;
615 return old;
616 }
617
618Again, the incrementing logic will usually be trivial if there's a
619real container involved, more complicated if you're working with a
620virtual container. In particular, watch out for what happens when you
621increment past the last valid item -- this needs to produce an
622iterator that will compare equal to container.end(), and making this
623work is often nontrivial for virtual containers.
624
625The post-increment function is just boilerplate again (and
626incidentally makes it obvious why all the experts recommend using
627pre-increment wherever possible).
628
629 bool operator==(container::const_iterator lhs,
630 container::const_iterator rhs) {
631 // equality comparison goes here
632 }
633
634 bool operator!=(container::const_iterator lhs,
635 container::const_iterator rhs) {
636 return !(lhs == rhs);
637 }
638
639For a real container, the equality comparison will usually just
640compare the underlying iterators (or pointers or indices or whatever).
641The semantics of comparisons for virtual container iterators are often
642tricky. Remember that iterator comparison only needs to be defined for
643iterators into the same container, so you can often simplify things by
644taking for granted that lhs and rhs both point into the same container
645object. Again, the second function is just boilerplate.
646
647It's a matter of taste whether iterator arguments are passed by value
648or reference; I've shown tham passed by value to reduce clutter, but
649if the iterator contains several data members, passing by reference
650may be better.
651
652That convers the const-iterator-only situation. When we need separate
653const and mutable iterators, one small complication is added beyond
654the simple addition of a second class.
655
656 class container {
657 public:
658 typedef something_or_other value_type;
659 class const_iterator;
660 class iterator:
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000661 public std::iterator&lt;std::forward_iterator_tag, value_type&gt; {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000662 friend class container;
663 friend class container::const_iterator;
664 public:
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000665 value_type&amp; operator*() const;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000666 value_type* operator->() const;
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000667 iterator&amp; operator++();
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000668 iterator operator++(int);
669 friend bool operator==(iterator lhs, iterator rhs);
670 friend bool operator!=(iterator lhs, iterator rhs);
671 private:
672 //...
673 };
674 class const_iterator:
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000675 public std::iterator&lt;std::forward_iterator_tag, value_type&gt; {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000676 friend class container;
677 public:
678 const_iterator();
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000679 const_iterator(const iterator&amp; i);
680 const value_type&amp; operator*() const;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000681 const value_type* operator->() const;
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000682 const_iterator&amp; operator++();
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000683 const_iterator operator++(int);
684 friend bool operator==(const_iterator lhs,
685 const_iterator rhs);
686 friend bool operator!=(const_iterator lhs,
687 const_iterator rhs);
688 private:
689 //...
690 };
691 //...
692 };
693
694There needs to be a conversion from iterator to const_iterator (so
695that mixed-type operations, such as comparison between an iterator and
696a const_iterator, will work). This is done here by giving
697const_iterator a conversion constructor from iterator (equivalently,
698we could have given iterator an operator const_iterator()), which
699requires const_iterator to be a friend of iterator, so it can copy its
700data members. (It also requires the addition of an explicit default
701constructor to const_iterator, since the existence of another
702user-defined constructor inhibits the compiler-defined one.)
703
704Bidirectional iterators add just two member functions to forward
705iterators:
706
707 class iterator:
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000708 public std::iterator&lt;std::bidirectional_iterator_tag, value_type&gt; {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000709 public:
710 //...
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000711 iterator&amp; operator--();
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000712 iterator operator--(int);
713 //...
714 };
715
716I won't detail the implementations, they're obvious variations on
717operator++().
718
719Random access iterators add several more member and friend functions:
720
721 class iterator:
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000722 public std::iterator&lt;std::random_access_iterator_tag, value_type&gt; {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000723 public:
724 //...
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000725 iterator&amp; operator+=(difference_type rhs);
726 iterator&amp; operator-=(difference_type rhs);
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000727 friend iterator operator+(iterator lhs, difference_type rhs);
728 friend iterator operator+(difference_type lhs, iterator rhs);
729 friend iterator operator-(iterator lhs, difference_type rhs);
730 friend difference_type operator-(iterator lhs, iterator rhs);
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000731 friend bool operator&lt;(iterator lhs, iterator rhs);
732 friend bool operator&gt;(iterator lhs, iterator rhs);
733 friend bool operator&lt;=(iterator lhs, iterator rhs);
734 friend bool operator&gt;=(iterator lhs, iterator rhs);
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000735 //...
736 };
737
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000738 container::iterator&amp;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000739 container::iterator::operator+=(container::difference_type rhs) {
740 // add rhs to iterator position
741 return *this;
742 }
743
Chris Lattnerf7235cd2003-04-23 16:25:38 +0000744 container::iterator&amp;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000745 container::iterator::operator-=(container::difference_type rhs) {
746 // subtract rhs from iterator position
747 return *this;
748 }
749
750 container::iterator operator+(container::iterator lhs,
751 container::difference_type rhs) {
752 return iterator(lhs) += rhs;
753 }
754
755 container::iterator operator+(container::difference_type lhs,
756 container::iterator rhs) {
757 return iterator(rhs) += lhs;
758 }
759
760 container::iterator operator-(container::iterator lhs,
761 container::difference_type rhs) {
762 return iterator(lhs) -= rhs;
763 }
764
765 container::difference_type operator-(container::iterator lhs,
766 container::iterator rhs) {
767 // calculate distance between iterators
768 }
769
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000770 bool operator&lt;(container::iterator lhs, container::iterator rhs) {
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000771 // perform less-than comparison
772 }
773
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000774 bool operator&gt;(container::iterator lhs, container::iterator rhs) {
775 return rhs &lt; lhs;
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000776 }
777
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000778 bool operator&lt;=(container::iterator lhs, container::iterator rhs) {
779 return !(rhs &lt; lhs);
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000780 }
781
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000782 bool operator&gt;=(container::iterator lhs, container::iterator rhs) {
783 return !(lhs &lt; rhs);
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000784 }
785
786Four of the functions (operator+=(), operator-=(), the second
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000787operator-(), and operator&lt;()) are nontrivial; the rest are
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000788boilerplate.
789
790One feature of the above code that some experts may disapprove of is
791the declaration of all the free functions as friends, when in fact
792only a few of them need direct access to the iterator's private data.
793I originally got into the habit of doing this simply to keep the
794declarations together; declaring some functions inside the class and
795some outside seemed awkward. Since then, though, I've been told that
796there's a subtle difference in the way name lookup works for functions
797declared inside a class (as friends) and outside, so keeping them
798together in the class is probably a good idea for practical as well as
799aesthetic reasons.
800
801I hope all this is some help to anyone who needs to write their own
802STL-like containers and iterators.
803
804--
Chris Lattner2e5cbc22003-08-07 21:45:47 +0000805Ross Smith &lt;ross.s@ihug.co.nz&gt; The Internet Group, Auckland, New Zealand
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000806</pre>
807
808
809<!-- *********************************************************************** -->
810</ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0><tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
811<a name="seealso">See Also
812</b></font></td></tr></table><ul>
813<!-- *********************************************************************** -->
814
Misha Brukman1b36d672003-10-06 19:26:00 +0000815A lot of these comments and recommendations have been culled for other sources.
816Two particularly important books for our work are:<p>
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000817
818<ol>
819<li><a href="http://www.aw.com/product/0,2627,0201924889,00.html">Effective C++</a> by Scott Meyers. There is an online version of the book (only some chapters though) <a href="http://www.awlonline.com/cseng/meyerscddemo/">available as well</a>.
820<li><a href="http://cseng.aw.com/book/0,3828,0201633620,00.html">Large-Scale C++ Software Design</a> by John Lakos
821</ol><p>
822
Misha Brukman1b36d672003-10-06 19:26:00 +0000823If you get some free time, and you haven't read them: do so, you might learn
824something. :)
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000825
826
827<!-- *********************************************************************** -->
828</ul>
829<!-- *********************************************************************** -->
830
831<hr>
832<font size=-1>
833<address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
834<!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
835<!-- hhmts start -->
Chris Lattnerc49fcd12003-10-13 14:58:11 +0000836Last modified: Sun Oct 12 22:12:43 CDT 2003
Chris Lattnerd80c43c2001-07-09 03:27:08 +0000837<!-- hhmts end -->
838</font>
839</body></html>