Skip to content

Commit 0462dfe

Browse files
[llvm][docs] Refresh "Restrict Visibility" in Coding Standards (#150914)
No change of meaning, just formatting and an extra example to make it easier to comprehend: * Split separate, important points into their own paragraphs. * Remove a contraction. * Finally, show to to use "static" on a function. As before we just showed why namespaces were bad, but not what you should do instead.
1 parent 38cd66a commit 0462dfe

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

llvm/docs/CodingStandards.rst

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,20 +1594,25 @@ Restrict Visibility
15941594
^^^^^^^^^^^^^^^^^^^
15951595

15961596
Functions and variables should have the most restricted visibility possible.
1597+
15971598
For class members, that means using appropriate ``private``, ``protected``, or
1598-
``public`` keyword to restrict their access. For non-member functions, variables,
1599-
and classes, that means restricting visibility to a single ``.cpp`` file if it's
1600-
not referenced outside that file.
1599+
``public`` keyword to restrict their access.
1600+
1601+
For non-member functions, variables, and classes, that means restricting
1602+
visibility to a single ``.cpp`` file if it is not referenced outside that file.
16011603

16021604
Visibility of file-scope non-member variables and functions can be restricted to
16031605
the current translation unit by using either the ``static`` keyword or an anonymous
1604-
namespace. Anonymous namespaces are a great language feature that tells the C++
1606+
namespace.
1607+
1608+
Anonymous namespaces are a great language feature that tells the C++
16051609
compiler that the contents of the namespace are only visible within the current
16061610
translation unit, allowing more aggressive optimization and eliminating the
1607-
possibility of symbol name collisions. Anonymous namespaces are to C++ as
1608-
``static`` is to C functions and global variables. While ``static`` is available
1609-
in C++, anonymous namespaces are more general: they can make entire classes
1610-
private to a file.
1611+
possibility of symbol name collisions.
1612+
1613+
Anonymous namespaces are to C++ as ``static`` is to C functions and global
1614+
variables. While ``static`` is available in C++, anonymous namespaces are more
1615+
general: they can make entire classes private to a file.
16111616

16121617
The problem with anonymous namespaces is that they naturally want to encourage
16131618
indentation of their body, and they reduce locality of reference: if you see a
@@ -1653,10 +1658,17 @@ Avoid putting declarations other than classes into anonymous namespaces:
16531658

16541659
} // namespace
16551660

1656-
When you are looking at "``runHelper``" in the middle of a large C++ file,
1657-
you have no immediate way to tell if this function is local to the file. In
1658-
contrast, when the function is marked static, you don't need to cross-reference
1659-
faraway places in the file to tell that the function is local.
1661+
When you are looking at ``runHelper`` in the middle of a large C++ file,
1662+
you have no immediate way to tell if this function is local to the file.
1663+
1664+
In contrast, when the function is marked static, you don't need to cross-reference
1665+
faraway places in the file to tell that the function is local:
1666+
1667+
.. code-block:: c++
1668+
1669+
static void runHelper() {
1670+
...
1671+
}
16601672

16611673
Don't Use Braces on Simple Single-Statement Bodies of if/else/loop Statements
16621674
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)