[PATCH] D55409: [clang-tidy] check for using declarations not in an anonymous namespace when there exists one
Ywicheng created this revision. Ywicheng added reviewers: alexfh, hokein, aaron.ballman, astrelni, EricWF, JonasToth. Herald added subscribers: cfe-commits, xazax.hun, mgorny. This patch adds one check corresponding to the Unnamed Namespace section in https://abseil.io/tips/119. In particular, if there exists an anonymous, it is better to put all aliases there. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D55409 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h clang-tidy/abseil/CMakeLists.txt docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp Index: test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp === --- /dev/null +++ test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy %s abseil-anonymous-enclosed-aliases %t +namespace bar { + +class something { + +}; +} + +namespace foo { + +using bar::something; + +namespace { + +} // anonymous namespace +} // namespace foo +// CHECK-MESSAGES: :[[@LINE-6]]:12: warning: UsingDecl 'something' should be in the anonymous namespace. See: https://abseil.io/tips/119 [abseil-anonymous-enclosed-aliases] + + +namespace foo { + +namespace { + +using ::bar::something; + +} // anonymous namespace +} // namespace foo Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -4,14 +4,15 @@ = .. toctree:: + abseil-anonymous-enclosed-aliases abseil-duration-division abseil-duration-factory-float abseil-faster-strsplit-delimiter abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -151,6 +152,7 @@ hicpp-special-member-functions (redirects to cppcoreguidelines-special-member-functions) hicpp-static-assert (redirects to misc-static-assert) hicpp-undelegated-constructor (redirects to bugprone-undelegated-constructor) + hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) hicpp-use-auto (redirects to modernize-use-auto) hicpp-use-emplace (redirects to modernize-use-emplace) hicpp-use-equals-default (redirects to modernize-use-equals-default) @@ -159,7 +161,6 @@ hicpp-use-nullptr (redirects to modernize-use-nullptr) hicpp-use-override (redirects to modernize-use-override) hicpp-vararg (redirects to cppcoreguidelines-pro-type-vararg) - hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) llvm-header-guard llvm-include-order llvm-namespace-comment Index: docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst @@ -0,0 +1,6 @@ +.. title:: clang-tidy - abseil-anonymous-enclosed-aliases + +abseil-anonymous-enclosed-aliases += + +FIXME: Describe what patterns does the check detect and why. Give examples. Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,11 @@ Improvements to clang-tidy -- +- New :doc:`abseil-anonymous-enclosed-aliases + ` check. + + FIXME: add release notes. + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/abseil/CMakeLists.txt === --- clang-tidy/abseil/CMakeLists.txt +++ clang-tidy/abseil/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAbseilModule AbseilTidyModule.cpp + AnonymousEnclosedAliasesCheck.cpp DurationDivisionCheck.cpp DurationFactoryFloatCheck.cpp FasterStrsplitDelimiterCheck.cpp Index: clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h === --- /dev/null +++ clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h @@ -0,0 +1,40 @@ +//===--- AnonymousEnclosedAliasesCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENCLOSEDALIASESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENCLOSEDA
[PATCH] D55409: [clang-tidy] check for using declarations not in an anonymous namespace when there exists one
Ywicheng updated this revision to Diff 177115. Ywicheng added a comment. Updated abseil-anonymous-enclosed-alises.rst and ReleaseNotes.rst CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55409/new/ https://reviews.llvm.org/D55409 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h clang-tidy/abseil/CMakeLists.txt docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp Index: test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp === --- /dev/null +++ test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy %s abseil-anonymous-enclosed-aliases %t +namespace bar { + +class something { + +}; +} + +namespace foo { + +using bar::something; + +namespace { + +} // anonymous namespace +} // namespace foo +// CHECK-MESSAGES: :[[@LINE-6]]:12: warning: UsingDecl 'something' should be in the anonymous namespace. See: https://abseil.io/tips/119 [abseil-anonymous-enclosed-aliases] + + +namespace foo { + +namespace { + +using ::bar::something; + +} // anonymous namespace +} // namespace foo Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -4,14 +4,15 @@ = .. toctree:: + abseil-anonymous-enclosed-aliases abseil-duration-division abseil-duration-factory-float abseil-faster-strsplit-delimiter abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -151,6 +152,7 @@ hicpp-special-member-functions (redirects to cppcoreguidelines-special-member-functions) hicpp-static-assert (redirects to misc-static-assert) hicpp-undelegated-constructor (redirects to bugprone-undelegated-constructor) + hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) hicpp-use-auto (redirects to modernize-use-auto) hicpp-use-emplace (redirects to modernize-use-emplace) hicpp-use-equals-default (redirects to modernize-use-equals-default) @@ -159,7 +161,6 @@ hicpp-use-nullptr (redirects to modernize-use-nullptr) hicpp-use-override (redirects to modernize-use-override) hicpp-vararg (redirects to cppcoreguidelines-pro-type-vararg) - hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) llvm-header-guard llvm-include-order llvm-namespace-comment Index: docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst @@ -0,0 +1,20 @@ +.. title:: clang-tidy - abseil-anonymous-enclosed-aliases + +abseil-anonymous-enclosed-aliases += + +Finds using declarations outside of anonymous namespaces, and +suggests those declarations be moved to that namespace. + +Example: +.. code-block:: c++ + + namespace foo { + + using something; // should be inside the anonymous namespace below + + namespace { + + } // anonymous namespace + + } // foo Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-anonymous-enclosed-aliases + ` check. + + Finds instances of using declarations not in an anonymous namespace + when there exists one. + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/abseil/CMakeLists.txt === --- clang-tidy/abseil/CMakeLists.txt +++ clang-tidy/abseil/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAbseilModule AbseilTidyModule.cpp + AnonymousEnclosedAliasesCheck.cpp DurationDivisionCheck.cpp DurationFactoryFloatCheck.cpp FasterStrsplitDelimiterCheck.cpp Index: clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h === --- /dev/null +++ clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h @@ -0,0 +1,40 @@ +//===--- AnonymousEnclosedAliasesCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_
[PATCH] D55409: [clang-tidy] check for using declarations not in an anonymous namespace when there exists one
Ywicheng updated this revision to Diff 177121. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55409/new/ https://reviews.llvm.org/D55409 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h clang-tidy/abseil/CMakeLists.txt docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp Index: test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp === --- /dev/null +++ test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy %s abseil-anonymous-enclosed-aliases %t +namespace bar { + +class something { + +}; +} + +namespace foo { + +using bar::something; + +namespace { + +} // anonymous namespace +} // namespace foo +// CHECK-MESSAGES: :[[@LINE-6]]:12: warning: UsingDecl 'something' should be in the anonymous namespace. See: https://abseil.io/tips/119 [abseil-anonymous-enclosed-aliases] + + +namespace foo { + +namespace { + +using ::bar::something; + +} // anonymous namespace +} // namespace foo Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -4,14 +4,15 @@ = .. toctree:: + abseil-anonymous-enclosed-aliases abseil-duration-division abseil-duration-factory-float abseil-faster-strsplit-delimiter abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -151,6 +152,7 @@ hicpp-special-member-functions (redirects to cppcoreguidelines-special-member-functions) hicpp-static-assert (redirects to misc-static-assert) hicpp-undelegated-constructor (redirects to bugprone-undelegated-constructor) + hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) hicpp-use-auto (redirects to modernize-use-auto) hicpp-use-emplace (redirects to modernize-use-emplace) hicpp-use-equals-default (redirects to modernize-use-equals-default) @@ -159,7 +161,6 @@ hicpp-use-nullptr (redirects to modernize-use-nullptr) hicpp-use-override (redirects to modernize-use-override) hicpp-vararg (redirects to cppcoreguidelines-pro-type-vararg) - hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) llvm-header-guard llvm-include-order llvm-namespace-comment Index: docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst @@ -0,0 +1,20 @@ +.. title:: clang-tidy - abseil-anonymous-enclosed-aliases + +abseil-anonymous-enclosed-aliases += + +Finds using declarations outside of anonymous namespaces, and +suggests those declarations be moved to that namespace. + +Example: +.. code-block:: c++ + + namespace foo { + + using something; // should be inside the anonymous namespace below + + namespace { + + } // anonymous namespace + + } // foo Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-anonymous-enclosed-aliases + ` check. + + Finds instances of using declarations not in an anonymous namespace + when there exists one. + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/abseil/CMakeLists.txt === --- clang-tidy/abseil/CMakeLists.txt +++ clang-tidy/abseil/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAbseilModule AbseilTidyModule.cpp + AnonymousEnclosedAliasesCheck.cpp DurationDivisionCheck.cpp DurationFactoryFloatCheck.cpp FasterStrsplitDelimiterCheck.cpp Index: clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h === --- /dev/null +++ clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h @@ -0,0 +1,40 @@ +//===--- AnonymousEnclosedAliasesCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENCLOSEDALIASESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENC
[PATCH] D55411: [clang-tidy] check for flagging using declarations not in the inner most namespace
Ywicheng created this revision. Ywicheng added a reviewer: JonasToth. Herald added subscribers: cfe-commits, xazax.hun, mgorny. This patch adds one check corresponding to the Scope of the Alias section in https://abseil.io/tips/119. In particular, it is better to put aliases in the innermost namespace Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D55411 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/SafelyScopedCheck.cpp clang-tidy/abseil/SafelyScopedCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-safely-scoped.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-safely-scoped.cpp Index: test/clang-tidy/abseil-safely-scoped.cpp === --- /dev/null +++ test/clang-tidy/abseil-safely-scoped.cpp @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy %s abseil-safely-scoped %t +namespace bar { + +class something { + +}; +} + +namespace foo { + +using bar::something; + +namespace { + +} // anonymous namespace +} // namespace foo +// CHECK-MESSAGES: :[[@LINE-6]]:12: warning: UsingDecl 'something' should be in the innermost scope. See: https://abseil.io/tips/119 [abseil-safely-scoped] + +namespace foo { + +namespace { + +using ::bar::something; + +} // anonymous namespace +} // namespace foo + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -10,8 +10,9 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith + abseil-safely-scoped abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -151,6 +152,7 @@ hicpp-special-member-functions (redirects to cppcoreguidelines-special-member-functions) hicpp-static-assert (redirects to misc-static-assert) hicpp-undelegated-constructor (redirects to bugprone-undelegated-constructor) + hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) hicpp-use-auto (redirects to modernize-use-auto) hicpp-use-emplace (redirects to modernize-use-emplace) hicpp-use-equals-default (redirects to modernize-use-equals-default) @@ -159,7 +161,6 @@ hicpp-use-nullptr (redirects to modernize-use-nullptr) hicpp-use-override (redirects to modernize-use-override) hicpp-vararg (redirects to cppcoreguidelines-pro-type-vararg) - hicpp-uppercase-literal-suffix (redirects to readability-uppercase-literal-suffix) llvm-header-guard llvm-include-order llvm-namespace-comment Index: docs/clang-tidy/checks/abseil-safely-scoped.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-safely-scoped.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-safely-scoped + +abseil-safely-scoped + + +Flags using declarations that are not contained in an innermost +namespace, and suggests these declarations be moved elsewhere. + +Example: + +.. code-block:: c++ + + using something; // should be inside the innermost namespace bar below + + namespace foo { + namespace bar { + + } // bar + + using something_else; // shoulw be inside the innermost namespace bar above + + } // foo + +Placing convenience aliases in upper level namespaces can lead to ambiguity in +which name the compiler should use. + +See https://abseil.io/tips/119 for more explanation. + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-safely-scoped + ` check. + + Finds instances of using declarations not in the innermost layer + of a series of namespaces. + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/abseil/SafelyScopedCheck.h === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.h @@ -0,0 +1,36 @@ +//===--- SafelyScopedCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting using declarations not in a namespace declaration or not in +/// the innermost layer of namespace declarations. +/// +/// For the user-facing documentation see:
[PATCH] D55411: [clang-tidy] check for flagging using declarations not in the inner most namespace
Ywicheng updated this revision to Diff 194614. Ywicheng marked an inline comment as done. Ywicheng edited the summary of this revision. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55411/new/ https://reviews.llvm.org/D55411 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/SafelyScopedCheck.cpp clang-tidy/abseil/SafelyScopedCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-safely-scoped.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-safely-scoped.cpp Index: test/clang-tidy/abseil-safely-scoped.cpp === --- /dev/null +++ test/clang-tidy/abseil-safely-scoped.cpp @@ -0,0 +1,43 @@ +// RUN: %check_clang_tidy %s abseil-safely-scoped %t +namespace bar { + +class A {}; +class B {}; +} // namespace bar + +namespace foo1 { + +using bar::A; +void f(A a); + +namespace {} // anonymous namespace +} // namespace foo1 +// CHECK-MESSAGES: :[[@LINE-4]]:12: warning: using declaration 'A' not +// declared in the innermost namespace. [abseil-safely-scoped] + +namespace foo2 { + +namespace { + +using ::bar::B; + +} // anonymous namespace +void g(B b); +} // namespace foo2 + + +// Check should not be triggered below when we are at +// function (instead of namespace) scope. +namespace outer { + + int fun_scope() { +using ::bar::A; +return 0; + } // function scope + + namespace inner { + + } // namespace inner + +} // namespace outer + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -16,6 +16,7 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls + abseil-safely-scoped abseil-str-cat-append abseil-string-find-startswith abseil-time-subtraction Index: docs/clang-tidy/checks/abseil-safely-scoped.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-safely-scoped.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-safely-scoped + +abseil-safely-scoped + + +Flags using declarations that are not contained in an innermost +namespace, and suggests these declarations be moved elsewhere. + +Example: + +.. code-block:: c++ + + using something; // should be inside the innermost namespace bar below + + namespace foo { + namespace bar { + + } // bar + + using something_else; // shoulw be inside the innermost namespace bar above + + } // foo + +Placing convenience aliases in upper level namespaces can lead to ambiguity in +which name the compiler should use. + +See https://abseil.io/tips/119 for more explanation. + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,8 +67,15 @@ Improvements to clang-tidy -- +- New :doc:`abseil-safely-scoped + ` check. + + Finds instances of using declarations not in the innermost layer + of a series of namespaces. + - New :doc:`abseil-duration-addition ` check. +>>> master Checks for cases where addition should be performed in the ``absl::Time`` domain. Index: clang-tidy/abseil/SafelyScopedCheck.h === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.h @@ -0,0 +1,36 @@ +//===--- SafelyScopedCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting using declarations not in a namespace declaration or not in +/// the innermost layer of namespace declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-safely-scoped.html +class SafelyScopedCheck : public ClangTidyCheck { +public: + SafelyScopedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace abseil +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H Index: clang-tidy/abseil/SafelyScopedCheck.cpp === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.cpp @@ -0,0 +1,43 @@ +//===--- Safely
[PATCH] D55411: [clang-tidy] check for flagging using declarations not in the inner most namespace
Ywicheng updated this revision to Diff 194623. Ywicheng marked 2 inline comments as done. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55411/new/ https://reviews.llvm.org/D55411 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/SafelyScopedCheck.cpp clang-tidy/abseil/SafelyScopedCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-safely-scoped.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-safely-scoped.cpp Index: test/clang-tidy/abseil-safely-scoped.cpp === --- /dev/null +++ test/clang-tidy/abseil-safely-scoped.cpp @@ -0,0 +1,43 @@ +// RUN: %check_clang_tidy %s abseil-safely-scoped %t +namespace bar { + +class A {}; +class B {}; +} // namespace bar + +namespace foo1 { + +using bar::A; +void f(A a); + +namespace {} // anonymous namespace +} // namespace foo1 +// CHECK-MESSAGES: :[[@LINE-5]]:12: warning: using declaration 'A' not +// declared in the innermost namespace. [abseil-safely-scoped] + +namespace foo2 { + +namespace { + +using ::bar::B; + +} // anonymous namespace +void g(B b); +} // namespace foo2 + + +// Check should not be triggered below when we are at +// function (instead of namespace) scope. +namespace outer { + + int fun_scope() { +using ::bar::A; +return 0; + } // function scope + + namespace inner { + + } // namespace inner + +} // namespace outer + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -16,6 +16,7 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls + abseil-safely-scoped abseil-str-cat-append abseil-string-find-startswith abseil-time-subtraction Index: docs/clang-tidy/checks/abseil-safely-scoped.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-safely-scoped.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-safely-scoped + +abseil-safely-scoped + + +Flags using declarations that are not contained in an innermost +namespace, and suggests these declarations be moved elsewhere. + +Example: + +.. code-block:: c++ + + using something; // should be inside the innermost namespace bar below + + namespace foo { + namespace bar { + + } // bar + + using something_else; // shoulw be inside the innermost namespace bar above + + } // foo + +Placing convenience aliases in upper level namespaces can lead to ambiguity in +which name the compiler should use. + +See https://abseil.io/tips/119 for more explanation. + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,8 +67,15 @@ Improvements to clang-tidy -- +- New :doc:`abseil-safely-scoped + ` check. + + Finds instances of using declarations not in the innermost layer + of a series of namespaces. + - New :doc:`abseil-duration-addition ` check. +>>> master Checks for cases where addition should be performed in the ``absl::Time`` domain. Index: clang-tidy/abseil/SafelyScopedCheck.h === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.h @@ -0,0 +1,36 @@ +//===--- SafelyScopedCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting using declarations not in a namespace declaration or not in +/// the innermost layer of namespace declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-safely-scoped.html +class SafelyScopedCheck : public ClangTidyCheck { +public: + SafelyScopedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace abseil +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H Index: clang-tidy/abseil/SafelyScopedCheck.cpp === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.cpp @@ -0,0 +1,43 @@ +//===--- SafelyScopedCheck.cpp - clang-tidy -
[PATCH] D55411: [clang-tidy] check for flagging using declarations not in the inner most namespace
Ywicheng updated this revision to Diff 194631. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55411/new/ https://reviews.llvm.org/D55411 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/SafelyScopedCheck.cpp clang-tidy/abseil/SafelyScopedCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-safely-scoped.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-safely-scoped.cpp Index: test/clang-tidy/abseil-safely-scoped.cpp === --- /dev/null +++ test/clang-tidy/abseil-safely-scoped.cpp @@ -0,0 +1,43 @@ +// RUN: %check_clang_tidy %s abseil-safely-scoped %t +namespace bar { + +class A {}; +class B {}; +} // namespace bar + +namespace foo1 { + +using bar::A; +void f(A a); + +namespace {} // anonymous namespace +} // namespace foo1 +// CHECK-MESSAGES: :[[@LINE-5]]:12: warning: using declaration 'A' not +// declared in the innermost namespace. [abseil-safely-scoped] + +namespace foo2 { + +namespace { + +using ::bar::B; + +} // anonymous namespace +void g(B b); +} // namespace foo2 + + +// Check should not be triggered below when we are at +// function (instead of namespace) scope. +namespace outer { + + int fun_scope() { +using ::bar::A; +return 0; + } // function scope + + namespace inner { + + } // namespace inner + +} // namespace outer + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -16,6 +16,7 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls + abseil-safely-scoped abseil-str-cat-append abseil-string-find-startswith abseil-time-subtraction Index: docs/clang-tidy/checks/abseil-safely-scoped.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-safely-scoped.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-safely-scoped + +abseil-safely-scoped + + +Flags using declarations that are not contained in an innermost +namespace, and suggests these declarations be moved elsewhere. + +Example: + +.. code-block:: c++ + + using something; // should be inside the innermost namespace bar below + + namespace foo { + namespace bar { + + } // bar + + using something_else; // shoulw be inside the innermost namespace bar above + + } // foo + +Placing convenience aliases in upper level namespaces can lead to ambiguity in +which name the compiler should use. + +See https://abseil.io/tips/119 for more explanation. + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-safely-scoped + ` check. + + Finds instances of using declarations not in the innermost layer + of a series of namespaces. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/abseil/SafelyScopedCheck.h === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.h @@ -0,0 +1,36 @@ +//===--- SafelyScopedCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting using declarations not in a namespace declaration or not in +/// the innermost layer of namespace declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-safely-scoped.html +class SafelyScopedCheck : public ClangTidyCheck { +public: + SafelyScopedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace abseil +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H Index: clang-tidy/abseil/SafelyScopedCheck.cpp === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.cpp @@ -0,0 +1,43 @@ +//===--- SafelyScopedCheck.cpp - clang-tidy ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois
[PATCH] D55409: [clang-tidy] check for using declarations not in an anonymous namespace when there exists one
Ywicheng updated this revision to Diff 195656. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55409/new/ https://reviews.llvm.org/D55409 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.cpp clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h clang-tidy/abseil/CMakeLists.txt docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp Index: test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp === --- /dev/null +++ test/clang-tidy/abseil-anonymous-enclosed-aliases.cpp @@ -0,0 +1,56 @@ +// RUN: %check_clang_tidy %s abseil-anonymous-enclosed-aliases %t +namespace bar { + +class A {}; +class B {}; + +} // namespace bar + +namespace foo1 { + +// CHECK-MESSAGES: :[[@LINE+4]]:12: warning: using declaration 'A' should +// be in the anonymous namespace. Use discretion when moving using declarations +// as it might necessitate moving lines containing relevant aliases. +// [abseil-anonymous-enclosed-aliases] +using bar::A; +void f(A a); + +namespace {} // anonymous namespace + +} // namespace foo1 + +namespace foo2 { + +namespace { + +// This is okay +using ::bar::B; + +} // anonymous namespace + +void g(B b); + +} // namespace foo2 + +// Check should not be triggered below when the using declaration is at +// function or class (instead of namespace) scope. +namespace outer { + +int fun_scope() { + using ::bar::A; + return 0; +} // function scope + +class Base { +public: + void f(); +}; // class scope + +class Derived : public Base { +public: + using Base::f; +}; // class scope + +namespace {} // anonymous namespace + +} // namespace outer \ No newline at end of file Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -4,6 +4,7 @@ = .. toctree:: + abseil-anonymous-enclosed-aliases abseil-duration-addition abseil-duration-comparison abseil-duration-conversion-cast Index: docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-anonymous-enclosed-aliases.rst @@ -0,0 +1,20 @@ +.. title:: clang-tidy - abseil-anonymous-enclosed-aliases + +abseil-anonymous-enclosed-aliases += + +Finds using declarations outside of anonymous namespaces, and +suggests those declarations be moved to that namespace. + +Example: +.. code-block:: c++ + + namespace foo { + + using something; // should be inside the anonymous namespace below + + namespace { + + } // anonymous namespace + + } // foo Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-anonymous-enclosed-aliases + ` check. + + Finds instances of using declarations not in an anonymous namespace + when there exists one. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/abseil/CMakeLists.txt === --- clang-tidy/abseil/CMakeLists.txt +++ clang-tidy/abseil/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyAbseilModule AbseilTidyModule.cpp + AnonymousEnclosedAliasesCheck.cpp DurationAdditionCheck.cpp DurationComparisonCheck.cpp DurationConversionCastCheck.cpp Index: clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h === --- /dev/null +++ clang-tidy/abseil/AnonymousEnclosedAliasesCheck.h @@ -0,0 +1,40 @@ +//===--- AnonymousEnclosedAliasesCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENCLOSEDALIASESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_ANONYMOUSENCLOSEDALIASESCHECK_H + +#include "../ClangTidy.h" +#include + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting incorrect practice of putting using declarations outside an +/// anonymous namespace when there exists one. +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/ +/// abseil-anonymous-enclosed-aliases.html +class AnonymousEnclosedAliasesCheck : public ClangTidyCheck { +public: + AnonymousEnclosedAliasesCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerM
[PATCH] D55411: [clang-tidy] check for flagging using declarations not in the inner most namespace
Ywicheng updated this revision to Diff 195661. Ywicheng marked 3 inline comments as done. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D55411/new/ https://reviews.llvm.org/D55411 Files: clang-tidy/abseil/AbseilTidyModule.cpp clang-tidy/abseil/CMakeLists.txt clang-tidy/abseil/SafelyScopedCheck.cpp clang-tidy/abseil/SafelyScopedCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/abseil-safely-scoped.rst docs/clang-tidy/checks/list.rst test/clang-tidy/abseil-safely-scoped.cpp Index: test/clang-tidy/abseil-safely-scoped.cpp === --- /dev/null +++ test/clang-tidy/abseil-safely-scoped.cpp @@ -0,0 +1,55 @@ +// RUN: %check_clang_tidy %s abseil-safely-scoped %t +namespace bar { + +class A {}; +class B {}; + +} // namespace bar + +namespace foo1 { + +// CHECK-MESSAGES: :[[@LINE+4]]:12: warning: using declaration 'A' is not +// declared in the innermost namespace. Use discretion when moving using +// declarations as it might necessitate moving lines containing relevant +// aliases. [abseil-safely-scoped] +using bar::A; +void f(A a); + +namespace {} // anonymous namespace + +} // namespace foo1 + +namespace foo2 { + +namespace { + +using ::bar::B; + +} // anonymous namespace + +void g(B b); + +} // namespace foo2 + +// Check should not be triggered below when we are at +// function (instead of namespace) scope. +namespace outer { + +int fun_scope() { + using ::bar::A; + return 0; +} // function scope + +class Base { +public: + void f(); +}; // class scope + +class Derived : public Base { +public: + using Base::f; +}; // class scope + +namespace inner {} // namespace inner + +} // namespace outer Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -16,6 +16,7 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls + abseil-safely-scoped abseil-str-cat-append abseil-string-find-startswith abseil-time-subtraction Index: docs/clang-tidy/checks/abseil-safely-scoped.rst === --- /dev/null +++ docs/clang-tidy/checks/abseil-safely-scoped.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - abseil-safely-scoped + +abseil-safely-scoped + + +Flags using declarations that are not contained in an innermost +namespace, and suggests these declarations be moved elsewhere. + +Example: + +.. code-block:: c++ + + using something; // should be inside the innermost namespace bar below + + namespace foo { + namespace bar { + + } // bar + + using something_else; // shoulw be inside the innermost namespace bar above + + } // foo + +Placing convenience aliases in upper level namespaces can lead to ambiguity in +which name the compiler should use. + +See https://abseil.io/tips/119 for more explanation. + Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -67,6 +67,12 @@ Improvements to clang-tidy -- +- New :doc:`abseil-safely-scoped + ` check. + + Finds instances of using declarations not in the innermost layer + of a series of namespaces. + - New :doc:`abseil-duration-addition ` check. Index: clang-tidy/abseil/SafelyScopedCheck.h === --- /dev/null +++ clang-tidy/abseil/SafelyScopedCheck.h @@ -0,0 +1,36 @@ +//===--- SafelyScopedCheck.h - clang-tidy ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace abseil { + +/// Detecting using declarations not in a namespace declaration or not in +/// the innermost layer of namespace declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-safely-scoped.html +class SafelyScopedCheck : public ClangTidyCheck { +public: + SafelyScopedCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace abseil +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_SAFELYSCOPEDCHECK_H Index: clang-tidy/abseil/SafelyScopedCheck.cpp =