Skip to content

[CodingStandard] Rework anonymous namespace section to cover visibility more broadly #126775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 24, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Feb 11, 2025

  • 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.

@jurahul jurahul marked this pull request as ready for review February 11, 2025 18:37
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should clarify https://llvm.org/docs/CodingStandards.html#anonymous-namespaces rather than add a new section specifically. WDYT?

@jurahul
Copy link
Contributor Author

jurahul commented Feb 12, 2025 via email

@jurahul jurahul force-pushed the static_coding_standard branch from 3fd77ac to 57e03cd Compare February 12, 2025 19:23
@jurahul
Copy link
Contributor Author

jurahul commented Feb 12, 2025

Sound good. This note is about what to internalize and the anonymous namespace is about how but I can move this line to the end of that section.

Moved it to the end of the anonymous namespaces section. PTAL.

Comment on lines 1643 to 1644
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`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is really just clarifying the above paragraph. What if we said:

When you are looking at "``runHelper``" in the middle of a large C++ file,
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. For this reason, non-
member functions and variables should be marked ``static`` rather than added
to an anonymous namespace.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite see this as achieving what we want here actually: this is just saying to prefer using static instead of anonymous, but it does not say anything about the policy of making thing private as much as possible.

What about adding it at the end of the intro like this (last paragraph is added):

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.
We aim to make private as much as possible every function or variable: if it is
defined in a .cpp file and need not be referenced outside that file, it should be
made private to this file.

That phrasing would cover also classes that can be put in anonymous namespaces as part of the intent to make things as private as possible.

Copy link
Contributor Author

@jurahul jurahul Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 that file, it should be made private to this file (by using anonymous namespace).

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or to belabor on this more, may be this should be the first paragraph, saying that things should be as private as possible, and then going into how to achieve that via static or anon namespaces. The section can be renamed to something like privatize aggressively or keep it as is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or to belabor on this more, may be this should be the first paragraph, saying that things should be as private as possible, and then going into how to achieve that via static or anon namespaces. The section can be renamed to something like privatize aggressively or keep it as is.

This format would make more sense to me. Basically, "Here's the principle we want to follow and why. Here's how to follow that principle." Should we also strongly encourage using private and protected access specifiers within classes too? Then the section is basically about all the ways in which you can restrict access to identifiers. (I don't know if I'm scope-creeping too much though.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally got around to updating this based on the suggestion above. @AaronBallman and @joker-eph PTAL.

@jurahul jurahul requested a review from AaronBallman February 12, 2025 23:36
@jurahul
Copy link
Contributor Author

jurahul commented Feb 14, 2025 via email

- Add a rule suggesting that non-member definitions in CPP files should
  be made static unless they are referenced outside that file.
@jurahul jurahul force-pushed the static_coding_standard branch from 57e03cd to 8e9d2e7 Compare February 21, 2025 15:48
@jurahul jurahul changed the title [CodingStandard] Add a rule about non-member definitions in CPP files [CodingStandard] Rework anonymous namespace section to cover visibility more broadly Feb 21, 2025
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some minor nits

@jurahul
Copy link
Contributor Author

jurahul commented Feb 21, 2025

Thanks, applied your suggestions.

@AaronBallman
Copy link
Collaborator

Thanks, applied your suggestions.

Thanks! Do you need us to land this for you?

@jurahul
Copy link
Contributor Author

jurahul commented Feb 21, 2025

Thanks, applied your suggestions.

Thanks! Do you need us to land this for you?

No. I have commit access, will land once the checks are green again.

@jurahul jurahul merged commit 5bddadf into llvm:main Feb 24, 2025
10 of 12 checks passed
@jurahul jurahul deleted the static_coding_standard branch February 24, 2025 16:24
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit


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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the great here feels a bit like editorializing to me, while I don't disagree it kind of stands out wrt to the rest of the paragraph. I wouldn't bother changing it but for future reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants