This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0e11d65a58da: [clang-tidy] Don't emit
misc-unused-using-decl warnings for header files. (authored by hokein).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D140894/new/
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, use an empty string or leave 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,18 @@
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::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
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits