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