blob: 2e46f3c1b357043438dd9d351f7dc79fbbb69894 [file] [log] [blame]
Daniel Jasper727fd1a2016-04-19 13:48:39 +00001//===--- UnusedUsingDeclsCheck.h - clang-tidy--------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Jasper727fd1a2016-04-19 13:48:39 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H
11
Alexander Kornienko478fc5c2019-03-25 12:38:26 +000012#include "../ClangTidyCheck.h"
Haojian Wu1cea6e52016-05-20 08:34:32 +000013#include "llvm/ADT/SmallPtrSet.h"
14#include <vector>
Daniel Jasper727fd1a2016-04-19 13:48:39 +000015
16namespace clang {
17namespace tidy {
18namespace misc {
19
20/// Finds unused using declarations.
21///
22/// For the user-facing documentation see:
23/// http://clang.llvm.org/extra/clang-tidy/checks/misc-unused-using-decls.html
24class UnusedUsingDeclsCheck : public ClangTidyCheck {
25public:
26 UnusedUsingDeclsCheck(StringRef Name, ClangTidyContext *Context)
27 : ClangTidyCheck(Name, Context) {}
28 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
29 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
30 void onEndOfTranslationUnit() override;
31
32private:
Haojian Wu769eb0d2016-05-09 13:37:12 +000033 void removeFromFoundDecls(const Decl *D);
34
Haojian Wu1cea6e52016-05-20 08:34:32 +000035 struct UsingDeclContext {
36 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
37 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
Haojian Wud80c7c42016-06-03 08:05:11 +000038 // A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
39 // can introduce multiple UsingShadowDecls in some cases (such as
40 // overloaded functions).
Haojian Wu1cea6e52016-05-20 08:34:32 +000041 llvm::SmallPtrSet<const Decl *, 4> UsingTargetDecls;
Haojian Wud80c7c42016-06-03 08:05:11 +000042 // The original UsingDecl.
Haojian Wu1cea6e52016-05-20 08:34:32 +000043 const UsingDecl *FoundUsingDecl;
Haojian Wud80c7c42016-06-03 08:05:11 +000044 // The source range of the UsingDecl.
Haojian Wu1cea6e52016-05-20 08:34:32 +000045 CharSourceRange UsingDeclRange;
Haojian Wud80c7c42016-06-03 08:05:11 +000046 // Whether the UsingDecl is used.
Haojian Wu1cea6e52016-05-20 08:34:32 +000047 bool IsUsed;
48 };
49
50 std::vector<UsingDeclContext> Contexts;
Daniel Jasper727fd1a2016-04-19 13:48:39 +000051};
52
53} // namespace misc
54} // namespace tidy
55} // namespace clang
56
57#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H