X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FCodingStandards.html;h=ee9443d164d5a55284f49b063dfedd4dae7e9e46;hb=9e6d1d1f5034347d237941f1bf08fba5c1583cd3;hp=2068c0f2c5a1532211875008782e9e928e15cace;hpb=59fec6a53234df91d6b66d5826ef1f8ce60af2fa;p=oota-llvm.git diff --git a/docs/CodingStandards.html b/docs/CodingStandards.html index 2068c0f2c5a..ee9443d164d 100644 --- a/docs/CodingStandards.html +++ b/docs/CodingStandards.html @@ -43,6 +43,8 @@ Private
  • Use Early Exits and 'continue' to Simplify Code
  • +
  • Don't use "else" after a + return
  • Turn Predicate Loops into Predicate Functions
  • @@ -57,6 +59,7 @@
  • #include <iostream> is forbidden
  • Avoid std::endl
  • +
  • Use raw_ostream
  • Microscopic Details @@ -300,7 +303,7 @@ for debate.

    In all cases, prefer spaces to tabs in source files. People have different -prefered indentation levels, and different styles of indentation that they +preferred indentation levels, and different styles of indentation that they like... this is fine. What isn't is that different editors/viewers expand tabs out to different tab stops. This can cause your code to look completely unreadable, and it is not worth dealing with.

    @@ -416,7 +419,8 @@ different symbols based on whether class or struct was used to declare the symbol. This can lead to problems at link time.

    So, the rule for LLVM is to always use the class keyword, unless -all members are public, in which case struct is allowed.

    +all members are public and the type is a C++ "POD" type, in which case +struct is allowed.

    @@ -487,7 +491,7 @@ most cases, you simply don't need the definition of a class... and not must include all of the header files that you are using -- you can include them either directly or indirectly (through another header file). To make sure that you don't -accidently forget to include a header file in your module header, make sure to +accidentally forget to include a header file in your module header, make sure to include your module header first in the implementation file (as mentioned above). This way there won't be any hidden dependencies that you'll find out about later...

    @@ -623,6 +627,88 @@ be a big understandability win.

    + +
    + Don't use "else" after a return +
    + +
    + +

    For similar reasons above (reduction of indentation and easier reading), + please do not use "else" or "else if" after something that interrupts + control flow like return, break, continue, goto, etc. For example, this is + "bad":

    + +
    +
    +  case 'J': {
    +    if (Signed) {
    +      Type = Context.getsigjmp_bufType();
    +      if (Type.isNull()) {
    +        Error = ASTContext::GE_Missing_sigjmp_buf;
    +        return QualType();
    +      } else {
    +        break;
    +      }
    +    } else {
    +      Type = Context.getjmp_bufType();
    +      if (Type.isNull()) {
    +        Error = ASTContext::GE_Missing_jmp_buf;
    +        return QualType();
    +      } else {
    +        break;
    +      }
    +    }
    +  }
    +  }
    +
    +
    + +

    It is better to write this something like:

    + +
    +
    +  case 'J':
    +    if (Signed) {
    +      Type = Context.getsigjmp_bufType();
    +      if (Type.isNull()) {
    +        Error = ASTContext::GE_Missing_sigjmp_buf;
    +        return QualType();
    +      }
    +    } else {
    +      Type = Context.getjmp_bufType();
    +      if (Type.isNull()) {
    +        Error = ASTContext::GE_Missing_jmp_buf;
    +        return QualType();
    +      }
    +    }
    +    break;
    +
    +
    + +

    Or better yet (in this case), as:

    + +
    +
    +  case 'J':
    +    if (Signed)
    +      Type = Context.getsigjmp_bufType();
    +    else
    +      Type = Context.getjmp_bufType();
    +    
    +    if (Type.isNull()) {
    +      Error = Signed ? ASTContext::GE_Missing_sigjmp_buf :
    +                       ASTContext::GE_Missing_jmp_buf;
    +      return QualType();
    +    }
    +    break;
    +
    +
    + +

    The idea is to reduce indentation and the amount of code you have to keep + track of when reading the code.

    + +
    @@ -704,7 +790,7 @@ locality.

    Use the "assert" function to its fullest. Check all of your -preconditions and assumptions, you never know when a bug (not neccesarily even +preconditions and assumptions, you never know when a bug (not necessarily even yours) might be caught early by an assertion, which reduces debugging time dramatically. The "<cassert>" header file is probably already included by the header files you are using, so it doesn't cost anything to use @@ -904,12 +990,14 @@ library. There are two problems with this:

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

    +example) is not problematic in this regard (just <iostream>). +However, raw_ostream provides various APIs that are better performing for almost +every use than std::ostream style APIs, so you should just use it for new +code.

    -

    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).

    +

    New code should always +use raw_ostream for writing, or +the llvm::MemoryBuffer API for reading files.

    @@ -938,6 +1026,26 @@ it's better to use a literal '\n'.

    + +
    + Use raw_ostream +
    + +
    + +

    LLVM includes a lightweight, simple, and efficient stream implementation +in llvm/Support/raw_ostream.h which provides all of the common features +of std::ostream. All new code should use raw_ostream instead +of ostream.

    + +

    Unlike std::ostream, raw_ostream is not a template and can +be forward declared as class raw_ostream. Public headers should +generally not include the raw_ostream header, but use forward +declarations and constant references to raw_ostream instances.

    + +
    + +
    Microscopic Details