[RFC] Coding convention clarification for file scoped global variables

I had filed a rather innocuous issue for a minor code cleanup here: Change all cl::opt<> in .cpp files to static · Issue #125983 · llvm/llvm-project

Based on the comments I see on the issue and the PR for the fix, it seems this may help clarification in the coding standard, so I wanted to see if this something we could add a line about in LLVM coding standard (assuming folks agree with it).

Based on Google C++ Style Guide, we can add something like:

When definitions in a .cpp file do not need to be referenced outside that file, give them internal linkage by declaring them static. Do not use this construct in .h files.

2 Likes

+1 to this. It at least aligns with what I understood the standard to actually be. That being said, I’d be tempted to omit the sentence about .h files as there are (admittedly very limited - I don’t think I’ve ever done it myself) cases where static makes sense.

The final text may also want to reference the section on anonymous namespaces too. I have no specific wording suggestion there.

1 Like

Sounds reasonable to me. Note that we already have guidance (LLVM Coding Standards — LLVM 21.0.0git documentation) to use static over anon namespaces, it doesn’t mentioned that, yes, you should be making everything static that can be static.

1 Like

Thanks. I’ll post a PR