X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=docs%2Fcoding-style.html;h=8fbb27688cc975c3823ed19517201e9f299a7a72;hb=b20e2bee6b0a734563f456dc346f91a3286d84b0;hp=af744ebe2b54d5b91558ee6c9e94325bb04a560f;hpb=81cb7610337b33508066dd400cd371ff6033c987;p=ghc-hetmet.git
diff --git a/docs/coding-style.html b/docs/coding-style.html
index af744eb..8fbb276 100644
--- a/docs/coding-style.html
+++ b/docs/coding-style.html
@@ -1,11 +1,12 @@
- GHC Style Guidelines for C code
+
+ Style Guidelines for fptools
-GHC Style guidelines for C code
+Style Guidelines for fptools
Comments
@@ -57,12 +58,12 @@ Hill C Style and Coding Standards.
Portability issues
-- We try to stick to C99 where possible. We use the following
+
- We try to stick to C99 where possible. We use the following
C99 features relative to C89, some of which were previously GCC
extensions (possibly with different syntax):
-- Variable length arrays as the last field of a struct. GCC has
+
- Variable length arrays as the last field of a struct. GCC has
a similar extension, but the syntax is slightly different: in GCC you
would declare the array as arr[0], whereas in C99 it is
declared as arr[].
@@ -90,9 +91,9 @@ header file).
#ifdef __GNUC__:
-- Function attributes (mostly just
no_return
and
+ - Function attributes (mostly just
no_return
and
unused
)
- - Inline assembly.
+
- Inline assembly.
-
@@ -223,24 +224,24 @@ code is removed altogether if DEBUG is not defined).
Particular guidelines for writing robust code:
--
+
-
Use assertions. Use lots of assertions. If you write a comment
that says "takes a +ve number" add an assertion. If you're casting
an int to a nat, add an assertion. If you're casting an int to a char,
add an assertion. We use the ASSERT macro for writing
assertions; it goes away when DEBUG is not defined.
-
-
+
-
Write special debugging code to check the integrity of your data structures.
(Most of the runtime checking code is in rts/Sanity.c)
Add extra assertions which call this code at the start and end of any
code that operates on your data structures.
-
-
+
-
When you find a hard-to-spot bug, try to think of some assertions,
sanity checks or whatever that would have made the bug easier to find.
-
-
+
-
When defining an enumeration, it's a good idea not to use 0 for normal
values. Instead, make 0 raise an internal error. The idea here is to
make it easier to detect pointer-related errors on the assumption that
@@ -268,7 +269,7 @@ piece of incomplete/broken code.
Syntactic details
-- Important: Put "redundant" braces or parens in your code.
+
- Important: Put "redundant" braces or parens in your code.
Omitting braces and parens leads to very hard to spot bugs -
especially if you use macros (and you might have noticed that GHC does
this a lot!)
@@ -276,13 +277,13 @@ this a lot!)
In particular:
--
+
-
Put braces round the body of for loops, while loops, if statements, etc.
even if they "aren't needed" because it's really hard to find the resulting
bug if you mess up. Indent them any way you like but put them in there!
- -
+
-
When defining a macro, always put parens round args - just in case.
For example, write:
@@ -293,25 +294,25 @@ instead of
#define add(x,y) x+y
- - Don't declare and initialize variables at the same time.
+
- 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
less tricky to get right and don't suffer from the usual problems
of side effects, evaluation order, multiple evaluation, etc.
-- Inline functions get the naming issue right. E.g. they
+
- Inline functions get the naming issue right. E.g. they
can have local variables which (in an expression context)
macros can't.
-
- Inline functions have call-by-value semantics whereas macros
+
- Inline functions have call-by-value semantics whereas macros
are call-by-name. You can be bitten by duplicated computation
if you aren't careful.
-
- You can use inline functions from inside gdb if you compile with
+
- You can use inline functions from inside gdb if you compile with
-O0 or -fkeep-inline-functions. If you use macros, you'd better
know what they expand to.
@@ -327,13 +328,13 @@ can be "polymorphic" as these examples show:
#define min(x,y) (((x)<=(y)) ? (x) : (y))
- -
+
-
Inline functions should be "static inline" because:
-
-
+
-
This code
int* p, q;
@@ -416,7 +416,7 @@ int *p, *q;
but it is preferrable to split the declarations.
- -
+
-
Try to use ANSI C's enum feature when defining lists of constants of
the same type. Among other benefits, you'll notice that gdb uses the
name instead of its (usually inscrutable) number when printing values
@@ -467,16 +467,16 @@ instead of
# define TIMEchar 'T'
-
- Please keep to 80 columns: the line has to be drawn somewhere,
+
- 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.
-
- When commenting out large chunks of code, use
#ifdef 0
+- 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
+
- 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
@@ -488,10 +488,10 @@ leading underscore. For example:
} Foo;
-
- Do not use ! instead of explicit comparison against
+
- 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
+
- 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.
@@ -501,7 +501,7 @@ tab width of 4 is preferred.
CVS issues
--
+
-
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.
@@ -512,5 +512,31 @@ it in case anyone else is changing that file.
+Commandline arguments
+
+A program in fptools should try follow the following rules for
+commandline arguments:
+
+
+- The
-v
and --verbose
options should be
+used to generate verbose output (intended for the user).
+
+ - The
-d
and --debug
options should be
+used to generate debugging output (intended for the developer).
+
+ - The
-?
and --help
options should be used
+to display usage information on stdout. The program should exit
+successfully afterwards.
+
+ - The
-V
and --version
options should be
+used to output version information on stdout, which includes one line
+of the form 'Program version
+Major.Minor[.Patchlevel] ...
'. The program
+should exit successfully afterwards.
+
+
+When an unknown commandline argument is encountered, the program
+should display usage information on stderr and exit unsuccessfully.
+