=====================
LLVM Coding Standards
=====================
.. contents::
:local:
Introduction
============
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
absolute requirements to be followed in all instances, coding standards are
particularly important for large-scale code bases that follow a library-based
design (like LLVM).
This document intentionally does not prescribe fixed standards for religious
issues such as brace placement and space usage. For issues like this, follow
the golden rule:
.. _Golden Rule:
**If you are extending, enhancing, or bug fixing already implemented code,
use the style that is already being used so that the source is uniform and
easy to follow.**
Note that some code bases (e.g. ``libc++``) have really good reasons to deviate
from the coding standards. In the case of ``libc++``, this is because the
naming and other conventions are dictated by the C++ standard. If you think
there is a specific good reason to deviate from the standards here, please bring
it up on the LLVMdev mailing list.
There are some conventions that are not uniformly followed in the code base
(e.g. the naming convention). This is because they are relatively new, and a
lot of code was written before they were put in place. Our long term goal is
for the entire codebase to follow the convention, but we explicitly *do not*
want patches that do large-scale reformating of existing code. On the other
hand, it is reasonable to rename the methods of a class if you're about to
change it in some other way. Just do the reformating as a separate commit from
the functionality change.
The ultimate goal of these guidelines is the increase readability and
maintainability of our common source base. If you have suggestions for topics to
be included, please mail them to `Chris <mailto:sabre@nondot.org>`_.
Mechanical Source Issues
========================
Source Code Formatting
----------------------
Commenting
^^^^^^^^^^
Comments are one critical part of readability and maintainability. Everyone
knows they should comment their code, and so should you. When writing comments,
write them as English prose, which means they should use proper capitalization,
punctuation, etc. Aim to describe what the code is trying to do and why, not
*how* it does it at a micro level. Here are a few critical things to document:
.. _header file comment:
File Headers
""""""""""""
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 checked into the
tree. The standard header looks like this:
.. code-block:: c++
//===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file contains the declaration of the Instruction class, which is
/// the base class for all of the VM instructions.
///
//===----------------------------------------------------------------------===//
A few things to note about this particular format: The "``-*- C++ -*-``" string
on the first line is there to tell Emacs that the source file is a C++ file, not
a C file (Emacs assumes ``.h`` files are C files by default).
.. note::
This tag is not necessary in ``.cpp`` files. The name of the file is also
on the first line, along with a very short description of the purpose of the
file. This is important when printing out code and flipping though lots of
pages.
The next section in the file is a concise note that defines the license that the
file is released under. This makes it perfectly clear what terms the source
code can be distributed under and should not be modified in any way.
The main body is a ``doxygen`` comment describing the purpose of the file. It
should have a ``\brief`` command that describes the file in one or two
sentences. Any additional information should be separated by a blank line. If
an algorithm is being implemented or something 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.
Class overviews
"""""""""""""""
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
used for and how it works. Every non-trivial class is expected to have a
``doxygen`` comment block.
Method information
""""""""""""""""""
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
borderline behaviour is all that is necessary here (unless something
particularly tricky or insidious is going on). The hope is that people can
figure out how to use your interfaces without reading the code itself.
Good things to talk about here are what happens when something unexpected
happens: does the method return null? Abort? Format your hard disk?
Comment Formatting
^^^^^^^^^^^^^^^^^^
In general, prefer C++ style (``//``) comments. They take less space, require
less typing, don't have nesting problems, etc. There are a few cases when it is
useful to use C style (``/* */``) comments however:
#. When writing C code: Obviously if you are writing C code, use C style
comments.
#. When writing a header file that may be ``#include``\d by a C source file.
#. When writing a source file that is used by a tool that only accepts C style
comments.
To comment out a large block of code, use ``#if 0`` and ``#endif``. These nest
properly and are better behaved in general than C style comments.
Doxygen Use in Documentation Comments
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Use the ``\file`` command to turn the standard file header into a file-level
comment.
Include descriptive ``\brief`` paragraphs for all public interfaces (public
classes, member and non-member functions). Explain API use and purpose in
``\brief`` paragraphs, don't just restate the information that can be inferred
from the API name. Put detailed discussion into separate paragraphs.
To refer to parameter names inside a paragraph, use the ``\p name`` command.
Don't use the ``\arg name`` command since it starts a new paragraph that
contains documentation for the parameter.
Wrap non-inline code examples in ``\code ... \endcode``.
To document a function parameter, start a new paragraph with the
``\param name`` command. If the parameter is