12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
18using namespace ast_matchers;
21 : SourceMgr(SourceMgr) {}
24 ast_matchers::internal::Matcher<NamespaceDecl>, innerMatcher) {
25 return innerMatcher.matches(*
Node.getNamespace(), Finder,
Builder);
28std::optional<FixItHint>
31 const std::vector<std::string> &Abbreviations) {
33 if (!Function || !Function->hasBody())
36 if (AddedAliases[Function].count(Namespace.str()) != 0)
44 const auto *ExistingAlias = selectFirst<NamedDecl>(
45 "alias", match(functionDecl(hasBody(compoundStmt(has(declStmt(
46 has(namespaceAliasDecl(hasTargetNamespace(hasName(
47 std::string(Namespace))))
51 if (ExistingAlias !=
nullptr) {
52 AddedAliases[Function][Namespace.str()] = ExistingAlias->getName().str();
56 for (
const auto &Abbreviation : Abbreviations) {
57 DeclarationMatcher ConflictMatcher = namedDecl(hasName(Abbreviation));
58 const auto HasConflictingChildren =
59 !match(findAll(ConflictMatcher), *Function, Context).empty();
60 const auto HasConflictingAncestors =
61 !match(functionDecl(hasAncestor(decl(has(ConflictMatcher)))), *Function,
64 if (HasConflictingAncestors || HasConflictingChildren)
67 std::string Declaration =
68 (llvm::Twine(
"\nnamespace ") + Abbreviation +
" = " + Namespace +
";")
71 Lexer::getLocForEndOfToken(Function->getBody()->getBeginLoc(), 0,
72 SourceMgr, Context.getLangOpts());
73 AddedAliases[Function][Namespace.str()] = Abbreviation;
74 return FixItHint::CreateInsertion(
Loc, Declaration);
81 const Stmt &Statement,
82 StringRef Namespace)
const {
84 auto FunctionAliases = AddedAliases.find(Function);
85 if (FunctionAliases != AddedAliases.end()) {
86 if (FunctionAliases->second.count(Namespace) != 0) {
87 return FunctionAliases->second.find(Namespace)->getValue();
90 return Namespace.str();
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
std::string getNamespaceName(ASTContext &Context, const Stmt &Statement, llvm::StringRef Namespace) const
NamespaceAliaser(const SourceManager &SourceMgr)
std::optional< FixItHint > createAlias(ASTContext &Context, const Stmt &Statement, llvm::StringRef Namespace, const std::vector< std::string > &Abbreviations)
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)