[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
juliehockett planned changes to this revision. juliehockett marked 5 inline comments as done. juliehockett added a comment. In https://reviews.llvm.org/D53882#1282219, @aaron.ballman wrote: > I am a bit confused by what this check is trying to accomplish. It seems like > this is intended to catch use of anything declared within the standard > library, but that includes library support things that are needed to write > useful code. For instance, it seems this means users cannot use initializer > lists or type traits. I'm not even certain users can overload `operator > new()` because that uses `std::size_t` as an argument. > > Can you expound on the requirements in a bit more detail? Is it truly "no > standard library functionality at all, including things required to be > supported in freestanding implementations"? Yes. We're in the process of enabling the C++ standard library in the Zircon userspace code, and there is a firm requirement that no kernel code use anything in the std namespace (which declares its own implementations of things like type traits). That said, the policy rollout has shifted a bit since I implemented this and so I'll be reworking it a bit (and also likely renaming it to zircon-kernel-no-std-namespace, so we can disable it for userspace code). Thanks all for your comments! https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
Eugene.Zelenko added inline comments. Comment at: clang-tools-extra/docs/ReleaseNotes.rst:176 + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. JonasToth wrote: > alexfh wrote: > > JonasToth wrote: > > > s/its/it's/ > > > > > > Could `std` be considered code here? Not sure, but maybe using quotes is > > > better? > > Actually, "its" is correct in this context ("its use" vs "it's used"). > whupsi. sry for noise. Please synchronize with first statement in documentation. https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
aaron.ballman added a comment. I am a bit confused by what this check is trying to accomplish. It seems like this is intended to catch use of anything declared within the standard library, but that includes library support things that are needed to write useful code. For instance, it seems this means users cannot use initializer lists or type traits. I'm not even certain users can overload `operator new()` because that uses `std::size_t` as an argument. Can you expound on the requirements in a bit more detail? Is it truly "no standard library functionality at all, including things required to be supported in freestanding implementations"? Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:57-60 + Finder->addMatcher(callExpr(callee(functionDecl(hasDeclContext( + namespaceDecl(isStdNamespace()) + .bind("stdCall"), + this); Is the intention here to diagnose code like this? ``` #include int main() { abort(); // Diagnose this? } ``` https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
JonasToth added inline comments. Comment at: clang-tools-extra/docs/ReleaseNotes.rst:176 + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. alexfh wrote: > JonasToth wrote: > > s/its/it's/ > > > > Could `std` be considered code here? Not sure, but maybe using quotes is > > better? > Actually, "its" is correct in this context ("its use" vs "it's used"). whupsi. sry for noise. https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
alexfh added inline comments. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:30 + if (const NamedDecl *AN = Node.getAliasedNamespace()) { +// If this aliases to an actual namespace, check if its std. +if (const auto *N = dyn_cast(AN)) s/its/it's/, but maybe rephrase this a bit more: ", check that the target namespace of the alias is the `std` namespace.". Comment at: clang-tools-extra/docs/ReleaseNotes.rst:176 + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. JonasToth wrote: > s/its/it's/ > > Could `std` be considered code here? Not sure, but maybe using quotes is > better? Actually, "its" is correct in this context ("its use" vs "it's used"). Comment at: clang-tools-extra/docs/ReleaseNotes.rst:177 + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. + I guess that you're referring here to this wording from https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md: "Zircon does not use the C++ standard library". If so, it's better to avoid using the term `libc++`, which is the name of one particular implementation of the C++ standard library (http://libcxx.llvm.org/docs/). https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
steveire added inline comments. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:52 + Finder->addMatcher( + valueDecl(hasType(decl(hasDeclContext(namespaceDecl(isStdNamespace()) + .bind("stdVar"), Recommend extracting `namespaceDecl(isStdNamespace())` to a local variable. Comment at: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp:13 +int b = func(); +std_int c; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code Are you sure it's wise to warn on every declaration inside the `std` namespace? Surely warning only on declarations outside it is enough. https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
JonasToth added inline comments. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:71 +void NoStdNamespaceCheck::check(const MatchFinder::MatchResult ) { + if (const auto *D = Result.Nodes.getNodeAs("stdVar")) +diag(D->getBeginLoc(), juliehockett wrote: > JonasToth wrote: > > Please create a `StringRef` for the diagnostic message and reuse that. > > > > Did you consider merging all `Decl` classes if you just use > > `getNodeAs("common_decl_name")`? > Yes, but getting the location right posed an issue there. The `std` token is > not always at `D->getLocation()`. Ok, thats unfortunate but no problem :) Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:52 + Finder->addMatcher( + valueDecl(hasType(decl(hasDeclContext(namespaceDecl(isStdNamespace()) + .bind("stdVar"), are `FunctionDecl`, `RecordDecl` (maybe better `TagDecl`) covered already? Comment at: clang-tools-extra/docs/ReleaseNotes.rst:176 + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. s/its/it's/ Could `std` be considered code here? Not sure, but maybe using quotes is better? Comment at: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp:33 +int x = func(); +std::std_int y; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code juliehockett wrote: > JonasToth wrote: > > What about using `int64_t` and these typedefs. Are they forbidden, too? > Yes, which is why that's tested. Is there an additional test case I'm missing > regarding those? Thanks for clarifying. I think the usual problems (templates, macros) would need some test coverage. Especially ``` template void MyFunc(); ``` these cases are interesting to check. https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
juliehockett updated this revision to Diff 171810. juliehockett marked 5 inline comments as done. https://reviews.llvm.org/D53882 Files: clang-tools-extra/clang-tidy/zircon/CMakeLists.txt clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.h clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp Index: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp === --- /dev/null +++ clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp @@ -0,0 +1,50 @@ +// RUN: %check_clang_tidy %s zircon-no-std-namespace %t + +int func() { return 1; } + +namespace std { +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +typedef int std_int; +int std_func() { return 1; } + +int a = 1; +int b = func(); +std_int c; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int d = std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +} + +// Warn on uses of quailfied std namespace. +int i = 1; +int j = func(); +std::std_int k; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int l = std::std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + + +namespace foo { + +int w = 1; +int x = func(); +std::std_int y; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int z = std::std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +} + +// Warn on the alias declaration, and on users of it. +namespace bar = std; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use of the 'std' namespace is not allowed in Zircon kernel code +bar::std_int q; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +// Warn on the using declaration, and on users of it. +using namespace std; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use of the 'std' namespace is not allowed in Zircon kernel code +std_int r = 1; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code Index: clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst === --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst @@ -0,0 +1,30 @@ +.. title:: clang-tidy - zircon-no-std-namespace + +zircon-no-std-namespace +=== + +Ensures code does not use ``namespace std`` as that violates Zircon's +kernel libc++ policy. + +Any code that uses: + +.. code-block:: c++ + + namespace std { + ... + } + +or + +.. code-block:: c++ + + using namespace std; + +or uses a declaration or function from the ``std`` namespace: + +.. code-block:: c++ + + std::string foo; + std::move(foo); + +will be prompted with a warning. Index: clang-tools-extra/docs/clang-tidy/checks/list.rst === --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -252,4 +252,5 @@ readability-string-compare readability-uniqueptr-delete-release readability-uppercase-literal-suffix + zircon-no-std-namespace zircon-temporary-objects Index: clang-tools-extra/docs/ReleaseNotes.rst === --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -170,6 +170,12 @@ ` check does not warn about calls inside macros anymore by default. +- New :doc:`zircon-no-std-namespace + ` check. + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. + Improvements to include-fixer - Index: clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp === --- clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp +++ clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "NoStdNamespaceCheck.h" #include "TemporaryObjectsCheck.h" using namespace clang::ast_matchers; @@ -22,6 +23,8 @@ class ZirconModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories ) override { +
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
juliehockett added inline comments. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:71 +void NoStdNamespaceCheck::check(const MatchFinder::MatchResult ) { + if (const auto *D = Result.Nodes.getNodeAs("stdVar")) +diag(D->getBeginLoc(), JonasToth wrote: > Please create a `StringRef` for the diagnostic message and reuse that. > > Did you consider merging all `Decl` classes if you just use > `getNodeAs("common_decl_name")`? Yes, but getting the location right posed an issue there. The `std` token is not always at `D->getLocation()`. Comment at: clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst:1 +.. title:: clang-tidy - zircon-no-std-namespace + JonasToth wrote: > Could you please clarify what exactly is forbidden? > > __Using__ stuff from `std` like `std::string` in normal "user" code or > __opening__ `std` to add stuff? > > If the first thing is the case why are the variables flagged but not the > functions? And adding things to namespace `std` is AFAIK UB with the > exception of template-function specializations, so maybe that would be worth > a general check? Both. Documentation updated -- the uses in particular are to be flagged. Comment at: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp:33 +int x = func(); +std::std_int y; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code JonasToth wrote: > What about using `int64_t` and these typedefs. Are they forbidden, too? Yes, which is why that's tested. Is there an additional test case I'm missing regarding those? https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
JonasToth added inline comments. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:29 +AST_MATCHER(NamespaceAliasDecl, isAliasedToStd) { + if (const auto *AN = Node.getAliasedNamespace()) { +// If this aliases to an actual namespace, check if its std. If it doesn't, please do not use `auto` here as the type is not clear, same below (line 32 right now) Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:31 +// If this aliases to an actual namespace, check if its std. If it doesn't, +// it aliases to another alias and thus shouldn't be flagged. +if (const auto *N = dyn_cast(AN)) The second sentence sounds a bit weird. I think you can even ellide it completely, as the first sentence does clarify quite well. Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp:71 +void NoStdNamespaceCheck::check(const MatchFinder::MatchResult ) { + if (const auto *D = Result.Nodes.getNodeAs("stdVar")) +diag(D->getBeginLoc(), Please create a `StringRef` for the diagnostic message and reuse that. Did you consider merging all `Decl` classes if you just use `getNodeAs("common_decl_name")`? Comment at: clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.h:1 +//===--- NoStdNamespace.h - clang-tidy---*- C++ -*-===// +// There was a bug in the template, please add a blank after `clang-tidy` but keep the total length. Comment at: clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp:28 "zircon-temporary-objects"); +CheckFactories.registerCheck( +"zircon-no-std-namespace"); please keep lexigraphical ordering. Comment at: clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst:1 +.. title:: clang-tidy - zircon-no-std-namespace + Could you please clarify what exactly is forbidden? __Using__ stuff from `std` like `std::string` in normal "user" code or __opening__ `std` to add stuff? If the first thing is the case why are the variables flagged but not the functions? And adding things to namespace `std` is AFAIK UB with the exception of template-function specializations, so maybe that would be worth a general check? Comment at: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp:33 +int x = func(); +std::std_int y; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code What about using `int64_t` and these typedefs. Are they forbidden, too? Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D53882 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D53882: [clang-tidy] Adding Zircon checker for std namespace
juliehockett created this revision. juliehockett added reviewers: aaron.ballman, hokein, ilya-biryukov. juliehockett added a project: clang-tools-extra. Herald added subscribers: xazax.hun, mgorny. Adds a checker to warn against using the std namespace, as Zircon's kernel lib++ policy does not allow it. Written documentation of the policy is not yet published, I will add the link when it is. https://reviews.llvm.org/D53882 Files: clang-tools-extra/clang-tidy/zircon/CMakeLists.txt clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.cpp clang-tools-extra/clang-tidy/zircon/NoStdNamespaceCheck.h clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp Index: clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp === --- /dev/null +++ clang-tools-extra/test/clang-tidy/zircon-no-std-namespace.cpp @@ -0,0 +1,50 @@ +// RUN: %check_clang_tidy %s zircon-no-std-namespace %t + +int func() { return 1; } + +namespace std { +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +typedef int std_int; +int std_func() { return 1; } + +int a = 1; +int b = func(); +std_int c; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int d = std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +} + +// Warn on uses of quailfied std namespace. +int i = 1; +int j = func(); +std::std_int k; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int l = std::std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + + +namespace foo { + +int w = 1; +int x = func(); +std::std_int y; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code +int z = std::std_func(); +// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +} + +// Warn on the alias declaration, and on users of it. +namespace bar = std; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use of the 'std' namespace is not allowed in Zircon kernel code +bar::std_int q; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code + +// Warn on the using declaration, and on users of it. +using namespace std; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use of the 'std' namespace is not allowed in Zircon kernel code +std_int r = 1; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of the 'std' namespace is not allowed in Zircon kernel code Index: clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst === --- /dev/null +++ clang-tools-extra/docs/clang-tidy/checks/zircon-no-std-namespace.rst @@ -0,0 +1,29 @@ +.. title:: clang-tidy - zircon-no-std-namespace + +zircon-no-std-namespace +=== + +Ensures code does not open ``namespace std`` as that violates Zircon's +kernel libc++ policy. + +Any code that uses: + +.. code-block:: c++ + + namespace std { + ... + } + +or + +.. code-block:: c++ + + using namespace std; + +or uses a declaration from the ``std`` namespace: + +.. code-block:: c++ + + std::string foo; + +will be prompted with a warning. Index: clang-tools-extra/docs/clang-tidy/checks/list.rst === --- clang-tools-extra/docs/clang-tidy/checks/list.rst +++ clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -252,4 +252,5 @@ readability-string-compare readability-uniqueptr-delete-release readability-uppercase-literal-suffix + zircon-no-std-namespace zircon-temporary-objects Index: clang-tools-extra/docs/ReleaseNotes.rst === --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -170,6 +170,12 @@ ` check does not warn about calls inside macros anymore by default. +- New :doc:`zircon-no-std-namespace + ` check. + + Warns when the `std` namespace is used, as its use is against Zircon's libc++ + policy for the kernel. + Improvements to include-fixer - Index: clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp === --- clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp +++ clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp @@ -10,6 +10,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include