logo_kerberos.gif

Difference between revisions of "Coding style"

From K5Wiki
Jump to: navigation, search
(Emacs cc-mode style: rephrase last change - update isn't automatic, you get prompted)
(indent.pro settings)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
The C language '''Coding style''' described here is based on the BSD coding style, with some additional elements from the GNU coding standards and the SunOS coding standards.
+
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.
   
 
* [[Coding style/Formatting|Formatting]]
 
* [[Coding style/Formatting|Formatting]]
 
* [[Coding style/Practices|Practices]]
 
* [[Coding style/Practices|Practices]]
  +
* [[Coding style/Style checker|Style checker]]
  +
* [[Coding style/Version control practices|Version control practices]]
  +
* [[Coding style/Transition_strategies|Transition strategies]]
  +
* [[Coding style/Reindenting|Reindenting]]
   
 
== External links ==
 
== External links ==
Line 9: Line 13:
 
* [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]
 
== Old version ==
 
 
Old content to be moved elsewhere is below.
 
 
----
 
 
== WRITING C CODE ==
 
 
The code in the krb5 source tree largely follows BSD KNF
 
(/usr/share/misc/style on NetBSD) except that it uses a four column
 
basic offset. The style described here is a synthesis of BSD KNF and
 
the GNU coding standards for the C language. The formatting described
 
in the "Formatting Your Source Code" section of the GNU coding
 
standards is mostly what we want, except we use BSD brace style and
 
BSD-ish conventions for the spacing around operators.
 
 
=== Coding practices for C ===
 
   
  +
== General information ==
   
 
=== Aspects of C style in GNU coding std but not here ===
 
=== Aspects of C style in GNU coding std but not here ===
Line 64: Line 69:
 
=== Emacs cc-mode style ===
 
=== Emacs cc-mode style ===
   
Putting the following code in your .emacs file will result in mostly
 
  +
Load the elisp file {{trunkref|src/util/krb5-c-style.el}} to get mostly
the right thing happening with respect to formatting style. Note that
 
  +
the right thing to happen with respect to formatting style. <code>krb5-c-style.el</code> uses a heuristic to detect whether a file should have the "krb5" C coding style applied. Currently, it uses the combined presence of <code>c-basic-offset:&nbsp;4</code> and <code>indent-tabs-mode:&nbsp;nil</code> as a signal to use the "krb5" style. See [[Coding_style/Transition strategies]] for some details. Also, if you are newly adding the file-local variable settings line to a file, use <code>M-x&nbsp;normal-mode</code> to reinitialize cc-mode with the new settings.
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
 
 
You may want to turn on auto-newline feature of cc-mode, though that
'("bsd"
 
 
seems to have some bugs with brace-elseif-brace handling at least in
(c-cleanup-list
 
  +
old versions of cc-mode (Emacs 20.3 or so).
brace-elseif-brace brace-else-brace defun-close-semi)
 
(c-comment-continuation-stars . "* ")
 
(c-electric-pound-behavior alignleft)
 
(c-hanging-braces-alist
 
(brace-list-open)
 
(class-open after)
 
(substatement-open after)
 
(block-close . c-snug-do-while)
 
(extern-lang-open after))
 
(c-hanging-colons-alist
 
(case-label after)
 
(label after))
 
(c-hanging-comment-starter-p)
 
(c-hanging-comment-ender-p)
 
(c-indent-comments-syntactically-p . t)
 
(c-label-minimum-indentation . 0)
 
(c-special-indent-hook)))
 
(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):
 
You might also want to try (for Emacs 22 and later):
Line 105: Line 87:
 
reasonable approximation to the C coding style described here, though
 
reasonable approximation to the C coding style described here, though
 
some manual cleanup may be necessary. Note that the gindent installed
 
some manual cleanup may be necessary. Note that the gindent installed
in the gnu locker does not currently handle -psl correctly though.
+
in the gnu locker does not currently handle -nut or -psl correctly though.
   
 
<pre>
 
<pre>
Line 120: Line 102:
 
-nbc
 
-nbc
 
-ncdb
 
-ncdb
  +
-ncs
 
-ndj
 
-ndj
 
-nfc1
 
-nfc1
 
-lp
 
-lp
 
-npcs
 
-npcs
  +
-nut
 
-psl
 
-psl
 
-sc
 
-sc
 
-sob
 
-sob
  +
</pre>
  +
  +
=== vim/gvim editor settings ===
  +
These settings allow the vim or gvim editor to conform to the MITKC code style:
  +
  +
<pre>
  +
set shiftwidth=4
  +
set tabstop=8
  +
set softtabstop=4
  +
set expandtab
  +
set nosmartindent
  +
set cindent
  +
set cinoptions=p0,t0,+4,(0,u4,U1,:0
  +
set formatoptions=croq
  +
set comments=sr:/*,mb:*,ex:*/,://
  +
set textwidth=79
 
</pre>
 
</pre>

Latest revision as of 12:59, 2 February 2016

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

Load the elisp file src/util/krb5-c-style.el (raw | annotated | history) to get mostly the right thing to happen with respect to formatting style. krb5-c-style.el uses a heuristic to detect whether a file should have the "krb5" C coding style applied. Currently, it uses the combined presence of c-basic-offset: 4 and indent-tabs-mode: nil as a signal to use the "krb5" style. See Coding_style/Transition strategies for some details. Also, if you are newly adding the file-local variable settings line to a file, use M-x normal-mode to reinitialize cc-mode with the new settings.

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 old versions of cc-mode (Emacs 20.3 or so).

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.

-bap
-br
-ce
-ci4
-cli0
-d0
-di8
-i4
-ip4
-l79
-nbc
-ncdb
-ncs
-ndj
-nfc1
-lp
-npcs
-nut
-psl
-sc
-sob

vim/gvim editor settings

These settings allow the vim or gvim editor to conform to the MITKC code style:

set shiftwidth=4
set tabstop=8
set softtabstop=4
set expandtab
set nosmartindent
set cindent 
set cinoptions=p0,t0,+4,(0,u4,U1,:0
set formatoptions=croq
set comments=sr:/*,mb:*,ex:*/,://
set textwidth=79