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 | #include <optional> |
16 | namespace clang::tidy::utils { |
17 | |
18 | using namespace ast_matchers; |
19 | namespace { |
20 | AST_MATCHER_P(NamespaceAliasDecl, hasTargetNamespace, |
21 | ast_matchers::internal::Matcher<NamespaceDecl>, InnerMatcher) { |
22 | return InnerMatcher.matches(Node: *Node.getNamespace(), Finder, Builder); |
23 | } |
24 | } // namespace |
25 | |
26 | NamespaceAliaser::NamespaceAliaser(const SourceManager &SourceMgr) |
27 | : SourceMgr(SourceMgr) {} |
28 | |
29 | std::optional<FixItHint> |
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 std::nullopt; |
36 | |
37 | if (AddedAliases[Function].count(Key: Namespace.str()) != 0) |
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 | BoundTo: "alias" , Results: match(Matcher: functionDecl(hasBody(InnerMatcher: compoundStmt(has(declStmt( |
47 | has(namespaceAliasDecl(hasTargetNamespace(InnerMatcher: hasName( |
48 | Name: std::string(Namespace)))) |
49 | .bind(ID: "alias" ))))))), |
50 | Node: *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 | DeclarationMatcher ConflictMatcher = namedDecl(hasName(Name: Abbreviation)); |
59 | const auto HasConflictingChildren = |
60 | !match(Matcher: findAll(Matcher: ConflictMatcher), Node: *Function, Context).empty(); |
61 | const auto HasConflictingAncestors = |
62 | !match(Matcher: functionDecl(hasAncestor(decl(has(ConflictMatcher)))), Node: *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(Loc: Function->getBody()->getBeginLoc(), Offset: 0, |
73 | SM: SourceMgr, LangOpts: Context.getLangOpts()); |
74 | AddedAliases[Function][Namespace.str()] = Abbreviation; |
75 | return FixItHint::CreateInsertion(InsertionLoc: Loc, Code: Declaration); |
76 | } |
77 | |
78 | return std::nullopt; |
79 | } |
80 | |
81 | 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(Val: Function); |
86 | if (FunctionAliases != AddedAliases.end()) { |
87 | if (FunctionAliases->second.count(Key: Namespace) != 0) { |
88 | return FunctionAliases->second.find(Key: Namespace)->getValue(); |
89 | } |
90 | } |
91 | return Namespace.str(); |
92 | } |
93 | |
94 | } // namespace clang::tidy::utils |
95 | |