juliehockett created this revision.
juliehockett added reviewers: aaron.ballman, alexfh, hokein.
juliehockett added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, mgorny.

Adds a check to convert <fbl/limits.h> to std <limits>.

This check is part of a set of migration checks as we prepare to move Zircon 
user code to use the C++ standard library, and should prevent regressions after 
the migration is complete.


https://reviews.llvm.org/D54169

Files:
  clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
  clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.cpp
  clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.h
  clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-limits.rst
  clang-tools-extra/test/clang-tidy/Inputs/zircon/fbl/limits.h
  clang-tools-extra/test/clang-tidy/Inputs/zircon/transitive.h
  clang-tools-extra/test/clang-tidy/zircon-fbl-limits-transitive.cpp
  clang-tools-extra/test/clang-tidy/zircon-fbl-limits.cpp

Index: clang-tools-extra/test/clang-tidy/zircon-fbl-limits.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/zircon-fbl-limits.cpp
@@ -0,0 +1,45 @@
+// RUN: %check_clang_tidy %s zircon-fbl-limits %t -- -- -isystem %S/Inputs/zircon
+
+#include <fbl/limits.h>
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: including fbl/limits.h is deprecated
+// CHECK-FIXES-NOT: #include <fbl/limits.h>
+// CHECK-FIXES: #include <limits>
+
+namespace fbl {
+
+template <class T>
+class numeric_limits {};
+
+#define SPECIALIZE_INT_FBL(type) \
+  template <>                    \
+  class numeric_limits<type> {};
+
+SPECIALIZE_INT_FBL(short)
+
+numeric_limits<short> lim;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use of fbl::numeric_limits is deprecated, use std::numeric_limits instead
+// CHECK-FIXES: std::numeric_limits<short> lim;
+
+} // namespace fbl
+
+namespace std {
+
+template <class T>
+class numeric_limits {};
+
+#define SPECIALIZE_INT_STD(type) \
+  template <>                    \
+  class numeric_limits<type> {};
+
+SPECIALIZE_INT_STD(short)
+
+numeric_limits<short> lim;
+
+} // namespace std
+
+int main() {
+  fbl::numeric_limits<short> fbllim;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of fbl::numeric_limits is deprecated, use std::numeric_limits instead
+  // CHECK-FIXES: std::numeric_limits<short> fbllim;
+  std::numeric_limits<short> stdlim;
+}
Index: clang-tools-extra/test/clang-tidy/zircon-fbl-limits-transitive.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/zircon-fbl-limits-transitive.cpp
@@ -0,0 +1,11 @@
+// RUN: rm -rf %T/Headers
+// RUN: mkdir %T/Headers
+// RUN: cp -r %S/Inputs/zircon %T/Headers/zircon
+// RUN: %check_clang_tidy %s zircon-fbl-limits %t -- -header-filter=.* -- -std=c++11 -I %T/Headers/zircon
+// RUN: FileCheck -input-file=%T/Headers/zircon/transitive.h %s -check-prefix=CHECK-FIXES
+// RUN: rm -rf %T/Headers
+
+// transitive.h includes <fbl/limits.h>
+#include "transitive.h"
+// CHECK-NOTES: :2:1: warning: including fbl/limits.h is deprecated, transitively included from {{.*}}.
+// CHECK-FIXES-NOT: #include <fbl/limits.h>
Index: clang-tools-extra/test/clang-tidy/Inputs/zircon/transitive.h
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/Inputs/zircon/transitive.h
@@ -0,0 +1,2 @@
+// Transitively included headers.
+#include <fbl/limits.h>
Index: clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-limits.rst
===================================================================
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/zircon-fbl-limits.rst
@@ -0,0 +1,13 @@
+.. title:: clang-tidy - zircon-fbl-limits
+
+zircon-fbl-limits
+=================
+
+This check is part of the migration checks for moving Zircon user code to use
+the C++ standard library.
+
+It suggests converting uses of ``fbl::numeric_limits<T>`` to
+``std::numeric_limits<T>``, and suggests inserting the ``<limits>`` header. It
+also suggests the removal of all uses of the ``<fbl/limits.h>`` header, as all
+declarations therein will be replaced by the check and the appropriate
+``<limits>`` replacement header recommended.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -254,4 +254,5 @@
    readability-string-compare
    readability-uniqueptr-delete-release
    readability-uppercase-literal-suffix
+   zircon-fbl-limits
    zircon-temporary-objects
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -182,6 +182,13 @@
   <clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn
   about calls inside macros anymore by default.
 
+- New :doc:`zircon-fbl-limits
+  <clang-tidy/checks/zircon-fbl-limits>` check.
+
+  This check is part of the migration checks for moving Zircon user code to use
+  the C++ standard library. It suggests converting uses of declarations in the
+  ``<fbl/limits.h>`` header to their std counterparts in ``<limits>``.
+
 Improvements to include-fixer
 -----------------------------
 
Index: clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
+++ clang-tools-extra/clang-tidy/zircon/ZirconTidyModule.cpp
@@ -10,6 +10,7 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
+#include "FblLimitsCheck.h"
 #include "TemporaryObjectsCheck.h"
 
 using namespace clang::ast_matchers;
@@ -22,6 +23,8 @@
 class ZirconModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+    CheckFactories.registerCheck<FblLimitsCheck>(
+        "zircon-fbl-limits");
     CheckFactories.registerCheck<TemporaryObjectsCheck>(
         "zircon-temporary-objects");
   }
Index: clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.h
@@ -0,0 +1,46 @@
+//===--- FblLimitsCheck.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_ZIRCON_FBLLIMITSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ZIRCON_FBLLIMITSCHECK_H
+
+#include "../ClangTidy.h"
+#include "../utils/IncludeInserter.h"
+
+namespace clang {
+namespace tidy {
+namespace zircon {
+
+/// Replace instances of fbl::numeric_limits with std::numeric_limits, removes
+/// <fbl/limits.h> includes and adds <limits> include if std::numeric_limits is
+/// used.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/zircon-fbl-limits.html
+class FblLimitsCheck : public ClangTidyCheck {
+public:
+  FblLimitsCheck(StringRef Name, ClangTidyContext *Context)
+      : ClangTidyCheck(Name, Context),
+        IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+            Options.getLocalOrGlobal("IncludeStyle", "google"))) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void registerPPCallbacks(clang::CompilerInstance &Compiler) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
+private:
+  std::unique_ptr<utils::IncludeInserter> Inserter;
+  const utils::IncludeSorter::IncludeStyle IncludeStyle;
+};
+
+} // namespace zircon
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ZIRCON_FBLLIMITSCHECK_H
Index: clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/zircon/FblLimitsCheck.cpp
@@ -0,0 +1,124 @@
+//===--- FblLimitsCheck.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 "FblLimitsCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace zircon {
+
+class FblLimitsPPCallbacks : public PPCallbacks {
+public:
+  explicit FblLimitsPPCallbacks(FblLimitsCheck &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:
+  CharSourceRange IncludeRange;
+
+  FblLimitsCheck &Check;
+  SourceManager &SM;
+};
+
+void FblLimitsPPCallbacks::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 (FileName == "fbl/limits.h") {
+    unsigned End = std::strcspn(SM.getCharacterData(HashLoc), "\n") + 1;
+    IncludeRange =
+        CharSourceRange::getCharRange(HashLoc, HashLoc.getLocWithOffset(End));
+  }
+}
+
+void FblLimitsPPCallbacks::EndOfMainFile() {
+  // Any instance of <fbl/limits.h> should be removed, since the decls in it
+  // will be replaced in this check and <limits> added.
+  if (!SM.isInMainFile(IncludeRange.getBegin())) {
+    Check.diag(IncludeRange.getBegin(),
+               "including fbl/limits.h is deprecated, transitively "
+               "included from %0")
+        << SM.getFilename(IncludeRange.getBegin())
+        << FixItHint::CreateRemoval(IncludeRange);
+    return;
+  }
+  Check.diag(IncludeRange.getBegin(), "including fbl/limits.h is deprecated")
+      << FixItHint::CreateRemoval(IncludeRange);
+}
+
+void FblLimitsCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(varDecl(hasType(cxxRecordDecl(allOf(
+                                 hasDeclContext(namespaceDecl(hasName("fbl"))),
+                                 hasName("numeric_limits")))))
+                         .bind("limits"),
+                     this);
+}
+
+void FblLimitsCheck::registerPPCallbacks(CompilerInstance &Compiler) {
+  Inserter = llvm::make_unique<utils::IncludeInserter>(
+      Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle);
+  Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
+  Compiler.getPreprocessor().addPPCallbacks(
+      llvm::make_unique<FblLimitsPPCallbacks>(*this,
+                                              Compiler.getSourceManager()));
+}
+
+void FblLimitsCheck::check(const MatchFinder::MatchResult &Result) {
+  SourceManager &SM = *Result.SourceManager;
+  const auto *V = Result.Nodes.getNodeAs<VarDecl>("limits");
+
+  bool Invalid = false;
+  StringRef ExprStr = Lexer::getSourceText(
+      CharSourceRange::getCharRange(V->getBeginLoc(), V->getEndLoc()), SM,
+      getLangOpts(), &Invalid);
+  if (Invalid)
+    return;
+
+  size_t LAngle = ExprStr.find("<");
+  SourceLocation End;
+  if (LAngle == std::string::npos)
+    End = V->getEndLoc();
+  else
+    End = V->getBeginLoc().getLocWithOffset(LAngle - 1);
+
+  auto Diag =
+      diag(V->getBeginLoc(), "use of fbl::numeric_limits is deprecated, use "
+                             "std::numeric_limits instead")
+      << FixItHint::CreateReplacement(SourceRange(V->getBeginLoc(), End),
+                                      "std::numeric_limits");
+
+  // Add in the <limits> header, since we know this file uses it.
+  if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
+          SM.getFileID(V->getLocation()), "limits",
+          /*IsAngled=*/true)) {
+    Diag << *IncludeFixit;
+  }
+}
+
+void FblLimitsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle", IncludeStyle);
+}
+
+} // namespace zircon
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
===================================================================
--- clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/zircon/CMakeLists.txt
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyZirconModule
+  FblLimitsCheck.cpp
   TemporaryObjectsCheck.cpp
   ZirconTidyModule.cpp
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to