blob: 2e46f3c1b357043438dd9d351f7dc79fbbb69894 [file] [log] [blame]
Olivier Deprezf4ef2d02021-04-20 13:36:24 +02001//===--- UnusedUsingDeclsCheck.h - clang-tidy--------------------*- C++ -*-===//
2//
3// 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
6//
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
12#include "../ClangTidyCheck.h"
13#include "llvm/ADT/SmallPtrSet.h"
14#include <vector>
15
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:
33 void removeFromFoundDecls(const Decl *D);
34
35 struct UsingDeclContext {
36 explicit UsingDeclContext(const UsingDecl *FoundUsingDecl)
37 : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {}
38 // A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl
39 // can introduce multiple UsingShadowDecls in some cases (such as
40 // overloaded functions).
41 llvm::SmallPtrSet<const Decl *, 4> UsingTargetDecls;
42 // The original UsingDecl.
43 const UsingDecl *FoundUsingDecl;
44 // The source range of the UsingDecl.
45 CharSourceRange UsingDeclRange;
46 // Whether the UsingDecl is used.
47 bool IsUsed;
48 };
49
50 std::vector<UsingDeclContext> Contexts;
51};
52
53} // namespace misc
54} // namespace tidy
55} // namespace clang
56
57#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H