X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FCodingStandards.html;h=dbe95a8d58815e0c14b1ed1cbac9218719820d46;hb=c25e7581b9b8088910da31702d4ca21c4734c6d7;hp=858bbe1f7b6fc60166187c77011094bee27aefef;hpb=788ee28a3e9e3347549d3777a8327fc9e67cd95f;p=oota-llvm.git diff --git a/docs/CodingStandards.html b/docs/CodingStandards.html index 858bbe1f7b6..dbe95a8d588 100644 --- a/docs/CodingStandards.html +++ b/docs/CodingStandards.html @@ -3,12 +3,12 @@ - A Few Coding Standards + LLVM Coding Standards
- A Few Coding Standards + LLVM Coding Standards
    @@ -50,6 +50,8 @@
  1. Do not use 'using namespace std'
  2. Provide a virtual method anchor for classes in headers
  3. +
  4. Don't evaluate end() every time through a + loop
  5. Prefer Preincrement
  6. Avoid std::endl
@@ -131,12 +133,12 @@ this:

 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by <whoever started the file> and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
 //===----------------------------------------------------------------------===//
 //
 // This file contains the declaration of the Instruction class, which is the
@@ -146,9 +148,7 @@ this:

-

A few things to note about this particular format: The 'developed by' line -should be the name of the person or organization who initially contributed the -file. The "-*- C++ +

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 that this tag is not necessary in .cpp files. The name of the file is also @@ -156,9 +156,9 @@ 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.

+

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 of the description does not have to be very long in most cases. Here it's only two lines. If an algorithm is being implemented or something @@ -240,7 +240,7 @@ order:

  • System #includes
  • -

    ... and each catagory should be sorted by name.

    +

    ... and each category should be sorted by name.

    The "Main Module Header" file applies to .cpp file which implement an interface defined by a .h file. This #include @@ -264,6 +264,18 @@ implements are defined.

    like to print out code and look at your code in an xterm without resizing it.

    +

    The longer answer is that there must be some limit to the width of the code +in order to reasonably allow developers to have multiple files side-by-side in +windows on a modest display. If you are going to pick a width limit, it is +somewhat arbitrary but you might as well pick something standard. Going with +90 columns (for example) instead of 80 columns wouldn't add any significant +value and would be detrimental to printing out code. Also many other projects +have standardized on 80 columns, so some people have already configured their +editors for it (vs something else, like 90 columns).

    + +

    This is one of many contentious issues in coding standards, but is not up +for debate.

    + @@ -369,9 +381,8 @@ code, isolate it behind a well defined (and well documented) interface.

    In practice, this means that you shouldn't assume much about the host compiler, including its support for "high tech" features like partial -specialization of templates. In fact, Visual C++ 6 could be an important target -for our work in the future, and we don't want to have to rewrite all of our code -to support it.

    +specialization of templates. If these features are used, they should only be +an implementation detail of a library which has a simple exposed API.

    @@ -512,63 +523,13 @@ library. There are two problems with this:

    put more pressure on the VM system on low-memory machines. -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Old WayNew Way
    #include <iostream>
    #include "llvm/Support/Streams.h"
    DEBUG(std::cerr << ...);
    -DEBUG(dump(std::cerr));
    DOUT << ...;
    -DEBUG(dump(DOUT));
    std::cerr << "Hello world\n";
    llvm::cerr << "Hello world\n";
    std::cout << "Hello world\n";
    llvm::cout << "Hello world\n";
    std::cin >> Var;
    llvm::cin >> Var;
    std::ostream
    llvm::OStream
    std::istream
    llvm::IStream
    std::stringstream
    llvm::StringStream
    void print(std::ostream &Out);
    -// ...
    -print(std::cerr);
    void print(llvm::OStream Out);1
    -// ...
    -print(llvm::cerr);
    - -
    -
    +

    Note that using the other stream headers (<sstream> for +example) is allowed normally, it is just <iostream> that is +causing problems.

    -
    -

    1llvm::OStream is a light-weight class so it should never -be passed by reference. This is important because in some configurations, -DOUT is an rvalue.

    -
    +

    The preferred replacement for stream functionality is the +llvm::raw_ostream class (for writing to output streams of various +sorts) and the llvm::MemoryBuffer API (for reading in files).

    @@ -625,6 +586,29 @@ assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!"

    You get the idea...

    +

    Please be aware when adding assert statements that not all compilers are aware of +the semantics of the assert. In some places, asserts are used to indicate a piece of +code that should not be reached. These are typically of the form:

    + +
    +
    +assert(0 && "Some helpful error message");
    +
    +
    + +

    When used in a function that returns a value, they should be followed with a return +statement and a comment indicating that this line is never reached. This will prevent +a compiler which is unable to deduce that the assert statement never returns from +generating a warning.

    + +
    +
    +assert(0 && "Some helpful error message");
    +// Not reached
    +return 0;
    +
    +
    + @@ -638,8 +622,8 @@ namespace with an "std::" prefix, rather than rely on "using namespace std;".

    In header files, adding a 'using namespace XXX' directive pollutes -the namespace of any source file that includes the header. This is clearly a -bad thing.

    +the namespace of any source file that #includes the header. This is +clearly a bad thing.

    In implementation files (e.g. .cpp files), the rule is more of a stylistic rule, but is still important. Basically, using explicit namespace prefixes @@ -673,9 +657,70 @@ others.

    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 always have at least one out-of-line virtual method in the class. Without -this, the compiler will copy the vtable and RTTI into every .o file that -#includes the header, bloating .o file sizes and increasing link times. -

    +this, the compiler will copy the vtable and RTTI into every .o file +that #includes the header, bloating .o file sizes and +increasing link times.

    + + + + +
    + Don't evaluate end() every time through a loop +
    + +
    + +

    Because C++ doesn't have a standard "foreach" loop (though it can be emulated +with macros and may be coming in C++'0x) we end up writing a lot of loops that +manually iterate from begin to end on a variety of containers or through other +data structures. One common mistake is to write a loop in this style:

    + +
    +
    +  BasicBlock *BB = ...
    +  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
    +     ... use I ...
    +
    +
    + +

    The problem with this construct is that it evaluates "BB->end()" +every time through the loop. Instead of writing the loop like this, we strongly +prefer loops to be written so that they evaluate it once before the loop starts. +A convenient way to do this is like so:

    + +
    +
    +  BasicBlock *BB = ...
    +  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
    +     ... use I ...
    +
    +
    + +

    The observant may quickly point out that these two loops may have different +semantics: if the container (a basic block in this case) is being mutated, then +"BB->end()" may change its value every time through the loop and the +second loop may not in fact be correct. If you actually do depend on this +behavior, please write the loop in the first form and add a comment indicating +that you did it intentionally.

    + +

    Why do we prefer the second form (when correct)? Writing the loop in the +first form has two problems: First it may be less efficient than evaluating it +at the start of the loop. In this case, the cost is probably minor: a few extra +loads every time through the loop. However, if the base expression is more +complex, then the cost can rise quickly. I've seen loops where the end +expression was actually something like: "SomeMap[x]->end()" and map +lookups really aren't cheap. By writing it in the second form consistently, you +eliminate the issue entirely and don't even have to think about it.

    + +

    The second (even bigger) issue is that writing the loop in the first form +hints to the reader that the loop is mutating the container (a fact that a +comment would handily confirm!). If you write the loop in the second form, it +is immediately obvious without even looking at the body of the loop that the +container isn't being modified, which makes it easier to read the code and +understand what it does.

    + +

    While the second form of the loop is a few extra keystrokes, we do strongly +prefer it.

    @@ -756,9 +801,9 @@ something.


    Valid CSS! + src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"> Valid HTML 4.01! + src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"> Chris Lattner
    LLVM Compiler Infrastructure