From 8e9d2e71bbfa9023d6e8bc166f2162a40e821319 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Tue, 11 Feb 2025 10:14:48 -0800 Subject: [PATCH 1/4] [CodingStandard] Add a rule about non-member definitions in CPP files - Add a rule suggesting that non-member definitions in CPP files should be made static unless they are referenced outside that file. --- llvm/docs/CodingStandards.rst | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst index d6d6ecf759cf3..cfd9ead6a79fb 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-scoped non-members variables and functions can be restricted to +the current translation unit by using either `static` keyword or 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 From 3ff4b917fdf4e2d0391a9dd7f9ab8597d8be9590 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Fri, 21 Feb 2025 11:03:19 -0800 Subject: [PATCH 2/4] Update llvm/docs/CodingStandards.rst Co-authored-by: Aaron Ballman --- llvm/docs/CodingStandards.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst index cfd9ead6a79fb..c24c30771699a 100644 --- a/llvm/docs/CodingStandards.rst +++ b/llvm/docs/CodingStandards.rst @@ -1583,8 +1583,8 @@ 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, +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. From efbfa3d8ffe86fed2bff5f9330c1e9d75d62f475 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Fri, 21 Feb 2025 11:03:29 -0800 Subject: [PATCH 3/4] Update llvm/docs/CodingStandards.rst Co-authored-by: Aaron Ballman --- llvm/docs/CodingStandards.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst index c24c30771699a..4538b97f9add3 100644 --- a/llvm/docs/CodingStandards.rst +++ b/llvm/docs/CodingStandards.rst @@ -1588,7 +1588,7 @@ keyword to restrict their access. For non-member functions, variables, and class that means restricting visibility to a single `.cpp` file if it's not referenced outside that file. -Visibility of file-scoped non-members variables and functions can be restricted to +Visibility of file-scope non-member variables and functions can be restricted to the current translation unit by using either `static` keyword or 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, From e9406190d3bb048a7cbb7f844750d18316544955 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Fri, 21 Feb 2025 11:03:41 -0800 Subject: [PATCH 4/4] Update llvm/docs/CodingStandards.rst Co-authored-by: Aaron Ballman --- llvm/docs/CodingStandards.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst index 4538b97f9add3..dde859d1e0f71 100644 --- a/llvm/docs/CodingStandards.rst +++ b/llvm/docs/CodingStandards.rst @@ -1589,7 +1589,7 @@ 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 `static` keyword or anonymous namespace. +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