[clang-tools-extra] r340800 - [clang-tidy] Abseil: no namepsace check

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 28 00:48:28 PDT 2018


Author: hokein
Date: Tue Aug 28 00:48:28 2018
New Revision: 340800

URL: http://llvm.org/viewvc/llvm-project?rev=340800&view=rev
Log:
[clang-tidy] Abseil: no namepsace check

This check ensures that users of Abseil do not open namespace absl in their code, as that violates our compatibility guidelines.

AbseilMatcher.h written by Hugo Gonzalez.

Patch by Deanna Garcia!

Added:
    clang-tools-extra/trunk/clang-tidy/abseil/AbseilMatcher.h
    clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.cpp
    clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-no-namespace.rst
    clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/
    clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/external-file.h
    clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/
    clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/internal-file.h
    clang-tools-extra/trunk/test/clang-tidy/abseil-no-namespace.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
    clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Added: clang-tools-extra/trunk/clang-tidy/abseil/AbseilMatcher.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilMatcher.h?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilMatcher.h (added)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilMatcher.h Tue Aug 28 00:48:28 2018
@@ -0,0 +1,51 @@
+//===- AbseilMatcher.h - clang-tidy ---------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+namespace clang {
+namespace ast_matchers {
+
+/// Matches AST nodes that were found within Abseil files.
+///
+/// Example matches Y but not X
+///     (matcher = cxxRecordDecl(isInAbseilFile())
+/// \code
+///   #include "absl/strings/internal-file.h"
+///   class X {};
+/// \endcode
+/// absl/strings/internal-file.h:
+/// \code
+///   class Y {};
+/// \endcode
+///
+/// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>,
+/// Matcher<NestedNameSpecifierLoc>
+
+AST_POLYMORPHIC_MATCHER(isInAbseilFile,
+                        AST_POLYMORPHIC_SUPPORTED_TYPES(
+                            Decl, Stmt, TypeLoc, NestedNameSpecifierLoc)) {
+  auto &SourceManager = Finder->getASTContext().getSourceManager();
+  SourceLocation Loc = Node.getBeginLoc();
+  if (Loc.isInvalid())
+    return false;
+  const FileEntry *FileEntry =
+      SourceManager.getFileEntryForID(SourceManager.getFileID(Loc));
+  if (!FileEntry)
+    return false;
+  StringRef Filename = FileEntry->getName();
+  llvm::Regex RE(
+      "absl/(algorithm|base|container|debugging|memory|meta|numeric|strings|"
+      "synchronization|time|types|utility)");
+  return RE.match(Filename);
+}
+
+} // namespace ast_matchers
+} // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=340800&r1=340799&r2=340800&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Tue Aug 28 00:48:28 2018
@@ -12,6 +12,7 @@
 #include "../ClangTidyModuleRegistry.h"
 #include "DurationDivisionCheck.h"
 #include "FasterStrsplitDelimiterCheck.h"
+#include "NoNamespaceCheck.h"
 #include "StringFindStartswithCheck.h"
 
 namespace clang {
@@ -25,6 +26,7 @@ public:
         "abseil-duration-division");
     CheckFactories.registerCheck<FasterStrsplitDelimiterCheck>(
         "abseil-faster-strsplit-delimiter");
+    CheckFactories.registerCheck<NoNamespaceCheck>("abseil-no-namespace");
     CheckFactories.registerCheck<StringFindStartswithCheck>(
         "abseil-string-find-startswith");
   }

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=340800&r1=340799&r2=340800&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Tue Aug 28 00:48:28 2018
@@ -4,6 +4,7 @@ add_clang_library(clangTidyAbseilModule
   AbseilTidyModule.cpp
   DurationDivisionCheck.cpp
   FasterStrsplitDelimiterCheck.cpp
+  NoNamespaceCheck.cpp
   StringFindStartswithCheck.cpp
 
   LINK_LIBS

Added: clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.cpp?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.cpp Tue Aug 28 00:48:28 2018
@@ -0,0 +1,42 @@
+//===--- NoNamespaceCheck.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 "NoNamespaceCheck.h"
+#include "AbseilMatcher.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace abseil {
+
+void NoNamespaceCheck::registerMatchers(MatchFinder *Finder) {
+  if (!getLangOpts().CPlusPlus)
+    return;
+
+  Finder->addMatcher(
+      namespaceDecl(hasName("::absl"), unless(isInAbseilFile()))
+          .bind("abslNamespace"),
+      this);
+}
+
+void NoNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *abslNamespaceDecl =
+      Result.Nodes.getNodeAs<NamespaceDecl>("abslNamespace");
+
+  diag(abslNamespaceDecl->getLocation(),
+       "namespace 'absl' is reserved for implementation of the Abseil library "
+       "and should not be opened in user code");
+}
+
+} // namespace abseil
+} // namespace tidy
+} // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.h?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/abseil/NoNamespaceCheck.h Tue Aug 28 00:48:28 2018
@@ -0,0 +1,36 @@
+//===--- NoNamespaceCheck.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_NONAMESPACECHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_NONAMESPACECHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace abseil {
+
+/// This check ensures users don't open namespace absl, as that violates
+/// Abseil's compatibility guidelines.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-no-namespace.html
+class NoNamespaceCheck : public ClangTidyCheck {
+public:
+  NoNamespaceCheck(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_NONAMESPACECHECK_H

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=340800&r1=340799&r2=340800&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue Aug 28 00:48:28 2018
@@ -70,6 +70,12 @@ Improvements to clang-tidy
   Finds instances of ``absl::StrSplit()`` or ``absl::MaxSplits()`` where the
   delimiter is a single character string literal and replaces with a character.
 
+- New :doc:`abseil-no-namespace
+  <clang-tidy/checks/abseil-no-namespace>` check.
+
+  Ensures code does not open ``namespace absl`` as that violates Abseil's
+  compatibility guidelines.
+
 - New :doc:`readability-magic-numbers
   <clang-tidy/checks/readability-magic-numbers>` check.
 

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-no-namespace.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-no-namespace.rst?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-no-namespace.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-no-namespace.rst Tue Aug 28 00:48:28 2018
@@ -0,0 +1,21 @@
+.. title:: clang-tidy - abseil-no-namespace
+
+abseil-no-namespace
+===================
+
+Ensures code does not open ``namespace absl`` as that violates Abseil's
+compatibility guidelines. Code should not open ``namespace absl`` as that
+conflicts with Abseil's compatibility guidelines and may result in breakage.
+
+Any code that uses:
+
+.. code-block:: c++
+
+ namespace absl {
+  ...
+ }
+
+will be prompted with a warning.
+
+See `the full Abseil compatibility guidelines <https://
+abseil.io/about/compatibility>`_ for more information.

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=340800&r1=340799&r2=340800&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Tue Aug 28 00:48:28 2018
@@ -6,6 +6,7 @@ Clang-Tidy Checks
 .. toctree::
    abseil-duration-division
    abseil-faster-strsplit-delimiter
+   abseil-no-namespace
    abseil-string-find-startswith
    android-cloexec-accept
    android-cloexec-accept4

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/external-file.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/external-file.h?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/external-file.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/external-file.h Tue Aug 28 00:48:28 2018
@@ -0,0 +1 @@
+namespace absl {}

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/internal-file.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/internal-file.h?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/internal-file.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/strings/internal-file.h Tue Aug 28 00:48:28 2018
@@ -0,0 +1 @@
+namespace absl {}

Added: clang-tools-extra/trunk/test/clang-tidy/abseil-no-namespace.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/abseil-no-namespace.cpp?rev=340800&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/abseil-no-namespace.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/abseil-no-namespace.cpp Tue Aug 28 00:48:28 2018
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy %s abseil-no-namespace %t -- -- -I %S/Inputs
+// RUN: clang-tidy -checks='-*, abseil-no-namespace' -header-filter='.*' %s -- -I %S/Inputs 2>&1 | FileCheck %s
+
+/// Warning will not be triggered on internal Abseil code that is included.
+#include "absl/strings/internal-file.h"
+// CHECK-NOT: warning:
+
+/// Warning will be triggered on code that is not internal that is included.
+#include "absl/external-file.h"
+// CHECK: absl/external-file.h:1:11: warning: namespace 'absl' is reserved
+
+namespace absl {}
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: namespace 'absl' is reserved for implementation of the Abseil library and should not be opened in user code [abseil-no-namespace]
+
+namespace absl {
+// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: namespace 'absl'
+namespace std {
+int i = 5;
+}
+}
+
+// Things that shouldn't trigger the check
+int i = 5;
+namespace std {}




More information about the cfe-commits mailing list