[clang-tools-extra] r331930 - [clang-tidy] Adding RestrictSystemIncludes check to Fuchsia module
Julie Hockett via cfe-commits
cfe-commits at lists.llvm.org
Wed May 9 15:25:42 PDT 2018
Author: juliehockett
Date: Wed May 9 15:25:42 2018
New Revision: 331930
URL: http://llvm.org/viewvc/llvm-project?rev=331930&view=rev
Log:
[clang-tidy] Adding RestrictSystemIncludes check to Fuchsia module
Adding a check to restrict system includes to a whitelist. Given a list
of includes that are explicitly allowed, the check issues a fixit to
remove any system include not on that list from the source file.
Differential Revision: https://reviews.llvm.org/D43778
Added:
clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp
clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-restrict-system-includes.rst
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/a.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/j.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/r.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/s.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/t.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/transitive.h
clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/transitive2.h
clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes-headers.cpp
clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
Modified: clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt?rev=331930&r1=331929&r2=331930&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt Wed May 9 15:25:42 2018
@@ -5,6 +5,7 @@ add_clang_library(clangTidyFuchsiaModule
FuchsiaTidyModule.cpp
MultipleInheritanceCheck.cpp
OverloadedOperatorCheck.cpp
+ RestrictSystemIncludesCheck.cpp
StaticallyConstructedObjectsCheck.cpp
TrailingReturnCheck.cpp
VirtualInheritanceCheck.cpp
Modified: clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp?rev=331930&r1=331929&r2=331930&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp Wed May 9 15:25:42 2018
@@ -14,6 +14,7 @@
#include "DefaultArgumentsCheck.h"
#include "MultipleInheritanceCheck.h"
#include "OverloadedOperatorCheck.h"
+#include "RestrictSystemIncludesCheck.h"
#include "StaticallyConstructedObjectsCheck.h"
#include "TrailingReturnCheck.h"
#include "VirtualInheritanceCheck.h"
@@ -36,6 +37,8 @@ public:
"fuchsia-multiple-inheritance");
CheckFactories.registerCheck<OverloadedOperatorCheck>(
"fuchsia-overloaded-operator");
+ CheckFactories.registerCheck<RestrictSystemIncludesCheck>(
+ "fuchsia-restrict-system-includes");
CheckFactories.registerCheck<StaticallyConstructedObjectsCheck>(
"fuchsia-statically-constructed-objects");
CheckFactories.registerCheck<TrailingReturnCheck>(
Added: clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.cpp Wed May 9 15:25:42 2018
@@ -0,0 +1,126 @@
+//===--- RestrictSystemIncludesCheck.cpp - clang-tidy----------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "RestrictSystemIncludesCheck.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Lex/HeaderSearch.h"
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Path.h"
+#include <cstring>
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+class RestrictedIncludesPPCallbacks : public PPCallbacks {
+public:
+ explicit RestrictedIncludesPPCallbacks(RestrictSystemIncludesCheck &Check,
+ 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<IncludeDirective, 8>;
+ llvm::SmallDenseMap<FileID, FileIncludes> IncludeDirectives;
+
+ RestrictSystemIncludesCheck &Check;
+ SourceManager &SM;
+};
+
+void RestrictedIncludesPPCallbacks::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) {
+ if (!llvm::is_contained(Check.getAllowedIncludes(), FileName) &&
+ SrcMgr::isSystem(FileType)) {
+ SmallString<256> FullPath;
+ llvm::sys::path::append(FullPath, SearchPath);
+ llvm::sys::path::append(FullPath, RelativePath);
+ // Bucket the allowed include directives by the id of the file they were
+ // declared in.
+ IncludeDirectives[SM.getFileID(HashLoc)].emplace_back(
+ HashLoc, FilenameRange, FileName, FullPath.str(),
+ SM.isInMainFile(HashLoc));
+ }
+}
+
+void RestrictedIncludesPPCallbacks::EndOfMainFile() {
+ if (IncludeDirectives.empty())
+ return;
+
+ for (const auto &Bucket : IncludeDirectives) {
+ const FileIncludes &FileDirectives = Bucket.second;
+
+ // Emit fixits for all restricted includes.
+ for (const auto &Include : FileDirectives) {
+ // Fetch the length of the include statement from the start to just after
+ // the newline, for finding the end (including the newline).
+ unsigned ToLen = std::strcspn(SM.getCharacterData(Include.Loc), "\n") + 1;
+ CharSourceRange ToRange = CharSourceRange::getCharRange(
+ Include.Loc, Include.Loc.getLocWithOffset(ToLen));
+
+ if (!Include.IsInMainFile) {
+ auto D = Check.diag(
+ Include.Loc,
+ "system include %0 not allowed, transitively included from %1");
+ D << Include.IncludeFile << SM.getFilename(Include.Loc);
+ D << FixItHint::CreateRemoval(ToRange);
+ continue;
+ }
+ auto D = Check.diag(Include.Loc, "system include %0 not allowed");
+ D << Include.IncludeFile;
+ D << FixItHint::CreateRemoval(ToRange);
+ }
+ }
+}
+
+void RestrictSystemIncludesCheck::registerPPCallbacks(
+ CompilerInstance &Compiler) {
+ // Do nothing if there are no restricted includes.
+ if (AllowedIncludes.empty())
+ return;
+ Compiler.getPreprocessor().addPPCallbacks(
+ llvm::make_unique<RestrictedIncludesPPCallbacks>(
+ *this, Compiler.getSourceManager()));
+}
+
+void RestrictSystemIncludesCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "Includes",
+ utils::options::serializeStringList(AllowedIncludes));
+}
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/RestrictSystemIncludesCheck.h Wed May 9 15:25:42 2018
@@ -0,0 +1,46 @@
+//===--- RestrictSystemIncludesCheck.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_FUCHSIA_RESTRICTINCLUDESSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H
+
+#include "../ClangTidy.h"
+#include "../utils/OptionsUtils.h"
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// 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
+class RestrictSystemIncludesCheck : public ClangTidyCheck {
+public:
+ RestrictSystemIncludesCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ AllowedIncludes(
+ utils::options::parseStringList(Options.get("Includes", ""))) {}
+
+ void registerPPCallbacks(CompilerInstance &Compiler) override;
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+ const std::vector<std::string> &getAllowedIncludes() const {
+ return AllowedIncludes;
+ }
+
+private:
+ std::vector<std::string> AllowedIncludes;
+};
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_RESTRICTINCLUDESSCHECK_H
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=331930&r1=331929&r2=331930&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed May 9 15:25:42 2018
@@ -110,8 +110,14 @@ Improvements to clang-tidy
Checks whether a ``std::string::find()`` result is compared with 0, and
suggests replacing with ``absl::StartsWith()``.
-- New :doc:`fuchsia-statically-constructed-objects
- <clang-tidy/checks/fuchsia-statically-constructed-objects>` check.
+- New `fuchsia-restrict-system-includes
+ <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-restrict-system-includes.html>`_ check
+
+ Checks for allowed system includes and suggests removal of any others. If no
+ includes are specified, the check will exit without issuing any warnings.
+
+- New `fuchsia-statically-constructed-objects
+ <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-statically-constructed-objects.html>`_ check
Warns if global, non-trivial objects with static storage are constructed,
unless the object is statically initialized with a ``constexpr`` constructor
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-restrict-system-includes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-restrict-system-includes.rst?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-restrict-system-includes.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-restrict-system-includes.rst Wed May 9 15:25:42 2018
@@ -0,0 +1,32 @@
+.. title:: clang-tidy - fuchsia-restrict-system-includes
+
+fuchsia-restrict-system-includes
+================================
+
+Checks for allowed system includes and suggests removal of any others. If no
+includes are specified, the check will exit without issuing any warnings.
+
+It is important to note that running this check with fixes may break code, as
+the fix removes headers. Fixes are applied to source and header files, but not
+to system headers.
+
+Note that the separator for identifying allowed includes is a semi-colon, and
+therefore this check is unable to allow an include with a semi-colon in the
+filename (e.g. 'foo;bar.h' will be parsed as allowing 'foo' and 'bar.h', and not
+as allowing a file called 'foo;bar.h').
+
+For example, given the allowed system includes 'a.h; b.h':
+
+.. code-block:: c++
+
+ #include <a.h>
+ #include <b.h>
+ #include <c.h> // Warning, as c.h is not explicitly allowed
+
+Options
+-------
+
+.. option:: Includes
+
+ A string containing a semi-colon separated list of allowed include filenames.
+ The default is an empty string, which allows all includes.
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=331930&r1=331929&r2=331930&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Wed May 9 15:25:42 2018
@@ -95,6 +95,7 @@ Clang-Tidy Checks
fuchsia-default-arguments
fuchsia-multiple-inheritance
fuchsia-overloaded-operator
+ fuchsia-restrict-system-includes
fuchsia-statically-constructed-objects
fuchsia-trailing-return
fuchsia-virtual-inheritance
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/a.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/a.h?rev=331930&view=auto
==============================================================================
(empty)
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/j.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/j.h?rev=331930&view=auto
==============================================================================
(empty)
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/r.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/r.h?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/r.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/r.h Wed May 9 15:25:42 2018
@@ -0,0 +1 @@
+void f() {}
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/s.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/s.h?rev=331930&view=auto
==============================================================================
(empty)
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/t.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/t.h?rev=331930&view=auto
==============================================================================
(empty)
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/transitive.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/transitive.h?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/transitive.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/system/transitive.h Wed May 9 15:25:42 2018
@@ -0,0 +1,3 @@
+#include <r.h>
+#include <t.h>
+#include <s.h>
Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/transitive2.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/transitive2.h?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/transitive2.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/fuchsia-restrict-system-includes/transitive2.h Wed May 9 15:25:42 2018
@@ -0,0 +1,2 @@
+#include <s.h>
+#include <t.h>
Added: clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes-headers.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes-headers.cpp?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes-headers.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes-headers.cpp Wed May 9 15:25:42 2018
@@ -0,0 +1,20 @@
+// RUN: cp -r %S/Inputs/fuchsia-restrict-system-includes %T/Inputs
+// RUN: %check_clang_tidy %s fuchsia-restrict-system-includes %t \
+// RUN: -- -config="{CheckOptions: [{key: fuchsia-restrict-system-includes.Includes, value: 'transitive.h;s.h'}]}" \
+// RUN: -system-headers -header-filter=.* \
+// RUN: -- -std=c++11 -I %T/Inputs/fuchsia-restrict-system-includes -isystem %T/Inputs/fuchsia-restrict-system-includes/system
+// RUN: FileCheck -input-file=%T/Inputs/transitive2.h %s -check-prefix=CHECK-HEADER-FIXES
+
+// transitive.h includes <r.h> and <t.h>
+#include <transitive.h>
+// CHECK-MESSAGES: :1:1: warning: system include r.h not allowed, transitively included from {{(.*\/)*}}Inputs/fuchsia-restrict-system-includes/system/transitive.h
+// CHECK-MESSAGES: :2:1: warning: system include t.h not allowed, transitively included from {{(.*\/)*}}Inputs/fuchsia-restrict-system-includes/system/transitive.h
+
+// transitive.h includes <s.h> and <t.h>
+#include "transitive2.h"
+// CHECK-MESSAGES: :2:1: warning: system include t.h not allowed, transitively included from {{(.*\/)*}}Inputs/fuchsia-restrict-system-includes/transitive2.h
+// CHECK-HEADER-FIXES-NOT: #include <t.h>
+
+int main() {
+ // f() is declared in r.h
+}
Added: clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes.cpp?rev=331930&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/fuchsia-restrict-system-includes.cpp Wed May 9 15:25:42 2018
@@ -0,0 +1,25 @@
+// RUN: %check_clang_tidy %s fuchsia-restrict-system-includes %t \
+// RUN: -- -config="{CheckOptions: [{key: fuchsia-restrict-system-includes.Includes, value: 's.h'}]}" \
+// RUN: -- -std=c++11 -I %S/Inputs/fuchsia-restrict-system-includes -isystem %S/Inputs/fuchsia-restrict-system-includes/system
+
+#include "a.h"
+
+#include <s.h>
+#include <t.h>
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include t.h not allowed
+// CHECK-FIXES-NOT: #include <t.h>
+
+#include "s.h"
+#include "t.h"
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include t.h not allowed
+// CHECK-FIXES-NOT: #include "t.h"
+
+#define foo <j.h>
+
+#include foo
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include j.h not allowed
+// CHECK-FIXES-NOT: #include foo
+
+#/* comment */ include /* comment */ foo
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: system include j.h not allowed
+// CHECK-FIXES-NOT: # /* comment */ include /* comment */ foo
More information about the cfe-commits
mailing list