This is an archive of the discontinued LLVM Phabricator instance.

Added a check for forward declaration in the potentially wrong namespace in clang-tidy [misc-forward-declaration-namespace]
ClosedPublic

Authored by ioeric on Feb 12 2016, 4:41 AM.

Details

Summary

In check, A forward declaration is considerred in a potentially wrong namespace
if there is any definition/declaration with the same name exists in a different
namespace.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric updated this revision to Diff 47792.Feb 12 2016, 4:41 AM
ioeric retitled this revision from to Added a check for forward declaration in the potentially wrong namespace in clang-tidy [misc-forward-declaration-namespace].
ioeric updated this object.
ioeric added reviewers: alexfh, akuegel.
akuegel edited edge metadata.Feb 12 2016, 5:09 AM

Just a couple of nits in the first round.

clang-tidy/misc/CMakeLists.txt
30 ↗(On Diff #47792)

Put this into alphabetical position in this list.

clang-tidy/misc/MiscTidyModule.cpp
38 ↗(On Diff #47792)

Nit: put this in alphabetical position.

39 ↗(On Diff #47792)

Is this additional empty line intentional?

95 ↗(On Diff #47792)

Again, please put into alphabetical order.

ioeric updated this revision to Diff 47796.Feb 12 2016, 5:27 AM
ioeric marked 4 inline comments as done.
ioeric edited edge metadata.
  • formatted code

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

In general, please make sure all comments you write are a complete phrase, don't abbreviate. See also http://llvm.org/docs/CodingStandards.html#commenting

clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
46 ↗(On Diff #47796)

Seems there is an unnecessary space between "used" and "in".

62 ↗(On Diff #47796)

According to http://llvm.org/docs/CodingStandards.html#assert-liberally
I think you should actually assert that this is a friend_decl, since you only expect to match either record_decls or friend_decls.

64 ↗(On Diff #47796)

I don't really understand that NOTE; can you please make it clearer?

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
10 ↗(On Diff #47796)

Please use the header guard
LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H_

22 ↗(On Diff #47796)

It would be nice if you can add a comment here describing what your check does.
See for example the IncorrectRoundings check.

42 ↗(On Diff #47796)

Don't forget to update the comment here accordingly.

hokein added inline comments.Feb 15 2016, 1:51 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
53 ↗(On Diff #47796)

Please following LLVM code style (http://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly). The variable name should be in camel case.

76 ↗(On Diff #47796)

Instead of using static function here, I prefer to define these function in an anonymous namespace.

Looks like the function does more its name says, it also checks whether it's in the same TranslationUnit?

90 ↗(On Diff #47796)

The same, put it in anonymous namespace.

116 ↗(On Diff #47796)

You can use for-range loop.

127 ↗(On Diff #47796)

The comment seems useless.

130 ↗(On Diff #47796)

for-range loop.

154 ↗(On Diff #47796)

Use const auto *def : definitions to make the type more clear.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
22 ↗(On Diff #47796)

Normally, for each check, you need to add rst document in docs/clang-tidy/checks/. The add_new_check.py will help you to do that.

ioeric updated this revision to Diff 47968.Feb 15 2016, 2:54 AM
ioeric marked 14 inline comments as done.
  • improved code style and commenting; added asserts for assumptions; added ast doc

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

alexfh added inline comments.Feb 15 2016, 3:07 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
77 ↗(On Diff #47968)

Not really, it's actually more common in LLVM code to use static functions instead of putting them in unnamed namespaces: http://llvm.org/docs/CodingStandards.html#anonymous-namespaces

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
1 ↗(On Diff #47796)

nit: Remove extra minuses to align this to 80 columns.

10 ↗(On Diff #47796)

This should be LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H_

ioeric updated this revision to Diff 47972.Feb 15 2016, 3:29 AM
ioeric marked 3 inline comments as done.
  • improved formatting; changed back to static function to follow llvm coding standard

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

hokein added inline comments.Feb 15 2016, 3:46 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
43 ↗(On Diff #47972)

s/declaration/declarations/

159 ↗(On Diff #47972)

s/definitions/Definitions

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
37 ↗(On Diff #47972)

Adding the following paragraph at the end of the comment.

// For the user-facing documentation see:
// http://clang.llvm.org/extra/clang-tidy/checks/misc-forward-declaration-namespace.html
38 ↗(On Diff #47972)

Extra blank here.

49 ↗(On Diff #47972)

I think you can use llvm::StringRef as map key? CXXRecordDecl->getName() returns llvm::StringRef.

Using string here will require extra std::string construction&copy operations.

10 ↗(On Diff #47796)

I think it should be LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FORWARDDECLARATIONNAMESPACECHECK_H, no underscore at the end.

docs/clang-tidy/checks/misc-forward-declaration-namespace.rst
1 ↗(On Diff #47972)

You also need to add an entry item in docs/clang-tidy/checks/list.rst

6 ↗(On Diff #47972)

Extra blank line here.

ioeric marked 5 inline comments as done.Feb 15 2016, 4:08 AM
ioeric added inline comments.
clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
49 ↗(On Diff #47972)

I didn't know StringRef works with std::map insce it stores a pointer to a c string and the length of the string. But looks like there is a StringMap that can be used with StringRef, I'll look into this.

docs/clang-tidy/checks/misc-forward-declaration-namespace.rst
6 ↗(On Diff #47972)

I saw there are two blank lines put here in many other .rst files. Is this a convention?

ioeric updated this revision to Diff 47977.Feb 15 2016, 4:59 AM
ioeric marked 2 inline comments as done.
  • added check to list.rst; using StringRef as map key to improve effiency

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

ioeric added inline comments.Feb 15 2016, 5:02 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
50 ↗(On Diff #47977)

You are right! llvm::StringRef should work fine with std::map since the related compare operations are provided.

ioeric updated this revision to Diff 47982.Feb 15 2016, 5:23 AM
  • using llvm::StringMap instead of std::map since it is optimized for string key

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

akuegel added inline comments.Feb 15 2016, 6:00 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
81 ↗(On Diff #47982)

Please align with previous line.

125 ↗(On Diff #47982)

nit: namesapce -> namespace.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
11 ↗(On Diff #47982)

True, I just looked at other headers and you are right. Didn't find anything in LLVM styleguide about this though, but maybe I overlooked it.

docs/clang-tidy/checks/misc-forward-declaration-namespace.rst
20 ↗(On Diff #47982)

nit: "warning" -> "warnings", "fix" -> "a fix".

ioeric updated this revision to Diff 47991.Feb 15 2016, 7:20 AM
ioeric marked 8 inline comments as done.
  • removed redundant headers and did more formatting

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

ioeric updated this revision to Diff 48173.Feb 17 2016, 4:39 AM
  • removed unused headers

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

akuegel added inline comments.Feb 17 2016, 4:53 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
56 ↗(On Diff #48173)

nit: add a space between "namespace" and "(".

71 ↗(On Diff #48173)

nit: start variable name with uppercase letter.

72 ↗(On Diff #48173)

nit: start variable name with uppercase letter.

158 ↗(On Diff #48173)

nit: add a space between "name" and "(".

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
58 ↗(On Diff #48173)

Please also remove the trailing "_".

test/clang-tidy/misc-forward-declaration-namespace.cpp
107 ↗(On Diff #48173)

The formatting looks weird. It is a test, but still I think I would prefer the normal formatting.

120 ↗(On Diff #48173)

Same here about formatting.

ioeric updated this revision to Diff 48177.Feb 17 2016, 5:26 AM
ioeric marked 6 inline comments as done.
  • formatting and code styling

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

LGTM
(you need someone else to approve this change though).

hokein added inline comments.Feb 19 2016, 1:59 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
64 ↗(On Diff #48177)

s/classes/Classes
Please make sure all the comments is a complete sentence, use proper capitalization, punctuation.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
21 ↗(On Diff #48177)

missing . at the end of sentence.

test/clang-tidy/misc-forward-declaration-namespace.cpp
4 ↗(On Diff #48177)

s/Delaration/Declaration

8 ↗(On Diff #48177)

Top level means global namespace here? If so, why not using ... found in global namespace?

13 ↗(On Diff #48177)

The same.

15 ↗(On Diff #48177)

:: means anonymous namespace here.

So changing warning messages to found in <namespace-name> namespace is a more clear way, such as:

found in anonymous namespace
found in global namespace
found in 'na' namespace
found in 'nb::na' namespace
alexfh added inline comments.Feb 19 2016, 7:29 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
30 ↗(On Diff #48177)

That will filter out all nested class declarations, which is probably fine, but the comment is somewhat confusing.

44 ↗(On Diff #48177)

Please add a trailing period.

55 ↗(On Diff #48177)

Please use proper Capitalization and punctuation.

70 ↗(On Diff #48177)

Please use proper Capitalization and punctuation.

70 ↗(On Diff #48177)

Enclose inline code snippets in backquotes.

84 ↗(On Diff #48177)

Please use proper punctuation.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
24 ↗(On Diff #48177)

I'm not a native speaker, but "existing" doesn't parse in this phrase ("there is ... existing" sounds weird).

25 ↗(On Diff #48177)

s/is in a potentially wrong namespace/is potentially in a wrong namespace/

35 ↗(On Diff #48177)

"a fix" or "fixes"

Also, to remove redundant text: "This check doesn't suggest fixes at this point."

46 ↗(On Diff #48177)

There should be an extra empty line before private. clang-format should take care of this.

51 ↗(On Diff #48177)

The problem with std::map<llvm::StringRef, ...> is that a StringRef can be created from a temporary std::string or otherwise point to a string which lifetime is less than the lifetime of the map. llvm::StringMap<> is the right thing here.

test/clang-tidy/misc-forward-declaration-namespace.cpp
4 ↗(On Diff #48177)

Trailing period.

8 ↗(On Diff #48177)

We usually specify the whole message in one CHECK line and remove repeated parts (like the name of the check) in all other CHECK lines to make the tests easier to read.

ioeric updated this revision to Diff 48656.Feb 22 2016, 1:29 AM
ioeric marked 18 inline comments as done.
  • Formatted comments; changed namespace print format; remove repeated part

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

alexfh added inline comments.Feb 22 2016, 6:30 AM
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
29 ↗(On Diff #48656)

nit: Please add 3 spaces before the first word to align it with the first word of the previous line. Same for the next line.

30 ↗(On Diff #48656)

nit: Please use backquotes for inline code snippets.

50 ↗(On Diff #48656)

Please use const auto * to make it clear that the pointer points to a constant.

64 ↗(On Diff #48656)

nit: I'd insert an empty line before the comment to make it obvious that it relates to the following lines, not the lines above it.

Same elsewhere.

66 ↗(On Diff #48656)

The last line doesn't parse well. Maybe "reduce false positive rate."?

71 ↗(On Diff #48656)

I meant backquotes, not double quotes:

// `A` will  not be marked as "referenced" in the AST.
100 ↗(On Diff #48656)

Doesn't Decl->getLexicalParent()->printQualifiedName(...) give a reasonable result here?

101 ↗(On Diff #48656)

Heap allocations should be avoided when possible. Using llvm::SmallVector<> is one way to do this here.

101 ↗(On Diff #48656)

nit: s/Stk/Stack/

LLVM code normally uses short names, but here saving two characters at the expense of making the name cryptic is a bad choice, IMO.

102 ↗(On Diff #48656)

nit: const auto *

105 ↗(On Diff #48656)

const auto *

106 ↗(On Diff #48656)
  1. What ensures ParentDecl is indeed a NamespaceDecl? Please add a comment.
  1. If you're sure this cast should always work, use llvm::cast<> instead, it asserts that the type is correct.
113 ↗(On Diff #48656)

I suppose, it won't make much difference here, but it's more common to use llvm::raw_string_ostream for this purpose in LLVM.

114 ↗(On Diff #48656)

nit: No need for the parentheses around the condition.

122 ↗(On Diff #48656)

nit: It's not an Iterator, it's a KeyValue or something like that.

123 ↗(On Diff #48656)

const auto &

138 ↗(On Diff #48656)

const auto *

145 ↗(On Diff #48656)

I'd move the actual name closer to the start of the message:

"declaration '%0' is never referenced, but a declaration with the "
"same name is found in another namespace '%1'"

Also, warning messages are not complete sentences, so they don't need capitalization and a trailing period. Same for notes.

151 ↗(On Diff #48656)

nit: "We only generate" and "for each declaration."

160 ↗(On Diff #48656)

nit: Trailing period missing.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.h
35 ↗(On Diff #48656)

nit: "warnings,"

50 ↗(On Diff #48656)

Use llvm::SmallPtrSet<const Type*, ...>

docs/clang-tidy/checks/misc-forward-declaration-namespace.rst
7 ↗(On Diff #48656)

Don't think so. Rather a copy-paste from a bad source or something like that.

test/clang-tidy/misc-forward-declaration-namespace.cpp
15 ↗(On Diff #48656)

I think, the message should use 'global namespace' only when referring to the global namespace, not anything inside it. We also need to distinguish global namespace from namespace global { ... } (same for "anonymous namespace" vs namespace anonymous { ... }).

found in another namespace '<global>'
found in another namespace '::<anonymous namespace>'
found in another namespace '::asdf::<anonymous namespace>'
ioeric updated this revision to Diff 48680.Feb 22 2016, 7:49 AM
ioeric marked 25 inline comments as done.
  • Changed the implementation of getNameOfNamespace to use provided interface; more formatting

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

alexfh edited edge metadata.Feb 23 2016, 9:47 AM

Looks better now, still a few comments.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
31 ↗(On Diff #48680)

Single backquotes should be fine. Three backquotes are used for starting a code block.

110 ↗(On Diff #48680)

getQualifiedNameAsString is deprecated. You should use printQualifiedName instead.

145 ↗(On Diff #48680)

nit: "We only generates" => "We only generate"

docs/clang-tidy/checks/misc-forward-declaration-namespace.rst
19 ↗(On Diff #48680)

nit: add a comma after "warnings".

test/clang-tidy/misc-forward-declaration-namespace.cpp
15 ↗(On Diff #48680)

How is an anonymous namespace inside a named namespace represented in the message? ::na::(anonymous) or (anonymous)? Please add a test.

ioeric updated this revision to Diff 48903.Feb 24 2016, 3:14 AM
ioeric marked 6 inline comments as done.
ioeric edited edge metadata.
  • added test case for nested anonymous namespace

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

alexfh accepted this revision.Feb 24 2016, 3:29 AM
alexfh edited edge metadata.

Looks good with one last comment. Once you address it, I can submit the patch for you.

clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp
26 ↗(On Diff #48903)
  1. Three backquotes => one backqoute
  2. s/. Eg/, e.g./
  3. s/class A inside A/class A inside A/ (with backqoutes)
This revision is now accepted and ready to land.Feb 24 2016, 3:29 AM
ioeric updated this revision to Diff 48910.Feb 24 2016, 4:25 AM
ioeric marked an inline comment as done.
ioeric edited edge metadata.
  • final formatting

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

The patch doesn't apply cleanly. Please update your working copy and regenerate the patch.

ioeric updated this revision to Diff 48914.Feb 24 2016, 5:10 AM
  • Merge remote-tracking branch 'origin/master'

Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy

[misc-forward-declaration-namespace]

This revision was automatically updated to reflect the committed changes.