[clang-tools-extra] 0e11d65 - [clang-tidy] Don't emit misc-unused-using-decl warnings for header files.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 4 01:11:54 PST 2023


Author: Haojian Wu
Date: 2023-01-04T10:07:28+01:00
New Revision: 0e11d65a58da32311b562ecea2b5ba9d4d655659

URL: https://github.com/llvm/llvm-project/commit/0e11d65a58da32311b562ecea2b5ba9d4d655659
DIFF: https://github.com/llvm/llvm-project/commit/0e11d65a58da32311b562ecea2b5ba9d4d655659.diff

LOG: [clang-tidy] Don't emit misc-unused-using-decl warnings for header files.

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.

Differential Revision: https://reviews.llvm.org/D140894

Added: 
    clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
index 244cf719b5a8c..6aee03083282d 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
@@ -38,6 +38,18 @@ static bool shouldCheckDecl(const Decl *TargetDecl) {
          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 +78,12 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
 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.

diff  --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
index 1383fcadc6cd2..41eedb3873707 100644
--- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.h
+++ b/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 @@ namespace misc {
 /// 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 @@ class UnusedUsingDeclsCheck : public ClangTidyCheck {
   };
 
   std::vector<UsingDeclContext> Contexts;
+
+  const StringRef RawStringHeaderFileExtensions;
+  utils::FileExtensionsSet HeaderFileExtensions;
 };
 
 } // namespace misc

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
index 9de1cb2cc3588..26dcf40c01b46 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/unused-using-decls.rst
@@ -5,9 +5,24 @@ misc-unused-using-decls
 
 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, use an empty string or leave an
+   empty string between "," if there are other filename extensions.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx b/clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.hxx
new file mode 100644
index 0000000000000..f15e4fae80c0b
--- /dev/null
+++ b/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;


        


More information about the cfe-commits mailing list