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.
Details
- Reviewers
akuegel hokein alexfh - Commits
- rGea9fd9921508: [clang-tidy] Added a check for forward declaration in the potentially wrong…
rCTE261737: [clang-tidy] Added a check for forward declaration in the potentially wrong…
rL261737: [clang-tidy] Added a check for forward declaration in the potentially wrong…
Diff Detail
- Repository
- rL LLVM
Event Timeline
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. |
- 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 |
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 |
22 ↗ | (On Diff #47796) | It would be nice if you can add a comment here describing what your check does. |
42 ↗ | (On Diff #47796) | Don't forget to update the comment here accordingly. |
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. |
- 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]
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_ |
- 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]
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© 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. |
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? |
- 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]
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. |
- 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]
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". |
- 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]
- removed unused headers
Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy
[misc-forward-declaration-namespace]
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. |
- formatting and code styling
Updating D17195: Added a check for forward declaration in the potentially wrong namespace in clang-tidy
[misc-forward-declaration-namespace]
clang-tidy/misc/ForwardDeclarationNamespaceCheck.cpp | ||
---|---|---|
64 ↗ | (On Diff #48177) | s/classes/Classes |
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 |
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. |
- 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]
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) |
|
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>' |
- 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]
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. |
- 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]
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) |
|
- 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.
- 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]