[llvm] 5bddadf - [CodingStandard] Rework anonymous namespace section to cover visibility more broadly (#126775)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 24 08:24:58 PST 2025


Author: Rahul Joshi
Date: 2025-02-24T08:24:54-08:00
New Revision: 5bddadf783c177943fa4f86fa0d295d4e88e7dea

URL: https://github.com/llvm/llvm-project/commit/5bddadf783c177943fa4f86fa0d295d4e88e7dea
DIFF: https://github.com/llvm/llvm-project/commit/5bddadf783c177943fa4f86fa0d295d4e88e7dea.diff

LOG: [CodingStandard] Rework anonymous namespace section to cover visibility more broadly (#126775)

- Rename anonymous namespace section and rework it to
  cover visibility more broadly.
- Add language suggesting restricting visibility as much as
  possible, using various C++ facilities.

---------

Co-authored-by: Aaron Ballman <aaron at aaronballman.com>

Added: 
    

Modified: 
    llvm/docs/CodingStandards.rst

Removed: 
    


################################################################################
diff  --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index d6d6ecf759cf3..dde859d1e0f71 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -1579,17 +1579,23 @@ clarification.
 
 .. _static:
 
-Anonymous Namespaces
-^^^^^^^^^^^^^^^^^^^^
-
-After talking about namespaces in general, you may be wondering about anonymous
-namespaces in particular.  Anonymous namespaces are a great language feature
-that tells the C++ compiler that the contents of the namespace are only visible
-within the current translation unit, allowing more aggressive optimization and
-eliminating the possibility of symbol name collisions.  Anonymous namespaces are
-to C++ as "static" is to C functions and global variables.  While "``static``"
-is available in C++, anonymous namespaces are more general: they can make entire
-classes private to a file.
+Restrict Visibility
+^^^^^^^^^^^^^^^^^^^
+
+Functions and variables should have the most restricted visibility possible.
+For class members, that means using appropriate `private`, `protected`, or `public`
+keyword to restrict their access. For non-member functions, variables, and classes,
+that means restricting visibility to a single `.cpp` file if it's not referenced
+outside that file.
+
+Visibility of file-scope non-member variables and functions can be restricted to
+the current translation unit by using either the `static` keyword or an anonymous namespace.
+Anonymous namespaces are a great language feature that tells the C++ compiler that
+the contents of the namespace are only visible within the current translation unit,
+allowing more aggressive optimization and eliminating the possibility of symbol
+name collisions.  Anonymous namespaces are to C++ as `static` is to C functions and
+global variables.  While `static` is available in C++, anonymous namespaces are more
+general: they can make entire classes private to a file.
 
 The problem with anonymous namespaces is that they naturally want to encourage
 indentation of their body, and they reduce locality of reference: if you see a


        


More information about the llvm-commits mailing list