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

Update C++ section of contributing guide and include .clang-format #90

Merged
merged 8 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
Language: Cpp
BasedOnStyle: Google

ColumnLimit: 120
arcondello marked this conversation as resolved.
Show resolved Hide resolved
NamespaceIndentation: Inner

# Scaled by a factor of 2 such that the base indent is 4
AccessModifierOffset: -2
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
arcondello marked this conversation as resolved.
Show resolved Hide resolved
IndentWidth: 4
...
82 changes: 40 additions & 42 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,57 +134,55 @@ Documentation
C++
---

.clang-format
~~~~~~~~~~~~~

* When starting a new C++ project, copy the .clang-format file included here.
* Our style is based on Google (as opposed to LLVM, Chromium, Mozilla, or Webkit) with minor differences.
* :code:`ColumnLimit` is set to :code:`120`, as specified in `Coding Conventions`_.
* :code:`NamespaceIndentation` is set to :code:`Inner` as a middle ground between :code:`None` (Google) and :code:`All`,
such that every line in a file defining a namespace isn't indented, but nested namespaces are easily spotted.
* Various indent-width specifiers are scaled by a factor of 2 such that the base indent is :code:`4`, as specified in
`Coding Conventions`_, instead of :code:`2` (Google). This is especially helpful for readibility in cases like

.. code-block:: c++

if (condition) {
foo();
} else {
bar();
}

as opposed to
C++ Version
~~~~~~~~~~~

.. code-block:: c++
C++ code should be compatible with standard C++11.

if (condition) {
foo();
} else {
bar();
}
Naming
~~~~~~

Additional Style
~~~~~~~~~~~~~~~~
* File names should be lowercase with underscores or dashes and end with ``.cpp`` or ``.hpp``.
* Namespaces should be lowercase with underscores.
* Class names should be PascalCase; i.e., :code:`AdjArrayBQM`.
* Type aliases may follow other naming coventions to be more like the standard library; i.e., :code:`MyVector::value_type`
* Function names should be lowercase with underscores. i.e. :code:`num_variables()`.
* Variable names should be lowercase with underscores. Private data members should have a trailing underscore.
* Global variable names should be ``ALL_CAPS_WITH_UNDERSCORES``.
* Macros should be ``ALL_CAPS_WITH_UNDERSCORES``.

Favor the use of the optional braces for single-line control statements, which enhance consistency and extensibility.
Format
~~~~~~

Example:
* When starting a new C++ project, use clang-format with the `.clang-format <.clang-format>`_ file included here.
* Our format is based on `Google C++ style guide <https://google.github.io/styleguide/cppguide.html>`_ with some exceptions:

Use the following format
- The naming conventions are as stated above.
- Column width is limited to 120 characters. Best effort should be made to keep to 80 characters, but up to 120 can be used for clarity.
- Indent widths are doubled so the base indent level is 4 spaces, line continuations indent 8 spaces, and access modifiers indent 2 spaces.
arcondello marked this conversation as resolved.
Show resolved Hide resolved

Example Code
~~~~~~~~~~~~

.. code-block:: c++

// example_project/src/my_class.cpp
namespace example_project {
int GLOBAL_MATRIX_OF_INTS[2][2] = {{1, 2}, // Arrays representing matricies may be formatted as such.
{3, 4}};

template <typename T, typename IntType = bool>
class MyClass {
public:
using value_type = T;
value_type* y = nullptr;
value_type& find_element(int& argument) { return *(y + GLOBAL_MATRIX_OF_INTS[x_][argument++]); }

private:
IntType x_;
};
} // namespace example_project

if (a) {
return;
}

as opposed to

.. code-block:: c++

if (a) return;

This could potentially be enforced by :code:`clang-tidy`.

Versioning Scheme
-----------------
Expand Down