12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Lex/Lexer.h"
19 using namespace ast_matchers;
25 ast_matchers::internal::Matcher<NamespaceDecl>, innerMatcher) {
26 return innerMatcher.matches(*Node.getNamespace(), Finder,
Builder);
32 const std::vector<std::string> &Abbreviations) {
34 if (!Function || !Function->hasBody())
37 if (AddedAliases[Function].count(Namespace.str()) != 0)
45 const auto *ExistingAlias = selectFirst<NamedDecl>(
46 "alias",
match(functionDecl(hasBody(compoundStmt(has(declStmt(
47 has(namespaceAliasDecl(hasTargetNamespace(hasName(
48 std::string(Namespace))))
52 if (ExistingAlias !=
nullptr) {
53 AddedAliases[Function][Namespace.str()] = ExistingAlias->getName().str();
57 for (
const auto &Abbreviation : Abbreviations) {
58 DeclarationMatcher ConflictMatcher = namedDecl(hasName(Abbreviation));
59 const auto HasConflictingChildren =
60 !
match(findAll(ConflictMatcher), *Function, Context).empty();
61 const auto HasConflictingAncestors =
62 !
match(functionDecl(hasAncestor(decl(has(ConflictMatcher)))), *Function,
65 if (HasConflictingAncestors || HasConflictingChildren)
68 std::string Declaration =
69 (llvm::Twine(
"\nnamespace ") + Abbreviation +
" = " + Namespace +
";")
72 Lexer::getLocForEndOfToken(Function->getBody()->getBeginLoc(), 0,
73 SourceMgr, Context.getLangOpts());
74 AddedAliases[Function][Namespace.str()] = Abbreviation;
75 return FixItHint::CreateInsertion(
Loc, Declaration);
82 const Stmt &Statement,
83 StringRef Namespace)
const {
85 auto FunctionAliases = AddedAliases.find(Function);
86 if (FunctionAliases != AddedAliases.end()) {
87 if (FunctionAliases->second.count(Namespace) != 0) {
88 return FunctionAliases->second.find(Namespace)->getValue();
91 return Namespace.str();