This is an archive of the discontinued LLVM Phabricator instance.

Add coding standard recommending use of qualifiers in cpp files
ClosedPublic

Authored by rnk on Feb 12 2020, 3:48 PM.

Details

Summary

There is prior art for this in the code base itself, and a recent
example of this here: c45f8d49897f

This came up in discussion on this review where @MaskRay was going the
opposite direction:

https://reviews.llvm.org/D68772

Given that there is disagreement, we should make a choice and document
it.

My wording could probably use improvement, but I figured I'd send this
out anyway to see if people are receptive to the guideline.

Diff Detail

Unit TestsFailed

Event Timeline

rnk created this revision.Feb 12 2020, 3:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 12 2020, 3:48 PM

Do we have any nontrivially-long namespaces in LLVM?

I strongly agree with the substance of this recommendation.

This section would be a lot shorter and sweeter if you dropped (1) the mentions of methods and (2) the Foo type from the example. You can just make the overloads char * vs. const char * or something.

Do we have any nontrivially-long namespaces in LLVM?

I don't think any of LLVM's top-level namespaces are more than 5 characters or so. Sometimes we have long nested namespaces to e.g. move the helper types/functions of a particular data structure out of llvm::, like with llvm::ilist_detail, but they're rare and generally local to a single file.

Some of our project libraries use nested namespaces, and the qualifiers all together can get pretty long, but that's not a problem for this recommendatiion because you can just use the interior namespace in the out-of-line definition:

using namespace clang;
using namespace CodeGen;

void CodeGen::foo() {...}
rnk updated this revision to Diff 244303.Feb 12 2020, 4:32 PM
  • shorten example
rjmccall added inline comments.Feb 12 2020, 6:33 PM
llvm/docs/CodingStandards.rst
789

Consider this:

Doing this helps to avoid bugs where the definition does not
match the declaration from the header. For example, the following
C++ code defines a new overload of llvm::foo instead of providing
a definition for the existing function declared in the header:

<example>

This error will not be caught until the build is nearly complete, when
the linker fails to find a definition for any uses of the original function.
If the function were instead defined with a namespace qualifier, the
error would have been caught immediately when the definition was
compiled.

rnk marked 2 inline comments as done.Feb 13 2020, 10:11 AM
rnk added inline comments.
llvm/docs/CodingStandards.rst
787

Bleh, this somehow got mangled while editing. :( Anyway, thanks for the precise wording, I'll use it.

rnk updated this revision to Diff 244469.Feb 13 2020, 10:13 AM
  • John writes very clearly, so use his recommended wording.

LG, but I'll defer to other reviewers who have been working on the project longer than me.

This revision was not accepted when it landed; it landed in state Needs Review.Feb 18 2020, 2:25 PM
This revision was automatically updated to reflect the committed changes.