xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/readability/operators-representation.rst (revision a084854266ca60748982228a4c98d036bca5f762)
1.. title:: clang-tidy - readability-operators-representation
2
3readability-operators-representation
4====================================
5
6Enforces consistent token representation for invoked binary, unary and
7overloaded operators in C++ code. The check supports both traditional and
8alternative representations of operators, such as ``&&`` and ``and``, ``||``
9and ``or``, and so on.
10
11In the realm of C++ programming, developers have the option to choose between
12two distinct representations for operators: traditional token representation
13and alternative token representation. Traditional tokens utilize symbols,
14such as ``&&``, ``||``, and ``!``, while alternative tokens employ more
15descriptive words like ``and``, ``or``, and ``not``.
16
17In the following mapping table, a comprehensive list of traditional and
18alternative tokens, along with their corresponding representations,
19is presented:
20
21.. table:: Token Representation Mapping Table
22    :widths: auto
23
24    =========== ===========
25    Traditional Alternative
26    =========== ===========
27    ``&&``      ``and``
28    ``&=``      ``and_eq``
29    ``&``       ``bitand``
30    ``|``       ``bitor``
31    ``~``       ``compl``
32    ``!``       ``not``
33    ``!=``      ``not_eq``
34    ``||``      ``or``
35    ``|=``      ``or_eq``
36    ``^``       ``xor``
37    ``^=``      ``xor_eq``
38    =========== ===========
39
40Example
41-------
42
43.. code-block:: c++
44
45    // Traditional Token Representation:
46
47    if (!a||!b)
48    {
49        // do something
50    }
51
52    // Alternative Token Representation:
53
54    if (not a or not b)
55    {
56        // do something
57    }
58
59Options
60-------
61
62Due to the distinct benefits and drawbacks of each representation, the default
63configuration doesn't enforce either. Explicit configuration is needed.
64
65To configure check to enforce Traditional Token Representation for all
66operators set options to `&&;&=;&;|;~;!;!=;||;|=;^;^=`.
67
68To configure check to enforce Alternative Token Representation for all
69operators set options to
70`and;and_eq;bitand;bitor;compl;not;not_eq;or;or_eq;xor;xor_eq`.
71
72Developers do not need to enforce all operators, and can mix the representations
73as desired by specifying a semicolon-separated list of both traditional and
74alternative tokens in the configuration, such as `and;||;not`.
75
76.. option:: BinaryOperators
77
78    This option allows you to specify a semicolon-separated list of binary
79    operators for which you want to enforce specific token representation.
80    The default value is empty string.
81
82.. option:: OverloadedOperators
83
84    This option allows you to specify a semicolon-separated list of overloaded
85    operators for which you want to enforce specific token representation.
86    The default value is empty string.
87