brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · c862364 Raw
95 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 "NamespaceAliaser.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>16namespace clang::tidy::utils {17 18using namespace ast_matchers;19namespace {20AST_MATCHER_P(NamespaceAliasDecl, hasTargetNamespace,21              ast_matchers::internal::Matcher<NamespaceDecl>, InnerMatcher) {22  return InnerMatcher.matches(*Node.getNamespace(), Finder, Builder);23}24} // namespace25 26NamespaceAliaser::NamespaceAliaser(const SourceManager &SourceMgr)27    : SourceMgr(SourceMgr) {}28 29std::optional<FixItHint>30NamespaceAliaser::createAlias(ASTContext &Context, const Stmt &Statement,31                              StringRef Namespace,32                              const std::vector<std::string> &Abbreviations) {33  const FunctionDecl *Function = getSurroundingFunction(Context, Statement);34  if (!Function || !Function->hasBody())35    return std::nullopt;36 37  if (AddedAliases[Function].contains(Namespace.str()))38    return std::nullopt;39 40  // FIXME: Doesn't consider the order of declarations.41  // If we accidentally pick an alias defined later in the function,42  // the output won't compile.43  // FIXME: Also doesn't consider file or class-scope aliases.44 45  const auto *ExistingAlias = selectFirst<NamedDecl>(46      "alias", match(functionDecl(hasBody(compoundStmt(has(declStmt(47                         has(namespaceAliasDecl(hasTargetNamespace(hasName(48                                                    std::string(Namespace))))49                                 .bind("alias"))))))),50                     *Function, Context));51 52  if (ExistingAlias != nullptr) {53    AddedAliases[Function][Namespace.str()] = ExistingAlias->getName().str();54    return std::nullopt;55  }56 57  for (const auto &Abbreviation : Abbreviations) {58    const 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,63               Context)64             .empty();65    if (HasConflictingAncestors || HasConflictingChildren)66      continue;67 68    const std::string Declaration =69        (llvm::Twine("\nnamespace ") + Abbreviation + " = " + Namespace + ";")70            .str();71    const SourceLocation Loc =72        Lexer::getLocForEndOfToken(Function->getBody()->getBeginLoc(), 0,73                                   SourceMgr, Context.getLangOpts());74    AddedAliases[Function][Namespace.str()] = Abbreviation;75    return FixItHint::CreateInsertion(Loc, Declaration);76  }77 78  return std::nullopt;79}80 81std::string NamespaceAliaser::getNamespaceName(ASTContext &Context,82                                               const Stmt &Statement,83                                               StringRef Namespace) const {84  const auto *Function = getSurroundingFunction(Context, Statement);85  auto FunctionAliases = AddedAliases.find(Function);86  if (FunctionAliases != AddedAliases.end()) {87    if (FunctionAliases->second.contains(Namespace)) {88      return FunctionAliases->second.find(Namespace)->getValue();89    }90  }91  return Namespace.str();92}93 94} // namespace clang::tidy::utils95