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