1<!--===- docs/C++style.md
2
3   Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4   See https://llvm.org/LICENSE.txt for license information.
5   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7-->
8
9# Flang C++ Style Guide
10
11```eval_rst
12.. contents::
13   :local:
14```
15
16This document captures the style guide rules that are followed in the Flang codebase.
17
18## In brief:
19* Use *clang-format*
20from llvm 7
21on all C++ source and header files before
22every merge to master.  All code layout should be determined
23by means of clang-format.
24* Where a clear precedent exists in the project, follow it.
25* Otherwise, where [LLVM's C++ style guide](https://llvm.org/docs/CodingStandards.html#style-issues)
26is clear on usage, follow it.
27* Otherwise, where a good public C++ style guide is relevant and clear,
28  follow it.  [Google's](https://google.github.io/styleguide/cppguide.html)
29  is pretty good and comes with lots of justifications for its rules.
30* Reasonable exceptions to these guidelines can be made.
31* Be aware of some workarounds for known issues in older C++ compilers that should
32  still be able to compile f18. They are listed at the end of this document.
33
34## In particular:
35
36Use serial commas in comments, error messages, and documentation
37unless they introduce ambiguity.
38
39### Error messages
401. Messages should be a single sentence with few exceptions.
411. Fortran keywords should appear in upper case.
421. Names from the program appear in single quotes.
431. Messages should start with a capital letter.
441. Messages should not end with a period.
45
46### Files
471. File names should use dashes, not underscores.  C++ sources have the
48extension ".cpp", not ".C" or ".cc" or ".cxx".  Don't create needless
49source directory hierarchies.
501. Header files should be idempotent.  Use the usual technique:
51```
52#ifndef FORTRAN_header_H_
53#define FORTRAN_header_H_
54// code
55#endif  // FORTRAN_header_H_
56```
571. `#include` every header defining an entity that your project header or source
58file actually uses directly.  (Exception: when foo.cpp starts, as it should,
59with `#include "foo.h"`, and foo.h includes bar.h in order to define the
60interface to the module foo, you don't have to redundantly `#include "bar.h"`
61in foo.cpp.)
621. In the source file "foo.cpp", put its corresponding `#include "foo.h"`
63first in the sequence of inclusions.
64Then `#include` other project headers in alphabetic order; then C++ standard
65headers, also alphabetically; then C and system headers.
661. Don't use `#include <iostream>`.  If you need it for temporary debugging,
67remove the inclusion before committing.
68
69### Naming
701. C++ names that correspond to well-known interfaces from the STL, LLVM,
71and Fortran standard
72can and should look like their models when the reader can safely assume that
73they mean the same thing -- e.g., `clear()` and `size()` member functions
74in a class that implements an STL-ish container.
75Fortran intrinsic function names are conventionally in ALL CAPS.
761. Non-public data members should be named with leading miniscule (lower-case)
77letters, internal camelCase capitalization, and a trailing underscore,
78e.g. `DoubleEntryBookkeepingSystem myLedger_;`.  POD structures with
79only public data members shouldn't use trailing underscores, since they
80don't have class functions from which data members need to be distinguishable.
811. Accessor member functions are named with the non-public data member's name,
82less the trailing underscore.  Mutator member functions are named `set_...`
83and should return `*this`.  Don't define accessors or mutators needlessly.
841. Other class functions should be named with leading capital letters,
85CamelCase, and no underscores, and, like all functions, should be based
86on imperative verbs, e.g. `HaltAndCatchFire()`.
871. It is fine to use short names for local variables with limited scopes,
88especially when you can declare them directly in a `for()`/`while()`/`if()`
89condition.  Otherwise, prefer complete English words to abbreviations
90when creating names.
91
92### Commentary
931. Use `//` for all comments except for short `/*notes*/` within expressions.
941. When `//` follows code on a line, precede it with two spaces.
951. Comments should matter.  Assume that the reader knows current C++ at least as
96well as you do and avoid distracting her by calling out usage of new
97features in comments.
98
99### Layout
100Always run `clang-format` on your changes before committing code. LLVM
101has a `git-clang-format` script to facilitate running clang-format only
102on the lines that have changed.
103
104Here's what you can expect to see `clang-format` do:
1051. Indent with two spaces.
1061. Don't indent public:, protected:, and private:
107accessibility labels.
1081. Never use more than 80 characters per source line.
1091. Don't use tabs.
1101. Don't indent the bodies of namespaces, even when nested.
1111. Function result types go on the same line as the function and argument
112names.
113
114Don't try to make columns of variable names or comments
115align vertically -- they are maintenance problems.
116
117Always wrap the bodies of `if()`, `else`, `while()`, `for()`, `do`, &c.
118with braces, even when the body is a single statement or empty.  The
119opening `{` goes on
120the end of the line, not on the next line.  Functions also put the opening
121`{` after the formal arguments or new-style result type, not on the next
122line.  Use `{}` for empty inline constructors and destructors in classes.
123
124If any branch of an `if`/`else if`/`else` cascade ends with a return statement,
125they all should, with the understanding that the cases are all unexceptional.
126When testing for an error case that should cause an early return, do so with
127an `if` that doesn't have a following `else`.
128
129Don't waste space on the screen with needless blank lines or elaborate block
130commentary (lines of dashes, boxes of asterisks, &c.).  Write code so as to be
131easily read and understood with a minimum of scrolling.
132
133Avoid using assignments in controlling expressions of `if()` &c., even with
134the idiom of wrapping them with extra parentheses.
135
136In multi-element initializer lists (especially `common::visitors{...}`),
137including a comma after the last element often causes `clang-format` to do
138a better jobs of formatting.
139
140### C++ language
141Use *C++17*, unless some compiler to which we must be portable lacks a feature
142you are considering.
143However:
1441. Never throw or catch exceptions.
1451. Never use run-time type information or `dynamic_cast<>`.
1461. Never declare static data that executes a constructor.
147   (This is why `#include <iostream>` is contraindicated.)
1481. Use `{braced initializers}` in all circumstances where they work, including
149default data member initialization.  They inhibit implicit truncation.
150Don't use `= expr` initialization just to effect implicit truncation;
151prefer an explicit `static_cast<>`.
152With C++17, braced initializers work fine with `auto` too.
153Sometimes, however, there are better alternatives to empty braces;
154e.g., prefer `return std::nullopt;` to `return {};` to make it more clear
155that the function's result type is a `std::optional<>`.
1561. Avoid unsigned types apart from `size_t`, which must be used with care.
157When `int` just obviously works, just use `int`.  When you need something
158bigger than `int`, use `std::int64_t` rather than `long` or `long long`.
1591. Use namespaces to avoid conflicts with client code.  Use one top-level
160`Fortran` project namespace.  Don't introduce needless nested namespaces within the
161project when names don't conflict or better solutions exist.  Never use
162`using namespace ...;` outside test code; never use `using namespace std;`
163anywhere.  Access STL entities with names like `std::unique_ptr<>`,
164without a leading `::`.
1651. Prefer `static` functions over functions in anonymous namespaces in source files.
1661. Use `auto` judiciously.  When the type of a local variable is known,
167monomorphic, and easy to type, be explicit rather than using `auto`.
168Don't use `auto` functions unless the type of the result of an outlined member
169function definition can be more clear due to its use of types declared in the
170class.
1711. Use move semantics and smart pointers to make dynamic memory ownership
172clear.  Consider reworking any code that uses `malloc()` or a (non-placement)
173`operator new`.
174See the section on Pointers below for some suggested options.
1751. When defining argument types, use values when object semantics are
176not required and the value is small and copyable without allocation
177(e.g., `int`);
178use `const` or rvalue references for larger values (e.g., `std::string`);
179use `const` references to rather than pointers to immutable objects;
180and use non-`const` references for mutable objects, including "output" arguments
181when they can't be function results.
182Put such output arguments last (_pace_ the standard C library conventions for `memcpy()` & al.).
1831. Prefer `typename` to `class` in template argument declarations.
1841. Prefer `enum class` to plain `enum` wherever `enum class` will work.
185We have an `ENUM_CLASS` macro that helps capture the names of constants.
1861. Use `constexpr` and `const` generously.
1871. When a `switch()` statement's labels do not cover all possible case values
188explicitly, it should contain either a `default:;` at its end or a
189`default:` label that obviously crashes; we have a `CRASH_NO_CASE` macro
190for such situations.
1911. On the other hand, when a `switch()` statement really does cover all of
192the values of an `enum class`, please insert a call to the `SWITCH_COVERS_ALL_CASES`
193macro at the top of the block.  This macro does the right thing for G++ and
194clang to ensure that no warning is emitted when the cases are indeed all covered.
1951. When using `std::optional` values, avoid unprotected access to their content.
196This is usually by means of `x.has_value()` guarding execution of `*x`.
197This is implicit when they are function results assigned to local variables
198in `if`/`while` predicates.
199When no presence test is obviously protecting a `*x` reference to the
200contents, and it is assumed that the contents are present, validate that
201assumption by using `x.value()` instead.
2021. We use `c_str()` rather than `data()` when converting a `std::string`
203to a `const char *` when the result is expected to be NUL-terminated.
2041. Avoid explicit comparisions of pointers to `nullptr` and tests of
205presence of `optional<>` values with `.has_value()` in the predicate
206expressions of control flow statements, but prefer them to implicit
207conversions to `bool` when initializing `bool` variables and arguments,
208and to the use of the idiom `!!`.
209
210#### Classes
2111. Define POD structures with `struct`.
2121. Don't use `this->` in (non-static) member functions, unless forced to
213do so in a template member function.
2141. Define accessor and mutator member functions (implicitly) inline in the
215class, after constructors and assignments.  Don't needlessly define
216(implicit) inline member functions in classes unless they really solve a
217performance problem.
2181. Try to make class definitions in headers concise specifications of
219interfaces, at least to the extent that C++ allows.
2201. When copy constructors and copy assignment are not necessary,
221and move constructors/assignment is present, don't declare them and they
222will be implicitly deleted.  When neither copy nor move constructors
223or assignments should exist for a class, explicitly `=delete` all of them.
2241. Make single-argument constructors (other than copy and move constructors)
225'explicit' unless you really want to define an implicit conversion.
226
227#### Pointers
228There are many -- perhaps too many -- means of indirect addressing
229data in this project.
230Some of these are standard C++ language and library features,
231while others are local inventions in `lib/Common`:
232* Bare pointers (`Foo *p`): these are obviously nullable, non-owning,
233undefined when uninitialized, shallowly copyable, reassignable, and often
234not the right abstraction to use in this project.
235But they can be the right choice to represent an optional
236non-owning reference, as in a function result.
237Use the `DEREF()` macro to convert a pointer to a reference that isn't
238already protected by an explicit test for null.
239* References (`Foo &r`, `const Foo &r`): non-nullable, not owning,
240shallowly copyable, and not reassignable.
241References are great for invisible indirection to objects whose lifetimes are
242broader than that of the reference.
243Take care when initializing a reference with another reference to ensure
244that a copy is not made because only one of the references is `const`;
245this is a pernicious C++ language pitfall!
246* Rvalue references (`Foo &&r`): These are non-nullable references
247*with* ownership, and they are ubiquitously used for formal arguments
248wherever appropriate.
249* `std::reference_wrapper<>`: non-nullable, not owning, shallowly
250copyable, and (unlike bare references) reassignable, so suitable for
251use in STL containers and for data members in classes that need to be
252copyable or assignable.
253* `common::Reference<>`: like `std::reference_wrapper<>`, but also supports
254move semantics, member access, and comparison for equality; suitable for use in
255`std::variant<>`.
256* `std::unique_ptr<>`: A nullable pointer with ownership, null by default,
257not copyable, reassignable.
258F18 has a helpful `Deleter<>` class template that makes `unique_ptr<>`
259easier to use with forward-referenced data types.
260* `std::shared_ptr<>`: A nullable pointer with shared ownership via reference
261counting, null by default, shallowly copyable, reassignable, and slow.
262* `Indirection<>`: A non-nullable pointer with ownership and
263optional deep copy semantics; reassignable.
264Often better than a reference (due to ownership) or `std::unique_ptr<>`
265(due to non-nullability and copyability).
266Can be wrapped in `std::optional<>` when nullability is required.
267Usable with forward-referenced data types with some use of `extern template`
268in headers and explicit template instantiation in source files.
269* `CountedReference<>`: A nullable pointer with shared ownership via
270reference counting, null by default, shallowly copyable, reassignable.
271Safe to use *only* when the data are private to just one
272thread of execution.
273Used sparingly in place of `std::shared_ptr<>` only when the overhead
274of that standard feature is prohibitive.
275
276A feature matrix:
277
278| indirection           | nullable | default null | owning | reassignable | copyable          | undefined type ok? |
279| -----------           | -------- | ------------ | ------ | ------------ | --------          | ------------------ |
280| `*p`                  | yes      | no           | no     | yes          | shallowly         | yes                |
281| `&r`                  | no       | n/a          | no     | no           | shallowly         | yes                |
282| `&&r`                 | no       | n/a          | yes    | no           | shallowly         | yes                |
283| `reference_wrapper<>` | no       | n/a          | no     | yes          | shallowly         | yes                |
284| `Reference<>`         | no       | n/a          | no     | yes          | shallowly         | yes                |
285| `unique_ptr<>`        | yes      | yes          | yes    | yes          | no                | yes, with work     |
286| `shared_ptr<>`        | yes      | yes          | yes    | yes          | shallowly         | no                 |
287| `Indirection<>`       | no       | n/a          | yes    | yes          | optionally deeply | yes, with work     |
288| `CountedReference<>`  | yes      | yes          | yes    | yes          | shallowly         | no                 |
289
290### Overall design preferences
291Don't use dynamic solutions to solve problems that can be solved at
292build time; don't solve build time problems by writing programs that
293produce source code when macros and templates suffice; don't write macros
294when templates suffice.  Templates are statically typed, checked by the
295compiler, and are (or should be) visible to debuggers.
296
297### Exceptions to these guidelines
298Reasonable exceptions will be allowed; these guidelines cannot anticipate
299all situations.
300For example, names that come from other sources might be more clear if
301their original spellings are preserved rather than mangled to conform
302needlessly to the conventions here, as Google's C++ style guide does
303in a way that leads to weirdly capitalized abbreviations in names
304like `Http`.
305Consistency is one of many aspects in the pursuit of clarity,
306but not an end in itself.
307
308## C++ compiler bug workarounds
309Below is a list of workarounds for C++ compiler bugs met with f18 that, even
310if the bugs are fixed in latest C++ compiler versions, need to be applied so
311that all desired tool-chains can compile f18.
312
313### Explicitly move noncopyable local variable into optional results
314
315The following code is legal C++ but fails to compile with the
316default Ubuntu 18.04 g++ compiler (7.4.0-1ubuntu1~18.0.4.1):
317
318```
319class CantBeCopied {
320 public:
321 CantBeCopied(const CantBeCopied&) = delete;
322 CantBeCopied(CantBeCopied&&) = default;
323 CantBeCopied() {}
324};
325std::optional<CantBeCopied> fooNOK() {
326  CantBeCopied result;
327  return result; // Legal C++, but does not compile with Ubuntu 18.04 default g++
328}
329std::optional<CantBeCopied> fooOK() {
330  CantBeCopied result;
331  return {std::move(result)}; // Compiles OK everywhere
332}
333```
334The underlying bug is actually not specific to `std::optional` but this is the most common
335case in f18 where the issue may occur. The actual bug can be reproduced with any class `B`
336that has a perfect forwarding constructor taking `CantBeCopied` as argument:
337`template<typename CantBeCopied> B(CantBeCopied&& x) x_{std::forward<CantBeCopied>(x)} {}`.
338In such scenarios, Ubuntu 18.04 g++ fails to instantiate the move constructor
339and to construct the returned value as it should, instead it complains about a
340missing copy constructor.
341
342Local result variables do not need to and should not be explicitly moved into optionals
343if they have a copy constructor.
344