X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=docs%2Fcoding-style.html;h=8fbb27688cc975c3823ed19517201e9f299a7a72;hp=ceca6b9babec64da02a610dbd85c29372756d8d1;hb=dcf739bd7fb7de140be3bafb4ce211e2e5c7bba9;hpb=3cdceb2d3ec67d8f96528f4cbc0a16a06b2f7fe4 diff --git a/docs/coding-style.html b/docs/coding-style.html index ceca6b9..8fbb276 100644 --- a/docs/coding-style.html +++ b/docs/coding-style.html @@ -1,22 +1,23 @@ - Access To The GHC CVS Repository - + + Style Guidelines for fptools -

Coding suggestions for GHC/Hugs related code

+

Style Guidelines for fptools

Comments

-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 FP-CVS mailing list or -reid-alastair@cs.yale.edu). +

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 GHC mailing list)

References

@@ -24,68 +25,90 @@ If you haven't read them already, you might like to check the following. Where they conflict with our suggestions, they're probably right.

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 @@ -399,6 +378,27 @@ 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
    @@ -414,7 +414,7 @@ You could also write this:
     
     int *p, *q;
     
    -but Alastair prefers to split the declarations. +but it is preferrable to split the declarations.
  • Try to use ANSI C's enum feature when defining lists of constants of @@ -466,44 +466,35 @@ instead of # define TYPEchar 'Y' # define TIMEchar 'T'
  • -ToDo: at the time of writing, we still use the former. -
  • -Alastair likes to use stgCast instead of C syntax. He claims -it's easier to write and easier to grep for. YMMV. -
    -#define stgCast(ty,e) ((ty)(e))
    -
    +
  • Please keep to 80 columns: the line has to be drawn somewhere, +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. -
  • 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). +
  • 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: -

    -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  '+)
    -    )
    +  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. @@ -515,12 +506,37 @@ 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. +

    Commandline arguments

    + +A program in fptools should try follow the following rules for +commandline arguments: + + + +When an unknown commandline argument is encountered, the program +should display usage information on stderr and exit unsuccessfully.