Skip to contentSkip to conversation

Add astyle option file for code formatting

PATCH In review Stefan Weil 4 participants 5 messages Sep 11, 2011 – Sep 19, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add astyle option file for code formatting

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.

---
 scripts/astylerc |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
 create mode 100644 scripts/astylerc
scripts/astylerc new file +16 -0
new file mode 100644
index 0000000..dcf9e3f
@@ -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
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 26 lines · Stefan Weil

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.

---
  scripts/astylerc |   16 ++++++++++++++++
  1 files changed, 16 insertions(+), 0 deletions(-)

create mode 100644 scripts/astylerc

scripts/astylerc new file +0 -0
new file mode 100644
index 0000000..dcf9e3f

scripts/astylerc line 6

Quoted 6 lines
scripts/astylerc new file +6 -0
@@ -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

Quoted 10 lines
scripts/astylerc new file +10 -0
+
+add-brackets
+align-pointer=name
+convert-tabs
+style=otbs
+brackets=linux
+indent=spaces=4
+pad-oper
+pad-header
+unpad-paren
#2 lore raw

Am 16.09.2011 15:07, schrieb Anthony Liguori:

Quoted 33 lines · Stefan Weil
Quoted 11 lines

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.

Quoted 9 lines

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

---
  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
Quoted 3 lines

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.

#3 lore raw
In reply to Anthony Liguori

Anthony Liguori <[email protected]> writes:

[...]

Quoted 9 lines

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)