Automatically Indenting C++ progams
There are three popular ways of indenting C programs.
- cb
- is a program which does simple indenting, but is not very
tailorable. It also indents with tabs, so you must post
process with a program like expand to replace tabs
with the appropriate number of spaces.
- indent
- is a very flexible indenting tool, with lots of options about
how indenting is performed.
indent -bad -bap -nbc -br -nce -i4
or
indent -bad -bap -nbc -bl -nce -i4
are commands which approxomate our reccomended FPCL coding
standards.
- emacs
- is an editor with on-the-fly formatting. can add a phrase like:
(defun my-c-hook nil "Simple function to setup cc-mode"
(c-toggle-auto-hungry-state 1)
(set-variable 'c-file-style "Stroustrup")
)
(add-hook 'c-mode-common-hook 'my-c-hook)
to their .emacs file, or add a block like
// Local Variables:
// mode: c++
// c-file-style: "Stroustrup"
// End:
to their C++ source files to set a particular indentation style.
More information on these tools can be found in the docuementaion
for
cb,
indent, and
emacs
on the web.
Avaliabilty
- free source implementations of
indent
and
emacs
are available.
- cb and indent come with the SUNWSpro compiler suite
from Sun
Reccomendations
Groups doing C++ development work should choose formatting conventions
that can be enforced by automated tools like indent and emacs,
and should provide .indent.pro files for indent and parameter
comment blocks for emacs to enforce their coding style.
Groups using cvs may want to automatically apply a formatter program
on source being checked into a code repository.
We may want to reccomend that Computing Division provide binary
versions of indent for our supported platforms.