<commit_before>============
Coding style
============

.. contents::

Naming conventions
==================

When reading libvirt code, a number of different naming
conventions will be evident due to various changes in thinking
over the course of the project's lifetime. The conventions
documented below should be followed when creating any entirely new
files in libvirt. When working on existing files, while it is
desirable to apply these conventions, keeping a consistent style
with existing code in that particular file is generally more
important. The overall guiding principal is that every file, enum,
struct, function, macro and typedef name must have a 'vir' or
'VIR' prefix. All local scope variable names are exempt, and
global variables are exempt, unless exported in a header file.

File names
   File naming varies depending on the subdirectory. The preferred
   style is to have a 'vir' prefix, followed by a name which
   matches the name of the functions / objects inside the file.
   For example, a file containing an object 'virHashtable' is
   stored in files 'virhashtable.c' and 'virhashtable.h'.
   Sometimes, methods which would otherwise be declared 'static'
   need to be exported for use by a test suite. For this purpose a
   second header file should be added with a suffix of 'priv',
   e.g. 'virhashtablepriv.h'. Use of underscores in file names is
   discouraged when using the 'vir' prefix style. The 'vir' prefix
   naming applies to src/util, src/rpc and tests/ directories.
   Most other directories do not follow this convention.

Enum type & field names
   All enums should have a 'vir' prefix in their typedef name, and
   each following word should have its first letter in uppercase.
   The enum name should match the typedef name with a leading
   underscore. The enum member names should be in all uppercase,
   and use an underscore to separate each word. The enum member
   name prefix should match the enum typedef name.

   ::

     typedef enum _virSocketType virSocketType;
     enum _virSocketType {
         VIR_SOCKET_TYPE_IPV4,
         VIR_SOCKET_TYPE_IPV6,
     };

Struct type names
   All structs should have a 'vir' prefix in their typedef name,
   and each following word should have its first letter in
   uppercase. The struct name should be the same as the typedef
   name with a leading underscore. A second typedef should be
   given for a pointer to the struct with a 'Ptr' suffix.

   ::

     typedef struct _virHashTable virHashTable;
     typedef virHashTable *virHashTablePtr;
     struct _virHashTable {
         ...
     };

Function names
   All functions should have a 'vir' prefix in their name,
   followed by one or more words with first letter of each word
   capitalized. Underscores should not be used in function names.
   If the function is operating on an object, then the function
   name prefix should match the object typedef name, otherwise it
   should match the filename. Following this comes the verb /
   action name, and finally an optional subject name. For example,
   given an object 'virHashTable', all functions should have a
   name 'virHashTable$VERB' or 'virHashTable$VERB$SUBJECT", e.g.
   'virHashTableLookup' or 'virHashTableGetValue'.

Macro names
   All macros should have a "VIR" prefix in their name, followed
   by one or more uppercase words separated by underscores. The
   macro argument names should be in lowercase. Aside from having
   a "VIR" prefix there are no common practices for the rest of
   the macro name.

Code indentation
================

Libvirt's C source code generally adheres to some basic
code-formatting conventions. The existing code base is not totally
consistent on this front, but we do prefer that contributed code
be formatted similarly. In short, use spaces-not-TABs for
indentation, use 4 spaces for each indentation level, and other
than that, follow the K&R style.

If you use Emacs, the project includes a file .dir-locals.el that
sets up the preferred indentation. If you use vim, append the
following to your ~/.vimrc file:

::

  set nocompatible
  filetype on
  set autoindent
  set smartindent
  set cindent
  set tabstop=8
  set shiftwidth=4
  set expandtab
  set cinoptions=(0,:0,l1,t0,L3
  filetype plugin indent on
  au FileType make setlocal noexpandtab
  au BufRead,BufNewFile *.am setlocal noexpandtab
  match ErrorMsg /\s\+$\| \+\ze\t/

Or if you don't want to mess your ~/.vimrc up, you can save the
above into a file called .lvimrc (not .vimrc) located at the root
of libvirt source, then install a vim script from
https://www.vim.org/scripts/script.php?script_id=1408, which will
load the .lvimrc only when you edit libvirt code.

Code formatting (especially for new code)
=========================================

With new code, we can be even more strict. Please apply the
following function (using GNU indent) to any new code. Note that
this also gives you an idea of the type of spacing we prefer
around operators and keywords:

::

  indent-libvirt()
  {
    indent -bad -bap -bbb -bli4 -br -ce -brs -cs -i4 -l75 -lc75 \
           -sbi4 -psl -saf -sai -saw -sbi4 -ss -sc -cdw -cli4 -npcs -nbc \
           --no-tabs "$@"
  }

Note that sometimes you'll have to post-process that output
further, by piping it through ``expand -i``, since some leading
TABs can get through. Usually they're in macro definitions or
strings, and should be converted anyhow.

Libvirt requires a C99 compiler for various reasons. However, most
of the code base prefers to stick to C89 syntax unless there is a
compelling reason otherwise. For example, it is preferable to use
``/* */`` comments rather than ``//``. Also, when declaring local
variables, the prevailing style has been to declare them at the
beginning of a scope, rather than immediately before use.

Bracket spacing
---------------

The keywords ``if``, ``for``, ``while``, and ``switch`` must have
a single space following them before the opening bracket. E.g.

::

  if(foo)   // Bad
  if (foo)  // Good

Function implementations must **not** have any whitespace between
the function name and the opening bracket. E.g.

::

  int foo (int wizz)  // Bad
  int foo(int wizz)   // Good<commit_msg>coding-style: Document 100 chars limit for line length<commit_after>============
Coding style
============

.. contents::

Naming conventions
==================

When reading libvirt code, a number of different naming
conventions will be evident due to various changes in thinking
over the course of the project's lifetime. The conventions
documented below should be followed when creating any entirely new
files in libvirt. When working on existing files, while it is
desirable to apply these conventions, keeping a consistent style
with existing code in that particular file is generally more
important. The overall guiding principal is that every file, enum,
struct, function, macro and typedef name must have a 'vir' or
'VIR' prefix. All local scope variable names are exempt, and
global variables are exempt, unless exported in a header file.

File names
   File naming varies depending on the subdirectory. The preferred
   style is to have a 'vir' prefix, followed by a name which
   matches the name of the functions / objects inside the file.
   For example, a file containing an object 'virHashtable' is
   stored in files 'virhashtable.c' and 'virhashtable.h'.
   Sometimes, methods which would otherwise be declared 'static'
   need to be exported for use by a test suite. For this purpose a
   second header file should be added with a suffix of 'priv',
   e.g. 'virhashtablepriv.h'. Use of underscores in file names is
   discouraged when using the 'vir' prefix style. The 'vir' prefix
   naming applies to src/util, src/rpc and tests/ directories.
   Most other directories do not follow this convention.

Enum type & field names
   All enums should have a 'vir' prefix in their typedef name, and
   each following word should have its first letter in uppercase.
   The enum name should match the typedef name with a leading
   underscore. The enum member names should be in all uppercase,
   and use an underscore to separate each word. The enum member
   name prefix should match the enum typedef name.

   ::

     typedef enum _virSocketType virSocketType;
     enum _virSocketType {
         VIR_SOCKET_TYPE_IPV4,
         VIR_SOCKET_TYPE_IPV6,
     };

Struct type names
   All structs should have a 'vir' prefix in their typedef name,
   and each following word should have its first letter in
   uppercase. The struct name should be the same as the typedef
   name with a leading underscore. A second typedef should be
   given for a pointer to the struct with a 'Ptr' suffix.

   ::

     typedef struct _virHashTable virHashTable;
     typedef virHashTable *virHashTablePtr;
     struct _virHashTable {
         ...
     };

Function names
   All functions should have a 'vir' prefix in their name,
   followed by one or more words with first letter of each word
   capitalized. Underscores should not be used in function names.
   If the function is operating on an object, then the function
   name prefix should match the object typedef name, otherwise it
   should match the filename. Following this comes the verb /
   action name, and finally an optional subject name. For example,
   given an object 'virHashTable', all functions should have a
   name 'virHashTable$VERB' or 'virHashTable$VERB$SUBJECT", e.g.
   'virHashTableLookup' or 'virHashTableGetValue'.

Macro names
   All macros should have a "VIR" prefix in their name, followed
   by one or more uppercase words separated by underscores. The
   macro argument names should be in lowercase. Aside from having
   a "VIR" prefix there are no common practices for the rest of
   the macro name.

Code indentation
================

Libvirt's C source code generally adheres to some basic
code-formatting conventions. The existing code base is not totally
consistent on this front, but we do prefer that contributed code
be formatted similarly. In short, use spaces-not-TABs for
indentation, use 4 spaces for each indentation level, and other
than that, follow the K&R style.

If you use Emacs, the project includes a file .dir-locals.el that
sets up the preferred indentation. If you use vim, append the
following to your ~/.vimrc file:

::

  set nocompatible
  filetype on
  set autoindent
  set smartindent
  set cindent
  set tabstop=8
  set shiftwidth=4
  set expandtab
  set cinoptions=(0,:0,l1,t0,L3
  filetype plugin indent on
  au FileType make setlocal noexpandtab
  au BufRead,BufNewFile *.am setlocal noexpandtab
  match ErrorMsg /\s\+$\| \+\ze\t/

Or if you don't want to mess your ~/.vimrc up, you can save the
above into a file called .lvimrc (not .vimrc) located at the root
of libvirt source, then install a vim script from
https://www.vim.org/scripts/script.php?script_id=1408, which will
load the .lvimrc only when you edit libvirt code.

Code formatting (especially for new code)
=========================================

With new code, we can be even more strict. Please apply the
following function (using GNU indent) to any new code. Note that
this also gives you an idea of the type of spacing we prefer
around operators and keywords:

::

  indent-libvirt()
  {
    indent -bad -bap -bbb -bli4 -br -ce -brs -cs -i4 -l100 -lc100 \
           -sbi4 -psl -saf -sai -saw -sbi4 -ss -sc -cdw -cli4 -npcs -nbc \
           --no-tabs "$@"
  }

Note that sometimes you'll have to post-process that output
further, by piping it through ``expand -i``, since some leading
TABs can get through. Usually they're in macro definitions or
strings, and should be converted anyhow.

The maximum permitted line length is 100 characters, but lines
should aim to be approximately 80 characters.

Libvirt requires a C99 compiler for various reasons. However, most
of the code base prefers to stick to C89 syntax unless there is a
compelling reason otherwise. For example, it is preferable to use
``/* */`` comments rather than ``//``. Also, when declaring local
variables, the prevailing style has been to declare them at the
beginning of a scope, rather than immediately before use.

Bracket spacing
---------------

The keywords ``if``, ``for``, ``while``, and ``switch`` must have
a single space following them before the opening bracket. E.g.

::

  if(foo)   // Bad
  if (foo)  // Good

Function implementations must **not** have any whitespace between
the function name and the opening bracket. E.g.

::

  int foo (int wizz)  // Bad
  int foo(int wizz)   // Good
{'fraction_non_alphanumeric': 0.07321925984878631, 'fraction_numerical': 0.005093513728611222, 'mean_word_length': 3.576110706482156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '314271', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3330, 'n_words': 1807}