Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed coding style for C, Linux++ #209

Merged
merged 6 commits into from
Nov 16, 2023
Merged

Proposed coding style for C, Linux++ #209

merged 6 commits into from
Nov 16, 2023

Conversation

troglobit
Copy link
Contributor

@troglobit troglobit commented Nov 15, 2023

The intention of this is to serve as a help/guidance for both devs and reviewers of pull requests.

Baseline is taken from Linux v6.5.6, with some (possibly) controversial additions (at the end of the file):

  • Do not enforce max line length
  • Alignment of variables, both when assigning and declaring
  • Use spaces to adjust when leading tabs are not enough (as Emacs)

I've also added a couple of exceptions for macros like TAILQ_FOREACH() which are used as control statements (if, while, for), so the opening brace should be on the same line -- otherwise clang-format thinks we are creating a recursive function ... 🙄

This PR also contains a set of small fixes/cleanup to confd and statd.

Emacs Users <3

Emacs users may want to set up format-all with use-package, it supports all common formatters. For some guidance, see https://ianyepan.github.io/posts/format-all/. Here's what I use myself:

(use-package format-all
  :preface
  (defun ian/format-code ()
    "Auto-format whole buffer."
    (interactive)
    (if (derived-mode-p 'prolog-mode)
        (prolog-indent-buffer)
      (format-all-buffer)))
  :config
  ;; (global-set-key (kbd "M-F") #'ian/format-code)
  (global-set-key (kbd "M-F") #'format-all-buffer)
  (add-hook 'prog-mode-hook #'format-all-ensure-formatter))

Wnich can be simplified to:

(use-package format-all
  :config
  (global-set-key (kbd "M-F") #'format-all-buffer)
  (add-hook 'prog-mode-hook #'format-all-ensure-formatter))

If you for some reason do not have use-package set up already, here's a quickie:

;; Bootstrap use-package if not installed, refresh remotes, install it.
;; https://github.com/jwiegley/use-package

(require 'package)
(add-to-list 'package-archives '("gnu"   . "https://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("org"   . "http://orgmode.org/elpa/"))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-and-compile
  (setq use-package-always-ensure t
	package-enable-at-startup nil
	use-package-expand-minimally t))

The intention of this is to serve as a help/guidance for both devs and
reviewers of pull requests.

Baseline is taken from Linux v6.5.6, with some (possibly) controversial
additions (at the end of the file):

 - Do not enforce max line length
 - Alignment of variables, both when assigning and declaring
 - Use spaces to adjust when leading tabs are not enough (as Emacs)

I've also added a couple of exceptions for macros like TAILQ_FOREACH()
which are used as control statments (if, while, for), so the opening
brace should be on the same line -- otherwise clang-format thinkts we
are creating a recursive function ...

Signed-off-by: Joachim Wiberg <[email protected]>
@troglobit troglobit added documentation Improvements or additions to documentation enhancement New feature or request labels Nov 15, 2023
@troglobit troglobit requested review from wkz and mattiaswal November 15, 2023 16:54
@troglobit troglobit mentioned this pull request Nov 15, 2023
Signed-off-by: Joachim Wiberg <[email protected]>
[skip ci]

Signed-off-by: Joachim Wiberg <[email protected]>
@troglobit
Copy link
Contributor Author

AFK review OK by @wkz and @mattiaswal, after removal of green goblins.

@troglobit troglobit merged commit e7c6f25 into main Nov 16, 2023
@troglobit troglobit deleted the c-coding-style branch November 16, 2023 09:35
@troglobit troglobit added this to the Infix v23.11 milestone Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants