brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 6a591c1 Raw
86 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "UsingInserter.h"10 11#include "ASTUtils.h"12#include "clang/ASTMatchers/ASTMatchFinder.h"13#include "clang/ASTMatchers/ASTMatchers.h"14#include "clang/Lex/Lexer.h"15#include <optional>16 17namespace clang::tidy::utils {18 19using namespace ast_matchers;20 21static StringRef getUnqualifiedName(StringRef QualifiedName) {22  const size_t LastSeparatorPos = QualifiedName.rfind("::");23  if (LastSeparatorPos == StringRef::npos)24    return QualifiedName;25  return QualifiedName.drop_front(LastSeparatorPos + 2);26}27 28UsingInserter::UsingInserter(const SourceManager &SourceMgr)29    : SourceMgr(SourceMgr) {}30 31std::optional<FixItHint> UsingInserter::createUsingDeclaration(32    ASTContext &Context, const Stmt &Statement, StringRef QualifiedName) {33  const StringRef UnqualifiedName = getUnqualifiedName(QualifiedName);34  const FunctionDecl *Function = getSurroundingFunction(Context, Statement);35  if (!Function)36    return std::nullopt;37 38  if (AddedUsing.count(std::make_pair(Function, QualifiedName.str())) != 0)39    return std::nullopt;40 41  const SourceLocation InsertLoc = Lexer::getLocForEndOfToken(42      Function->getBody()->getBeginLoc(), 0, SourceMgr, Context.getLangOpts());43 44  // Only use using declarations in the main file, not in includes.45  if (SourceMgr.getFileID(InsertLoc) != SourceMgr.getMainFileID())46    return std::nullopt;47 48  // FIXME: This declaration could be masked. Investigate if49  // there is a way to avoid using Sema.50  const bool AlreadyHasUsingDecl =51      !match(stmt(hasAncestor(decl(has(usingDecl(hasAnyUsingShadowDecl(52                 hasTargetDecl(hasName(QualifiedName.str())))))))),53             Statement, Context)54           .empty();55  if (AlreadyHasUsingDecl) {56    AddedUsing.emplace(Function, QualifiedName.str());57    return std::nullopt;58  }59  // Find conflicting declarations and references.60  auto ConflictingDecl = namedDecl(hasName(UnqualifiedName));61  const bool HasConflictingDeclaration =62      !match(findAll(ConflictingDecl), *Function, Context).empty();63  const bool HasConflictingDeclRef =64      !match(findAll(declRefExpr(to(ConflictingDecl))), *Function, Context)65           .empty();66  if (HasConflictingDeclaration || HasConflictingDeclRef)67    return std::nullopt;68 69  const std::string Declaration =70      (llvm::Twine("\nusing ") + QualifiedName + ";").str();71 72  AddedUsing.emplace(Function, QualifiedName.str());73  return FixItHint::CreateInsertion(InsertLoc, Declaration);74}75 76StringRef UsingInserter::getShortName(ASTContext &Context,77                                      const Stmt &Statement,78                                      StringRef QualifiedName) {79  const FunctionDecl *Function = getSurroundingFunction(Context, Statement);80  if (AddedUsing.count(NameInFunction(Function, QualifiedName.str())) != 0)81    return getUnqualifiedName(QualifiedName);82  return QualifiedName;83}84 85} // namespace clang::tidy::utils86