"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="llvm.css" type="text/css">
<title>LLVM Coding Standards</title>
</head>
Errors</a></li>
<li><a href="#ci_portable_code">Write Portable Code</a></li>
<li><a href="#ci_rtti_exceptions">Do not use RTTI or Exceptions</a></li>
+ <li><a href="#ci_static_ctors">Do not use Static Constructors</a></li>
<li><a href="#ci_class_struct">Use of <tt>class</tt>/<tt>struct</tt> Keywords</a></li>
</ol></li>
</ol></li>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>This document attempts to describe a few coding standards that are being used
in the LLVM source tree. Although no coding standards should be regarded as
</h2>
<!-- *********************************************************************** -->
+<div>
+
<!-- ======================================================================= -->
<h3>
<a name="sourceformating">Source Code Formatting</a>
</h3>
+<div>
+
<!-- _______________________________________________________________________ -->
<h4>
<a name="scf_commenting">Commenting</a>
</h4>
-<div class="doc_text">
+<div>
<p>Comments are one critical part of readability and maintainability. Everyone
knows they should comment, so should you. When writing comments, write them as
comment our code more than we do, there are a few very critical places that
documentation is very useful:</p>
-<b>File Headers</b>
+<h5>File Headers</h5>
+
+<div>
<p>Every source file should have a header on it that describes the basic
purpose of the file. If a file does not have a header, it should not be
tricky is going on, a reference to the paper where it is published should be
included, as well as any notes or "gotchas" in the code to watch out for.</p>
-<b>Class overviews</b>
+</div>
+
+<h5>Class overviews</h5>
<p>Classes are one fundamental part of a good object oriented design. As such,
a class definition should have a comment block that explains what the class is
something sane goes a long ways towards avoiding writing documentation.</p>
-<b>Method information</b>
+<h5>Method information</h5>
+
+<div>
<p>Methods defined in a class (as well as any global functions) should also be
documented properly. A quick note about what it does and a description of the
</div>
+</div>
+
<!-- _______________________________________________________________________ -->
<h4>
<a name="scf_commentformat">Comment Formatting</a>
</h4>
-<div class="doc_text">
+<div>
<p>In general, prefer C++ style (<tt>//</tt>) comments. They take less space,
require less typing, don't have nesting problems, etc. There are a few cases
<a name="scf_includes"><tt>#include</tt> Style</a>
</h4>
-<div class="doc_text">
+<div>
<p>Immediately after the <a href="#scf_commenting">header file comment</a> (and
include guards if working on a header file), the <a
<a name="scf_codewidth">Source Code Width</a>
</h4>
-<div class="doc_text">
+<div>
<p>Write 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
<a name="scf_spacestabs">Use Spaces Instead of Tabs</a>
</h4>
-<div class="doc_text">
+<div>
<p>In all cases, prefer spaces to tabs in source files. People have different
preferred indentation levels, and different styles of indentation that they
<a name="scf_indentation">Indent Code Consistently</a>
</h4>
-<div class="doc_text">
+<div>
<p>Okay, in your first year of programming you were told that indentation is
important. If you didn't believe and internalize this then, now is the time.
</div>
+</div>
<!-- ======================================================================= -->
<h3>
<a name="compilerissues">Compiler Issues</a>
</h3>
+<div>
<!-- _______________________________________________________________________ -->
<h4>
<a name="ci_warningerrors">Treat Compiler Warnings Like Errors</a>
</h4>
-<div class="doc_text">
+<div>
<p>If your code has compiler warnings in it, something is wrong — you
aren't casting values correctly, your have "questionable" constructs in your
<a name="ci_portable_code">Write Portable Code</a>
</h4>
-<div class="doc_text">
+<div>
<p>In almost all cases, it is possible and within reason to write completely
portable code. If there are cases where it isn't possible to write portable
<h4>
<a name="ci_rtti_exceptions">Do not use RTTI or Exceptions</a>
</h4>
-<div class="doc_text">
+<div>
<p>In an effort to reduce code and executable size, LLVM does not use RTTI
(e.g. <tt>dynamic_cast<></tt>) or exceptions. These two language features
</div>
+<!-- _______________________________________________________________________ -->
+<h4>
+<a name="ci_static_ctors">Do not use Static Constructors</a>
+</h4>
+<div>
+
+<p>Static constructors and destructors (e.g. global variables whose types have
+a constructor or destructor) should not be added to the code base, and should be
+removed wherever possible. Besides <a
+href="http://yosefk.com/c++fqa/ctors.html#fqa-10.12">well known problems</a>
+where the order of initialization is undefined between globals in different
+source files, the entire concept of static constructors is at odds with the
+common use case of LLVM as a library linked into a larger application.</p>
+
+<p>Consider the use of LLVM as a JIT linked into another application (perhaps
+for <a href="http://llvm.org/Users.html">OpenGL, custom languages</a>,
+<a href="http://llvm.org/devmtg/2010-11/Gritz-OpenShadingLang.pdf">shaders in
+movies</a>, etc</a>). Due to the design of static constructors, they must be
+executed at startup time of the entire application, regardless of whether or
+how LLVM is used in that larger application. There are two problems with
+this:</p>
+
+<ol>
+ <li>The time to run the static constructors impacts startup time of
+ applications — a critical time for GUI apps, among others.</li>
+
+ <li>The static constructors cause the app to pull many extra pages of memory
+ off the disk: both the code for the constructor in each <tt>.o</tt> file and
+ the small amount of data that gets touched. In addition, touched/dirty pages
+ put more pressure on the VM system on low-memory machines.</li>
+</ol>
+
+<p>We would really like for there to be zero cost for linking in an additional
+LLVM target or other library into an application, but static constructors
+violate this goal.</p>
+
+<p>That said, LLVM unfortunately does contain static constructors. It would be
+a <a href="http://llvm.org/PR11944">great project</a> for someone to purge all
+static constructors from LLVM, and then enable the
+<tt>-Wglobal-constructors</tt> warning flag (when building with Clang) to ensure
+we do not regress in the future.
+</p>
+
+</div>
+
<!-- _______________________________________________________________________ -->
<h4>
<a name="ci_class_struct">Use of <tt>class</tt> and <tt>struct</tt> Keywords</a>
</h4>
-<div class="doc_text">
+<div>
<p>In C++, the <tt>class</tt> and <tt>struct</tt> keywords can be used almost
interchangeably. The only difference is when they are used to declare a class:
</div>
+</div>
+
+</div>
+
<!-- *********************************************************************** -->
<h2>
<a name="styleissues">Style Issues</a>
</h2>
<!-- *********************************************************************** -->
+<div>
<!-- ======================================================================= -->
<h3>
</h3>
<!-- ======================================================================= -->
+<div>
<!-- _______________________________________________________________________ -->
<h4>
<a name="hl_module">A Public Header File <b>is</b> a Module</a>
</h4>
-<div class="doc_text">
+<div>
<p>C++ doesn't do too well in the modularity department. There is no real
encapsulation or data hiding (unless you use expensive protocol classes), but it
<a name="hl_dontinclude"><tt>#include</tt> as Little as Possible</a>
</h4>
-<div class="doc_text">
+<div>
<p><tt>#include</tt> hurts compile time performance. Don't do it unless you
have to, especially in header files.</p>
<a name="hl_privateheaders">Keep "Internal" Headers Private</a>
</h4>
-<div class="doc_text">
+<div>
<p>Many modules have a complex implementation that causes them to use more than
one implementation (<tt>.cpp</tt>) file. It is often tempting to put the
<a name="hl_earlyexit">Use Early Exits and <tt>continue</tt> to Simplify Code</a>
</h4>
-<div class="doc_text">
+<div>
<p>When reading code, keep in mind how much state and how many previous
decisions have to be remembered by the reader to understand a block of code.
<a name="hl_else_after_return">Don't use <tt>else</tt> after a <tt>return</tt></a>
</h4>
-<div class="doc_text">
+<div>
<p>For similar reasons above (reduction of indentation and easier reading),
please do not use '<tt>else</tt>' or '<tt>else if</tt>' after something that
<a name="hl_predicateloops">Turn Predicate Loops into Predicate Functions</a>
</h4>
-<div class="doc_text">
+<div>
<p>It is very common to write small loops that just compute a boolean value.
There are a number of ways that people commonly write these, but an example of
</div>
+</div>
<!-- ======================================================================= -->
<h3>
</h3>
<!-- ======================================================================= -->
+<div>
<!-- _______________________________________________________________________ -->
<h4>
</a>
</h4>
-<div class="doc_text">
+<div>
<p>Poorly-chosen names can mislead the reader and cause bugs. We cannot stress
enough how important it is to use <em>descriptive</em> names. Pick names that
<ul>
<li><p><b>Type names</b> (including classes, structs, enums, typedefs, etc)
- should be nouns and start with an upper-case letter (e.g.
- <tt>TextFileReader</tt>).</p></li>
+ should be nouns and start with an upper-case letter (e.g.
+ <tt>TextFileReader</tt>).</p></li>
+
+<li><p><b>Variable names</b> should be nouns (as they represent state). The
+ name should be camel case, and start with an upper case letter (e.g.
+ <tt>Leader</tt> or <tt>Boats</tt>).</p></li>
<li><p><b>Function names</b> should be verb phrases (as they represent
actions), and command-like function should be imperative. The name should
<a name="ll_assert">Assert Liberally</a>
</h4>
-<div class="doc_text">
+<div>
<p>Use the "<tt>assert</tt>" macro to its fullest. Check all of your
preconditions and assumptions, you never know when a bug (not necessarily even
<a name="ll_ns_std">Do Not Use '<tt>using namespace std</tt>'</a>
</h4>
-<div class="doc_text">
+<div>
<p>In LLVM, we prefer to explicitly prefix all identifiers from the standard
namespace with an "<tt>std::</tt>" prefix, rather than rely on
</a>
</h4>
-<div class="doc_text">
+<div>
<p>If a class is defined in a header file and has a v-table (either it has
virtual methods or it derives from classes with virtual methods), it must
<a name="ll_end">Don't evaluate <tt>end()</tt> every time through a loop</a>
</h4>
-<div class="doc_text">
+<div>
<p>Because C++ doesn't have a standard "<tt>foreach</tt>" loop (though it can be
emulated with macros and may be coming in C++'0x) we end up writing a lot of
<a name="ll_iostream"><tt>#include <iostream></tt> is Forbidden</a>
</h4>
-<div class="doc_text">
+<div>
<p>The use of <tt>#include <iostream></tt> in library files is
-hereby <b><em>forbidden</em></b>. The primary reason for doing this is to
-support clients using LLVM libraries as part of larger systems. In particular,
-we statically link LLVM into some dynamic libraries. Even if LLVM isn't used,
-the static constructors are run whenever an application starts up that uses the
-dynamic library. There are two problems with this:</p>
-
-<ol>
- <li>The time to run the static c'tors impacts startup time of applications
- — a critical time for GUI apps.</li>
-
- <li>The static c'tors cause the app to pull many extra pages of memory off the
- disk: both the code for the static c'tors in each <tt>.o</tt> file and the
- small amount of data that gets touched. In addition, touched/dirty pages
- put more pressure on the VM system on low-memory machines.</li>
-</ol>
-
+hereby <b><em>forbidden</em></b>, because many common implementations
+transparently inject a <a href="#ci_static_ctors">static constructor</a> into
+every translation unit that includes it.</p>
+
<p>Note that using the other stream headers (<tt><sstream></tt> for
example) is not problematic in this regard —
just <tt><iostream></tt>. However, <tt>raw_ostream</tt> provides various
<a name="ll_raw_ostream">Use <tt>raw_ostream</tt></a>
</h4>
-<div class="doc_text">
+<div>
<p>LLVM includes a lightweight, simple, and efficient stream implementation
in <tt>llvm/Support/raw_ostream.h</tt>, which provides all of the common
<a name="ll_avoidendl">Avoid <tt>std::endl</tt></a>
</h4>
-<div class="doc_text">
+<div>
<p>The <tt>std::endl</tt> modifier, when used with <tt>iostreams</tt> outputs a
newline to the output stream specified. In addition to doing this, however, it
</div>
+</div>
<!-- ======================================================================= -->
<h3>
</h3>
<!-- ======================================================================= -->
+<div>
+
<p>This section describes preferred low-level formatting guidelines along with
reasoning on why we prefer them.</p>
<a name="micro_spaceparen">Spaces Before Parentheses</a>
</h4>
-<div class="doc_text">
+<div>
<p>We prefer to put a space before an open parenthesis only in control flow
statements, but not in normal function call expressions and function-like
<a name="micro_preincrement">Prefer Preincrement</a>
</h4>
-<div class="doc_text">
+<div>
<p>Hard fast rule: Preincrement (<tt>++X</tt>) may be no slower than
postincrement (<tt>X++</tt>) and could very well be a lot faster than it. Use
<a name="micro_namespaceindent">Namespace Indentation</a>
</h4>
-<div class="doc_text">
+<div>
<p>
In general, we strive to reduce indentation wherever possible. This is useful
<a name="micro_anonns">Anonymous Namespaces</a>
</h4>
-<div class="doc_text">
+<div>
<p>After talking about namespaces in general, you may be wondering about
anonymous namespaces in particular.
</div>
+</div>
+</div>
<!-- *********************************************************************** -->
<h2>
</h2>
<!-- *********************************************************************** -->
-<div class="doc_text">
+<div>
<p>A lot of these comments and recommendations have been culled for other
sources. Two particularly important books for our work are:</p>