NamespaceAliaser.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //===---------- NamespaceAliaser.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_NAMESPACEALIASER_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_NAMESPACEALIASER_H
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/Stmt.h"
  12. #include "clang/Basic/Diagnostic.h"
  13. #include "clang/Basic/SourceManager.h"
  14. #include "llvm/ADT/DenseMap.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include <map>
  17. #include <optional>
  18. namespace clang::tidy::utils {
  19. // This class creates function-level namespace aliases.
  20. class NamespaceAliaser {
  21. public:
  22. explicit NamespaceAliaser(const SourceManager &SourceMgr);
  23. // Adds a namespace alias for \p Namespace valid near \p
  24. // Statement. Picks the first available name from \p Abbreviations.
  25. // Returns ``std::nullopt`` if an alias already exists or there is an error.
  26. std::optional<FixItHint>
  27. createAlias(ASTContext &Context, const Stmt &Statement,
  28. llvm::StringRef Namespace,
  29. const std::vector<std::string> &Abbreviations);
  30. // Get an alias name for \p Namespace valid at \p Statement. Returns \p
  31. // Namespace if there is no alias.
  32. std::string getNamespaceName(ASTContext &Context, const Stmt &Statement,
  33. llvm::StringRef Namespace) const;
  34. private:
  35. const SourceManager &SourceMgr;
  36. llvm::DenseMap<const FunctionDecl *, llvm::StringMap<std::string>>
  37. AddedAliases;
  38. };
  39. } // namespace clang::tidy::utils
  40. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_NAMESPACEALIASER_H