Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 1 | //===-- ClangMove.cpp - Implement ClangMove functationalities ---*- 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 | #include "ClangMove.h" |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 11 | #include "HelperDeclRefGraph.h" |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
| 14 | #include "clang/Format/Format.h" |
| 15 | #include "clang/Frontend/CompilerInstance.h" |
| 16 | #include "clang/Lex/Lexer.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Rewrite/Core/Rewriter.h" |
| 19 | #include "clang/Tooling/Core/Replacement.h" |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 22 | |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "clang-move" |
| 24 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 25 | using namespace clang::ast_matchers; |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace move { |
| 29 | namespace { |
| 30 | |
Haojian Wu | 7bd492c | 2016-10-14 10:07:58 +0000 | [diff] [blame] | 31 | // FIXME: Move to ASTMatchers. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 32 | AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); } |
Haojian Wu | 7bd492c | 2016-10-14 10:07:58 +0000 | [diff] [blame] | 33 | |
Haojian Wu | b3d9888 | 2017-01-17 10:08:11 +0000 | [diff] [blame] | 34 | AST_MATCHER(NamedDecl, notInMacro) { return !Node.getLocation().isMacroID(); } |
| 35 | |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 36 | AST_MATCHER_P(Decl, hasOutermostEnclosingClass, |
| 37 | ast_matchers::internal::Matcher<Decl>, InnerMatcher) { |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 38 | const auto *Context = Node.getDeclContext(); |
| 39 | if (!Context) |
| 40 | return false; |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 41 | while (const auto *NextContext = Context->getParent()) { |
| 42 | if (isa<NamespaceDecl>(NextContext) || |
| 43 | isa<TranslationUnitDecl>(NextContext)) |
| 44 | break; |
| 45 | Context = NextContext; |
| 46 | } |
| 47 | return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder, |
| 48 | Builder); |
| 49 | } |
| 50 | |
| 51 | AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass, |
| 52 | ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) { |
| 53 | const CXXRecordDecl *Parent = Node.getParent(); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 54 | if (!Parent) |
| 55 | return false; |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 56 | while (const auto *NextParent = |
| 57 | dyn_cast<CXXRecordDecl>(Parent->getParent())) { |
| 58 | Parent = NextParent; |
| 59 | } |
| 60 | |
| 61 | return InnerMatcher.matches(*Parent, Finder, Builder); |
| 62 | } |
| 63 | |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 64 | std::string CleanPath(StringRef PathRef) { |
| 65 | llvm::SmallString<128> Path(PathRef); |
| 66 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
Eric Liu | 07d4730 | 2018-05-17 14:59:15 +0000 | [diff] [blame] | 67 | // FIXME: figure out why this is necessary. |
| 68 | llvm::sys::path::native(Path); |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 69 | return Path.str(); |
| 70 | } |
| 71 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 72 | // Make the Path absolute using the CurrentDir if the Path is not an absolute |
| 73 | // path. An empty Path will result in an empty string. |
| 74 | std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) { |
| 75 | if (Path.empty()) |
| 76 | return ""; |
| 77 | llvm::SmallString<128> InitialDirectory(CurrentDir); |
| 78 | llvm::SmallString<128> AbsolutePath(Path); |
| 79 | if (std::error_code EC = |
| 80 | llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath)) |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 81 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 82 | << '\n'; |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 83 | return CleanPath(std::move(AbsolutePath)); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Make the Path absolute using the current working directory of the given |
| 87 | // SourceManager if the Path is not an absolute path. |
| 88 | // |
| 89 | // The Path can be a path relative to the build directory, or retrieved from |
| 90 | // the SourceManager. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 91 | std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) { |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 92 | llvm::SmallString<128> AbsolutePath(Path); |
| 93 | if (std::error_code EC = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 94 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute( |
| 95 | AbsolutePath)) |
| 96 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 97 | << '\n'; |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 98 | // Handle symbolic link path cases. |
| 99 | // We are trying to get the real file path of the symlink. |
| 100 | const DirectoryEntry *Dir = SM.getFileManager().getDirectory( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 101 | llvm::sys::path::parent_path(AbsolutePath.str())); |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 102 | if (Dir) { |
| 103 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); |
Eric Liu | ad3fed6 | 2018-05-16 20:10:10 +0000 | [diff] [blame] | 104 | // FIXME: getCanonicalName might fail to get real path on VFS. |
| 105 | if (llvm::sys::path::is_absolute(DirName)) { |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 106 | SmallString<128> AbsoluteFilename; |
Eric Liu | ad3fed6 | 2018-05-16 20:10:10 +0000 | [diff] [blame] | 107 | llvm::sys::path::append(AbsoluteFilename, DirName, |
| 108 | llvm::sys::path::filename(AbsolutePath.str())); |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 109 | return CleanPath(AbsoluteFilename); |
Eric Liu | ad3fed6 | 2018-05-16 20:10:10 +0000 | [diff] [blame] | 110 | } |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 111 | } |
Eric Liu | 5e72137 | 2018-05-17 12:40:50 +0000 | [diff] [blame] | 112 | return CleanPath(AbsolutePath); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Matches AST nodes that are expanded within the given AbsoluteFilePath. |
| 116 | AST_POLYMORPHIC_MATCHER_P(isExpansionInFile, |
| 117 | AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc), |
| 118 | std::string, AbsoluteFilePath) { |
| 119 | auto &SourceManager = Finder->getASTContext().getSourceManager(); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 120 | auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc()); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 121 | if (ExpansionLoc.isInvalid()) |
| 122 | return false; |
| 123 | auto FileEntry = |
| 124 | SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc)); |
| 125 | if (!FileEntry) |
| 126 | return false; |
| 127 | return MakeAbsolutePath(SourceManager, FileEntry->getName()) == |
| 128 | AbsoluteFilePath; |
| 129 | } |
| 130 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 131 | class FindAllIncludes : public PPCallbacks { |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 132 | public: |
| 133 | explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool) |
| 134 | : SM(*SM), MoveTool(MoveTool) {} |
| 135 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 136 | void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 137 | StringRef FileName, bool IsAngled, |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 138 | CharSourceRange FilenameRange, |
| 139 | const FileEntry * /*File*/, StringRef SearchPath, |
| 140 | StringRef /*RelativePath*/, |
| 141 | const Module * /*Imported*/, |
Julie Hockett | 546943f | 2018-05-10 19:13:14 +0000 | [diff] [blame] | 142 | SrcMgr::CharacteristicKind /*FileType*/) override { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 143 | if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc))) |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 144 | MoveTool->addIncludes(FileName, IsAngled, SearchPath, |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 145 | FileEntry->getName(), FilenameRange, SM); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private: |
| 149 | const SourceManager &SM; |
| 150 | ClangMoveTool *const MoveTool; |
| 151 | }; |
| 152 | |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 153 | /// Add a declatration being moved to new.h/cc. Note that the declaration will |
| 154 | /// also be deleted in old.h/cc. |
| 155 | void MoveDeclFromOldFileToNewFile(ClangMoveTool *MoveTool, const NamedDecl *D) { |
| 156 | MoveTool->getMovedDecls().push_back(D); |
| 157 | MoveTool->addRemovedDecl(D); |
| 158 | MoveTool->getUnremovedDeclsInOldHeader().erase(D); |
| 159 | } |
| 160 | |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 161 | class FunctionDeclarationMatch : public MatchFinder::MatchCallback { |
| 162 | public: |
| 163 | explicit FunctionDeclarationMatch(ClangMoveTool *MoveTool) |
| 164 | : MoveTool(MoveTool) {} |
| 165 | |
| 166 | void run(const MatchFinder::MatchResult &Result) override { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 167 | const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("function"); |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 168 | assert(FD); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 169 | const NamedDecl *D = FD; |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 170 | if (const auto *FTD = FD->getDescribedFunctionTemplate()) |
| 171 | D = FTD; |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 172 | MoveDeclFromOldFileToNewFile(MoveTool, D); |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | ClangMoveTool *MoveTool; |
| 177 | }; |
| 178 | |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 179 | class VarDeclarationMatch : public MatchFinder::MatchCallback { |
| 180 | public: |
| 181 | explicit VarDeclarationMatch(ClangMoveTool *MoveTool) |
| 182 | : MoveTool(MoveTool) {} |
| 183 | |
| 184 | void run(const MatchFinder::MatchResult &Result) override { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 185 | const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var"); |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 186 | assert(VD); |
| 187 | MoveDeclFromOldFileToNewFile(MoveTool, VD); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | ClangMoveTool *MoveTool; |
| 192 | }; |
| 193 | |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 194 | class TypeAliasMatch : public MatchFinder::MatchCallback { |
| 195 | public: |
| 196 | explicit TypeAliasMatch(ClangMoveTool *MoveTool) |
| 197 | : MoveTool(MoveTool) {} |
| 198 | |
| 199 | void run(const MatchFinder::MatchResult &Result) override { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 200 | if (const auto *TD = Result.Nodes.getNodeAs<TypedefDecl>("typedef")) |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 201 | MoveDeclFromOldFileToNewFile(MoveTool, TD); |
| 202 | else if (const auto *TAD = |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 203 | Result.Nodes.getNodeAs<TypeAliasDecl>("type_alias")) { |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 204 | const NamedDecl * D = TAD; |
| 205 | if (const auto * TD = TAD->getDescribedAliasTemplate()) |
| 206 | D = TD; |
| 207 | MoveDeclFromOldFileToNewFile(MoveTool, D); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private: |
| 212 | ClangMoveTool *MoveTool; |
| 213 | }; |
| 214 | |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 215 | class EnumDeclarationMatch : public MatchFinder::MatchCallback { |
| 216 | public: |
| 217 | explicit EnumDeclarationMatch(ClangMoveTool *MoveTool) |
| 218 | : MoveTool(MoveTool) {} |
| 219 | |
| 220 | void run(const MatchFinder::MatchResult &Result) override { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 221 | const auto *ED = Result.Nodes.getNodeAs<EnumDecl>("enum"); |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 222 | assert(ED); |
| 223 | MoveDeclFromOldFileToNewFile(MoveTool, ED); |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private: |
| 227 | ClangMoveTool *MoveTool; |
| 228 | }; |
| 229 | |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 230 | class ClassDeclarationMatch : public MatchFinder::MatchCallback { |
| 231 | public: |
| 232 | explicit ClassDeclarationMatch(ClangMoveTool *MoveTool) |
| 233 | : MoveTool(MoveTool) {} |
| 234 | void run(const MatchFinder::MatchResult &Result) override { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 235 | SourceManager *SM = &Result.Context->getSourceManager(); |
| 236 | if (const auto *CMD = Result.Nodes.getNodeAs<CXXMethodDecl>("class_method")) |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 237 | MatchClassMethod(CMD, SM); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 238 | else if (const auto *VD = |
| 239 | Result.Nodes.getNodeAs<VarDecl>("class_static_var_decl")) |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 240 | MatchClassStaticVariable(VD, SM); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 241 | else if (const auto *CD = |
| 242 | Result.Nodes.getNodeAs<CXXRecordDecl>("moved_class")) |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 243 | MatchClassDeclaration(CD, SM); |
| 244 | } |
| 245 | |
| 246 | private: |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 247 | void MatchClassMethod(const CXXMethodDecl *CMD, SourceManager *SM) { |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 248 | // Skip inline class methods. isInline() ast matcher doesn't ignore this |
| 249 | // case. |
| 250 | if (!CMD->isInlined()) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 251 | MoveTool->getMovedDecls().push_back(CMD); |
| 252 | MoveTool->addRemovedDecl(CMD); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 253 | // Get template class method from its method declaration as |
| 254 | // UnremovedDecls stores template class method. |
| 255 | if (const auto *FTD = CMD->getDescribedFunctionTemplate()) |
| 256 | MoveTool->getUnremovedDeclsInOldHeader().erase(FTD); |
| 257 | else |
| 258 | MoveTool->getUnremovedDeclsInOldHeader().erase(CMD); |
| 259 | } |
| 260 | } |
| 261 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 262 | void MatchClassStaticVariable(const NamedDecl *VD, SourceManager *SM) { |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 263 | MoveDeclFromOldFileToNewFile(MoveTool, VD); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 266 | void MatchClassDeclaration(const CXXRecordDecl *CD, SourceManager *SM) { |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 267 | // Get class template from its class declaration as UnremovedDecls stores |
| 268 | // class template. |
| 269 | if (const auto *TC = CD->getDescribedClassTemplate()) |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 270 | MoveTool->getMovedDecls().push_back(TC); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 271 | else |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 272 | MoveTool->getMovedDecls().push_back(CD); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 273 | MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back()); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 274 | MoveTool->getUnremovedDeclsInOldHeader().erase( |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 275 | MoveTool->getMovedDecls().back()); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | ClangMoveTool *MoveTool; |
| 279 | }; |
| 280 | |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 281 | // Expand to get the end location of the line where the EndLoc of the given |
| 282 | // Decl. |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 283 | SourceLocation getLocForEndOfDecl(const Decl *D, |
| 284 | const LangOptions &LangOpts = LangOptions()) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 285 | const auto &SM = D->getASTContext().getSourceManager(); |
Richard Smith | 4bb15ab | 2018-04-30 05:26:07 +0000 | [diff] [blame] | 286 | // If the expansion range is a character range, this is the location of |
| 287 | // the first character past the end. Otherwise it's the location of the |
| 288 | // first character in the final token in the range. |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 289 | auto EndExpansionLoc = SM.getExpansionRange(D->getEndLoc()).getEnd(); |
Haojian Wu | dc4edba | 2016-12-13 15:35:47 +0000 | [diff] [blame] | 290 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(EndExpansionLoc); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 291 | // Try to load the file buffer. |
| 292 | bool InvalidTemp = false; |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 293 | llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 294 | if (InvalidTemp) |
| 295 | return SourceLocation(); |
| 296 | |
| 297 | const char *TokBegin = File.data() + LocInfo.second; |
| 298 | // Lex from the start of the given location. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 299 | Lexer Lex(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 300 | TokBegin, File.end()); |
| 301 | |
| 302 | llvm::SmallVector<char, 16> Line; |
| 303 | // FIXME: this is a bit hacky to get ReadToEndOfLine work. |
| 304 | Lex.setParsingPreprocessorDirective(true); |
| 305 | Lex.ReadToEndOfLine(&Line); |
Haojian Wu | dc4edba | 2016-12-13 15:35:47 +0000 | [diff] [blame] | 306 | SourceLocation EndLoc = EndExpansionLoc.getLocWithOffset(Line.size()); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 307 | // If we already reach EOF, just return the EOF SourceLocation; |
| 308 | // otherwise, move 1 offset ahead to include the trailing newline character |
| 309 | // '\n'. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 310 | return SM.getLocForEndOfFile(LocInfo.first) == EndLoc |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 311 | ? EndLoc |
| 312 | : EndLoc.getLocWithOffset(1); |
| 313 | } |
| 314 | |
| 315 | // Get full range of a Decl including the comments associated with it. |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 316 | CharSourceRange getFullRange(const Decl *D, |
| 317 | const LangOptions &options = LangOptions()) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 318 | const auto &SM = D->getASTContext().getSourceManager(); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 319 | SourceRange Full(SM.getExpansionLoc(D->getBeginLoc()), getLocForEndOfDecl(D)); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 320 | // Expand to comments that are associated with the Decl. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 321 | if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) { |
Stephen Kelly | c09197e | 2018-08-09 22:43:02 +0000 | [diff] [blame] | 322 | if (SM.isBeforeInTranslationUnit(Full.getEnd(), Comment->getEndLoc())) |
| 323 | Full.setEnd(Comment->getEndLoc()); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 324 | // FIXME: Don't delete a preceding comment, if there are no other entities |
| 325 | // it could refer to. |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 326 | if (SM.isBeforeInTranslationUnit(Comment->getBeginLoc(), Full.getBegin())) |
| 327 | Full.setBegin(Comment->getBeginLoc()); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 330 | return CharSourceRange::getCharRange(Full); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 333 | std::string getDeclarationSourceText(const Decl *D) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 334 | const auto &SM = D->getASTContext().getSourceManager(); |
| 335 | llvm::StringRef SourceText = |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 336 | Lexer::getSourceText(getFullRange(D), SM, LangOptions()); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 337 | return SourceText.str(); |
| 338 | } |
| 339 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 340 | bool isInHeaderFile(const Decl *D, llvm::StringRef OriginalRunningDirectory, |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 341 | llvm::StringRef OldHeader) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 342 | const auto &SM = D->getASTContext().getSourceManager(); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 343 | if (OldHeader.empty()) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 344 | return false; |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 345 | auto ExpansionLoc = SM.getExpansionLoc(D->getBeginLoc()); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 346 | if (ExpansionLoc.isInvalid()) |
| 347 | return false; |
| 348 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 349 | if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) { |
| 350 | return MakeAbsolutePath(SM, FE->getName()) == |
| 351 | MakeAbsolutePath(OriginalRunningDirectory, OldHeader); |
| 352 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 353 | |
| 354 | return false; |
| 355 | } |
| 356 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 357 | std::vector<std::string> getNamespaces(const Decl *D) { |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 358 | std::vector<std::string> Namespaces; |
| 359 | for (const auto *Context = D->getDeclContext(); Context; |
| 360 | Context = Context->getParent()) { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 361 | if (llvm::isa<TranslationUnitDecl>(Context) || |
| 362 | llvm::isa<LinkageSpecDecl>(Context)) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 363 | break; |
| 364 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 365 | if (const auto *ND = llvm::dyn_cast<NamespaceDecl>(Context)) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 366 | Namespaces.push_back(ND->getName().str()); |
| 367 | } |
| 368 | std::reverse(Namespaces.begin(), Namespaces.end()); |
| 369 | return Namespaces; |
| 370 | } |
| 371 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 372 | tooling::Replacements |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 373 | createInsertedReplacements(const std::vector<std::string> &Includes, |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 374 | const std::vector<const NamedDecl *> &Decls, |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 375 | llvm::StringRef FileName, bool IsHeader = false, |
| 376 | StringRef OldHeaderInclude = "") { |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 377 | std::string NewCode; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 378 | std::string GuardName(FileName); |
| 379 | if (IsHeader) { |
Haojian Wu | ac97fc3 | 2016-10-17 15:26:34 +0000 | [diff] [blame] | 380 | for (size_t i = 0; i < GuardName.size(); ++i) { |
| 381 | if (!isAlphanumeric(GuardName[i])) |
| 382 | GuardName[i] = '_'; |
| 383 | } |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 384 | GuardName = StringRef(GuardName).upper(); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 385 | NewCode += "#ifndef " + GuardName + "\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 386 | NewCode += "#define " + GuardName + "\n\n"; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 387 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 388 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 389 | NewCode += OldHeaderInclude; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 390 | // Add #Includes. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 391 | for (const auto &Include : Includes) |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 392 | NewCode += Include; |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 393 | |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 394 | if (!Includes.empty()) |
| 395 | NewCode += "\n"; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 396 | |
| 397 | // Add moved class definition and its related declarations. All declarations |
| 398 | // in same namespace are grouped together. |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 399 | // |
| 400 | // Record namespaces where the current position is in. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 401 | std::vector<std::string> CurrentNamespaces; |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 402 | for (const auto *MovedDecl : Decls) { |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 403 | // The namespaces of the declaration being moved. |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 404 | std::vector<std::string> DeclNamespaces = getNamespaces(MovedDecl); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 405 | auto CurrentIt = CurrentNamespaces.begin(); |
| 406 | auto DeclIt = DeclNamespaces.begin(); |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 407 | // Skip the common prefix. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 408 | while (CurrentIt != CurrentNamespaces.end() && |
| 409 | DeclIt != DeclNamespaces.end()) { |
| 410 | if (*CurrentIt != *DeclIt) |
| 411 | break; |
| 412 | ++CurrentIt; |
| 413 | ++DeclIt; |
| 414 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 415 | // Calculate the new namespaces after adding MovedDecl in CurrentNamespace, |
| 416 | // which is used for next iteration of this loop. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 417 | std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(), |
| 418 | CurrentIt); |
| 419 | NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end()); |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 420 | |
| 421 | |
| 422 | // End with CurrentNamespace. |
| 423 | bool HasEndCurrentNamespace = false; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 424 | auto RemainingSize = CurrentNamespaces.end() - CurrentIt; |
| 425 | for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0; |
| 426 | --RemainingSize, ++It) { |
| 427 | assert(It < CurrentNamespaces.rend()); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 428 | NewCode += "} // namespace " + *It + "\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 429 | HasEndCurrentNamespace = true; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 430 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 431 | // Add trailing '\n' after the nested namespace definition. |
| 432 | if (HasEndCurrentNamespace) |
| 433 | NewCode += "\n"; |
| 434 | |
| 435 | // If the moved declaration is not in CurrentNamespace, add extra namespace |
| 436 | // definitions. |
| 437 | bool IsInNewNamespace = false; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 438 | while (DeclIt != DeclNamespaces.end()) { |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 439 | NewCode += "namespace " + *DeclIt + " {\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 440 | IsInNewNamespace = true; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 441 | ++DeclIt; |
| 442 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 443 | // If the moved declaration is in same namespace CurrentNamespace, add |
| 444 | // a preceeding `\n' before the moved declaration. |
Haojian Wu | 50a45d9 | 2016-11-18 10:51:16 +0000 | [diff] [blame] | 445 | // FIXME: Don't add empty lines between using declarations. |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 446 | if (!IsInNewNamespace) |
| 447 | NewCode += "\n"; |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 448 | NewCode += getDeclarationSourceText(MovedDecl); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 449 | CurrentNamespaces = std::move(NextNamespaces); |
| 450 | } |
| 451 | std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end()); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 452 | for (const auto &NS : CurrentNamespaces) |
| 453 | NewCode += "} // namespace " + NS + "\n"; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 454 | |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 455 | if (IsHeader) |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 456 | NewCode += "\n#endif // " + GuardName + "\n"; |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 457 | return tooling::Replacements(tooling::Replacement(FileName, 0, 0, NewCode)); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 460 | // Return a set of all decls which are used/referenced by the given Decls. |
| 461 | // Specically, given a class member declaration, this method will return all |
| 462 | // decls which are used by the whole class. |
| 463 | llvm::DenseSet<const Decl *> |
| 464 | getUsedDecls(const HelperDeclRefGraph *RG, |
| 465 | const std::vector<const NamedDecl *> &Decls) { |
| 466 | assert(RG); |
| 467 | llvm::DenseSet<const CallGraphNode *> Nodes; |
| 468 | for (const auto *D : Decls) { |
| 469 | auto Result = RG->getReachableNodes( |
| 470 | HelperDeclRGBuilder::getOutmostClassOrFunDecl(D)); |
| 471 | Nodes.insert(Result.begin(), Result.end()); |
| 472 | } |
| 473 | llvm::DenseSet<const Decl *> Results; |
| 474 | for (const auto *Node : Nodes) |
| 475 | Results.insert(Node->getDecl()); |
| 476 | return Results; |
| 477 | } |
| 478 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 479 | } // namespace |
| 480 | |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 481 | std::unique_ptr<ASTConsumer> |
| 482 | ClangMoveAction::CreateASTConsumer(CompilerInstance &Compiler, |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 483 | StringRef /*InFile*/) { |
| 484 | Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>( |
| 485 | &Compiler.getSourceManager(), &MoveTool)); |
| 486 | return MatchFinder.newASTConsumer(); |
| 487 | } |
| 488 | |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 489 | ClangMoveTool::ClangMoveTool(ClangMoveContext *const Context, |
| 490 | DeclarationReporter *const Reporter) |
| 491 | : Context(Context), Reporter(Reporter) { |
| 492 | if (!Context->Spec.NewHeader.empty()) |
| 493 | CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n"); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 496 | void ClangMoveTool::addRemovedDecl(const NamedDecl *Decl) { |
| 497 | const auto &SM = Decl->getASTContext().getSourceManager(); |
| 498 | auto Loc = Decl->getLocation(); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 499 | StringRef FilePath = SM.getFilename(Loc); |
| 500 | FilePathToFileID[FilePath] = SM.getFileID(Loc); |
| 501 | RemovedDecls.push_back(Decl); |
| 502 | } |
| 503 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 504 | void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 505 | auto InOldHeader = |
| 506 | isExpansionInFile(makeAbsolutePath(Context->Spec.OldHeader)); |
| 507 | auto InOldCC = isExpansionInFile(makeAbsolutePath(Context->Spec.OldCC)); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 508 | auto InOldFiles = anyOf(InOldHeader, InOldCC); |
Haojian Wu | 03c8963 | 2017-05-02 12:15:11 +0000 | [diff] [blame] | 509 | auto classTemplateForwardDecls = |
| 510 | classTemplateDecl(unless(has(cxxRecordDecl(isDefinition())))); |
| 511 | auto ForwardClassDecls = namedDecl( |
| 512 | anyOf(cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition()))), |
| 513 | classTemplateForwardDecls)); |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 514 | auto TopLevelDecl = |
| 515 | hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 516 | |
| 517 | //============================================================================ |
| 518 | // Matchers for old header |
| 519 | //============================================================================ |
| 520 | // Match all top-level named declarations (e.g. function, variable, enum) in |
| 521 | // old header, exclude forward class declarations and namespace declarations. |
| 522 | // |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 523 | // We consider declarations inside a class belongs to the class. So these |
| 524 | // declarations will be ignored. |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 525 | auto AllDeclsInHeader = namedDecl( |
Haojian Wu | 03c8963 | 2017-05-02 12:15:11 +0000 | [diff] [blame] | 526 | unless(ForwardClassDecls), unless(namespaceDecl()), |
| 527 | unless(usingDirectiveDecl()), // using namespace decl. |
Haojian Wu | d478634 | 2018-02-09 15:57:30 +0000 | [diff] [blame] | 528 | notInMacro(), |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 529 | InOldHeader, |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 530 | hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), |
| 531 | hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl())))); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 532 | Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 533 | |
| 534 | // Don't register other matchers when dumping all declarations in header. |
| 535 | if (Context->DumpDeclarations) |
| 536 | return; |
| 537 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 538 | // Match forward declarations in old header. |
Haojian Wu | 03c8963 | 2017-05-02 12:15:11 +0000 | [diff] [blame] | 539 | Finder->addMatcher(namedDecl(ForwardClassDecls, InOldHeader).bind("fwd_decl"), |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 540 | this); |
| 541 | |
| 542 | //============================================================================ |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 543 | // Matchers for old cc |
| 544 | //============================================================================ |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 545 | auto IsOldCCTopLevelDecl = allOf( |
| 546 | hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), InOldCC); |
| 547 | // Matching using decls/type alias decls which are in named/anonymous/global |
| 548 | // namespace, these decls are always copied to new.h/cc. Those in classes, |
| 549 | // functions are covered in other matchers. |
Haojian Wu | b3d9888 | 2017-01-17 10:08:11 +0000 | [diff] [blame] | 550 | Finder->addMatcher(namedDecl(anyOf(usingDecl(IsOldCCTopLevelDecl), |
Argyrios Kyrtzidis | 2b6ec65 | 2018-08-31 03:51:33 +0000 | [diff] [blame] | 551 | usingDirectiveDecl(unless(isImplicit()), |
| 552 | IsOldCCTopLevelDecl), |
Haojian Wu | b3d9888 | 2017-01-17 10:08:11 +0000 | [diff] [blame] | 553 | typeAliasDecl(IsOldCCTopLevelDecl)), |
| 554 | notInMacro()) |
| 555 | .bind("using_decl"), |
| 556 | this); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 557 | |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 558 | // Match static functions/variable definitions which are defined in named |
| 559 | // namespaces. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 560 | Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames; |
| 561 | for (StringRef SymbolName : Context->Spec.Names) { |
| 562 | llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':'); |
| 563 | const auto HasName = hasName(("::" + GlobalSymbolName).str()); |
| 564 | HasAnySymbolNames = |
| 565 | HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName; |
| 566 | } |
| 567 | |
| 568 | if (!HasAnySymbolNames) { |
| 569 | llvm::errs() << "No symbols being moved.\n"; |
| 570 | return; |
| 571 | } |
| 572 | auto InMovedClass = |
| 573 | hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames)); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 574 | |
| 575 | // Matchers for helper declarations in old.cc. |
| 576 | auto InAnonymousNS = hasParent(namespaceDecl(isAnonymous())); |
Haojian Wu | 4775ce5 | 2017-01-17 13:22:37 +0000 | [diff] [blame] | 577 | auto NotInMovedClass= allOf(unless(InMovedClass), InOldCC); |
| 578 | auto IsOldCCHelper = |
| 579 | allOf(NotInMovedClass, anyOf(isStaticStorageClass(), InAnonymousNS)); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 580 | // Match helper classes separately with helper functions/variables since we |
| 581 | // want to reuse these matchers in finding helpers usage below. |
Haojian Wu | 4775ce5 | 2017-01-17 13:22:37 +0000 | [diff] [blame] | 582 | // |
| 583 | // There could be forward declarations usage for helpers, especially for |
| 584 | // classes and functions. We need include these forward declarations. |
| 585 | // |
| 586 | // Forward declarations for variable helpers will be excluded as these |
| 587 | // declarations (with "extern") are not supposed in cpp file. |
| 588 | auto HelperFuncOrVar = |
| 589 | namedDecl(notInMacro(), anyOf(functionDecl(IsOldCCHelper), |
| 590 | varDecl(isDefinition(), IsOldCCHelper))); |
Haojian Wu | b3d9888 | 2017-01-17 10:08:11 +0000 | [diff] [blame] | 591 | auto HelperClasses = |
Haojian Wu | 4775ce5 | 2017-01-17 13:22:37 +0000 | [diff] [blame] | 592 | cxxRecordDecl(notInMacro(), NotInMovedClass, InAnonymousNS); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 593 | // Save all helper declarations in old.cc. |
| 594 | Finder->addMatcher( |
| 595 | namedDecl(anyOf(HelperFuncOrVar, HelperClasses)).bind("helper_decls"), |
| 596 | this); |
| 597 | |
| 598 | // Construct an AST-based call graph of helper declarations in old.cc. |
| 599 | // In the following matcheres, "dc" is a caller while "helper_decls" and |
| 600 | // "used_class" is a callee, so a new edge starting from caller to callee will |
| 601 | // be add in the graph. |
| 602 | // |
| 603 | // Find helper function/variable usages. |
| 604 | Finder->addMatcher( |
| 605 | declRefExpr(to(HelperFuncOrVar), hasAncestor(decl().bind("dc"))) |
| 606 | .bind("func_ref"), |
| 607 | &RGBuilder); |
| 608 | // Find helper class usages. |
| 609 | Finder->addMatcher( |
| 610 | typeLoc(loc(recordType(hasDeclaration(HelperClasses.bind("used_class")))), |
| 611 | hasAncestor(decl().bind("dc"))), |
| 612 | &RGBuilder); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 613 | |
| 614 | //============================================================================ |
| 615 | // Matchers for old files, including old.h/old.cc |
| 616 | //============================================================================ |
| 617 | // Create a MatchCallback for class declarations. |
| 618 | MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this)); |
| 619 | // Match moved class declarations. |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 620 | auto MovedClass = cxxRecordDecl(InOldFiles, *HasAnySymbolNames, |
| 621 | isDefinition(), TopLevelDecl) |
| 622 | .bind("moved_class"); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 623 | Finder->addMatcher(MovedClass, MatchCallbacks.back().get()); |
| 624 | // Match moved class methods (static methods included) which are defined |
| 625 | // outside moved class declaration. |
| 626 | Finder->addMatcher( |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 627 | cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames), |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 628 | isDefinition()) |
| 629 | .bind("class_method"), |
| 630 | MatchCallbacks.back().get()); |
| 631 | // Match static member variable definition of the moved class. |
| 632 | Finder->addMatcher( |
| 633 | varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember()) |
| 634 | .bind("class_static_var_decl"), |
| 635 | MatchCallbacks.back().get()); |
| 636 | |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 637 | MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this)); |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 638 | Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl) |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 639 | .bind("function"), |
| 640 | MatchCallbacks.back().get()); |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 641 | |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 642 | MatchCallbacks.push_back(llvm::make_unique<VarDeclarationMatch>(this)); |
| 643 | Finder->addMatcher( |
| 644 | varDecl(InOldFiles, *HasAnySymbolNames, TopLevelDecl).bind("var"), |
| 645 | MatchCallbacks.back().get()); |
| 646 | |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 647 | // Match enum definition in old.h. Enum helpers (which are defined in old.cc) |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 648 | // will not be moved for now no matter whether they are used or not. |
| 649 | MatchCallbacks.push_back(llvm::make_unique<EnumDeclarationMatch>(this)); |
| 650 | Finder->addMatcher( |
| 651 | enumDecl(InOldHeader, *HasAnySymbolNames, isDefinition(), TopLevelDecl) |
| 652 | .bind("enum"), |
| 653 | MatchCallbacks.back().get()); |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 654 | |
| 655 | // Match type alias in old.h, this includes "typedef" and "using" type alias |
| 656 | // declarations. Type alias helpers (which are defined in old.cc) will not be |
| 657 | // moved for now no matter whether they are used or not. |
| 658 | MatchCallbacks.push_back(llvm::make_unique<TypeAliasMatch>(this)); |
| 659 | Finder->addMatcher(namedDecl(anyOf(typedefDecl().bind("typedef"), |
| 660 | typeAliasDecl().bind("type_alias")), |
| 661 | InOldHeader, *HasAnySymbolNames, TopLevelDecl), |
| 662 | MatchCallbacks.back().get()); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 666 | if (const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decls_in_header")) { |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 667 | UnremovedDeclsInOldHeader.insert(D); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 668 | } else if (const auto *FWD = |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 669 | Result.Nodes.getNodeAs<CXXRecordDecl>("fwd_decl")) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 670 | // Skip all forward declarations which appear after moved class declaration. |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 671 | if (RemovedDecls.empty()) { |
Haojian Wu | b53ec46 | 2016-11-10 05:33:26 +0000 | [diff] [blame] | 672 | if (const auto *DCT = FWD->getDescribedClassTemplate()) |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 673 | MovedDecls.push_back(DCT); |
Haojian Wu | b53ec46 | 2016-11-10 05:33:26 +0000 | [diff] [blame] | 674 | else |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 675 | MovedDecls.push_back(FWD); |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 676 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 677 | } else if (const auto *ND = |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 678 | Result.Nodes.getNodeAs<NamedDecl>("helper_decls")) { |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 679 | MovedDecls.push_back(ND); |
| 680 | HelperDeclarations.push_back(ND); |
Nicola Zaghen | 0efd524 | 2018-05-15 16:37:45 +0000 | [diff] [blame] | 681 | LLVM_DEBUG(llvm::dbgs() << "Add helper : " << ND->getNameAsString() << " (" |
| 682 | << ND << ")\n"); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 683 | } else if (const auto *UD = Result.Nodes.getNodeAs<NamedDecl>("using_decl")) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 684 | MovedDecls.push_back(UD); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 688 | std::string ClangMoveTool::makeAbsolutePath(StringRef Path) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 689 | return MakeAbsolutePath(Context->OriginalRunningDirectory, Path); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 692 | void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled, |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 693 | llvm::StringRef SearchPath, |
| 694 | llvm::StringRef FileName, |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 695 | CharSourceRange IncludeFilenameRange, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 696 | const SourceManager &SM) { |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 697 | SmallVector<char, 128> HeaderWithSearchPath; |
| 698 | llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader); |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 699 | std::string AbsoluteIncludeHeader = |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 700 | MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(), |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 701 | HeaderWithSearchPath.size())); |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 702 | std::string IncludeLine = |
| 703 | IsAngled ? ("#include <" + IncludeHeader + ">\n").str() |
| 704 | : ("#include \"" + IncludeHeader + "\"\n").str(); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 705 | |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 706 | std::string AbsoluteOldHeader = makeAbsolutePath(Context->Spec.OldHeader); |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 707 | std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName); |
| 708 | if (AbsoluteOldHeader == AbsoluteCurrentFile) { |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 709 | // Find old.h includes "old.h". |
| 710 | if (AbsoluteOldHeader == AbsoluteIncludeHeader) { |
| 711 | OldHeaderIncludeRangeInHeader = IncludeFilenameRange; |
| 712 | return; |
| 713 | } |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 714 | HeaderIncludes.push_back(IncludeLine); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 715 | } else if (makeAbsolutePath(Context->Spec.OldCC) == AbsoluteCurrentFile) { |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 716 | // Find old.cc includes "old.h". |
| 717 | if (AbsoluteOldHeader == AbsoluteIncludeHeader) { |
| 718 | OldHeaderIncludeRangeInCC = IncludeFilenameRange; |
| 719 | return; |
| 720 | } |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 721 | CCIncludes.push_back(IncludeLine); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 722 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 725 | void ClangMoveTool::removeDeclsInOldFiles() { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 726 | if (RemovedDecls.empty()) return; |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 727 | |
| 728 | // If old_header is not specified (only move declarations from old.cc), remain |
| 729 | // all the helper function declarations in old.cc as UnremovedDeclsInOldHeader |
| 730 | // is empty in this case, there is no way to verify unused/used helpers. |
| 731 | if (!Context->Spec.OldHeader.empty()) { |
| 732 | std::vector<const NamedDecl *> UnremovedDecls; |
| 733 | for (const auto *D : UnremovedDeclsInOldHeader) |
| 734 | UnremovedDecls.push_back(D); |
| 735 | |
| 736 | auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), UnremovedDecls); |
| 737 | |
| 738 | // We remove the helper declarations which are not used in the old.cc after |
| 739 | // moving the given declarations. |
| 740 | for (const auto *D : HelperDeclarations) { |
Nicola Zaghen | 0efd524 | 2018-05-15 16:37:45 +0000 | [diff] [blame] | 741 | LLVM_DEBUG(llvm::dbgs() << "Check helper is used: " |
| 742 | << D->getNameAsString() << " (" << D << ")\n"); |
Haojian Wu | 4775ce5 | 2017-01-17 13:22:37 +0000 | [diff] [blame] | 743 | if (!UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl( |
| 744 | D->getCanonicalDecl()))) { |
Nicola Zaghen | 0efd524 | 2018-05-15 16:37:45 +0000 | [diff] [blame] | 745 | LLVM_DEBUG(llvm::dbgs() << "Helper removed in old.cc: " |
| 746 | << D->getNameAsString() << " (" << D << ")\n"); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 747 | RemovedDecls.push_back(D); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 752 | for (const auto *RemovedDecl : RemovedDecls) { |
| 753 | const auto &SM = RemovedDecl->getASTContext().getSourceManager(); |
| 754 | auto Range = getFullRange(RemovedDecl); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 755 | tooling::Replacement RemoveReplacement( |
| 756 | SM, CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()), |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 757 | ""); |
| 758 | std::string FilePath = RemoveReplacement.getFilePath().str(); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 759 | auto Err = Context->FileToReplacements[FilePath].add(RemoveReplacement); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 760 | if (Err) |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 761 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 762 | } |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 763 | const auto &SM = RemovedDecls[0]->getASTContext().getSourceManager(); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 764 | |
| 765 | // Post process of cleanup around all the replacements. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 766 | for (auto &FileAndReplacements : Context->FileToReplacements) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 767 | StringRef FilePath = FileAndReplacements.first; |
| 768 | // Add #include of new header to old header. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 769 | if (Context->Spec.OldDependOnNew && |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 770 | MakeAbsolutePath(SM, FilePath) == |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 771 | makeAbsolutePath(Context->Spec.OldHeader)) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 772 | // FIXME: Minimize the include path like include-fixer. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 773 | std::string IncludeNewH = |
| 774 | "#include \"" + Context->Spec.NewHeader + "\"\n"; |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 775 | // This replacment for inserting header will be cleaned up at the end. |
| 776 | auto Err = FileAndReplacements.second.add( |
| 777 | tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH)); |
| 778 | if (Err) |
| 779 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 780 | } |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 781 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 782 | auto SI = FilePathToFileID.find(FilePath); |
| 783 | // Ignore replacements for new.h/cc. |
| 784 | if (SI == FilePathToFileID.end()) continue; |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 785 | llvm::StringRef Code = SM.getBufferData(SI->second); |
Eric Liu | fa63e98 | 2018-08-02 10:30:56 +0000 | [diff] [blame] | 786 | auto Style = format::getStyle(format::DefaultFormatStyle, FilePath, |
| 787 | Context->FallbackStyle); |
Antonio Maiorano | 0d7d9c2 | 2017-01-17 00:13:32 +0000 | [diff] [blame] | 788 | if (!Style) { |
| 789 | llvm::errs() << llvm::toString(Style.takeError()) << "\n"; |
| 790 | continue; |
| 791 | } |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 792 | auto CleanReplacements = format::cleanupAroundReplacements( |
Antonio Maiorano | 0d7d9c2 | 2017-01-17 00:13:32 +0000 | [diff] [blame] | 793 | Code, Context->FileToReplacements[FilePath], *Style); |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 794 | |
| 795 | if (!CleanReplacements) { |
| 796 | llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n"; |
| 797 | continue; |
| 798 | } |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 799 | Context->FileToReplacements[FilePath] = *CleanReplacements; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 803 | void ClangMoveTool::moveDeclsToNewFiles() { |
| 804 | std::vector<const NamedDecl *> NewHeaderDecls; |
| 805 | std::vector<const NamedDecl *> NewCCDecls; |
| 806 | for (const auto *MovedDecl : MovedDecls) { |
| 807 | if (isInHeaderFile(MovedDecl, Context->OriginalRunningDirectory, |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 808 | Context->Spec.OldHeader)) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 809 | NewHeaderDecls.push_back(MovedDecl); |
| 810 | else |
| 811 | NewCCDecls.push_back(MovedDecl); |
| 812 | } |
| 813 | |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 814 | auto UsedDecls = getUsedDecls(RGBuilder.getGraph(), RemovedDecls); |
| 815 | std::vector<const NamedDecl *> ActualNewCCDecls; |
| 816 | |
| 817 | // Filter out all unused helpers in NewCCDecls. |
| 818 | // We only move the used helpers (including transively used helpers) and the |
| 819 | // given symbols being moved. |
| 820 | for (const auto *D : NewCCDecls) { |
| 821 | if (llvm::is_contained(HelperDeclarations, D) && |
Haojian Wu | 4775ce5 | 2017-01-17 13:22:37 +0000 | [diff] [blame] | 822 | !UsedDecls.count(HelperDeclRGBuilder::getOutmostClassOrFunDecl( |
| 823 | D->getCanonicalDecl()))) |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 824 | continue; |
| 825 | |
Nicola Zaghen | 0efd524 | 2018-05-15 16:37:45 +0000 | [diff] [blame] | 826 | LLVM_DEBUG(llvm::dbgs() << "Helper used in new.cc: " << D->getNameAsString() |
| 827 | << " " << D << "\n"); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 828 | ActualNewCCDecls.push_back(D); |
| 829 | } |
| 830 | |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 831 | if (!Context->Spec.NewHeader.empty()) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 832 | std::string OldHeaderInclude = |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 833 | Context->Spec.NewDependOnOld |
| 834 | ? "#include \"" + Context->Spec.OldHeader + "\"\n" |
| 835 | : ""; |
| 836 | Context->FileToReplacements[Context->Spec.NewHeader] = |
| 837 | createInsertedReplacements(HeaderIncludes, NewHeaderDecls, |
| 838 | Context->Spec.NewHeader, /*IsHeader=*/true, |
| 839 | OldHeaderInclude); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 840 | } |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 841 | if (!Context->Spec.NewCC.empty()) |
| 842 | Context->FileToReplacements[Context->Spec.NewCC] = |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 843 | createInsertedReplacements(CCIncludes, ActualNewCCDecls, |
| 844 | Context->Spec.NewCC); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 847 | // Move all contents from OldFile to NewFile. |
| 848 | void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile, |
| 849 | StringRef NewFile) { |
| 850 | const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile)); |
| 851 | if (!FE) { |
| 852 | llvm::errs() << "Failed to get file: " << OldFile << "\n"; |
| 853 | return; |
| 854 | } |
| 855 | FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User); |
| 856 | auto Begin = SM.getLocForStartOfFile(ID); |
| 857 | auto End = SM.getLocForEndOfFile(ID); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 858 | tooling::Replacement RemoveAll(SM, CharSourceRange::getCharRange(Begin, End), |
| 859 | ""); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 860 | std::string FilePath = RemoveAll.getFilePath().str(); |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 861 | Context->FileToReplacements[FilePath] = tooling::Replacements(RemoveAll); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 862 | |
| 863 | StringRef Code = SM.getBufferData(ID); |
| 864 | if (!NewFile.empty()) { |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 865 | auto AllCode = |
| 866 | tooling::Replacements(tooling::Replacement(NewFile, 0, 0, Code)); |
| 867 | auto ReplaceOldInclude = [&](CharSourceRange OldHeaderIncludeRange) { |
| 868 | AllCode = AllCode.merge(tooling::Replacements(tooling::Replacement( |
| 869 | SM, OldHeaderIncludeRange, '"' + Context->Spec.NewHeader + '"'))); |
Haojian Wu | fb68ca1 | 2018-01-31 12:12:29 +0000 | [diff] [blame] | 870 | }; |
| 871 | // Fix the case where old.h/old.cc includes "old.h", we replace the |
| 872 | // `#include "old.h"` with `#include "new.h"`. |
| 873 | if (Context->Spec.NewCC == NewFile && OldHeaderIncludeRangeInCC.isValid()) |
| 874 | ReplaceOldInclude(OldHeaderIncludeRangeInCC); |
| 875 | else if (Context->Spec.NewHeader == NewFile && |
| 876 | OldHeaderIncludeRangeInHeader.isValid()) |
| 877 | ReplaceOldInclude(OldHeaderIncludeRangeInHeader); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 878 | Context->FileToReplacements[NewFile] = std::move(AllCode); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 882 | void ClangMoveTool::onEndOfTranslationUnit() { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 883 | if (Context->DumpDeclarations) { |
| 884 | assert(Reporter); |
| 885 | for (const auto *Decl : UnremovedDeclsInOldHeader) { |
| 886 | auto Kind = Decl->getKind(); |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 887 | bool Templated = Decl->isTemplated(); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 888 | const std::string QualifiedName = Decl->getQualifiedNameAsString(); |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 889 | if (Kind == Decl::Kind::Var) |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 890 | Reporter->reportDeclaration(QualifiedName, "Variable", Templated); |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 891 | else if (Kind == Decl::Kind::Function || |
| 892 | Kind == Decl::Kind::FunctionTemplate) |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 893 | Reporter->reportDeclaration(QualifiedName, "Function", Templated); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 894 | else if (Kind == Decl::Kind::ClassTemplate || |
| 895 | Kind == Decl::Kind::CXXRecord) |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 896 | Reporter->reportDeclaration(QualifiedName, "Class", Templated); |
Haojian Wu | 8586772 | 2017-01-16 09:34:07 +0000 | [diff] [blame] | 897 | else if (Kind == Decl::Kind::Enum) |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 898 | Reporter->reportDeclaration(QualifiedName, "Enum", Templated); |
| 899 | else if (Kind == Decl::Kind::Typedef || Kind == Decl::Kind::TypeAlias || |
Haojian Wu | 8586772 | 2017-01-16 09:34:07 +0000 | [diff] [blame] | 900 | Kind == Decl::Kind::TypeAliasTemplate) |
Eric Liu | bce181c | 2018-10-08 17:22:50 +0000 | [diff] [blame] | 901 | Reporter->reportDeclaration(QualifiedName, "TypeAlias", Templated); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 902 | } |
| 903 | return; |
| 904 | } |
| 905 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 906 | if (RemovedDecls.empty()) |
| 907 | return; |
Haojian Wu | d478634 | 2018-02-09 15:57:30 +0000 | [diff] [blame] | 908 | // Ignore symbols that are not supported when checking if there is unremoved |
| 909 | // symbol in old header. This makes sure that we always move old files to new |
| 910 | // files when all symbols produced from dump_decls are moved. |
Fangrui Song | 95b45cb | 2018-10-11 16:09:26 +0000 | [diff] [blame] | 911 | auto IsSupportedKind = [](const NamedDecl *Decl) { |
Eric Liu | 47a42d5 | 2016-12-06 10:12:23 +0000 | [diff] [blame] | 912 | switch (Decl->getKind()) { |
| 913 | case Decl::Kind::Function: |
| 914 | case Decl::Kind::FunctionTemplate: |
| 915 | case Decl::Kind::ClassTemplate: |
| 916 | case Decl::Kind::CXXRecord: |
Haojian Wu | 32a552f | 2017-01-03 14:22:25 +0000 | [diff] [blame] | 917 | case Decl::Kind::Enum: |
Haojian Wu | d69d907 | 2017-01-04 14:50:49 +0000 | [diff] [blame] | 918 | case Decl::Kind::Typedef: |
| 919 | case Decl::Kind::TypeAlias: |
| 920 | case Decl::Kind::TypeAliasTemplate: |
Haojian Wu | 4a92050 | 2017-02-27 13:19:13 +0000 | [diff] [blame] | 921 | case Decl::Kind::Var: |
Eric Liu | 47a42d5 | 2016-12-06 10:12:23 +0000 | [diff] [blame] | 922 | return true; |
| 923 | default: |
| 924 | return false; |
| 925 | } |
| 926 | }; |
| 927 | if (std::none_of(UnremovedDeclsInOldHeader.begin(), |
| 928 | UnremovedDeclsInOldHeader.end(), IsSupportedKind) && |
| 929 | !Context->Spec.OldHeader.empty()) { |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 930 | auto &SM = RemovedDecls[0]->getASTContext().getSourceManager(); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 931 | moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader); |
| 932 | moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
Nicola Zaghen | 0efd524 | 2018-05-15 16:37:45 +0000 | [diff] [blame] | 935 | LLVM_DEBUG(RGBuilder.getGraph()->dump()); |
Haojian Wu | 08e402a | 2016-12-02 12:39:39 +0000 | [diff] [blame] | 936 | moveDeclsToNewFiles(); |
Haojian Wu | 3626516 | 2017-01-03 09:00:51 +0000 | [diff] [blame] | 937 | removeDeclsInOldFiles(); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | } // namespace move |
| 941 | } // namespace clang |