diff --git a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt index c03d8b677f2638..cc213d35a57289 100644 --- a/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/llvmlibc/CMakeLists.txt @@ -10,6 +10,7 @@ add_clang_library(clangTidyLLVMLibcModule clangBasic clangLex clangTidy + clangTidyPortabilityModule clangTidyUtils clangTooling ) diff --git a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp index 8a597c0b2a246a..4a19b5359d4f9d 100644 --- a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp @@ -18,12 +18,14 @@ namespace llvm_libc { namespace { -class RestrictedIncludesPPCallbacks : public PPCallbacks { +class RestrictedIncludesPPCallbacks + : public portability::RestrictedIncludesPPCallbacks { public: explicit RestrictedIncludesPPCallbacks( RestrictSystemLibcHeadersCheck &Check, const SourceManager &SM, const SmallString<128> CompilerIncudeDir) - : Check(Check), SM(SM), CompilerIncudeDir(CompilerIncudeDir) {} + : portability::RestrictedIncludesPPCallbacks(Check, SM), + CompilerIncudeDir(CompilerIncudeDir) {} void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, @@ -33,8 +35,6 @@ class RestrictedIncludesPPCallbacks : public PPCallbacks { SrcMgr::CharacteristicKind FileType) override; private: - RestrictSystemLibcHeadersCheck &Check; - const SourceManager &SM; const SmallString<128> CompilerIncudeDir; }; @@ -45,18 +45,12 @@ void RestrictedIncludesPPCallbacks::InclusionDirective( bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) { - if (SrcMgr::isSystem(FileType)) { - // Compiler provided headers are allowed (e.g stddef.h). - if (SearchPath == CompilerIncudeDir) return; - if (!SM.isInMainFile(HashLoc)) { - Check.diag( - HashLoc, - "system libc header %0 not allowed, transitively included from %1") - << FileName << SM.getFilename(HashLoc); - } else { - Check.diag(HashLoc, "system libc header %0 not allowed") << FileName; - } - } + // Compiler provided headers are allowed (e.g stddef.h). + if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir) + return; + portability::RestrictedIncludesPPCallbacks::InclusionDirective( + HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath, + RelativePath, Imported, FileType); } void RestrictSystemLibcHeadersCheck::registerPPCallbacks( diff --git a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.h b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.h index 3910a29a28e4f8..9eead7a228828b 100644 --- a/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.h +++ b/clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_LLVMLIBC_RESTRICTSYSTEMLIBCHEADERSCHECK_H #include "../ClangTidyCheck.h" +#include "../portability/RestrictSystemIncludesCheck.h" namespace clang { namespace tidy { @@ -20,10 +21,11 @@ namespace llvm_libc { /// /// For the user-facing documentation see: /// http://clang.llvm.org/extra/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.html -class RestrictSystemLibcHeadersCheck : public ClangTidyCheck { +class RestrictSystemLibcHeadersCheck + : public portability::RestrictSystemIncludesCheck { public: RestrictSystemLibcHeadersCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : portability::RestrictSystemIncludesCheck(Name, Context, "-*") {} void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override; }; diff --git a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp index 15076d01a7713b..f6163989a461a5 100644 --- a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.cpp @@ -20,42 +20,6 @@ namespace clang { namespace tidy { namespace portability { -class RestrictedIncludesPPCallbacks : public PPCallbacks { -public: - explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, - const SourceManager &SM) - : Check(Check), SM(SM) {} - - void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, - StringRef FileName, bool IsAngled, - CharSourceRange FilenameRange, const FileEntry *File, - StringRef SearchPath, StringRef RelativePath, - const Module *Imported, - SrcMgr::CharacteristicKind FileType) override; - void EndOfMainFile() override; - -private: - struct IncludeDirective { - IncludeDirective() = default; - IncludeDirective(SourceLocation Loc, CharSourceRange Range, - StringRef Filename, StringRef FullPath, bool IsInMainFile) - : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath), - IsInMainFile(IsInMainFile) {} - - SourceLocation Loc; // '#' location in the include directive - CharSourceRange Range; // SourceRange for the file name - std::string IncludeFile; // Filename as a string - std::string IncludePath; // Full file path as a string - bool IsInMainFile; // Whether or not the include is in the main file - }; - - using FileIncludes = llvm::SmallVector; - llvm::SmallDenseMap IncludeDirectives; - - RestrictSystemIncludesCheck &Check; - const SourceManager &SM; -}; - void RestrictedIncludesPPCallbacks::InclusionDirective( SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, diff --git a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h index db2f9935534b4f..c34f054fba2e07 100644 --- a/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h +++ b/clang-tools-extra/clang-tidy/portability/RestrictSystemIncludesCheck.h @@ -23,9 +23,10 @@ namespace portability { /// http://clang.llvm.org/extra/clang-tidy/checks/portability-restrict-system-includes.html class RestrictSystemIncludesCheck : public ClangTidyCheck { public: - RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context) + RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context, + std::string DefaultAllowedIncludes = "*") : ClangTidyCheck(Name, Context), - AllowedIncludes(Options.get("Includes", "*")), + AllowedIncludes(Options.get("Includes", DefaultAllowedIncludes)), AllowedIncludesGlobList(AllowedIncludes) {} void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, @@ -40,8 +41,44 @@ class RestrictSystemIncludesCheck : public ClangTidyCheck { GlobList AllowedIncludesGlobList; }; +class RestrictedIncludesPPCallbacks : public PPCallbacks { +public: + explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check, + const SourceManager &SM) + : Check(Check), SM(SM) {} + + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, + StringRef FileName, bool IsAngled, + CharSourceRange FilenameRange, const FileEntry *File, + StringRef SearchPath, StringRef RelativePath, + const Module *Imported, + SrcMgr::CharacteristicKind FileType) override; + void EndOfMainFile() override; + +private: + struct IncludeDirective { + IncludeDirective() = default; + IncludeDirective(SourceLocation Loc, CharSourceRange Range, + StringRef Filename, StringRef FullPath, bool IsInMainFile) + : Loc(Loc), Range(Range), IncludeFile(Filename), IncludePath(FullPath), + IsInMainFile(IsInMainFile) {} + + SourceLocation Loc; // '#' location in the include directive + CharSourceRange Range; // SourceRange for the file name + std::string IncludeFile; // Filename as a string + std::string IncludePath; // Full file path as a string + bool IsInMainFile; // Whether or not the include is in the main file + }; + + using FileIncludes = llvm::SmallVector; + llvm::SmallDenseMap IncludeDirectives; + + RestrictSystemIncludesCheck &Check; + const SourceManager &SM; +}; + } // namespace portability } // namespace tidy } // namespace clang -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H \ No newline at end of file +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PORTABILITY_RESTRICTINCLUDESSCHECK_H diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5e943c5003f0e4..37f020d6018fd3 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -187,7 +187,7 @@ Clang-Tidy Checks `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes" `llvm-prefer-register-over-unsigned `_, "Yes" `llvm-twine-local `_, "Yes" - `llvmlibc-restrict-system-libc-headers `_, + `llvmlibc-restrict-system-libc-headers `_, "Yes" `misc-definitions-in-headers `_, "Yes" `misc-misplaced-const `_, `misc-new-delete-overloads `_, diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.rst b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.rst index 0ec092584895dd..bf39dd62ba95bb 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/llvmlibc-restrict-system-libc-headers.rst @@ -18,3 +18,18 @@ lead to subtle and hard to detect bugs. For example consider a system libc whose ``dirent`` struct has slightly different field ordering than llvm-libc. While this will compile successfully, this can cause issues during runtime because they are ABI incompatible. + +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 disallows all includes. + + This can be used to allow known safe includes such as Linux development + headers. See :doc:`portability-restrict-system-includes + ` for more + details. diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/llvmlibc/system/math.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/llvmlibc/system/math.h deleted file mode 100644 index e69de29bb2d1d6..00000000000000 diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/llvmlibc/transitive.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/llvmlibc/transitive.h deleted file mode 100644 index a84546e5bc8361..00000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/llvmlibc/transitive.h +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers-transitive.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers-transitive.cpp deleted file mode 100644 index 745aa0bb34016d..00000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers-transitive.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// RUN: %check_clang_tidy %s llvmlibc-restrict-system-libc-headers %t \ -// RUN: -- -header-filter=.* \ -// RUN: -- -I %S/Inputs/llvmlibc \ -// RUN: -isystem %S/Inputs/llvmlibc/system \ -// RUN: -resource-dir %S/Inputs/llvmlibc/resource - -#include "transitive.h" -// CHECK-MESSAGES: :1:1: warning: system libc header math.h not allowed, transitively included from {{.*}} diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers.cpp index 43f5b1e94279a1..52e25faf190fd5 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/llvmlibc-restrict-system-libc-headers.cpp @@ -3,11 +3,11 @@ // RUN: -resource-dir %S/Inputs/llvmlibc/resource #include -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system libc header stdio.h not allowed +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include stdio.h not allowed #include -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system libc header stdlib.h not allowed +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include stdlib.h not allowed #include "string.h" -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system libc header string.h not allowed +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include string.h not allowed #include "stdatomic.h" #include // Compiler provided headers should not throw warnings.