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" |
| 11 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 12 | #include "clang/Basic/SourceManager.h" |
| 13 | #include "clang/Format/Format.h" |
| 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Lex/Lexer.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Rewrite/Core/Rewriter.h" |
| 18 | #include "clang/Tooling/Core/Replacement.h" |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang::ast_matchers; |
| 22 | |
| 23 | namespace clang { |
| 24 | namespace move { |
| 25 | namespace { |
| 26 | |
Haojian Wu | 7bd492c | 2016-10-14 10:07:58 +0000 | [diff] [blame] | 27 | // FIXME: Move to ASTMatchers. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 28 | AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); } |
Haojian Wu | 7bd492c | 2016-10-14 10:07:58 +0000 | [diff] [blame] | 29 | |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 30 | AST_MATCHER_P(Decl, hasOutermostEnclosingClass, |
| 31 | ast_matchers::internal::Matcher<Decl>, InnerMatcher) { |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 32 | const auto *Context = Node.getDeclContext(); |
| 33 | if (!Context) |
| 34 | return false; |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 35 | while (const auto *NextContext = Context->getParent()) { |
| 36 | if (isa<NamespaceDecl>(NextContext) || |
| 37 | isa<TranslationUnitDecl>(NextContext)) |
| 38 | break; |
| 39 | Context = NextContext; |
| 40 | } |
| 41 | return InnerMatcher.matches(*Decl::castFromDeclContext(Context), Finder, |
| 42 | Builder); |
| 43 | } |
| 44 | |
| 45 | AST_MATCHER_P(CXXMethodDecl, ofOutermostEnclosingClass, |
| 46 | ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) { |
| 47 | const CXXRecordDecl *Parent = Node.getParent(); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 48 | if (!Parent) |
| 49 | return false; |
Haojian Wu | e77bcc7 | 2016-10-13 10:31:00 +0000 | [diff] [blame] | 50 | while (const auto *NextParent = |
| 51 | dyn_cast<CXXRecordDecl>(Parent->getParent())) { |
| 52 | Parent = NextParent; |
| 53 | } |
| 54 | |
| 55 | return InnerMatcher.matches(*Parent, Finder, Builder); |
| 56 | } |
| 57 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 58 | // Make the Path absolute using the CurrentDir if the Path is not an absolute |
| 59 | // path. An empty Path will result in an empty string. |
| 60 | std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) { |
| 61 | if (Path.empty()) |
| 62 | return ""; |
| 63 | llvm::SmallString<128> InitialDirectory(CurrentDir); |
| 64 | llvm::SmallString<128> AbsolutePath(Path); |
| 65 | if (std::error_code EC = |
| 66 | llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath)) |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 67 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 68 | << '\n'; |
| 69 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); |
Haojian Wu | c6f125e | 2016-10-04 09:49:20 +0000 | [diff] [blame] | 70 | llvm::sys::path::native(AbsolutePath); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 71 | return AbsolutePath.str(); |
| 72 | } |
| 73 | |
| 74 | // Make the Path absolute using the current working directory of the given |
| 75 | // SourceManager if the Path is not an absolute path. |
| 76 | // |
| 77 | // The Path can be a path relative to the build directory, or retrieved from |
| 78 | // the SourceManager. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 79 | std::string MakeAbsolutePath(const SourceManager &SM, StringRef Path) { |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 80 | llvm::SmallString<128> AbsolutePath(Path); |
| 81 | if (std::error_code EC = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 82 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute( |
| 83 | AbsolutePath)) |
| 84 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 85 | << '\n'; |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 86 | // Handle symbolic link path cases. |
| 87 | // We are trying to get the real file path of the symlink. |
| 88 | const DirectoryEntry *Dir = SM.getFileManager().getDirectory( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 89 | llvm::sys::path::parent_path(AbsolutePath.str())); |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 90 | if (Dir) { |
| 91 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); |
| 92 | SmallVector<char, 128> AbsoluteFilename; |
| 93 | llvm::sys::path::append(AbsoluteFilename, DirName, |
| 94 | llvm::sys::path::filename(AbsolutePath.str())); |
| 95 | return llvm::StringRef(AbsoluteFilename.data(), AbsoluteFilename.size()) |
| 96 | .str(); |
| 97 | } |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 98 | return AbsolutePath.str(); |
| 99 | } |
| 100 | |
| 101 | // Matches AST nodes that are expanded within the given AbsoluteFilePath. |
| 102 | AST_POLYMORPHIC_MATCHER_P(isExpansionInFile, |
| 103 | AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc), |
| 104 | std::string, AbsoluteFilePath) { |
| 105 | auto &SourceManager = Finder->getASTContext().getSourceManager(); |
| 106 | auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart()); |
| 107 | if (ExpansionLoc.isInvalid()) |
| 108 | return false; |
| 109 | auto FileEntry = |
| 110 | SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc)); |
| 111 | if (!FileEntry) |
| 112 | return false; |
| 113 | return MakeAbsolutePath(SourceManager, FileEntry->getName()) == |
| 114 | AbsoluteFilePath; |
| 115 | } |
| 116 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 117 | class FindAllIncludes : public clang::PPCallbacks { |
| 118 | public: |
| 119 | explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool) |
| 120 | : SM(*SM), MoveTool(MoveTool) {} |
| 121 | |
| 122 | void InclusionDirective(clang::SourceLocation HashLoc, |
| 123 | const clang::Token & /*IncludeTok*/, |
| 124 | StringRef FileName, bool IsAngled, |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 125 | clang::CharSourceRange FilenameRange, |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 126 | const clang::FileEntry * /*File*/, |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 127 | StringRef SearchPath, StringRef /*RelativePath*/, |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 128 | const clang::Module * /*Imported*/) override { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 129 | if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc))) |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 130 | MoveTool->addIncludes(FileName, IsAngled, SearchPath, |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 131 | FileEntry->getName(), FilenameRange, SM); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private: |
| 135 | const SourceManager &SM; |
| 136 | ClangMoveTool *const MoveTool; |
| 137 | }; |
| 138 | |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 139 | class FunctionDeclarationMatch : public MatchFinder::MatchCallback { |
| 140 | public: |
| 141 | explicit FunctionDeclarationMatch(ClangMoveTool *MoveTool) |
| 142 | : MoveTool(MoveTool) {} |
| 143 | |
| 144 | void run(const MatchFinder::MatchResult &Result) override { |
| 145 | const auto *FD = Result.Nodes.getNodeAs<clang::FunctionDecl>("function"); |
| 146 | assert(FD); |
| 147 | const clang::NamedDecl *D = FD; |
| 148 | if (const auto *FTD = FD->getDescribedFunctionTemplate()) |
| 149 | D = FTD; |
| 150 | MoveTool->getMovedDecls().emplace_back(D, |
| 151 | &Result.Context->getSourceManager()); |
| 152 | MoveTool->getUnremovedDeclsInOldHeader().erase(D); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 153 | MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back()); |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | ClangMoveTool *MoveTool; |
| 158 | }; |
| 159 | |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 160 | class ClassDeclarationMatch : public MatchFinder::MatchCallback { |
| 161 | public: |
| 162 | explicit ClassDeclarationMatch(ClangMoveTool *MoveTool) |
| 163 | : MoveTool(MoveTool) {} |
| 164 | void run(const MatchFinder::MatchResult &Result) override { |
| 165 | clang::SourceManager* SM = &Result.Context->getSourceManager(); |
| 166 | if (const auto *CMD = |
| 167 | Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method")) |
| 168 | MatchClassMethod(CMD, SM); |
| 169 | else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>( |
| 170 | "class_static_var_decl")) |
| 171 | MatchClassStaticVariable(VD, SM); |
| 172 | else if (const auto *CD = Result.Nodes.getNodeAs<clang::CXXRecordDecl>( |
| 173 | "moved_class")) |
| 174 | MatchClassDeclaration(CD, SM); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | void MatchClassMethod(const clang::CXXMethodDecl* CMD, |
| 179 | clang::SourceManager* SM) { |
| 180 | // Skip inline class methods. isInline() ast matcher doesn't ignore this |
| 181 | // case. |
| 182 | if (!CMD->isInlined()) { |
| 183 | MoveTool->getMovedDecls().emplace_back(CMD, SM); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 184 | MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back()); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 185 | // Get template class method from its method declaration as |
| 186 | // UnremovedDecls stores template class method. |
| 187 | if (const auto *FTD = CMD->getDescribedFunctionTemplate()) |
| 188 | MoveTool->getUnremovedDeclsInOldHeader().erase(FTD); |
| 189 | else |
| 190 | MoveTool->getUnremovedDeclsInOldHeader().erase(CMD); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void MatchClassStaticVariable(const clang::NamedDecl *VD, |
| 195 | clang::SourceManager* SM) { |
| 196 | MoveTool->getMovedDecls().emplace_back(VD, SM); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 197 | MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back()); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 198 | MoveTool->getUnremovedDeclsInOldHeader().erase(VD); |
| 199 | } |
| 200 | |
| 201 | void MatchClassDeclaration(const clang::CXXRecordDecl *CD, |
| 202 | clang::SourceManager* SM) { |
| 203 | // Get class template from its class declaration as UnremovedDecls stores |
| 204 | // class template. |
| 205 | if (const auto *TC = CD->getDescribedClassTemplate()) |
| 206 | MoveTool->getMovedDecls().emplace_back(TC, SM); |
| 207 | else |
| 208 | MoveTool->getMovedDecls().emplace_back(CD, SM); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 209 | MoveTool->addRemovedDecl(MoveTool->getMovedDecls().back()); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 210 | MoveTool->getUnremovedDeclsInOldHeader().erase( |
| 211 | MoveTool->getMovedDecls().back().Decl); |
| 212 | } |
| 213 | |
| 214 | ClangMoveTool *MoveTool; |
| 215 | }; |
| 216 | |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 217 | // Expand to get the end location of the line where the EndLoc of the given |
| 218 | // Decl. |
| 219 | SourceLocation |
| 220 | getLocForEndOfDecl(const clang::Decl *D, const SourceManager *SM, |
| 221 | const LangOptions &LangOpts = clang::LangOptions()) { |
| 222 | std::pair<FileID, unsigned> LocInfo = SM->getDecomposedLoc(D->getLocEnd()); |
| 223 | // Try to load the file buffer. |
| 224 | bool InvalidTemp = false; |
| 225 | llvm::StringRef File = SM->getBufferData(LocInfo.first, &InvalidTemp); |
| 226 | if (InvalidTemp) |
| 227 | return SourceLocation(); |
| 228 | |
| 229 | const char *TokBegin = File.data() + LocInfo.second; |
| 230 | // Lex from the start of the given location. |
| 231 | Lexer Lex(SM->getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), |
| 232 | TokBegin, File.end()); |
| 233 | |
| 234 | llvm::SmallVector<char, 16> Line; |
| 235 | // FIXME: this is a bit hacky to get ReadToEndOfLine work. |
| 236 | Lex.setParsingPreprocessorDirective(true); |
| 237 | Lex.ReadToEndOfLine(&Line); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 238 | SourceLocation EndLoc = D->getLocEnd().getLocWithOffset(Line.size()); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 239 | // If we already reach EOF, just return the EOF SourceLocation; |
| 240 | // otherwise, move 1 offset ahead to include the trailing newline character |
| 241 | // '\n'. |
| 242 | return SM->getLocForEndOfFile(LocInfo.first) == EndLoc |
| 243 | ? EndLoc |
| 244 | : EndLoc.getLocWithOffset(1); |
| 245 | } |
| 246 | |
| 247 | // Get full range of a Decl including the comments associated with it. |
| 248 | clang::CharSourceRange |
| 249 | GetFullRange(const clang::SourceManager *SM, const clang::Decl *D, |
| 250 | const clang::LangOptions &options = clang::LangOptions()) { |
Haojian Wu | 2467539 | 2016-11-14 14:46:48 +0000 | [diff] [blame] | 251 | clang::SourceRange Full(SM->getExpansionLoc(D->getLocStart()), |
| 252 | getLocForEndOfDecl(D, SM)); |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 253 | // Expand to comments that are associated with the Decl. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 254 | if (const auto *Comment = D->getASTContext().getRawCommentForDeclNoCache(D)) { |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 255 | if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd())) |
| 256 | Full.setEnd(Comment->getLocEnd()); |
| 257 | // FIXME: Don't delete a preceding comment, if there are no other entities |
| 258 | // it could refer to. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 259 | if (SM->isBeforeInTranslationUnit(Comment->getLocStart(), Full.getBegin())) |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 260 | Full.setBegin(Comment->getLocStart()); |
| 261 | } |
| 262 | |
| 263 | return clang::CharSourceRange::getCharRange(Full); |
| 264 | } |
| 265 | |
| 266 | std::string getDeclarationSourceText(const clang::Decl *D, |
| 267 | const clang::SourceManager *SM) { |
| 268 | llvm::StringRef SourceText = clang::Lexer::getSourceText( |
| 269 | GetFullRange(SM, D), *SM, clang::LangOptions()); |
| 270 | return SourceText.str(); |
| 271 | } |
| 272 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 273 | bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D, |
| 274 | llvm::StringRef OriginalRunningDirectory, |
| 275 | llvm::StringRef OldHeader) { |
| 276 | if (OldHeader.empty()) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 277 | return false; |
| 278 | auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart()); |
| 279 | if (ExpansionLoc.isInvalid()) |
| 280 | return false; |
| 281 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 282 | if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) { |
| 283 | return MakeAbsolutePath(SM, FE->getName()) == |
| 284 | MakeAbsolutePath(OriginalRunningDirectory, OldHeader); |
| 285 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | std::vector<std::string> GetNamespaces(const clang::Decl *D) { |
| 291 | std::vector<std::string> Namespaces; |
| 292 | for (const auto *Context = D->getDeclContext(); Context; |
| 293 | Context = Context->getParent()) { |
| 294 | if (llvm::isa<clang::TranslationUnitDecl>(Context) || |
| 295 | llvm::isa<clang::LinkageSpecDecl>(Context)) |
| 296 | break; |
| 297 | |
| 298 | if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context)) |
| 299 | Namespaces.push_back(ND->getName().str()); |
| 300 | } |
| 301 | std::reverse(Namespaces.begin(), Namespaces.end()); |
| 302 | return Namespaces; |
| 303 | } |
| 304 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 305 | clang::tooling::Replacements |
| 306 | createInsertedReplacements(const std::vector<std::string> &Includes, |
| 307 | const std::vector<ClangMoveTool::MovedDecl> &Decls, |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 308 | llvm::StringRef FileName, bool IsHeader = false, |
| 309 | StringRef OldHeaderInclude = "") { |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 310 | std::string NewCode; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 311 | std::string GuardName(FileName); |
| 312 | if (IsHeader) { |
Haojian Wu | ac97fc3 | 2016-10-17 15:26:34 +0000 | [diff] [blame] | 313 | for (size_t i = 0; i < GuardName.size(); ++i) { |
| 314 | if (!isAlphanumeric(GuardName[i])) |
| 315 | GuardName[i] = '_'; |
| 316 | } |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 317 | GuardName = StringRef(GuardName).upper(); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 318 | NewCode += "#ifndef " + GuardName + "\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 319 | NewCode += "#define " + GuardName + "\n\n"; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 320 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 321 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 322 | NewCode += OldHeaderInclude; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 323 | // Add #Includes. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 324 | for (const auto &Include : Includes) |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 325 | NewCode += Include; |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 326 | |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 327 | if (!Includes.empty()) |
| 328 | NewCode += "\n"; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 329 | |
| 330 | // Add moved class definition and its related declarations. All declarations |
| 331 | // in same namespace are grouped together. |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 332 | // |
| 333 | // Record namespaces where the current position is in. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 334 | std::vector<std::string> CurrentNamespaces; |
| 335 | for (const auto &MovedDecl : Decls) { |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 336 | // The namespaces of the declaration being moved. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 337 | std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl); |
| 338 | auto CurrentIt = CurrentNamespaces.begin(); |
| 339 | auto DeclIt = DeclNamespaces.begin(); |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 340 | // Skip the common prefix. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 341 | while (CurrentIt != CurrentNamespaces.end() && |
| 342 | DeclIt != DeclNamespaces.end()) { |
| 343 | if (*CurrentIt != *DeclIt) |
| 344 | break; |
| 345 | ++CurrentIt; |
| 346 | ++DeclIt; |
| 347 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 348 | // Calculate the new namespaces after adding MovedDecl in CurrentNamespace, |
| 349 | // which is used for next iteration of this loop. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 350 | std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(), |
| 351 | CurrentIt); |
| 352 | NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end()); |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 353 | |
| 354 | |
| 355 | // End with CurrentNamespace. |
| 356 | bool HasEndCurrentNamespace = false; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 357 | auto RemainingSize = CurrentNamespaces.end() - CurrentIt; |
| 358 | for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0; |
| 359 | --RemainingSize, ++It) { |
| 360 | assert(It < CurrentNamespaces.rend()); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 361 | NewCode += "} // namespace " + *It + "\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 362 | HasEndCurrentNamespace = true; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 363 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 364 | // Add trailing '\n' after the nested namespace definition. |
| 365 | if (HasEndCurrentNamespace) |
| 366 | NewCode += "\n"; |
| 367 | |
| 368 | // If the moved declaration is not in CurrentNamespace, add extra namespace |
| 369 | // definitions. |
| 370 | bool IsInNewNamespace = false; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 371 | while (DeclIt != DeclNamespaces.end()) { |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 372 | NewCode += "namespace " + *DeclIt + " {\n"; |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 373 | IsInNewNamespace = true; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 374 | ++DeclIt; |
| 375 | } |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 376 | // If the moved declaration is in same namespace CurrentNamespace, add |
| 377 | // a preceeding `\n' before the moved declaration. |
Haojian Wu | 50a45d9 | 2016-11-18 10:51:16 +0000 | [diff] [blame] | 378 | // FIXME: Don't add empty lines between using declarations. |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 379 | if (!IsInNewNamespace) |
| 380 | NewCode += "\n"; |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 381 | NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 382 | CurrentNamespaces = std::move(NextNamespaces); |
| 383 | } |
| 384 | std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end()); |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 385 | for (const auto &NS : CurrentNamespaces) |
| 386 | NewCode += "} // namespace " + NS + "\n"; |
Haojian Wu | 220c755 | 2016-10-14 13:01:36 +0000 | [diff] [blame] | 387 | |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 388 | if (IsHeader) |
Haojian Wu | 53315a7 | 2016-11-15 09:06:59 +0000 | [diff] [blame] | 389 | NewCode += "\n#endif // " + GuardName + "\n"; |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 390 | return clang::tooling::Replacements( |
| 391 | clang::tooling::Replacement(FileName, 0, 0, NewCode)); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | } // namespace |
| 395 | |
| 396 | std::unique_ptr<clang::ASTConsumer> |
| 397 | ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler, |
| 398 | StringRef /*InFile*/) { |
| 399 | Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>( |
| 400 | &Compiler.getSourceManager(), &MoveTool)); |
| 401 | return MatchFinder.newASTConsumer(); |
| 402 | } |
| 403 | |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 404 | ClangMoveTool::ClangMoveTool(ClangMoveContext *const Context, |
| 405 | DeclarationReporter *const Reporter) |
| 406 | : Context(Context), Reporter(Reporter) { |
| 407 | if (!Context->Spec.NewHeader.empty()) |
| 408 | CCIncludes.push_back("#include \"" + Context->Spec.NewHeader + "\"\n"); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 411 | void ClangMoveTool::addRemovedDecl(const MovedDecl &Decl) { |
| 412 | const auto &SM = *Decl.SM; |
| 413 | auto Loc = Decl.Decl->getLocation(); |
| 414 | StringRef FilePath = SM.getFilename(Loc); |
| 415 | FilePathToFileID[FilePath] = SM.getFileID(Loc); |
| 416 | RemovedDecls.push_back(Decl); |
| 417 | } |
| 418 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 419 | void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 420 | auto InOldHeader = |
| 421 | isExpansionInFile(makeAbsolutePath(Context->Spec.OldHeader)); |
| 422 | auto InOldCC = isExpansionInFile(makeAbsolutePath(Context->Spec.OldCC)); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 423 | auto InOldFiles = anyOf(InOldHeader, InOldCC); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 424 | auto ForwardDecls = |
| 425 | cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition()))); |
| 426 | |
| 427 | //============================================================================ |
| 428 | // Matchers for old header |
| 429 | //============================================================================ |
| 430 | // Match all top-level named declarations (e.g. function, variable, enum) in |
| 431 | // old header, exclude forward class declarations and namespace declarations. |
| 432 | // |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 433 | // We consider declarations inside a class belongs to the class. So these |
| 434 | // declarations will be ignored. |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 435 | auto AllDeclsInHeader = namedDecl( |
| 436 | unless(ForwardDecls), unless(namespaceDecl()), |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 437 | unless(usingDirectiveDecl()), // using namespace decl. |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 438 | unless(classTemplateDecl(has(ForwardDecls))), // template forward decl. |
| 439 | InOldHeader, |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 440 | hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))), |
| 441 | hasDeclContext(decl(anyOf(namespaceDecl(), translationUnitDecl())))); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 442 | Finder->addMatcher(AllDeclsInHeader.bind("decls_in_header"), this); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 443 | |
| 444 | // Don't register other matchers when dumping all declarations in header. |
| 445 | if (Context->DumpDeclarations) |
| 446 | return; |
| 447 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 448 | // Match forward declarations in old header. |
| 449 | Finder->addMatcher(namedDecl(ForwardDecls, InOldHeader).bind("fwd_decl"), |
| 450 | this); |
| 451 | |
| 452 | //============================================================================ |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 453 | // Matchers for old cc |
| 454 | //============================================================================ |
Haojian Wu | 50a45d9 | 2016-11-18 10:51:16 +0000 | [diff] [blame] | 455 | auto InOldCCNamedOrGlobalNamespace = |
| 456 | allOf(hasParent(decl(anyOf(namespaceDecl(unless(isAnonymous())), |
| 457 | translationUnitDecl()))), |
| 458 | InOldCC); |
| 459 | // Matching using decls/type alias decls which are in named namespace or |
| 460 | // global namespace. Those in classes, functions and anonymous namespaces are |
| 461 | // covered in other matchers. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 462 | Finder->addMatcher( |
Haojian Wu | 50a45d9 | 2016-11-18 10:51:16 +0000 | [diff] [blame] | 463 | namedDecl(anyOf(usingDecl(InOldCCNamedOrGlobalNamespace), |
| 464 | usingDirectiveDecl(InOldCCNamedOrGlobalNamespace), |
| 465 | typeAliasDecl( InOldCCNamedOrGlobalNamespace))) |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 466 | .bind("using_decl"), |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 467 | this); |
| 468 | |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 469 | // Match anonymous namespace decl in old cc. |
| 470 | Finder->addMatcher(namespaceDecl(isAnonymous(), InOldCC).bind("anonymous_ns"), |
| 471 | this); |
| 472 | |
| 473 | // Match static functions/variable definitions which are defined in named |
| 474 | // namespaces. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 475 | Optional<ast_matchers::internal::Matcher<NamedDecl>> HasAnySymbolNames; |
| 476 | for (StringRef SymbolName : Context->Spec.Names) { |
| 477 | llvm::StringRef GlobalSymbolName = SymbolName.trim().ltrim(':'); |
| 478 | const auto HasName = hasName(("::" + GlobalSymbolName).str()); |
| 479 | HasAnySymbolNames = |
| 480 | HasAnySymbolNames ? anyOf(*HasAnySymbolNames, HasName) : HasName; |
| 481 | } |
| 482 | |
| 483 | if (!HasAnySymbolNames) { |
| 484 | llvm::errs() << "No symbols being moved.\n"; |
| 485 | return; |
| 486 | } |
| 487 | auto InMovedClass = |
| 488 | hasOutermostEnclosingClass(cxxRecordDecl(*HasAnySymbolNames)); |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 489 | auto IsOldCCStaticDefinition = |
Haojian Wu | 50a45d9 | 2016-11-18 10:51:16 +0000 | [diff] [blame] | 490 | allOf(isDefinition(), unless(InMovedClass), InOldCCNamedOrGlobalNamespace, |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 491 | isStaticStorageClass()); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 492 | Finder->addMatcher(namedDecl(anyOf(functionDecl(IsOldCCStaticDefinition), |
| 493 | varDecl(IsOldCCStaticDefinition))) |
| 494 | .bind("static_decls"), |
| 495 | this); |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 496 | |
| 497 | //============================================================================ |
| 498 | // Matchers for old files, including old.h/old.cc |
| 499 | //============================================================================ |
| 500 | // Create a MatchCallback for class declarations. |
| 501 | MatchCallbacks.push_back(llvm::make_unique<ClassDeclarationMatch>(this)); |
| 502 | // Match moved class declarations. |
| 503 | auto MovedClass = |
| 504 | cxxRecordDecl( |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 505 | InOldFiles, *HasAnySymbolNames, isDefinition(), |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 506 | hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()))) |
| 507 | .bind("moved_class"); |
| 508 | Finder->addMatcher(MovedClass, MatchCallbacks.back().get()); |
| 509 | // Match moved class methods (static methods included) which are defined |
| 510 | // outside moved class declaration. |
| 511 | Finder->addMatcher( |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 512 | cxxMethodDecl(InOldFiles, ofOutermostEnclosingClass(*HasAnySymbolNames), |
Haojian Wu | 35ca946 | 2016-11-14 14:15:44 +0000 | [diff] [blame] | 513 | isDefinition()) |
| 514 | .bind("class_method"), |
| 515 | MatchCallbacks.back().get()); |
| 516 | // Match static member variable definition of the moved class. |
| 517 | Finder->addMatcher( |
| 518 | varDecl(InMovedClass, InOldFiles, isDefinition(), isStaticDataMember()) |
| 519 | .bind("class_static_var_decl"), |
| 520 | MatchCallbacks.back().get()); |
| 521 | |
Haojian Wu | 4543fec | 2016-11-16 13:05:19 +0000 | [diff] [blame] | 522 | MatchCallbacks.push_back(llvm::make_unique<FunctionDeclarationMatch>(this)); |
| 523 | Finder->addMatcher(functionDecl(InOldFiles, *HasAnySymbolNames, |
| 524 | anyOf(hasDeclContext(namespaceDecl()), |
| 525 | hasDeclContext(translationUnitDecl()))) |
| 526 | .bind("function"), |
| 527 | MatchCallbacks.back().get()); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) { |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 531 | if (const auto *D = |
| 532 | Result.Nodes.getNodeAs<clang::NamedDecl>("decls_in_header")) { |
| 533 | UnremovedDeclsInOldHeader.insert(D); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 534 | } else if (const auto *FWD = |
| 535 | Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 536 | // Skip all forward declarations which appear after moved class declaration. |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 537 | if (RemovedDecls.empty()) { |
Haojian Wu | b53ec46 | 2016-11-10 05:33:26 +0000 | [diff] [blame] | 538 | if (const auto *DCT = FWD->getDescribedClassTemplate()) |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 539 | MovedDecls.emplace_back(DCT, &Result.Context->getSourceManager()); |
Haojian Wu | b53ec46 | 2016-11-10 05:33:26 +0000 | [diff] [blame] | 540 | else |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 541 | MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager()); |
Haojian Wu | 29c38f7 | 2016-10-21 19:26:43 +0000 | [diff] [blame] | 542 | } |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 543 | } else if (const auto *ANS = |
| 544 | Result.Nodes.getNodeAs<clang::NamespaceDecl>("anonymous_ns")) { |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 545 | MovedDecls.emplace_back(ANS, &Result.Context->getSourceManager()); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 546 | } else if (const auto *ND = |
| 547 | Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) { |
| 548 | MovedDecls.emplace_back(ND, &Result.Context->getSourceManager()); |
Haojian Wu | 67bb651 | 2016-10-19 14:13:21 +0000 | [diff] [blame] | 549 | } else if (const auto *UD = |
| 550 | Result.Nodes.getNodeAs<clang::NamedDecl>("using_decl")) { |
| 551 | MovedDecls.emplace_back(UD, &Result.Context->getSourceManager()); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 555 | std::string ClangMoveTool::makeAbsolutePath(StringRef Path) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 556 | return MakeAbsolutePath(Context->OriginalRunningDirectory, Path); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 559 | void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, bool IsAngled, |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 560 | llvm::StringRef SearchPath, |
| 561 | llvm::StringRef FileName, |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 562 | clang::CharSourceRange IncludeFilenameRange, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 563 | const SourceManager &SM) { |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 564 | SmallVector<char, 128> HeaderWithSearchPath; |
| 565 | llvm::sys::path::append(HeaderWithSearchPath, SearchPath, IncludeHeader); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 566 | std::string AbsoluteOldHeader = makeAbsolutePath(Context->Spec.OldHeader); |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 567 | // FIXME: Add old.h to the new.cc/h when the new target has dependencies on |
| 568 | // old.h/c. For instance, when moved class uses another class defined in |
| 569 | // old.h, the old.h should be added in new.h. |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 570 | if (AbsoluteOldHeader == |
| 571 | MakeAbsolutePath(SM, llvm::StringRef(HeaderWithSearchPath.data(), |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 572 | HeaderWithSearchPath.size()))) { |
| 573 | OldHeaderIncludeRange = IncludeFilenameRange; |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 574 | return; |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 575 | } |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 576 | |
| 577 | std::string IncludeLine = |
| 578 | IsAngled ? ("#include <" + IncludeHeader + ">\n").str() |
| 579 | : ("#include \"" + IncludeHeader + "\"\n").str(); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 580 | |
Haojian Wu | db72657 | 2016-10-12 15:50:30 +0000 | [diff] [blame] | 581 | std::string AbsoluteCurrentFile = MakeAbsolutePath(SM, FileName); |
| 582 | if (AbsoluteOldHeader == AbsoluteCurrentFile) { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 583 | HeaderIncludes.push_back(IncludeLine); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 584 | } else if (makeAbsolutePath(Context->Spec.OldCC) == AbsoluteCurrentFile) { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 585 | CCIncludes.push_back(IncludeLine); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 586 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void ClangMoveTool::removeClassDefinitionInOldFiles() { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 590 | if (RemovedDecls.empty()) return; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 591 | for (const auto &MovedDecl : RemovedDecls) { |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 592 | const auto &SM = *MovedDecl.SM; |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame] | 593 | auto Range = GetFullRange(&SM, MovedDecl.Decl); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 594 | clang::tooling::Replacement RemoveReplacement( |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 595 | SM, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 596 | clang::CharSourceRange::getCharRange(Range.getBegin(), Range.getEnd()), |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 597 | ""); |
| 598 | std::string FilePath = RemoveReplacement.getFilePath().str(); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 599 | auto Err = Context->FileToReplacements[FilePath].add(RemoveReplacement); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 600 | if (Err) |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 601 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 602 | } |
| 603 | const SourceManager* SM = RemovedDecls[0].SM; |
| 604 | |
| 605 | // Post process of cleanup around all the replacements. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 606 | for (auto &FileAndReplacements : Context->FileToReplacements) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 607 | StringRef FilePath = FileAndReplacements.first; |
| 608 | // Add #include of new header to old header. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 609 | if (Context->Spec.OldDependOnNew && |
| 610 | MakeAbsolutePath(*SM, FilePath) == |
| 611 | makeAbsolutePath(Context->Spec.OldHeader)) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 612 | // FIXME: Minimize the include path like include-fixer. |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 613 | std::string IncludeNewH = |
| 614 | "#include \"" + Context->Spec.NewHeader + "\"\n"; |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 615 | // This replacment for inserting header will be cleaned up at the end. |
| 616 | auto Err = FileAndReplacements.second.add( |
| 617 | tooling::Replacement(FilePath, UINT_MAX, 0, IncludeNewH)); |
| 618 | if (Err) |
| 619 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Haojian Wu | 53eab1e | 2016-10-14 13:43:49 +0000 | [diff] [blame] | 620 | } |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 621 | |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 622 | auto SI = FilePathToFileID.find(FilePath); |
| 623 | // Ignore replacements for new.h/cc. |
| 624 | if (SI == FilePathToFileID.end()) continue; |
| 625 | llvm::StringRef Code = SM->getBufferData(SI->second); |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 626 | format::FormatStyle Style = |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 627 | format::getStyle("file", FilePath, Context->FallbackStyle); |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 628 | auto CleanReplacements = format::cleanupAroundReplacements( |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 629 | Code, Context->FileToReplacements[FilePath], Style); |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 630 | |
| 631 | if (!CleanReplacements) { |
| 632 | llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n"; |
| 633 | continue; |
| 634 | } |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 635 | Context->FileToReplacements[FilePath] = *CleanReplacements; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | void ClangMoveTool::moveClassDefinitionToNewFiles() { |
| 640 | std::vector<MovedDecl> NewHeaderDecls; |
| 641 | std::vector<MovedDecl> NewCCDecls; |
| 642 | for (const auto &MovedDecl : MovedDecls) { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 643 | if (isInHeaderFile(*MovedDecl.SM, MovedDecl.Decl, |
| 644 | Context->OriginalRunningDirectory, |
| 645 | Context->Spec.OldHeader)) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 646 | NewHeaderDecls.push_back(MovedDecl); |
| 647 | else |
| 648 | NewCCDecls.push_back(MovedDecl); |
| 649 | } |
| 650 | |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 651 | if (!Context->Spec.NewHeader.empty()) { |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 652 | std::string OldHeaderInclude = |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 653 | Context->Spec.NewDependOnOld |
| 654 | ? "#include \"" + Context->Spec.OldHeader + "\"\n" |
| 655 | : ""; |
| 656 | Context->FileToReplacements[Context->Spec.NewHeader] = |
| 657 | createInsertedReplacements(HeaderIncludes, NewHeaderDecls, |
| 658 | Context->Spec.NewHeader, /*IsHeader=*/true, |
| 659 | OldHeaderInclude); |
Haojian Wu | 48ac304 | 2016-11-23 10:04:19 +0000 | [diff] [blame] | 660 | } |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 661 | if (!Context->Spec.NewCC.empty()) |
| 662 | Context->FileToReplacements[Context->Spec.NewCC] = |
| 663 | createInsertedReplacements(CCIncludes, NewCCDecls, Context->Spec.NewCC); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 666 | // Move all contents from OldFile to NewFile. |
| 667 | void ClangMoveTool::moveAll(SourceManager &SM, StringRef OldFile, |
| 668 | StringRef NewFile) { |
| 669 | const FileEntry *FE = SM.getFileManager().getFile(makeAbsolutePath(OldFile)); |
| 670 | if (!FE) { |
| 671 | llvm::errs() << "Failed to get file: " << OldFile << "\n"; |
| 672 | return; |
| 673 | } |
| 674 | FileID ID = SM.getOrCreateFileID(FE, SrcMgr::C_User); |
| 675 | auto Begin = SM.getLocForStartOfFile(ID); |
| 676 | auto End = SM.getLocForEndOfFile(ID); |
| 677 | clang::tooling::Replacement RemoveAll ( |
| 678 | SM, clang::CharSourceRange::getCharRange(Begin, End), ""); |
| 679 | std::string FilePath = RemoveAll.getFilePath().str(); |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 680 | Context->FileToReplacements[FilePath] = |
| 681 | clang::tooling::Replacements(RemoveAll); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 682 | |
| 683 | StringRef Code = SM.getBufferData(ID); |
| 684 | if (!NewFile.empty()) { |
| 685 | auto AllCode = clang::tooling::Replacements( |
| 686 | clang::tooling::Replacement(NewFile, 0, 0, Code)); |
| 687 | // If we are moving from old.cc, an extra step is required: excluding |
| 688 | // 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] | 689 | if (Context->Spec.NewCC == NewFile && OldHeaderIncludeRange.isValid()) { |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 690 | AllCode = AllCode.merge( |
| 691 | clang::tooling::Replacements(clang::tooling::Replacement( |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 692 | SM, OldHeaderIncludeRange, '"' + Context->Spec.NewHeader + '"'))); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 693 | } |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 694 | Context->FileToReplacements[NewFile] = std::move(AllCode); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 698 | void ClangMoveTool::onEndOfTranslationUnit() { |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 699 | if (Context->DumpDeclarations) { |
| 700 | assert(Reporter); |
| 701 | for (const auto *Decl : UnremovedDeclsInOldHeader) { |
| 702 | auto Kind = Decl->getKind(); |
| 703 | const std::string QualifiedName = Decl->getQualifiedNameAsString(); |
| 704 | if (Kind == Decl::Kind::Function || Kind == Decl::Kind::FunctionTemplate) |
| 705 | Reporter->reportDeclaration(QualifiedName, "Function"); |
| 706 | else if (Kind == Decl::Kind::ClassTemplate || |
| 707 | Kind == Decl::Kind::CXXRecord) |
| 708 | Reporter->reportDeclaration(QualifiedName, "Class"); |
| 709 | } |
| 710 | return; |
| 711 | } |
| 712 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 713 | if (RemovedDecls.empty()) |
| 714 | return; |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 715 | if (UnremovedDeclsInOldHeader.empty() && !Context->Spec.OldHeader.empty()) { |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 716 | auto &SM = *RemovedDecls[0].SM; |
Haojian Wu | b15c8da | 2016-11-24 10:17:17 +0000 | [diff] [blame] | 717 | moveAll(SM, Context->Spec.OldHeader, Context->Spec.NewHeader); |
| 718 | moveAll(SM, Context->Spec.OldCC, Context->Spec.NewCC); |
Haojian Wu | 2930be1 | 2016-11-08 19:55:13 +0000 | [diff] [blame] | 719 | return; |
| 720 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 721 | removeClassDefinitionInOldFiles(); |
| 722 | moveClassDefinitionToNewFiles(); |
| 723 | } |
| 724 | |
| 725 | } // namespace move |
| 726 | } // namespace clang |