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