13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/ASTMatchers/ASTMatchers.h" 15 #include "clang/Lex/Lexer.h" 21 using namespace ast_matchers;
24 size_t LastSeparatorPos = QualifiedName.rfind(
"::");
25 if (LastSeparatorPos == StringRef::npos)
27 return QualifiedName.drop_front(LastSeparatorPos + 2);
31 : SourceMgr(SourceMgr) {}
34 ASTContext &Context,
const Stmt &Statement, StringRef QualifiedName) {
40 if (AddedUsing.count(std::make_pair(Function, QualifiedName.str())) != 0)
43 SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
44 Function->getBody()->getLocStart(), 0, SourceMgr, Context.getLangOpts());
47 if (SourceMgr.getFileID(InsertLoc) != SourceMgr.getMainFileID())
52 bool AlreadyHasUsingDecl =
53 !match(stmt(hasAncestor(decl(has(usingDecl(hasAnyUsingShadowDecl(
54 hasTargetDecl(hasName(QualifiedName.str())))))))),
57 if (AlreadyHasUsingDecl) {
58 AddedUsing.emplace(NameInFunction(Function, QualifiedName.str()));
62 auto ConflictingDecl = namedDecl(hasName(UnqualifiedName));
63 bool HasConflictingDeclaration =
64 !match(findAll(ConflictingDecl), *Function, Context).empty();
65 bool HasConflictingDeclRef =
66 !match(findAll(declRefExpr(to(ConflictingDecl))), *Function, Context)
68 if (HasConflictingDeclaration || HasConflictingDeclRef)
71 std::string Declaration =
72 (llvm::Twine(
"\nusing ") + QualifiedName +
";").str();
74 AddedUsing.emplace(std::make_pair(Function, QualifiedName.str()));
75 return FixItHint::CreateInsertion(InsertLoc, Declaration);
79 const Stmt &Statement,
80 StringRef QualifiedName) {
82 if (AddedUsing.count(NameInFunction(Function, QualifiedName.str())) != 0)
UsingInserter(const SourceManager &SourceMgr)
static StringRef getUnqualifiedName(StringRef QualifiedName)
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)
llvm::Optional< FixItHint > createUsingDeclaration(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)
llvm::StringRef getShortName(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)