Hongxu Chen
Hongxu Chen

Reputation: 5350

Google c style settings for gnu indent?

I am using google c indent style for Emacs (google-c-style.el) and Vim(google.vim).

But since I have some existing code that is not this style and I hope I can change it. I find there is a tool called GNU indent that can do such thing automatically and it provides some common style settings on this page, however there is no for Google c indent style. SO is there equivalent for it as well?

(I tried the Linux and Berkley style and feel that they are by no means satisfactory for me)

Upvotes: 11

Views: 9071

Answers (2)

Hongxu Chen
Hongxu Chen

Reputation: 5350

For the record, there is an alternate solution for those who are interested in Clang and LLVM.

clang-format definitely can help format existing source code easily and efficiently. It has explicit builtin support for 5 format, namely LLVM(default), Google, Chromium, Mozilla, WebKit.

The simples way to format a file with Google style is:

clang-format -style=Google -i filename

Where -i means inplace modification, you may try without this option to preview the changes.

To batch format existing C/C++ code we can simply use the command like:

find . -name "*.cc" | xargs clang-format -style=Google -i

Apart from the listed 5 formats, there are actually other styles like GNU(added on revision 197138; it's a pity that the document is not synced).

Note that clang-format accepts rc like files named .clang-format or _clang-format in a project, the simplest way to add such a configuration file(as said in clang-format's official tutorial page) is to dump the configuration of an existing format like:

clang-format -style=Google -dump-config >.clang-format

Also you might also use BasedOnStyle option so a configuration file might look like:

---
BasedOnStyle:  Chromium
PointerBindsToType: false
ObjCSpaceAfterProperty: true
...

Use .clang-format or _clang-format as keywords to search on Github and there are other samples; or you might refer to this site to help build one.

There are also integrations for IDEs/Editors such as Visual Studio(in directory clang-format-vs), Sublime, Emacs, Vim(all in directory clang-format).

Another 3 tips:

  1. For Emacs integration(clang-format.el), personally I think it's better to bind key for clang-format-buffer rather than clang-format-region.

  2. For Mac OSX homebrew installation, use brew install --with-clang, --with-lld, --with-python --HEAD llvm can get clang-format support and its integration files are in $(brew --cache)/llvm--clang--svn-HEAD/tools/clang-format(bonus: there is even a git-clang-format there!!).

  3. There are other fabulous tools inside clang-extra-tools such as clang-modernize(which is used to "automatically convert C++ code written against old standards to use features of the newest C++ standard where appropriate"), really worthy of having a try!

Upvotes: 13

jxh
jxh

Reputation: 70382

A brief reading of the google coding style shows that it is mostly K&R coding style, except with 2 space indentation (including case statements), 80 column lines, and no tabs. So, the following options should accomplish that:

-kr -ci2 -cli2 -i2 -l80 -nut

Start with that. You may have to tweak the resulting code. C++ support, in particular, is weak for indent.

Legend:

  • -kr: K&R style
  • -ci2: Continuation indent, the lines following the first line of a multi-line code statement get indented by 2 spaces
  • -cli2: Case label indent, case labels are indented 2 spaces in from the switch
  • -i2: Indentation, 2 spaces
  • -l80: Length, 80 columns
  • -nut: No tabs

As an alternative, you may consider executing emacs in batch mode to apply indentation on your code for you. Briefly:

Create a file called emacs-format-file with the contents:

(defun emacs-format-function ()
   "Format the whole buffer."
   (c-set-style "Google")
   (indent-region (point-min) (point-max) nil)
   (untabify (point-min) (point-max))
   (save-buffer))

Execute the following command from the shell:

emacs -batch your_source_file.c \
    -l emacs-format-file -f emacs-format-function

Upvotes: 11

Related Questions