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