[llvm] [CodingStandard] Add a rule about non-member definitions in CPP files (PR #126775)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 15:34:45 PST 2025


================
@@ -1640,6 +1640,10 @@ you have no immediate way to tell if this function is local to the file.  In
 contrast, when the function is marked static, you don't need to cross-reference
 faraway places in the file to tell that the function is local.
 
+Related to this issue, when non-member functions or variables are defined in a
+`.cpp`` file and need not be referenced outside that file, make them `static`.
----------------
jurahul wrote:

I agree with @joker-eph, again distinguishing between *what* needs to be internalized (last paragraph that @joker-eph suggested) vs *how* to achieve that (static for non-member functions and variables) and anonymous namespaces for classes and structs. I'd reword the last paragraph a bit as follows, with some extra/redundant clarification:

> We aim to make functions, variables, and classes as private as possible: If a non-member function or variable is defined in a `.cpp` file and need not be referenced outside that file, it should be made private to this file (by using `static`). Similarly, if a class or struct is defined in a `.cpp` file and need not be referenced outside, it should be made private to this file (by using anonymous namespace).

WDYT?

https://github.com/llvm/llvm-project/pull/126775


More information about the llvm-commits mailing list