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