400 lines · plain
1===================2Variable Names Plan3===================4 5.. contents::6 :local:7 8This plan is *provisional*. It is not agreed upon. It is written with the9intention of capturing the desires and concerns of the LLVM community, and10forming them into a plan that can be agreed upon.11The original author is somewhat naïve in the ways of LLVM so there will12inevitably be some details that are flawed. You can help - you can edit this13page (preferably with a Phabricator review for larger changes) or reply to the14`Request For Comments thread15<http://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html>`_.16 17Too Long; Didn't Read18=====================19 20Improve the readability of LLVM code.21 22Introduction23============24 25The current `variable naming rule26<../CodingStandards.html#name-types-functions-variables-and-enumerators-properly>`_27states:28 29 Variable names should be nouns (as they represent state). The name should be30 camel case, and start with an upper case letter (e.g. Leader or Boats).31 32This rule is the same as that for type names. This is a problem because the33type name cannot be reused for a variable name [*]_. LLVM developers tend to34work around this by either prepending ``The`` to the type name::35 36 Triple TheTriple;37 38... or more commonly use an acronym, despite the coding standard stating "Avoid39abbreviations unless they are well known"::40 41 Triple T;42 43The proliferation of acronyms leads to hard-to-read code such as `this44<https://github.com/llvm/llvm-project/blob/0a8bc14ad7f3209fe702d18e250194cd90188596/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp#L7445>`_::45 46 InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, IC,47 &LVL, &CM);48 49Many other coding guidelines [LLDB]_ [Google]_ [WebKit]_ [Qt]_ [Rust]_ [Swift]_50[Python]_ require that variable names begin with a lower case letter in contrast51to class names which begin with a capital letter. This convention means that the52most readable variable name also requires the least thought::53 54 Triple triple;55 56There is some agreement that the current rule is broken [LattnerAgree]_57[ArsenaultAgree]_ [RobinsonAgree]_ and that acronyms are an obstacle to reading58new code [MalyutinDistinguish]_ [CarruthAcronym]_ [PicusAcronym]_. There are59some opposing views [ParzyszekAcronym2]_ [RicciAcronyms]_.60 61This work-in-progress proposal is to change the coding standard for variable62names to require that they start with a lower case letter.63 64.. [*] In `some cases65 <https://github.com/llvm/llvm-project/blob/8b72080d4d7b13072f371712eed333f987b7a18e/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp#L2727>`_66 the type name *is* reused as a variable name, but this shadows the type name67 and confuses many debuggers [DenisovCamelBack]_.68 69Variable Names Coding Standard Options70======================================71 72There are two main options for variable names that begin with a lower case73letter: ``camelBack`` and ``lower_case``. (These are also known by other names74but here we use the terminology from clang-tidy).75 76``camelBack`` is consistent with [WebKit]_, [Qt]_ and [Swift]_ while77``lower_case`` is consistent with [LLDB]_, [Google]_, [Rust]_ and [Python]_.78 79``camelBack`` is already used for function names, which may be considered an80advantage [LattnerFunction]_ or a disadvantage [CarruthFunction]_.81 82Approval for ``camelBack`` was expressed by [DenisovCamelBack]_83[LattnerFunction]_ [IvanovicDistinguish]_.84Opposition to ``camelBack`` was expressed by [CarruthCamelBack]_85[TurnerCamelBack]_.86Approval for ``lower_case`` was expressed by [CarruthLower]_87[CarruthCamelBack]_ [TurnerLLDB]_.88Opposition to ``lower_case`` was expressed by [LattnerLower]_.89 90Differentiating variable kinds91------------------------------92 93An additional requested change is to distinguish between different kinds of94variables [RobinsonDistinguish]_ [RobinsonDistinguish2]_ [JonesDistinguish]_95[IvanovicDistinguish]_ [CarruthDistinguish]_ [MalyutinDistinguish]_.96 97Others oppose this idea [HähnleDistinguish]_ [GreeneDistinguish]_98[HendersonPrefix]_.99 100A possibility is for member variables to be prefixed with ``m_`` and for global101variables to be prefixed with ``g_`` to distinguish them from local variables.102This is consistent with [LLDB]_. The ``m_`` prefix is consistent with [WebKit]_.103 104A variation is for member variables to be prefixed with ``m``105[IvanovicDistinguish]_ [BeylsDistinguish]_. This is consistent with [Mozilla]_.106 107Another option is for member variables to be suffixed with ``_`` which is108consistent with [Google]_ and similar to [Python]_. Opposed by109[ParzyszekDistinguish]_.110 111Reducing the number of acronyms112===============================113 114While switching coding standard will make it easier to use non-acronym names for115new code, it doesn't improve the existing large body of code that uses acronyms116extensively to the detriment of its readability. Further, it is natural and117generally encouraged that new code be written in the style of the surrounding118code. Therefore it is likely that much newly written code will also use119acronyms despite what the coding standard says, much as it is today.120 121As well as changing the case of variable names, they could also be expanded to122their non-acronym form e.g. ``Triple T`` → ``Triple triple``.123 124There is support for expanding many acronyms [CarruthAcronym]_ [PicusAcronym]_125but there is a preference that expanding acronyms be deferred126[ParzyszekAcronym]_ [CarruthAcronym]_.127 128The consensus within the community seems to be that at least some acronyms are129valuable [ParzyszekAcronym]_ [LattnerAcronym]_. The most commonly cited acronym130is ``TLI`` however that is used to refer to both ``TargetLowering`` and131``TargetLibraryInfo`` [GreeneDistinguish]_.132 133The following is a list of acronyms considered sufficiently useful that the134benefit of using them outweighs the cost of learning them. Acronyms that are135either not on the list or are used to refer to a different type should be136expanded.137 138============================ =============139Class name Variable name140============================ =============141DeterministicFiniteAutomaton dfa142DominatorTree dt143LoopInfo li144MachineFunction mf145MachineInstr mi146MachineRegisterInfo mri147ScalarEvolution se148TargetInstrInfo tii149TargetLibraryInfo tli150TargetRegisterInfo tri151============================ =============152 153In some cases renaming acronyms to the full type name will result in overly154verbose code. Unlike most classes, a variable's scope is limited and therefore155some of its purpose can implied from that scope, meaning that fewer words are156necessary to give it a clear name. For example, in an optimization pass the reader157can assume that a variable's purpose relates to optimization and therefore an158``OptimizationRemarkEmitter`` variable could be given the name ``remarkEmitter``159or even ``remarker``.160 161The following is a list of longer class names and the associated shorter162variable name.163 164========================= =============165Class name Variable name166========================= =============167BasicBlock block168ConstantExpr expr169ExecutionEngine engine170MachineOperand operand171OptimizationRemarkEmitter remarker172PreservedAnalyses analyses173PreservedAnalysesChecker checker174TargetLowering lowering175TargetMachine machine176========================= =============177 178Transition Options179==================180 181There are three main options for transitioning:182 1831. Keep the current coding standard1842. Laissez faire1853. Big bang186 187Keep the current coding standard188--------------------------------189 190Proponents of keeping the current coding standard (i.e. not transitioning at191all) question whether the cost of transition outweighs the benefit192[EmersonConcern]_ [ReamesConcern]_ [BradburyConcern]_.193The costs are that ``git blame`` will become less usable; and that merging the194changes will be costly for downstream maintainers. See `Big bang`_ for potential195mitigations.196 197Laissez faire198-------------199 200The coding standard could allow both ``CamelCase`` and ``camelBack`` styles for201variable names [LattnerTransition]_.202 203A code review to implement this is at https://reviews.llvm.org/D57896.204 205Advantages206**********207 208 * Very easy to implement initially.209 210Disadvantages211*************212 213 * Leads to inconsistency [BradburyConcern]_ [AminiInconsistent]_.214 * Inconsistency means it will be hard to know at a guess what name a variable215 will have [DasInconsistent]_ [CarruthInconsistent]_.216 * Some large-scale renaming may happen anyway, leading to its disadvantages217 without any mitigations.218 219Big bang220--------221 222With this approach, variables will be renamed by an automated script in a series223of large commits.224 225The principle advantage of this approach is that it minimises the cost of226inconsistency [BradburyTransition]_ [RobinsonTransition]_.227 228It goes against a policy of avoiding large-scale reformatting of existing code229[GreeneDistinguish]_.230 231It has been suggested that LLD would be a good starter project for the renaming232[Ueyama]_.233 234Keeping git blame usable235************************236 237``git blame`` (or ``git annotate``) permits quickly identifying the commit that238changed a given line in a file. After renaming variables, many lines will show239as being changed by that one commit, requiring a further invocation of ``git240blame`` to identify prior, more interesting commits [GreeneGitBlame]_241[RicciAcronyms]_.242 243**Mitigation**: `git-hyper-blame244<https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html>`_245can ignore or "look through" a given set of commits.246A ``.git-blame-ignore-revs`` file identifying the variable renaming commits247could be added to the LLVM git repository root directory.248It is being `investigated249<https://public-inbox.org/git/20190324235020.49706-1-michael@platin.gs/>`_250whether similar functionality could be added to ``git blame`` itself.251 252Minimising cost of downstream merges253************************************254 255There are many forks of LLVM with downstream changes. Merging a large-scale256renaming change could be difficult for the fork maintainers.257 258**Mitigation**: A large-scale renaming would be automated. A fork maintainer can259merge from the commit immediately before the renaming, then apply the renaming260script to their own branch. They can then merge again from the renaming commit,261resolving all conflicts by choosing their own version. This could be tested on262the [SVE]_ fork.263 264Provisional Plan265================266 267This is a provisional plan for the `Big bang`_ approach. It has not been agreed.268 269#. Investigate improving ``git blame``. The extent to which it can be made to270 "look through" commits may impact how big a change can be made.271 272#. Write a script to expand acronyms.273 274#. Experiment and perform dry runs of the various refactoring options.275 Results can be published in forks of the LLVM Git repository.276 277#. Consider the evidence and agree on the new policy.278 279#. Agree & announce a date for the renaming of the starter project (LLD).280 281#. Update the `policy page <../CodingStandards.html>`_. This will explain the282 old and new rules and which projects each applies to.283 284#. Refactor the starter project in two commits:285 286 1. Add or change the project's .clang-tidy to reflect the agreed rules.287 (This is in a separate commit to enable the merging process described in288 `Minimising cost of downstream merges`_).289 Also update the project list on the policy page.290 2. Apply ``clang-tidy`` to the project's files, with only the291 ``readability-identifier-naming`` rules enabled. ``clang-tidy`` will also292 reformat the affected lines according to the rules in ``.clang-format``.293 It is anticipated that this will be a good dog-fooding opportunity for294 clang-tidy, and bugs should be fixed in the process, likely including:295 296 * `readability-identifier-naming incorrectly fixes lambda capture297 <https://bugs.llvm.org/show_bug.cgi?id=41119>`_.298 * `readability-identifier-naming incorrectly fixes variables which299 become keywords <https://bugs.llvm.org/show_bug.cgi?id=41120>`_.300 * `readability-identifier-naming misses fixing member variables in301 destructor <https://bugs.llvm.org/show_bug.cgi?id=41122>`_.302 303#. Gather feedback and refine the process as appropriate.304 305#. Apply the process to the following projects, with a suitable delay between306 each (at least 4 weeks after the first change, at least 2 weeks subsequently)307 to allow gathering further feedback.308 This list should exclude projects that must adhere to an externally defined309 standard e.g. libcxx.310 The list is roughly in chronological order of renaming.311 Some items may not make sense to rename individually - it is expected that312 this list will change following experimentation:313 314 * TableGen315 * llvm/tools316 * clang-tools-extra317 * clang318 * ARM backend319 * AArch64 backend320 * AMDGPU backend321 * ARC backend322 * AVR backend323 * BPF backend324 * Hexagon backend325 * Lanai backend326 * MIPS backend327 * NVPTX backend328 * PowerPC backend329 * RISC-V backend330 * Sparc backend331 * SystemZ backend332 * WebAssembly backend333 * X86 backend334 * XCore backend335 * libLTO336 * Debug Information337 * Remainder of llvm338 * compiler-rt339 * libunwind340 * openmp341 * parallel-libs342 * polly343 * lldb344 345#. Remove the old variable name rule from the policy page.346 347#. Repeat many of the steps in the sequence, using a script to expand acronyms.348 349References350==========351 352.. [LLDB] LLDB Coding Conventions https://llvm.org/svn/llvm-project/lldb/branches/release_39/www/lldb-coding-conventions.html353.. [Google] Google C++ Style Guide https://google.github.io/styleguide/cppguide.html#Variable_Names354.. [WebKit] WebKit Code Style Guidelines https://webkit.org/code-style-guidelines/#names355.. [Qt] Qt Coding Style https://wiki.qt.io/Qt_Coding_Style#Declaring_variables356.. [Rust] Rust naming conventions https://doc.rust-lang.org/1.0.0/style/style/naming/README.html357.. [Swift] Swift API Design Guidelines https://swift.org/documentation/api-design-guidelines/#general-conventions358.. [Python] Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/#function-and-variable-names359.. [Mozilla] Mozilla Coding style: Prefixes https://firefox-source-docs.mozilla.org/code-quality/coding-style/coding_style_cpp.html#prefixes360.. [SVE] LLVM with support for SVE https://github.com/ARM-software/LLVM-SVE361.. [AminiInconsistent] Mehdi Amini, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130329.html362.. [ArsenaultAgree] Matt Arsenault, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129934.html363.. [BeylsDistinguish] Kristof Beyls, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130292.html364.. [BradburyConcern] Alex Bradbury, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130266.html365.. [BradburyTransition] Alex Bradbury, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130388.html366.. [CarruthAcronym] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130313.html367.. [CarruthCamelBack] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130214.html368.. [CarruthDistinguish] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130310.html369.. [CarruthFunction] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130309.html370.. [CarruthInconsistent] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130312.html371.. [CarruthLower] Chandler Carruth, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130430.html372.. [DasInconsistent] Sanjoy Das, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130304.html373.. [DenisovCamelBack] Alex Denisov, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130179.html374.. [EmersonConcern] Amara Emerson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129894.html375.. [GreeneDistinguish] David Greene, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130425.html376.. [GreeneGitBlame] David Greene, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130228.html377.. [HendersonPrefix] James Henderson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130465.html378.. [HähnleDistinguish] Nicolai Hähnle, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129923.html379.. [IvanovicDistinguish] Nemanja Ivanovic, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130249.html380.. [JonesDistinguish] JD Jones, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129926.html381.. [LattnerAcronym] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130353.html382.. [LattnerAgree] Chris Latter, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129907.html383.. [LattnerFunction] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130630.html384.. [LattnerLower] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130629.html385.. [LattnerTransition] Chris Lattner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130355.html386.. [MalyutinDistinguish] Danila Malyutin, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130320.html387.. [ParzyszekAcronym] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130306.html388.. [ParzyszekAcronym2] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130323.html389.. [ParzyszekDistinguish] Krzysztof Parzyszek, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129941.html390.. [PicusAcronym] Diana Picus, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130318.html391.. [ReamesConcern] Philip Reames, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130181.html392.. [RicciAcronyms] Bruno Ricci, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130328.html393.. [RobinsonAgree] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130111.html394.. [RobinsonDistinguish] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/129920.html395.. [RobinsonDistinguish2] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130229.html396.. [RobinsonTransition] Paul Robinson, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130415.html397.. [TurnerCamelBack] Zachary Turner, https://reviews.llvm.org/D57896#1402264398.. [TurnerLLDB] Zachary Turner, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130213.html399.. [Ueyama] Rui Ueyama, http://lists.llvm.org/pipermail/llvm-dev/2019-February/130435.html400