1 //===---------- UsingInserter.h - clang-tidy ----------------------------===//
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_USINGINSERTER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_USINGINSERTER_H
11 
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/Stmt.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/SourceManager.h"
16 #include <set>
17 
18 namespace clang {
19 namespace tidy {
20 namespace utils {
21 
22 // UsingInserter adds using declarations for |QualifiedName| to the surrounding
23 // function.
24 // This allows using a shorter name without clobbering other scopes.
25 class UsingInserter {
26 public:
27   UsingInserter(const SourceManager &SourceMgr);
28 
29   // Creates a \p using declaration fixit. Returns ``llvm::None`` on error
30   // or if the using declaration already exists.
31   llvm::Optional<FixItHint>
32   createUsingDeclaration(ASTContext &Context, const Stmt &Statement,
33                          llvm::StringRef QualifiedName);
34 
35   // Returns the unqualified version of the name if there is an
36   // appropriate using declaration and the qualified name otherwise.
37   llvm::StringRef getShortName(ASTContext &Context, const Stmt &Statement,
38                                llvm::StringRef QualifiedName);
39 
40 private:
41   typedef std::pair<const FunctionDecl *, std::string> NameInFunction;
42   const SourceManager &SourceMgr;
43   std::set<NameInFunction> AddedUsing;
44 };
45 
46 } // namespace utils
47 } // namespace tidy
48 } // namespace clang
49 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_USINGINSERTER_H
50