12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Lex/Lexer.h" 19 using namespace ast_matchers;
22 : SourceMgr(SourceMgr) {}
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>(
47 match(functionDecl(hasBody(compoundStmt(has(declStmt(
48 has(namespaceAliasDecl(hasTargetNamespace(hasName(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();
SourceLocation Loc
'#' location in the include directive
NamespaceAliaser(const SourceManager &SourceMgr)
std::string getNamespaceName(ASTContext &Context, const Stmt &Statement, llvm::StringRef Namespace) const
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< FixItHint > createAlias(ASTContext &Context, const Stmt &Statement, llvm::StringRef Namespace, const std::vector< std::string > &Abbreviations)
AST_MATCHER_P(NamespaceAliasDecl, hasTargetNamespace, ast_matchers::internal::Matcher< NamespaceDecl >, innerMatcher)