12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Lex/Lexer.h" 20 using namespace ast_matchers;
23 size_t LastSeparatorPos = QualifiedName.rfind(
"::");
24 if (LastSeparatorPos == StringRef::npos)
26 return QualifiedName.drop_front(LastSeparatorPos + 2);
30 : SourceMgr(SourceMgr) {}
33 ASTContext &Context,
const Stmt &Statement, StringRef QualifiedName) {
39 if (AddedUsing.count(std::make_pair(Function, QualifiedName.str())) != 0)
42 SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
43 Function->getBody()->getBeginLoc(), 0, SourceMgr, Context.getLangOpts());
46 if (SourceMgr.getFileID(InsertLoc) != SourceMgr.getMainFileID())
51 bool AlreadyHasUsingDecl =
52 !
match(stmt(hasAncestor(decl(has(usingDecl(hasAnyUsingShadowDecl(
53 hasTargetDecl(hasName(QualifiedName.str())))))))),
56 if (AlreadyHasUsingDecl) {
57 AddedUsing.emplace(NameInFunction(Function, QualifiedName.str()));
61 auto ConflictingDecl = namedDecl(hasName(UnqualifiedName));
62 bool HasConflictingDeclaration =
63 !
match(findAll(ConflictingDecl), *Function, Context).empty();
64 bool HasConflictingDeclRef =
65 !
match(findAll(declRefExpr(to(ConflictingDecl))), *Function, Context)
67 if (HasConflictingDeclaration || HasConflictingDeclRef)
70 std::string Declaration =
71 (llvm::Twine(
"\nusing ") + QualifiedName +
";").str();
73 AddedUsing.emplace(std::make_pair(Function, QualifiedName.str()));
74 return FixItHint::CreateInsertion(InsertLoc, Declaration);
78 const Stmt &Statement,
79 StringRef QualifiedName) {
81 if (AddedUsing.count(NameInFunction(Function, QualifiedName.str())) != 0)
UsingInserter(const SourceManager &SourceMgr)
static StringRef getUnqualifiedName(StringRef QualifiedName)
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
llvm::Optional< FixItHint > createUsingDeclaration(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef getShortName(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)