blob: c8fb7a22355f9c91f883090118f5ba5a2051d95f [file] [log] [blame]
Eric Liu495b2112016-09-19 17:40:32 +00001//===-- ChangeNamespace.h -- Change namespace ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CHANGE_NAMESPACE_CHANGENAMESPACE_H
11#define LLVM_CLANG_TOOLS_EXTRA_CHANGE_NAMESPACE_CHANGENAMESPACE_H
12
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Format/Format.h"
15#include "clang/Tooling/Core/Replacement.h"
Eric Liuc265b022016-12-01 17:25:55 +000016#include "llvm/Support/Regex.h"
Eric Liu495b2112016-09-19 17:40:32 +000017#include <string>
18
19namespace clang {
20namespace change_namespace {
21
22// This tool can be used to change the surrounding namespaces of class/function
23// definitions. Classes/functions in the moved namespace will have new
24// namespaces while references to symbols (e.g. types, functions) which are not
25// defined in the changed namespace will be correctly qualified by prepending
26// namespace specifiers before them.
Eric Liu97f87ad2016-12-07 20:08:02 +000027// This will try to add shortest namespace specifiers possible. When a symbol
28// reference needs to be fully-qualified, this adds a "::" prefix to the
29// namespace specifiers unless the new namespace is the global namespace.
Eric Liu495b2112016-09-19 17:40:32 +000030// For classes, only classes that are declared/defined in the given namespace in
31// speficifed files will be moved: forward declarations will remain in the old
32// namespace.
33// For example, changing "a" to "x":
34// Old code:
35// namespace a {
36// class FWD;
37// class A { FWD *fwd; }
38// } // a
39// New code:
40// namespace a {
41// class FWD;
42// } // a
43// namespace x {
Eric Liu97f87ad2016-12-07 20:08:02 +000044// class A { ::a::FWD *fwd; }
Eric Liu495b2112016-09-19 17:40:32 +000045// } // x
46// FIXME: support moving typedef, enums across namespaces.
47class ChangeNamespaceTool : public ast_matchers::MatchFinder::MatchCallback {
48public:
49 // Moves code in the old namespace `OldNs` to the new namespace `NewNs` in
50 // files matching `FilePattern`.
51 ChangeNamespaceTool(
52 llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern,
53 std::map<std::string, tooling::Replacements> *FileToReplacements,
54 llvm::StringRef FallbackStyle = "LLVM");
55
56 void registerMatchers(ast_matchers::MatchFinder *Finder);
57
58 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
59
60 // Moves the changed code in old namespaces but leaves class forward
61 // declarations behind.
62 void onEndOfTranslationUnit() override;
63
64private:
65 void moveOldNamespace(const ast_matchers::MatchFinder::MatchResult &Result,
66 const NamespaceDecl *NsDecl);
67
68 void moveClassForwardDeclaration(
69 const ast_matchers::MatchFinder::MatchResult &Result,
Eric Liu41552d62016-12-07 14:20:52 +000070 const NamedDecl *FwdDecl);
Eric Liu495b2112016-09-19 17:40:32 +000071
72 void replaceQualifiedSymbolInDeclContext(
73 const ast_matchers::MatchFinder::MatchResult &Result,
Eric Liub9bf1b52016-11-08 22:44:17 +000074 const DeclContext *DeclContext, SourceLocation Start, SourceLocation End,
75 const NamedDecl *FromDecl);
Eric Liu495b2112016-09-19 17:40:32 +000076
77 void fixTypeLoc(const ast_matchers::MatchFinder::MatchResult &Result,
78 SourceLocation Start, SourceLocation End, TypeLoc Type);
79
Eric Liu68765a82016-09-21 15:06:12 +000080 void fixUsingShadowDecl(const ast_matchers::MatchFinder::MatchResult &Result,
81 const UsingDecl *UsingDeclaration);
82
Eric Liuda22b3c2016-11-29 14:15:14 +000083 void fixDeclRefExpr(const ast_matchers::MatchFinder::MatchResult &Result,
84 const DeclContext *UseContext, const NamedDecl *From,
85 const DeclRefExpr *Ref);
86
Eric Liu495b2112016-09-19 17:40:32 +000087 // Information about moving an old namespace.
88 struct MoveNamespace {
89 // The start offset of the namespace block being moved in the original
90 // code.
91 unsigned Offset;
92 // The length of the namespace block in the original code.
93 unsigned Length;
94 // The offset at which the new namespace block will be inserted in the
95 // original code.
96 unsigned InsertionOffset;
97 // The file in which the namespace is declared.
Eric Liucc83c662016-09-19 17:58:59 +000098 FileID FID;
99 SourceManager *SourceMgr;
Eric Liu495b2112016-09-19 17:40:32 +0000100 };
101
102 // Information about inserting a class forward declaration.
103 struct InsertForwardDeclaration {
104 // The offset at while the forward declaration will be inserted in the
105 // original code.
106 unsigned InsertionOffset;
107 // The code to be inserted.
108 std::string ForwardDeclText;
109 };
110
111 std::string FallbackStyle;
112 // In match callbacks, this contains replacements for replacing `typeLoc`s in
113 // and deleting forward declarations in the moved namespace blocks.
114 // In `onEndOfTranslationUnit` callback, the previous added replacements are
115 // applied (on the moved namespace blocks), and then changed code in old
116 // namespaces re moved to new namespaces, and previously deleted forward
117 // declarations are inserted back to old namespaces, from which they are
118 // deleted.
119 std::map<std::string, tooling::Replacements> &FileToReplacements;
120 // A fully qualified name of the old namespace without "::" prefix, e.g.
121 // "a::b::c".
122 std::string OldNamespace;
123 // A fully qualified name of the new namespace without "::" prefix, e.g.
124 // "x::y::z".
125 std::string NewNamespace;
126 // The longest suffix in the old namespace that does not overlap the new
127 // namespace.
128 // For example, if `OldNamespace` is "a::b::c" and `NewNamespace` is
129 // "a::x::y", then `DiffOldNamespace` will be "b::c".
130 std::string DiffOldNamespace;
131 // The longest suffix in the new namespace that does not overlap the old
132 // namespace.
133 // For example, if `OldNamespace` is "a::b::c" and `NewNamespace` is
134 // "a::x::y", then `DiffNewNamespace` will be "x::y".
135 std::string DiffNewNamespace;
136 // A regex pattern that matches files to be processed.
137 std::string FilePattern;
Eric Liuc265b022016-12-01 17:25:55 +0000138 llvm::Regex FilePatternRE;
Eric Liu495b2112016-09-19 17:40:32 +0000139 // Information about moved namespaces grouped by file.
140 // Since we are modifying code in old namespaces (e.g. add namespace
141 // spedifiers) as well as moving them, we store information about namespaces
142 // to be moved and only move them after all modifications are finished (i.e.
143 // in `onEndOfTranslationUnit`).
144 std::map<std::string, std::vector<MoveNamespace>> MoveNamespaces;
145 // Information about forward declaration insertions grouped by files.
146 // A class forward declaration is not moved, so it will be deleted from the
147 // moved code block and inserted back into the old namespace. The insertion
148 // will be done after removing the code from the old namespace and before
149 // inserting it to the new namespace.
150 std::map<std::string, std::vector<InsertForwardDeclaration>> InsertFwdDecls;
Eric Liub9bf1b52016-11-08 22:44:17 +0000151 // Records all using declarations, which can be used to shorten namespace
152 // specifiers.
153 llvm::SmallPtrSet<const UsingDecl *, 8> UsingDecls;
154 // Records all using namespace declarations, which can be used to shorten
155 // namespace specifiers.
156 llvm::SmallPtrSet<const UsingDirectiveDecl *, 8> UsingNamespaceDecls;
Eric Liuff51f012016-11-16 16:54:53 +0000157 // TypeLocs of CXXCtorInitializer. Types of CXXCtorInitializers do not need to
158 // be fixed.
159 llvm::SmallVector<TypeLoc, 8> BaseCtorInitializerTypeLocs;
Eric Liue3f35e42016-12-20 14:39:04 +0000160 // Since a DeclRefExpr for a function call can be matched twice (one as
161 // CallExpr and one as DeclRefExpr), we record all DeclRefExpr's that have
162 // been processed so that we don't handle them twice.
163 llvm::SmallPtrSet<const clang::DeclRefExpr*, 16> ProcessedFuncRefs;
Eric Liu495b2112016-09-19 17:40:32 +0000164};
165
166} // namespace change_namespace
167} // namespace clang
168
169#endif // LLVM_CLANG_TOOLS_EXTRA_CHANGE_NAMESPACE_CHANGENAMESPACE_H