12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
19using namespace ast_matchers;
22 size_t LastSeparatorPos = QualifiedName.rfind(
"::");
23 if (LastSeparatorPos == StringRef::npos)
25 return QualifiedName.drop_front(LastSeparatorPos + 2);
29 : SourceMgr(SourceMgr) {}
32 ASTContext &Context,
const Stmt &Statement, StringRef QualifiedName) {
38 if (AddedUsing.count(std::make_pair(Function, QualifiedName.str())) != 0)
41 SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
42 Function->getBody()->getBeginLoc(), 0, SourceMgr, Context.getLangOpts());
45 if (SourceMgr.getFileID(InsertLoc) != SourceMgr.getMainFileID())
50 bool AlreadyHasUsingDecl =
51 !match(stmt(hasAncestor(decl(has(usingDecl(hasAnyUsingShadowDecl(
52 hasTargetDecl(hasName(QualifiedName.str())))))))),
55 if (AlreadyHasUsingDecl) {
56 AddedUsing.emplace(Function, QualifiedName.str());
60 auto ConflictingDecl = namedDecl(hasName(UnqualifiedName));
61 bool HasConflictingDeclaration =
62 !match(findAll(ConflictingDecl), *Function, Context).empty();
63 bool HasConflictingDeclRef =
64 !match(findAll(declRefExpr(to(ConflictingDecl))), *Function, Context)
66 if (HasConflictingDeclaration || HasConflictingDeclRef)
69 std::string Declaration =
70 (llvm::Twine(
"\nusing ") + QualifiedName +
";").str();
72 AddedUsing.emplace(Function, QualifiedName.str());
73 return FixItHint::CreateInsertion(InsertLoc, Declaration);
77 const Stmt &Statement,
78 StringRef QualifiedName) {
80 if (AddedUsing.count(NameInFunction(Function, QualifiedName.str())) != 0)
llvm::StringRef getShortName(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)
std::optional< FixItHint > createUsingDeclaration(ASTContext &Context, const Stmt &Statement, llvm::StringRef QualifiedName)
UsingInserter(const SourceManager &SourceMgr)
static StringRef getUnqualifiedName(StringRef QualifiedName)
const FunctionDecl * getSurroundingFunction(ASTContext &Context, const Stmt &Statement)