From d31d3e069e7f0811a17942414e5eb4f28c04bf5e Mon Sep 17 00:00:00 2001 From: simonmar Date: Mon, 5 Aug 2002 11:11:46 +0000 Subject: [project @ 2002-08-05 11:11:44 by simonmar] - Update the old coding style document - Move it to the commentary under the "RTS & libs" section --- docs/coding-style.html | 333 +++++++++++++++++++++++-------------------------- 1 file changed, 156 insertions(+), 177 deletions(-) (limited to 'docs/coding-style.html') diff --git a/docs/coding-style.html b/docs/coding-style.html index 4f39cbfc53..250ab88c3d 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 @@ -413,6 +377,28 @@ If any uses of the function remain, they will refer to the single copy 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
    @@ -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. - -- cgit v1.2.1