• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===---------- NamespaceAliaser.cpp - clang-tidy -------------------------===//
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-exception
6 //
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 namespace clang {
16 namespace tidy {
17 namespace utils {
18 
19 using namespace ast_matchers;
20 
NamespaceAliaser(const SourceManager & SourceMgr)21 NamespaceAliaser::NamespaceAliaser(const SourceManager &SourceMgr)
22     : SourceMgr(SourceMgr) {}
23 
AST_MATCHER_P(NamespaceAliasDecl,hasTargetNamespace,ast_matchers::internal::Matcher<NamespaceDecl>,innerMatcher)24 AST_MATCHER_P(NamespaceAliasDecl, hasTargetNamespace,
25               ast_matchers::internal::Matcher<NamespaceDecl>, innerMatcher) {
26   return innerMatcher.matches(*Node.getNamespace(), Finder, Builder);
27 }
28 
29 Optional<FixItHint>
createAlias(ASTContext & Context,const Stmt & Statement,StringRef Namespace,const std::vector<std::string> & Abbreviations)30 NamespaceAliaser::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 None;
36 
37   if (AddedAliases[Function].count(Namespace.str()) != 0)
38     return None;
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 None;
55   }
56 
57   for (const auto &Abbreviation : Abbreviations) {
58     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     std::string Declaration =
69         (llvm::Twine("\nnamespace ") + Abbreviation + " = " + Namespace + ";")
70             .str();
71     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 None;
79 }
80 
getNamespaceName(ASTContext & Context,const Stmt & Statement,StringRef Namespace) const81 std::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.count(Namespace) != 0) {
88       return FunctionAliases->second.find(Namespace)->getValue();
89     }
90   }
91   return Namespace.str();
92 }
93 
94 } // namespace utils
95 } // namespace tidy
96 } // namespace clang
97