Summary:
Following all relevant laws is of utmost importance for an emulation project like Dolphin.
If you know any confidential information related to the GameCube, Wii, or Triforce, either because you signed a non-disclosure agreement or because you looked at leaked materials, we ask that you don't contribute code to Dolphin at all. While accepting code from contributors who know confidential information is legal if the code is unrelated to the confidential information, we refuse to accept code from such contributors because it greatly increases our review burden and increases the legal risk we take.
Also, this probably goes without saying, but piracy is strictly forbidden both on GitHub and in all other Dolphin channels.
If you make any contributions to Dolphin after December 1st, 2014, you are agreeing that any code you have contributed will be licensed under the GNU GPL version 2 (or any later version).
Summary:
This guide is for developers who wish to contribute to the Dolphin codebase. It will detail how to properly style and format code to fit this project. This guide also offers suggestions on specific functions and other varia that may be used in code.
Following this guide and formatting your code as detailed will likely get your pull request merged much faster than if you don't (assuming the written code has no mistakes in itself).
This project uses clang-format (stable branch) to check for common style issues. In case of conflicts between this guide and clang-format rules, the latter should be followed instead of this guide.
Windows users need to be careful about line endings. Windows users should configure git to checkout UNIX-style line endings to keep clang-format simple.
In most cases, clang-format can and should be used to automatically reformat code and solve most formatting issues.
To run clang-format on all staged files:
git diff --cached --name-only | egrep '[.](cpp|h|mm)$' | xargs clang-format -i
Formatting issues can be checked for before committing with a lint script that is included with the codebase. To enable it as a pre-commit hook (assuming you are in the repository root):
ln -s ../../Tools/lint.sh .git/hooks/pre-commit
Alternatively, a custom git filter driver can be used to automatically and transparently reformat any changes:
git config filter.clang_format.smudge 'cat'
git config filter.clang_format.clean 'clang-format %f'
echo '/Source/Core/**/*.cpp filter=clang_format' >> .git/info/attributes
echo '/Source/Core/**/*.h filter=clang_format' >> .git/info/attributes
echo '/Source/Core/**/*.mm filter=clang_format' >> .git/info/attributes
Visual Studio supports automatically formatting the current document according to the clang-format configuration by pressing Control+K followed by Control+D (or selecting Edit → Advanced → Format Document). This can be used without separately installing clang-format.
Summary:
int* var
, not int *var
./* Comment text */
), use single-line comments (// Comment text
) instead.Don't collapse single line conditional or loop bodies onto the same line as its header. Put it on the next line.
if (condition)
return 0;
while (var != 0)
var--;
if (condition) return 0;
while (var != 0) var--;
class SomeClassName
enum IPCCommandType
constexpr double PI = 3.14159;
constexpr int MAX_PATH = 260;
int this_variable_name;
g_
m_
s_
Do not leave else
or else if
conditions dangling unless the if
condition lacks braces.
if (condition)
{
// code
}
else
{
// code
}
if (condition)
// code line
else
// code line
if (condition)
{
// code
}
else
// code line
struct
for this. Use a class
otherwise.public
, protected
, and then private
.
static
variables before the non-static ones.class ExampleClass : public SomeParent
{
public:
ExampleClass(int x, int y);
int GetX() const;
int GetY() const;
protected:
virtual void SomeProtectedFunction() = 0;
static float s_some_variable;
private:
int m_x;
int m_y;
};
Summary:
NULL
.goto
unless you have a really good reason for it.new
) as much as possible. There are cases where using a raw pointer is unavoidable, and in these situations it is OK to use them. An example of this is functions from a C library that require them. In cases where it is avoidable, the STL usually has a means to solve this (vector
, unique_ptr
, etc).auto
keyword everywhere. While it's nice that the type can be determined by the compiler, it cannot be resolved at 'readtime' by the developer as easily. Use auto only in cases where it is obvious what the type being assigned is (note: 'obvious' means not having to open other files or reading the header file). Some situations where it is appropriate to use auto
is when iterating over a std::map
container in a foreach loop, or to shorten the length of container iterator variable declarations.using namespace [x];
in headers. Try not to use it at all if you can.++var
).#ifdef
block unless the source file itself is system-specific).[Dolphin Root]/Source/Core
directory.#pragma once
as header guards.for (;;)
, use while (true)
.while (condition) {}
while (condition);
do
{
// code
} while (false);
const
.Functions that specifically modify their parameters should have the respective parameter(s) marked as a pointer so that the variables being modified are syntaxically obvious.
template<class T>
inline void Clamp(T& val, const T& min, const T& max)
{
if (val < min)
val = min;
else if (val > max)
val = max;
}
Example call: Clamp(var, 1000, 5000);
template<class T>
inline void Clamp(T* val, const T& min, const T& max)
{
if (*val < min)
*val = min;
else if (*val > max)
*val = max;
}
Example call: Clamp(&var, 1000, 5000);
Class member functions that you do not want to be overridden in inheriting classes should be marked with the final
specifier.
class ClassName : ParentClass
{
public:
void Update() final;
};
override
specifier to make it easier to see which functions belong to the parent class. class ClassName : ParentClass
{
public:
void Update() override;
};
final
specifier. class ClassName final : ParentClass
{
// Class definitions
};
If you are using Kotlin, just use the built-in official Kotlin code style.
To install the Java code style in Android Studio, select the gear icon in the Code Style settings as shown, select Import Scheme...
and select dolphin/Source/Android/code-style-java.xml
. The Code Style menu should look like this when complete.
You can now select any section of code and press Ctrl + Alt + L
to automatically format it.
If you have any questions about Dolphin's development or would like some help, Dolphin developers use #dolphin-emu @ irc.libera.chat
to communicate. If you are new to IRC, Libera.Chat has resources to get started chatting with IRC.