diff --git a/clang-tools-extra/clang-tidy/fuchsia/CMakeLists.txt b/clang-tools-extra/clang-tidy/fuchsia/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/fuchsia/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/fuchsia/CMakeLists.txt @@ -6,7 +6,6 @@ FuchsiaTidyModule.cpp MultipleInheritanceCheck.cpp OverloadedOperatorCheck.cpp - RestrictSystemIncludesCheck.cpp StaticallyConstructedObjectsCheck.cpp TrailingReturnCheck.cpp VirtualInheritanceCheck.cpp @@ -18,5 +17,6 @@ clangLex clangTidy clangTidyGoogleModule + clangTidyPortabilityModule clangTidyUtils ) diff --git a/clang-tools-extra/clang-tidy/fuchsia/FuchsiaTidyModule.cpp b/clang-tools-extra/clang-tidy/fuchsia/FuchsiaTidyModule.cpp --- a/clang-tools-extra/clang-tidy/fuchsia/FuchsiaTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/fuchsia/FuchsiaTidyModule.cpp @@ -10,11 +10,11 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "../google/UnnamedNamespaceInHeaderCheck.h" +#include "../portability/RestrictSystemIncludesCheck.h" #include "DefaultArgumentsCallsCheck.h" #include "DefaultArgumentsDeclarationsCheck.h" #include "MultipleInheritanceCheck.h" #include "OverloadedOperatorCheck.h" -#include "RestrictSystemIncludesCheck.h" #include "StaticallyConstructedObjectsCheck.h" #include "TrailingReturnCheck.h" #include "VirtualInheritanceCheck.h" @@ -39,7 +39,7 @@ "fuchsia-multiple-inheritance"); CheckFactories.registerCheck( "fuchsia-overloaded-operator"); - CheckFactories.registerCheck( + CheckFactories.registerCheck( "fuchsia-restrict-system-includes"); CheckFactories.registerCheck( "fuchsia-statically-constructed-objects"); diff --git a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/portability/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/portability/CMakeLists.txt @@ -2,6 +2,7 @@ add_clang_library(clangTidyPortabilityModule PortabilityTidyModule.cpp + RestrictSystemIncludesCheck.cpp SIMDIntrinsicsCheck.cpp LINK_LIBS diff --git a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp --- a/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/portability/PortabilityTidyModule.cpp @@ -9,6 +9,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" +#include "RestrictSystemIncludesCheck.h" #include "SIMDIntrinsicsCheck.h" namespace clang { @@ -18,6 +19,8 @@ class PortabilityModule : public ClangTidyModule { public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck( + "portability-restrict-system-includes"); CheckFactories.registerCheck( "portability-simd-intrinsics"); } diff --git a/clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h rename from clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h rename to clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h --- a/clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h @@ -1,4 +1,4 @@ -//===--- RestrictSystemIncludesCheck.h - clang-tidy---------- ----*- C++-*-===// +//===--- RestrictSystemIncludesCheck.h - clang-tidy --------------*- C++-*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,21 +6,21 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H #include "../ClangTidyCheck.h" #include "../GlobList.h" namespace clang { namespace tidy { -namespace fuchsia { +namespace portability { /// Checks for allowed includes and suggests removal of any others. If no /// includes are specified, the check will exit without issuing any warnings. /// /// For the user-facing documentation see: -/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-restrict-system-includes.html +/// http://clang.llvm.org/extra/clang-tidy/checks/portability-restrict-system-includes.html class RestrictSystemIncludesCheck : public ClangTidyCheck { public: RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context) @@ -40,8 +40,8 @@ GlobList AllowedIncludesGlobList; }; -} // namespace fuchsia +} // namespace portability } // namespace tidy } // namespace clang -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H \ No newline at end of file diff --git a/clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp rename from clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp rename to clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp --- a/clang-tools-extra/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp @@ -1,4 +1,4 @@ -//===--- RestrictSystemIncludesCheck.cpp - clang-tidy----------------------===// +//===--- RestrictSystemIncludesCheck.cpp - clang-tidy ---------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -18,7 +18,7 @@ namespace clang { namespace tidy { -namespace fuchsia { +namespace portability { class RestrictedIncludesPPCallbacks : public PPCallbacks { public: @@ -111,6 +111,6 @@ Options.store(Opts, "Includes", AllowedIncludes); } -} // namespace fuchsia +} // namespace portability } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -98,6 +98,15 @@ Finds recursive functions and diagnoses them. +- New :doc:`portability-restrict-system-includes + ` check. + + Checks to selectively allow or disallow a configurable list of system + headers. + + Moved fuchsia's restrict-system-includes check to portability to allow for + general use. + New check aliases ^^^^^^^^^^^^^^^^^ @@ -111,6 +120,11 @@ :doc:`bugprone-reserved-identifier ` was added. +- New alias :doc:`fuchsia-restrict-system-includes + ` to + :doc:`portability-restrict-system-includes + ` was added. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -255,6 +255,7 @@ `performance-type-promotion-in-math-fn `_, "Yes" `performance-unnecessary-copy-initialization `_, `performance-unnecessary-value-param `_, "Yes" + `portability-restrict-system-includes `_, "Yes" `portability-simd-intrinsics `_, `readability-avoid-const-params-in-decls `_, `readability-braces-around-statements `_, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/portability-restrict-system-includes.rst b/clang-tools-extra/docs/clang-tidy/checks/portability-restrict-system-includes.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/portability-restrict-system-includes.rst @@ -0,0 +1,51 @@ +.. title:: clang-tidy - portability-restrict-system-includes + +portability-restrict-system-includes +==================================== + +Checks to selectively allow or disallow a configurable list of system headers. + +For example: + +In order to **only** allow zlib.h from the system you would set the options to +`-*,zlib.h`. + +.. code-block:: c++ + + #include // Bad: disallowed system header. + #include // Bad: disallowed system header. + #include // Good: allowed system header. + #include "src/myfile.h" // Good: non-system header always allowed. + +In order to allow everything **except** zlib.h from the system you would set +the options to `*,-zlib.h`. + +.. code-block:: c++ + + #include // Good: allowed system header. + #include // Good: allowed system header. + #include // Bad: disallowed system header. + #include "src/myfile.h" // Good: non-system header always allowed. + +Since the opions support globbing you can use wildcarding to allow groups of +headers. + +`-*,openssl/*.h` will allow all openssl headers but disallow all others. + +.. code-block:: c++ + + #include // Bad: disallowed system header. + #include // Good: allowed system header. + #include // Good: allowed system header. + #include // Bad: disallowed system header. + #include "src/myfile.h" // Good: non-system header always allowed. + +Options +------- + +.. option:: Includes + + A string containing a comma separated glob list of allowed include + filenames. Similar to the -checks glob list for running clang-tidy itself, + the two wildcard characters are '*' and '-', to include and exclude globs, + respectively. The default is '*', which allows all includes. diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-allow.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-allow.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-allow.cpp @@ -0,0 +1,9 @@ +// RUN: %check_clang_tidy %s portability-restrict-system-includes %t \ +// RUN: -- -config="{CheckOptions: [{key: portability-restrict-system-includes.Includes, value: '*,-stdio.h'}]}" + +// Test block-list functionality: allow all but stdio.h. + +#include +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include stdio.h not allowed +#include +#include diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-disallow.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-disallow.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-disallow.cpp @@ -0,0 +1,10 @@ +// RUN: %check_clang_tidy %s portability-restrict-system-includes %t \ +// RUN: -- -config="{CheckOptions: [{key: portability-restrict-system-includes.Includes, value: '-*,stdio.h'}]}" + +// Test allow-list functionality: disallow all but stdio.h. + +#include +#include +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include stdlib.h not allowed +#include +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include string.h not allowed diff --git a/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-glob.cpp b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-glob.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/portability-restrict-system-includes-glob.cpp @@ -0,0 +1,10 @@ +// RUN: %check_clang_tidy %s portability-restrict-system-includes %t \ +// RUN: -- -config="{CheckOptions: [{key: portability-restrict-system-includes.Includes, value: '-*,std*.h'}]}" + +// Test glob functionality: disallow all headers except those that match +// pattern "std*.h". + +#include +#include +#include +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include string.h not allowed