UsingInserter.h 1.7 KB

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