Coding style

From K5Wiki
(Difference between revisions)
Jump to: navigation, search
(update SunOS cstyle link)
Line 10: Line 10:
* [http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style?rev=HEAD NetBSD <code>/usr/share/misc/style</code>] [http://cvsweb.netbsd.org/bsdweb.cgi/src/share/misc/style (log)]
* [http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style?rev=HEAD NetBSD <code>/usr/share/misc/style</code>] [http://cvsweb.netbsd.org/bsdweb.cgi/src/share/misc/style (log)]
* [http://www.gnu.org/prep/standards/ GNU coding standards]
* [http://www.gnu.org/prep/standards/ GNU coding standards]
* [http://opensolaris.org/os/community/documentation/getting_started_docs/cstyle.ms.pdf C Style and Coding Standards for SunOS]
* [http://opensolaris.org/os/community/on/cstyle.ms.pdf C Style and Coding Standards for SunOS]
== General information ==
== General information ==

Revision as of 17:49, 1 September 2009

The C language Coding style described here is based on the BSD coding style (Kernel Normal Form - KNF), with some additional elements from the GNU coding standards and the SunOS coding standards.

External links

General information

Aspects of C style in GNU coding std but not here

  • redundant parens to force extra indent of operators of different precedences
  • redundant parens to force general extra indent of expressions that are broken between lines
  • use of ^L characters to break up source files into pages
  • nitpicking about capitalization in comments of variable names when their values are meant
  • commenting usages of static variables
  • casts to void
  • separation of word in names with underscores vs case change
  • enum vs #define'd integer constants
  • 14 char filename limits, MS-DOS filename limits
  • portability
  • system library function quirks
  • internationalization
  • mmap()

Aspects of C style in BSD KNF but not here

  • sorting of header files
  • sorting of struct members
  • separating struct tag decl and struct typedef
  • sorting of var decl
  • lining up var names in decls
  • newline after decls
  • usage of __P
  • usage of getopt
  • not initializing vars in decls
  • stdarg/varargs handling

Emacs cc-mode style

Putting the following code in your .emacs file will result in mostly the right thing happening with respect to formatting style. Note that you may want to turn on auto-newline feature of cc-mode, though that seems to have some bugs with brace-elseif-brace handling at least in the version of cc-mode that comes with emacs 20.3.

       (defconst krb5-c-style
            brace-elseif-brace brace-else-brace defun-close-semi)
           (c-comment-continuation-stars . "* ")
           (c-electric-pound-behavior alignleft)
            (class-open after)
            (substatement-open after)
            (block-close . c-snug-do-while)
            (extern-lang-open after))
            (case-label after)
            (label after))
           (c-indent-comments-syntactically-p . t)
           (c-label-minimum-indentation . 0)
       (defun krb5-c-hook ()
         (c-add-style "krb5" krb5-c-style t))
       (add-hook 'c-mode-common-hook 'krb5-c-hook)

You might also want to try (for Emacs 22 and later):

       (add-hook 'before-save-hook 'copyright-update)

which will offer to update the year in the top-most copyright notice in a file when you save it, if it's not already current.

indent.pro settings

The following settings for the indent program should produce a reasonable approximation to the C coding style described here, though some manual cleanup may be necessary. Note that the gindent installed in the gnu locker does not currently handle -nut or -psl correctly though.

Personal tools