reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
===================
Variable Names Plan
===================

.. contents::
   :local:

This plan is *provisional*. It is not agreed upon. It is written with the
intention of capturing the desires and concerns of the LLVM community, and
forming them into a plan that can be agreed upon.
The original author is somewhat naïve in the ways of LLVM so there will
inevitably be some details that are flawed. You can help - you can edit this
page (preferably with a Phabricator review for larger changes) or reply to the
`Request For Comments thread
<http://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html>`_.

Too Long; Didn't Read
=====================

Improve the readability of LLVM code.

Introduction
============

The current `variable naming rule
<../CodingStandards.html#name-types-functions-variables-and-enumerators-properly>`_
states:

  Variable names should be nouns (as they represent state). The name should be
  camel case, and start with an upper case letter (e.g. Leader or Boats).

This rule is the same as that for type names. This is a problem because the
type name cannot be reused for a variable name [*]_. LLVM developers tend to
work around this by either prepending ``The`` to the type name::

  Triple TheTriple;

... or more commonly use an acronym, despite the coding standard stating "Avoid
abbreviations unless they are well known"::

  Triple T;

The proliferation of acronyms leads to hard-to-read code such as `this
<https://github.com/llvm/llvm-project/blob/0a8bc14ad7f3209fe702d18e250194cd90188596/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp#L7445>`_::

  InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, IC,
                         &LVL, &CM);

Many other coding guidelines [LLDB]_ [Google]_ [WebKit]_ [Qt]_ [Rust]_ [Swift]_
[Python]_ require that variable names begin with a lower case letter in contrast
to class names which begin with a capital letter. This convention means that the
most readable variable name also requires the least thought::

  Triple triple;

There is some agreement that the current rule is broken [LattnerAgree]_
[ArsenaultAgree]_ [RobinsonAgree]_ and that acronyms are an obstacle to reading
new code [MalyutinDistinguish]_ [CarruthAcronym]_ [PicusAcronym]_. There are
some opposing views [ParzyszekAcronym2]_ [RicciAcronyms]_.

This work-in-progress proposal is to change the coding standard for variable
names to require that they start with a lower case letter.

.. [*] In `some cases
   <https://github.com/llvm/llvm-project/blob/8b72080d4d7b13072f371712eed333f987b7a18e/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp#L2727>`_
   the type name *is* reused as a variable name, but this shadows the type name
   and confuses many debuggers [DenisovCamelBack]_.

Variable Names Coding Standard Options
======================================

There are two main options for variable names that begin with a lower case
letter: ``camelBack`` and ``lower_case``. (These are also known by other names
but here we use the terminology from clang-tidy).

``camelBack`` is consistent with [WebKit]_, [Qt]_ and [Swift]_ while
``lower_case`` is consistent with [LLDB]_, [Google]_, [Rust]_ and [Python]_.

``camelBack`` is already used for function names, which may be considered an
advantage [LattnerFunction]_ or a disadvantage [CarruthFunction]_.

Approval for ``camelBack`` was expressed by [DenisovCamelBack]_
[LattnerFunction]_ [IvanovicDistinguish]_.
Opposition to ``camelBack`` was expressed by [CarruthCamelBack]_
[TurnerCamelBack]_.
Approval for ``lower_case`` was expressed by [CarruthLower]_
[CarruthCamelBack]_ [TurnerLLDB]_.
Opposition to ``lower_case`` was expressed by [LattnerLower]_.

Differentiating variable kinds
------------------------------

An additional requested change is to distinguish between different kinds of
variables [RobinsonDistinguish]_ [RobinsonDistinguish2]_ [JonesDistinguish]_
[IvanovicDistinguish]_ [CarruthDistinguish]_ [MalyutinDistinguish]_.

Others oppose this idea [HähnleDistinguish]_ [GreeneDistinguish]_
[HendersonPrefix]_.

A possibility is for member variables to be prefixed with ``m_`` and for global
variables to be prefixed with ``g_`` to distinguish them from local variables.
This is consistent with [LLDB]_. The ``m_`` prefix is consistent with [WebKit]_.

A variation is for member variables to be prefixed with ``m``
[IvanovicDistinguish]_ [BeylsDistinguish]_. This is consistent with [Mozilla]_.

Another option is for member variables to be suffixed with ``_`` which is
consistent with [Google]_ and similar to [Python]_. Opposed by
[ParzyszekDistinguish]_.

Reducing the number of acronyms
===============================

While switching coding standard will make it easier to use non-acronym names for
new code, it doesn't improve the existing large body of code that uses acronyms
extensively to the detriment of its readability. Further, it is natural and
generally encouraged that new code be written in the style of the surrounding
code. Therefore it is likely that much newly written code will also use
acronyms despite what the coding standard says, much as it is today.

As well as changing the case of variable names, they could also be expanded to
their non-acronym form e.g. ``Triple T`` → ``Triple triple``.

There is support for expanding many acronyms [CarruthAcronym]_ [PicusAcronym]_
but there is a preference that expanding acronyms be deferred
[ParzyszekAcronym]_ [CarruthAcronym]_.

The consensus within the community seems to be that at least some acronyms are
valuable [ParzyszekAcronym]_ [LattnerAcronym]_. The most commonly cited acronym
is ``TLI`` however that is used to refer to both ``TargetLowering`` and
``TargetLibraryInfo`` [GreeneDistinguish]_.

The following is a list of acronyms considered sufficiently useful that the
benefit of using them outweighs the cost of learning them. Acronyms that are
either not on the list or are used to refer to a different type should be
expanded.

============================ =============
Class name                   Variable name
============================ =============
DeterministicFiniteAutomaton dfa
DominatorTree                dt
LoopInfo                     li
MachineFunction              mf
MachineInstr                 mi
MachineRegisterInfo          mri
ScalarEvolution              se
TargetInstrInfo              tii
TargetLibraryInfo            tli
TargetRegisterInfo           tri
============================ =============

In some cases renaming acronyms to the full type name will result in overly
verbose code. Unlike most classes, a variable's scope is limited and therefore
some of its purpose can implied from that scope, meaning that fewer words are
necessary to give it a clear name. For example, in an optization pass the reader
can assume that a variable's purpose relates to optimization and therefore an
``OptimizationRemarkEmitter`` variable could be given the name ``remarkEmitter``
or even ``remarker``.

The following is a list of longer class names and the associated shorter
variable name.

========================= =============
Class name                Variable name
========================= =============
BasicBlock                block
ConstantExpr              expr
ExecutionEngine           engine
MachineOperand            operand
OptimizationRemarkEmitter remarker
PreservedAnalyses         analyses
PreservedAnalysesChecker  checker
TargetLowering            lowering
TargetMachine             machine
========================= =============

Transition Options
==================

There are three main options for transitioning:

1. Keep the current coding standard
2. Laissez faire
3. Big bang

Keep the current coding standard
--------------------------------

Proponents of keeping the current coding standard (i.e. not transitioning at
all) question whether the cost of transition outweighs the benefit
[EmersonConcern]_ [ReamesConcern]_ [BradburyConcern]_.
The costs are that ``git blame`` will become less usable; and that merging the
changes will be costly for downstream maintainers. See `Big bang`_ for potential
mitigations.

Laissez faire
-------------

The coding standard could allow both ``CamelCase`` and ``camelBack`` styles for
variable names [LattnerTransition]_.

A code review to implement this is at https://reviews.llvm.org/D57896.

Advantages
**********

 * Very easy to implement initially.

Disadvantages
*************

 * Leads to inconsistency [BradburyConcern]_ [AminiInconsistent]_.
 * Inconsistency means it will be hard to know at a guess what name a variable
   will have [DasInconsistent]_ [CarruthInconsistent]_.
 * Some large-scale renaming may happen anyway, leading to its disadvantages
   without any mitigations.

Big bang
--------

With this approach, variables will be renamed by an automated script in a series
of large commits.

The principle advantage of this approach is that it minimises the cost of
inconsistency [BradburyTransition]_ [RobinsonTransition]_.

It goes against a policy of avoiding large-scale reformatting of existing code
[GreeneDistinguish]_.

It has been suggested that LLD would be a good starter project for the renaming
[Ueyama]_.

Keeping git blame usable
************************

``git blame`` (or ``git annotate``) permits quickly identifying the commit that
changed a given line in a file. After renaming variables, many lines will show
as being changed by that one commit, requiring a further invocation of ``git
blame`` to identify prior, more interesting commits [GreeneGitBlame]_
[RicciAcronyms]_.

**Mitigation**: `git-hyper-blame
<https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html>`_
can ignore or "look through" a given set of commits.
A ``.git-blame-ignore-revs`` file identifying the variable renaming commits
could be added to the LLVM git repository root directory.
It is being `investigated
<https://public-inbox.org/git/[email protected]/>`_
whether similar functionality could be added to ``git blame`` itself.

Minimising cost of downstream merges
************************************

There are many forks of LLVM with downstream changes. Merging a large-scale
renaming change could be difficult for the fork maintainers.

**Mitigation**: A large-scale renaming would be automated. A fork maintainer can
merge from the commit immediately before the renaming, then apply the renaming
script to their own branch. They can then merge again from the renaming commit,
resolving all conflicts by choosing their own version. This could be tested on
the [SVE]_ fork.

Provisional Plan
================

This is a provisional plan for the `Big bang`_ approach. It has not been agreed.

#. Investigate improving ``git blame``. The extent to which it can be made to
   "look through" commits may impact how big a change can be made.

#. Write a script to expand acronyms.

#. Experiment and perform dry runs of the various refactoring options.
   Results can be published in forks of the LLVM Git repository.

#. Consider the evidence and agree on the new policy.

#. Agree & announce a date for the renaming of the starter project (LLD).

#. Update the `policy page <../CodingStandards.html>`_. This will explain the
   old and new rules and which projects each applies to.

#. Refactor the starter project in two commits:

   1. Add or change the project's .clang-tidy to reflect the agreed rules.
      (This is in a separate commit to enable the merging process described in
      `Minimising cost of downstream merges`_).
      Also update the project list on the policy page.
   2. Apply ``clang-tidy`` to the project's files, with only the
      ``readability-identifier-naming`` rules enabled. ``clang-tidy`` will also
      reformat the affected lines according to the rules in ``.clang-format``.
      It is anticipated that this will be a good dog-fooding opportunity for
      clang-tidy, and bugs should be fixed in the process, likely including:

        * `readability-identifier-naming incorrectly fixes lambda capture
          <https://bugs.llvm.org/show_bug.cgi?id=41119>`_.
        * `readability-identifier-naming incorrectly fixes variables which
          become keywords <https://bugs.llvm.org/show_bug.cgi?id=41120>`_.
        * `readability-identifier-naming misses fixing member variables in
          destructor <https://bugs.llvm.org/show_bug.cgi?id=41122>`_.

#. Gather feedback and refine the process as appropriate.

#. Apply the process to the following projects, with a suitable delay between
   each (at least 4 weeks after the first change, at least 2 weeks subsequently)
   to allow gathering further feedback.
   This list should exclude projects that must adhere to an externally defined
   standard e.g. libcxx.
   The list is roughly in chronological order of renaming.
   Some items may not make sense to rename individually - it is expected that
   this list will change following experimentation:

   * TableGen
   * llvm/tools
   * clang-tools-extra
   * clang
   * ARM backend
   * AArch64 backend
   * AMDGPU backend
   * ARC backend
   * AVR backend
   * BPF backend
   * Hexagon backend
   * Lanai backend
   * MIPS backend
   * NVPTX backend
   * PowerPC backend
   * RISC-V backend
   * Sparc backend
   * SystemZ backend
   * WebAssembly backend
   * X86 backend
   * XCore backend
   * libLTO
   * Debug Information
   * Remainder of llvm
   * compiler-rt
   * libunwind
   * openmp
   * parallel-libs
   * polly
   * lldb

#. Remove the old variable name rule from the policy page.

#. Repeat many of the steps in the sequence, using a script to expand acronyms.

References
==========

.. [LLDB] LLDB Coding Conventions https://llvm.org/svn/llvm-project/lldb/branches/release_39/www/lldb-coding-conventions.html
.. [Google] Google C++ Style Guide https://google.github.io/styleguide/cppguide.html#Variable_Names
.. [WebKit] WebKit Code Style Guidelines https://webkit.org/code-style-guidelines/#names
.. [Qt] Qt Coding Style https://wiki.qt.io/Qt_Coding_Style#Declaring_variables
.. [Rust] Rust naming conventions https://doc.rust-lang.org/1.0.0/style/style/naming/README.html
.. [Swift] Swift API Design Guidelines https://swift.org/documentation/api-design-guidelines/#general-conventions
.. [Python] Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/#function-and-variable-names
.. [Mozilla] Mozilla Coding style: Prefixes https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style#Prefixes
.. [SVE] LLVM with support for SVE https://github.com/ARM-software/LLVM-SVE
.. [AminiInconsistent] Mehdi Amini, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130329.html
.. [ArsenaultAgree] Matt Arsenault, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129934.html
.. [BeylsDistinguish] Kristof Beyls, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130292.html
.. [BradburyConcern] Alex Bradbury, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130266.html
.. [BradburyTransition] Alex Bradbury, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130388.html
.. [CarruthAcronym] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130313.html
.. [CarruthCamelBack] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130214.html
.. [CarruthDistinguish] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130310.html
.. [CarruthFunction] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130309.html
.. [CarruthInconsistent] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130312.html
.. [CarruthLower] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130430.html
.. [DasInconsistent] Sanjoy Das, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130304.html
.. [DenisovCamelBack] Alex Denisov, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130179.html
.. [EmersonConcern] Amara Emerson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129894.html
.. [GreeneDistinguish] David Greene, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130425.html
.. [GreeneGitBlame] David Greene, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130228.html
.. [HendersonPrefix] James Henderson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130465.html
.. [HähnleDistinguish] Nicolai Hähnle, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129923.html
.. [IvanovicDistinguish] Nemanja Ivanovic, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130249.html
.. [JonesDistinguish] JD Jones, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129926.html
.. [LattnerAcronym] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130353.html
.. [LattnerAgree] Chris Latter, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129907.html
.. [LattnerFunction] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130630.html
.. [LattnerLower] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130629.html
.. [LattnerTransition] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130355.html
.. [MalyutinDistinguish] Danila Malyutin, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130320.html
.. [ParzyszekAcronym] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130306.html
.. [ParzyszekAcronym2] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130323.html
.. [ParzyszekDistinguish] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129941.html
.. [PicusAcronym] Diana Picus, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130318.html
.. [ReamesConcern] Philip Reames, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130181.html
.. [RicciAcronyms] Bruno Ricci, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130328.html
.. [RobinsonAgree] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130111.html
.. [RobinsonDistinguish] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129920.html
.. [RobinsonDistinguish2] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130229.html
.. [RobinsonTransition] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130415.html
.. [TurnerCamelBack] Zachary Turner, https://reviews.llvm.org/D57896#1402264
.. [TurnerLLDB] Zachary Turner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130213.html
.. [Ueyama] Rui Ueyama, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130435.html