Patchwork Add astyle option file for code formatting

login
register
mail settings
Submitter Stefan Weil
Date Sept. 11, 2011, 5 p.m.
Message ID <1315760411-26359-1-git-send-email-weil@mail.berlios.de>
Download mbox | patch
Permalink /patch/114238/
State New
Headers show

Comments

Stefan Weil - Sept. 11, 2011, 5 p.m.
For new C code (and maybe also for existing code), a code formatter
like Artistic Style is helpful because it can fix tabs, indentation
and other style issues.

This option file tries to set astyle options which match QEMU's
coding conventions.

The quality of astyle's code formatting depends on the astyle version
(I got best results with newer versions) and on the code which is going
to be formatted.

For some code, formatting of brackets and operators does not work well.
Fixing indentation and tabs always worked well for me.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 scripts/astylerc |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
 create mode 100644 scripts/astylerc
Anthony Liguori - Sept. 16, 2011, 1:07 p.m.
On 09/11/2011 12:00 PM, Stefan Weil wrote:
> For new C code (and maybe also for existing code), a code formatter
> like Artistic Style is helpful because it can fix tabs, indentation
> and other style issues.
>
> This option file tries to set astyle options which match QEMU's
> coding conventions.
>
> The quality of astyle's code formatting depends on the astyle version
> (I got best results with newer versions) and on the code which is going
> to be formatted.
>
> For some code, formatting of brackets and operators does not work well.
> Fixing indentation and tabs always worked well for me.
>
> Signed-off-by: Stefan Weil<weil@mail.berlios.de>
> ---
>   scripts/astylerc |   16 ++++++++++++++++
>   1 files changed, 16 insertions(+), 0 deletions(-)
>   create mode 100644 scripts/astylerc
>
> diff --git a/scripts/astylerc b/scripts/astylerc
> new file mode 100644
> index 0000000..dcf9e3f
> --- /dev/null
> +++ b/scripts/astylerc
> @@ -0,0 +1,16 @@
> +# Artistic Style (astyle) options for qemu source code.
> +
> +# Usage:
> +# astyle --options=scripts/astylerc {source files}
> +
> +# For best results, use latest astyle from http://astyle.sourceforge.net/.

Please put a copyright in this file.

FWIW, this is what I use with emacs:

(c-add-style "qemu"
         '("stroustrup"
                 (indent-tabs-mode . nil)
                 (c-basic-offset   . 4)
                 (tab-width . 8)
         )
         nil) ; t = set this style, nil = don't

Regards,

Anthony Liguori

> +
> +add-brackets
> +align-pointer=name
> +convert-tabs
> +style=otbs
> +brackets=linux
> +indent=spaces=4
> +pad-oper
> +pad-header
> +unpad-paren
Stefan Weil - Sept. 16, 2011, 2:31 p.m.
Am 16.09.2011 15:07, schrieb Anthony Liguori:
> On 09/11/2011 12:00 PM, Stefan Weil wrote:
>> For new C code (and maybe also for existing code), a code formatter
>> like Artistic Style is helpful because it can fix tabs, indentation
>> and other style issues.
>>
>> This option file tries to set astyle options which match QEMU's
>> coding conventions.
>>
>> The quality of astyle's code formatting depends on the astyle version
>> (I got best results with newer versions) and on the code which is going
>> to be formatted.
>>
>> For some code, formatting of brackets and operators does not work well.
>> Fixing indentation and tabs always worked well for me.
>>
>> Signed-off-by: Stefan Weil<weil@mail.berlios.de>
>> ---
>>   scripts/astylerc |   16 ++++++++++++++++
>>   1 files changed, 16 insertions(+), 0 deletions(-)
>>   create mode 100644 scripts/astylerc
>>
> Please put a copyright in this file.
>
> FWIW, this is what I use with emacs:
>
> (c-add-style "qemu"
>         '("stroustrup"
>                 (indent-tabs-mode . nil)
>                 (c-basic-offset   . 4)
>                 (tab-width . 8)
>         )
>         nil) ; t = set this style, nil = don't
>
> Regards,
>
> Anthony Liguori

Are you sure that you want a copyright even for simple configuration
files like this one? I don't think this file could be copyrighted in
my country because it is too simple.

There are more complex files in scripts without any copyright:

scripts/create_config
scripts/hxtool
scripts/make_device_config.sh
scripts/qemu-binfmt-conf.sh

If it is common understanding that every file should have a copyright,
this rule should be added to CODING_STYLE.

I don't mind adding a line "# public domain", nor do I mind if it
is committed as it is.

Regards,
Stefan Weil

PS. It might help some people if you would commit your emacs style, too.
Markus Armbruster - Sept. 19, 2011, 8:01 a.m.
Anthony Liguori <anthony@codemonkey.ws> writes:

[...]
> FWIW, this is what I use with emacs:
>
> (c-add-style "qemu"
>         '("stroustrup"
>                 (indent-tabs-mode . nil)
>                 (c-basic-offset   . 4)
>                 (tab-width . 8)
>         )
>         nil) ; t = set this style, nil = don't

Here's my configuration (excuse liberal use of sledgehammer):

(setq c-initialization-hook
      (lambda()
	(setcar (member '(other . "gnu") c-default-style)
		(cons 'other "stroustrup"))))
(set-default 'c-recognize-knr-p nil)
(setq c-electric-pound-behavior '(alignleft))

(defun linux-c-mode ()
  "C mode with adjusted defaults for use with the Linux kernel."
  (interactive)
  (c-mode)
  (c-set-style "K&R")
  (setq tab-width 8)
  (setq indent-tabs-mode t)
  (setq c-basic-offset 8))

(setq auto-mode-alist
      (cons '("/linux.*\\.[ch]\\'" . linux-c-mode)
	    auto-mode-alist))

;; avoid tabs for some projects
(defun my-choose-tabs ()
  (let ((fname (buffer-file-name)))
    (and fname (string-match "/qemu" fname)
	 (setq indent-tabs-mode nil))))
(add-hook 'after-change-major-mode-hook 'my-choose-tabs)
Peter Maydell - Sept. 19, 2011, 8:38 a.m.
On 19 September 2011 09:01, Markus Armbruster <armbru@redhat.com> wrote:
> Anthony Liguori <anthony@codemonkey.ws> writes:
>> FWIW, this is what I use with emacs:
>
> Here's my configuration (excuse liberal use of sledgehammer):

Anybody seeking yet a third emacs configuration can try

https://wiki.linaro.org/PeterMaydell/QemuEmacsStyle

:-)

-- PMM

Patch

diff --git a/scripts/astylerc b/scripts/astylerc
new file mode 100644
index 0000000..dcf9e3f
--- /dev/null
+++ b/scripts/astylerc
@@ -0,0 +1,16 @@ 
+# Artistic Style (astyle) options for qemu source code.
+
+# Usage:
+# astyle --options=scripts/astylerc {source files}
+
+# For best results, use latest astyle from http://astyle.sourceforge.net/.
+
+add-brackets
+align-pointer=name
+convert-tabs
+style=otbs
+brackets=linux
+indent=spaces=4
+pad-oper
+pad-header
+unpad-paren