hokein created this revision.
hokein added a reviewer: ymandel.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
hokein requested review of this revision.
Herald added a project: clang-tools-extra.

Using decls in header files are special, usually as part of the
public API, the check should not emit warnings on these.

The check already detects unused using-decls which are in the current main
file, but if the main file happens to be a header file, we still
emit warnings, this patch suppresses that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140894

Files:
  clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
  clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
  clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
  clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx

Index: clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx
@@ -0,0 +1,6 @@
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t -- --fix-notes -- -fno-delayed-template-parsing -isystem %S/Inputs
+
+// Verify that we don't generate the warnings on header files.
+namespace foo { class Foo {}; }
+
+using foo::Foo;
Index: clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
+++ clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
@@ -5,9 +5,24 @@
 
 Finds unused ``using`` declarations.
 
+Unused ``using``` declarations in header files will not be diagnosed since these
+using declarations are part of the header's public API. Allowed header file
+extensions can be configured via the `HeaderFileExtensions` option (see below).
+
 Example:
 
 .. code-block:: c++
 
+  // main.cpp
   namespace n { class C; }
   using n::C;  // Never actually used.
+
+Options
+-------
+
+.. option:: HeaderFileExtensions
+
+   A semicolon-separated list of filename extensions of header files (the filename
+   extensions should not include "." prefix). Default is ";h;hh;hpp;hxx".
+   For extension-less header files, using an empty string or leaving an
+   empty string between ";" if there are other filename extensions.
Index: clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
@@ -10,6 +10,7 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H
 
 #include "../ClangTidyCheck.h"
+#include "../utils/FileExtensionsUtils.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include <vector>
 
@@ -23,8 +24,7 @@
 /// http://clang.llvm.org/extra/clang-tidy/checks/misc/unused-using-decls.html
 class UnusedUsingDeclsCheck : public ClangTidyCheck {
 public:
-  UnusedUsingDeclsCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+  UnusedUsingDeclsCheck(StringRef Name, ClangTidyContext *Context);
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   void onEndOfTranslationUnit() override;
@@ -48,6 +48,9 @@
   };
 
   std::vector<UsingDeclContext> Contexts;
+
+  const StringRef RawStringHeaderFileExtensions;
+  utils::FileExtensionsSet HeaderFileExtensions;
 };
 
 } // namespace misc
Index: clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -38,6 +38,19 @@
          isa<EnumConstantDecl>(TargetDecl);
 }
 
+UnusedUsingDeclsCheck::UnusedUsingDeclsCheck(StringRef Name,
+                                             ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
+          "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
+  if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
+                                  HeaderFileExtensions,
+                                  utils::defaultFileExtensionDelimiters())) {
+    this->configurationDiag("Invalid header file extension: '%0'")
+        << RawStringHeaderFileExtensions;
+  }
+}
+
 void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
   auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
@@ -66,6 +79,12 @@
 void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
   if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
     return;
+  // We don't emit warnings on unused-using-decls from headers, so bail out if
+  // the main file is a header.
+  if (const auto *MainFile = Result.SourceManager->getFileEntryForID(
+          Result.SourceManager->getMainFileID());
+      utils::isFileExtension(MainFile->getName(), HeaderFileExtensions))
+    return;
 
   if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
     // Ignores using-declarations defined in macros.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to