This is an archive of the discontinued LLVM Phabricator instance.

[Clang-tidy] CERT-DCL58-CPP (checker for std namespace modification)
ClosedPublic

Authored by xazax.hun on Aug 11 2016, 1:17 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

falho updated this revision to Diff 67734.Aug 11 2016, 1:17 PM
falho retitled this revision from to CERT-MSC53-CPP (checker for std namespace modification).
falho updated this object.
falho added reviewers: xazax.hun, o.gyorgy, alex.

Please mention this check in docs/ReleaseNotes.rst (in alphabetical order).

docs/clang-tidy/checks/cert-msc53-cpp.rst
6 ↗(On Diff #67734)

Please highlight std and posix with ``.

Eugene.Zelenko retitled this revision from CERT-MSC53-CPP (checker for std namespace modification) to [Clang-tidy] CERT-MSC53-CPP (checker for std namespace modification).Aug 11 2016, 1:39 PM
Eugene.Zelenko set the repository for this revision to rL LLVM.
Eugene.Zelenko added a subscriber: cfe-commits.
aaron.ballman edited edge metadata.Aug 11 2016, 2:28 PM

Thank you for working on this check! There is a slight problem, however, in that the check as-written will flag false positives because there are times when it is permissible to modify the std namespace. The important bit of the CERT requirement is "Do not add declarations or definitions to the standard namespaces std or posix, or to a namespace contained therein, except for a template specialization that depends on a user-defined type that meets the standard library requirements for the original template." So the part that's missing from this check is excluding template specializations that depend on user-defined types. For instance, the following should be valid:

namespace std {
template <>
struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
}

(This comes from Error.h in LLVM.)

clang-tidy/cert/CERTTidyModule.cpp
37–38 ↗(On Diff #67734)

Should put this under a // MSC comment rather than the // DCL one.

clang-tidy/cert/DontModifyStdNamespaceCheck.cpp
33 ↗(On Diff #67734)

Should use format specifiers instead of building the string manually. e.g., using %0. Also, diagnostics should start with a lowercase letter and "to" should be "in".

clang-tidy/cert/DontModifyStdNamespaceCheck.h
19 ↗(On Diff #67734)

s/to/in

docs/clang-tidy/checks/list.rst
12 ↗(On Diff #67734)

Should keep this in alphabetical order.

hokein added inline comments.Aug 12 2016, 3:20 AM
clang-tidy/cert/DontModifyStdNamespaceCheck.cpp
20 ↗(On Diff #67734)

This check should be only available in cpp code.

if (!getLangOpts().CPlusPlus)
  return;
docs/clang-tidy/checks/cert-msc53-cpp.rst
4 ↗(On Diff #67734)

=== should be the same with cert-msc53-cpp.

6 ↗(On Diff #67734)

Adding an example explaining the check would make the doc more clear. An example is worth a thousand words :)

test/clang-tidy/cert-dont-modify-std-namespace.cpp
1 ↗(On Diff #67734)

Do we need c++1z flag here?

xazax.hun added inline comments.Aug 12 2016, 3:23 AM
test/clang-tidy/cert-dont-modify-std-namespace.cpp
1 ↗(On Diff #67734)

I think it is there for the following code snippet:

namespace posix::a {
xazax.hun edited edge metadata.Feb 6 2017, 6:44 AM

Benedek, do you have time to address the review comments or do you want me to commandeer this revision?

falho added a comment.Feb 7 2017, 8:45 AM

Unfortunately I wont have time to work on this check anymore... thank you for understanding!

xazax.hun commandeered this revision.Feb 14 2017, 1:20 AM
xazax.hun edited reviewers, added: falho; removed: xazax.hun.
xazax.hun updated this revision to Diff 88333.Feb 14 2017, 2:05 AM
xazax.hun marked 9 inline comments as done.
  • Updated to latest trunk.
  • The cert rule was renamed, the patch is updated accordingly.
  • Fixes as per review comments.
aaron.ballman added inline comments.Feb 14 2017, 12:53 PM
clang-tidy/cert/DontModifyStdNamespaceCheck.cpp
34 ↗(On Diff #88333)

I think this will still trigger false positives because there are times when it is permissible to modify the std namespace outside of a system header. The important bit of the CERT requirement is "Do not add declarations or definitions to the standard namespaces std or posix, or to a namespace contained therein, except for a template specialization that depends on a user-defined type that meets the standard library requirements for the original template." So the part that's missing from this check is excluding template specializations that depend on user-defined types. For instance, the following should be valid:

namespace std {
template <>
struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
}

(This comes from Error.h in LLVM.)

36 ↗(On Diff #88333)

No need to call getName() -- you can pass N directly. When you correct this, also remove the explicit quotes from the diagnostic (they're added automatically by the diagnostics engine).

docs/clang-tidy/checks/cert-dcl58-cpp.rst
8 ↗(On Diff #88333)

You should add a link to the CERT guideline this addresses. See docs/clang-tidy/checks/cert-dcl50-cpp.rst as an example.

xazax.hun updated this revision to Diff 88506.Feb 15 2017, 4:15 AM
xazax.hun marked 3 inline comments as done.
xazax.hun retitled this revision from [Clang-tidy] CERT-MSC53-CPP (checker for std namespace modification) to [Clang-tidy] CERT-DCL58-CPP (checker for std namespace modification).
xazax.hun edited the summary of this revision. (Show Details)
  • Add a simulated system header for tests.
  • Do not warn on explicit template specializations within std namespace.
  • Do not warn when the namespace is empty (so no declaration is introduced within the namespace)
  • Extended the test cases.
  • Added a link to the documentation
  • Updated the name of the checker in the description and title of this revision.
aaron.ballman added inline comments.Feb 15 2017, 5:48 AM
clang-tidy/cert/DontModifyStdNamespaceCheck.cpp
28 ↗(On Diff #88506)

I think this is missing function declarations that are explicit template specializations as well (a common instance of this is specializing std::swap).

test/clang-tidy/cert-dcl58-cpp.cpp
60 ↗(On Diff #88506)

I'd like to see a test where the user specializes a function template, like swap() to make sure we don't diagnose on that.

xazax.hun updated this revision to Diff 88541.Feb 15 2017, 8:11 AM
  • Do not warn for function specializations within the std namespace.
  • Add a test case for swap.
xazax.hun marked 3 inline comments as done.Feb 15 2017, 8:12 AM
aaron.ballman accepted this revision.Feb 15 2017, 11:13 AM

LGTM, thank you for working on this!

This revision is now accepted and ready to land.Feb 15 2017, 11:13 AM
This revision was automatically updated to reflect the committed changes.