X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fcoding-style.html;h=250ab88c3d91bc73dd888876a269367208c17e8b;hb=72f5cd2fbc56c266e92f974a4561fbe878628b63;hp=4f39cbfc53ea0fcfb9db4096c431c675e1935d1a;hpb=5e2b22a5efbc9444a56346f3f93d863703dd1263;p=ghc-hetmet.git diff --git a/docs/coding-style.html b/docs/coding-style.html index 4f39cbf..250ab88 100644 --- a/docs/coding-style.html +++ b/docs/coding-style.html @@ -5,11 +5,14 @@ -

Coding suggestions for GHC/Hugs related code

+

GHC Style guidelines for C code

Comments

-NB These are just suggestions. They're not set in stone. Some of +

These coding style guidelines are mainly intended for use in +ghc/rts and ghc/includes. + +

NB These are just suggestions. They're not set in stone. Some of them are probably misguided. If you disagree with them, feel free to modify this document (and make your commit message reasonably informative) or mail someone (eg. +

  • +The C99 standard. One reasonable reference is here. + +

  • Writing Solid Code, Microsoft Press. (Highly recommended. Possibly -the only Microsoft Press book that's worth reading. SimonPJ has a -copy.) +the only Microsoft Press book that's worth reading.)

  • -Autoconf documentation (which doesn't seem to be on the web). +Autoconf documentation. See also The autoconf macro archive and Cyclic Software's description @@ -50,57 +57,57 @@ Hill C Style and Coding Standards.

    Portability issues

    +

  • Don't declare and initialize variables at the same time. +Separating the declaration and initialization takes more lines, but +make the code clearer.

  • Use inline functions instead of macros if possible - they're a lot @@ -414,6 +378,28 @@ in the library.

  • +Don't define macros that expand to a list of statements. +You could just use braces as in: + +
    +  #define ASSIGN_CC_ID(ccID)              \
    +        {                                 \
    +        ccID = CC_ID;                     \
    +        CC_ID++;                          \
    +        }
    +
    + +(but it's usually better to use an inline function instead - see above). + +

  • +Don't even write macros that expand to 0 statements - they can mess you +up as well. Use the doNothing macro instead. +
    +  #define doNothing() do { } while (0)
    +
    + + +

  • This code
     int* p, q;
    @@ -486,36 +472,30 @@ and by keeping it to 80 columns we can ensure that code looks OK on
     everyone's screen.  Long lines are hard to read, and a sign that the
     code needs to be restructured anyway.
     
    +

  • When commenting out large chunks of code, use #ifdef 0 +... #endif rather than /* ... */ because C doesn't +have nested comments. + +

  • When declaring a typedef for a struct, give the struct a name +as well, so that other headers can forward-reference the struct name +and it becomes possible to have opaque pointers to the struct. Our +convention is to name the struct the same as the typedef, but add a +leading underscore. For example: + +
    +  typedef struct _Foo {
    +    ...
    +  } Foo;
    +
    + +

  • Do not use ! instead of explicit comparison against +NULL or '\0'; the latter is much clearer. +

  • We don't care too much about your indentation style but, if you're modifying a function, please try to use the same style as the rest of the function (or file). If you're writing new code, a tab width of 4 is preferred. -

    -On which note: -Hugs related pieces of code often start with the line: -

    -  /* -*- mode: hugs-c; -*- */
    -
    -which helps Emacs mimic the indentation style used by Mark P Jones -within Hugs. Add this to your .emacs file. -
    -  (defun hugs-c-mode ()
    -    "C mode with adjusted defaults for use with Hugs (based on linux-c-mode)"
    -    (interactive)
    -    (c-mode)
    -    (setq c-basic-offset 4)
    -    (setq indent-tabs-mode nil) ; don't use tabs to indent
    -    (setq c-recognize-knr-r nil)  ; no K&R here - don't pay the price
    -    ; no: (setq tab-width 4)
    -
    -    (c-set-offset 'knr-argdecl-intro    0)
    -    (c-set-offset 'case-label           0)
    -    (c-set-offset 'statement-case-intro '++)
    -    (c-set-offset 'statement-case-open  '+)
    -    )
    -
    -

    CVS issues

    @@ -526,12 +506,11 @@ Don't be tempted to reindent or reorganise large chunks of code - it generates large diffs in which it's hard to see whether anything else was changed.

    -If you must reindent or reorganise, don't do anything else in that commit -and give advance warning that you're about to do it in case anyone else -is changing that file. +If you must reindent or reorganise, don't include any functional +changes that commit and give advance warning that you're about to do +it in case anyone else is changing that file. -