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 | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 27 | // Make the Path absolute using the CurrentDir if the Path is not an absolute |
| 28 | // path. An empty Path will result in an empty string. |
| 29 | std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) { |
| 30 | if (Path.empty()) |
| 31 | return ""; |
| 32 | llvm::SmallString<128> InitialDirectory(CurrentDir); |
| 33 | llvm::SmallString<128> AbsolutePath(Path); |
| 34 | if (std::error_code EC = |
| 35 | llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath)) |
| 36 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
| 37 | << '\n'; |
| 38 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); |
Haojian Wu | c6f125e | 2016-10-04 09:49:20 +0000 | [diff] [blame] | 39 | llvm::sys::path::native(AbsolutePath); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 40 | return AbsolutePath.str(); |
| 41 | } |
| 42 | |
| 43 | // Make the Path absolute using the current working directory of the given |
| 44 | // SourceManager if the Path is not an absolute path. |
| 45 | // |
| 46 | // The Path can be a path relative to the build directory, or retrieved from |
| 47 | // the SourceManager. |
| 48 | std::string MakeAbsolutePath(const SourceManager& SM, StringRef Path) { |
| 49 | llvm::SmallString<128> AbsolutePath(Path); |
| 50 | if (std::error_code EC = |
| 51 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute(AbsolutePath)) |
| 52 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
| 53 | << '\n'; |
| 54 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); |
Haojian Wu | c6f125e | 2016-10-04 09:49:20 +0000 | [diff] [blame] | 55 | llvm::sys::path::native(AbsolutePath); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 56 | return AbsolutePath.str(); |
| 57 | } |
| 58 | |
| 59 | // Matches AST nodes that are expanded within the given AbsoluteFilePath. |
| 60 | AST_POLYMORPHIC_MATCHER_P(isExpansionInFile, |
| 61 | AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc), |
| 62 | std::string, AbsoluteFilePath) { |
| 63 | auto &SourceManager = Finder->getASTContext().getSourceManager(); |
| 64 | auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getLocStart()); |
| 65 | if (ExpansionLoc.isInvalid()) |
| 66 | return false; |
| 67 | auto FileEntry = |
| 68 | SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc)); |
| 69 | if (!FileEntry) |
| 70 | return false; |
| 71 | return MakeAbsolutePath(SourceManager, FileEntry->getName()) == |
| 72 | AbsoluteFilePath; |
| 73 | } |
| 74 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 75 | class FindAllIncludes : public clang::PPCallbacks { |
| 76 | public: |
| 77 | explicit FindAllIncludes(SourceManager *SM, ClangMoveTool *const MoveTool) |
| 78 | : SM(*SM), MoveTool(MoveTool) {} |
| 79 | |
| 80 | void InclusionDirective(clang::SourceLocation HashLoc, |
| 81 | const clang::Token & /*IncludeTok*/, |
| 82 | StringRef FileName, bool IsAngled, |
| 83 | clang::CharSourceRange /*FilenameRange*/, |
| 84 | const clang::FileEntry * /*File*/, |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 85 | StringRef SearchPath, StringRef /*RelativePath*/, |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 86 | const clang::Module * /*Imported*/) override { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 87 | if (const auto *FileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc))) |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 88 | MoveTool->addIncludes(FileName, IsAngled, SearchPath, |
| 89 | FileEntry->getName(), SM); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
| 93 | const SourceManager &SM; |
| 94 | ClangMoveTool *const MoveTool; |
| 95 | }; |
| 96 | |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame^] | 97 | // Expand to get the end location of the line where the EndLoc of the given |
| 98 | // Decl. |
| 99 | SourceLocation |
| 100 | getLocForEndOfDecl(const clang::Decl *D, const SourceManager *SM, |
| 101 | const LangOptions &LangOpts = clang::LangOptions()) { |
| 102 | std::pair<FileID, unsigned> LocInfo = SM->getDecomposedLoc(D->getLocEnd()); |
| 103 | // Try to load the file buffer. |
| 104 | bool InvalidTemp = false; |
| 105 | llvm::StringRef File = SM->getBufferData(LocInfo.first, &InvalidTemp); |
| 106 | if (InvalidTemp) |
| 107 | return SourceLocation(); |
| 108 | |
| 109 | const char *TokBegin = File.data() + LocInfo.second; |
| 110 | // Lex from the start of the given location. |
| 111 | Lexer Lex(SM->getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), |
| 112 | TokBegin, File.end()); |
| 113 | |
| 114 | llvm::SmallVector<char, 16> Line; |
| 115 | // FIXME: this is a bit hacky to get ReadToEndOfLine work. |
| 116 | Lex.setParsingPreprocessorDirective(true); |
| 117 | Lex.ReadToEndOfLine(&Line); |
| 118 | SourceLocation EndLoc = D->getLocEnd().getLocWithOffset(Line.size()); |
| 119 | // If we already reach EOF, just return the EOF SourceLocation; |
| 120 | // otherwise, move 1 offset ahead to include the trailing newline character |
| 121 | // '\n'. |
| 122 | return SM->getLocForEndOfFile(LocInfo.first) == EndLoc |
| 123 | ? EndLoc |
| 124 | : EndLoc.getLocWithOffset(1); |
| 125 | } |
| 126 | |
| 127 | // Get full range of a Decl including the comments associated with it. |
| 128 | clang::CharSourceRange |
| 129 | GetFullRange(const clang::SourceManager *SM, const clang::Decl *D, |
| 130 | const clang::LangOptions &options = clang::LangOptions()) { |
| 131 | clang::SourceRange Full = D->getSourceRange(); |
| 132 | Full.setEnd(getLocForEndOfDecl(D, SM)); |
| 133 | // Expand to comments that are associated with the Decl. |
| 134 | if (const auto* Comment = |
| 135 | D->getASTContext().getRawCommentForDeclNoCache(D)) { |
| 136 | if (SM->isBeforeInTranslationUnit(Full.getEnd(), Comment->getLocEnd())) |
| 137 | Full.setEnd(Comment->getLocEnd()); |
| 138 | // FIXME: Don't delete a preceding comment, if there are no other entities |
| 139 | // it could refer to. |
| 140 | if (SM->isBeforeInTranslationUnit(Comment->getLocStart(), |
| 141 | Full.getBegin())) |
| 142 | Full.setBegin(Comment->getLocStart()); |
| 143 | } |
| 144 | |
| 145 | return clang::CharSourceRange::getCharRange(Full); |
| 146 | } |
| 147 | |
| 148 | std::string getDeclarationSourceText(const clang::Decl *D, |
| 149 | const clang::SourceManager *SM) { |
| 150 | llvm::StringRef SourceText = clang::Lexer::getSourceText( |
| 151 | GetFullRange(SM, D), *SM, clang::LangOptions()); |
| 152 | return SourceText.str(); |
| 153 | } |
| 154 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 155 | clang::tooling::Replacement |
| 156 | getReplacementInChangedCode(const clang::tooling::Replacements &Replacements, |
| 157 | const clang::tooling::Replacement &Replacement) { |
| 158 | unsigned Start = Replacements.getShiftedCodePosition(Replacement.getOffset()); |
| 159 | unsigned End = Replacements.getShiftedCodePosition(Replacement.getOffset() + |
| 160 | Replacement.getLength()); |
| 161 | return clang::tooling::Replacement(Replacement.getFilePath(), Start, |
| 162 | End - Start, |
| 163 | Replacement.getReplacementText()); |
| 164 | } |
| 165 | |
| 166 | void addOrMergeReplacement(const clang::tooling::Replacement &Replacement, |
| 167 | clang::tooling::Replacements *Replacements) { |
| 168 | auto Err = Replacements->add(Replacement); |
| 169 | if (Err) { |
| 170 | llvm::consumeError(std::move(Err)); |
| 171 | auto Replace = getReplacementInChangedCode(*Replacements, Replacement); |
| 172 | *Replacements = Replacements->merge(clang::tooling::Replacements(Replace)); |
| 173 | } |
| 174 | } |
| 175 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 176 | bool isInHeaderFile(const clang::SourceManager &SM, const clang::Decl *D, |
| 177 | llvm::StringRef OriginalRunningDirectory, |
| 178 | llvm::StringRef OldHeader) { |
| 179 | if (OldHeader.empty()) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 180 | return false; |
| 181 | auto ExpansionLoc = SM.getExpansionLoc(D->getLocStart()); |
| 182 | if (ExpansionLoc.isInvalid()) |
| 183 | return false; |
| 184 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 185 | if (const auto *FE = SM.getFileEntryForID(SM.getFileID(ExpansionLoc))) { |
| 186 | return MakeAbsolutePath(SM, FE->getName()) == |
| 187 | MakeAbsolutePath(OriginalRunningDirectory, OldHeader); |
| 188 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | std::vector<std::string> GetNamespaces(const clang::Decl *D) { |
| 194 | std::vector<std::string> Namespaces; |
| 195 | for (const auto *Context = D->getDeclContext(); Context; |
| 196 | Context = Context->getParent()) { |
| 197 | if (llvm::isa<clang::TranslationUnitDecl>(Context) || |
| 198 | llvm::isa<clang::LinkageSpecDecl>(Context)) |
| 199 | break; |
| 200 | |
| 201 | if (const auto *ND = llvm::dyn_cast<clang::NamespaceDecl>(Context)) |
| 202 | Namespaces.push_back(ND->getName().str()); |
| 203 | } |
| 204 | std::reverse(Namespaces.begin(), Namespaces.end()); |
| 205 | return Namespaces; |
| 206 | } |
| 207 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 208 | clang::tooling::Replacements |
| 209 | createInsertedReplacements(const std::vector<std::string> &Includes, |
| 210 | const std::vector<ClangMoveTool::MovedDecl> &Decls, |
| 211 | llvm::StringRef FileName) { |
| 212 | clang::tooling::Replacements InsertedReplacements; |
| 213 | |
| 214 | // Add #Includes. |
| 215 | std::string AllIncludesString; |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 216 | // FIXME: Add header guard. |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 217 | for (const auto &Include : Includes) |
| 218 | AllIncludesString += Include; |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame^] | 219 | |
| 220 | if (!AllIncludesString.empty()) { |
| 221 | clang::tooling::Replacement InsertInclude(FileName, 0, 0, |
| 222 | AllIncludesString + "\n"); |
| 223 | addOrMergeReplacement(InsertInclude, &InsertedReplacements); |
| 224 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 225 | |
| 226 | // Add moved class definition and its related declarations. All declarations |
| 227 | // in same namespace are grouped together. |
| 228 | std::vector<std::string> CurrentNamespaces; |
| 229 | for (const auto &MovedDecl : Decls) { |
| 230 | std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl); |
| 231 | auto CurrentIt = CurrentNamespaces.begin(); |
| 232 | auto DeclIt = DeclNamespaces.begin(); |
| 233 | while (CurrentIt != CurrentNamespaces.end() && |
| 234 | DeclIt != DeclNamespaces.end()) { |
| 235 | if (*CurrentIt != *DeclIt) |
| 236 | break; |
| 237 | ++CurrentIt; |
| 238 | ++DeclIt; |
| 239 | } |
| 240 | std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(), |
| 241 | CurrentIt); |
| 242 | NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end()); |
| 243 | auto RemainingSize = CurrentNamespaces.end() - CurrentIt; |
| 244 | for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0; |
| 245 | --RemainingSize, ++It) { |
| 246 | assert(It < CurrentNamespaces.rend()); |
| 247 | auto code = "} // namespace " + *It + "\n"; |
| 248 | clang::tooling::Replacement InsertedReplacement(FileName, 0, 0, code); |
| 249 | addOrMergeReplacement(InsertedReplacement, &InsertedReplacements); |
| 250 | } |
| 251 | while (DeclIt != DeclNamespaces.end()) { |
| 252 | clang::tooling::Replacement InsertedReplacement( |
| 253 | FileName, 0, 0, "namespace " + *DeclIt + " {\n"); |
| 254 | addOrMergeReplacement(InsertedReplacement, &InsertedReplacements); |
| 255 | ++DeclIt; |
| 256 | } |
| 257 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 258 | clang::tooling::Replacement InsertedReplacement( |
| 259 | FileName, 0, 0, getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM)); |
| 260 | addOrMergeReplacement(InsertedReplacement, &InsertedReplacements); |
| 261 | |
| 262 | CurrentNamespaces = std::move(NextNamespaces); |
| 263 | } |
| 264 | std::reverse(CurrentNamespaces.begin(), CurrentNamespaces.end()); |
| 265 | for (const auto &NS : CurrentNamespaces) { |
| 266 | clang::tooling::Replacement InsertedReplacement( |
| 267 | FileName, 0, 0, "} // namespace " + NS + "\n"); |
| 268 | addOrMergeReplacement(InsertedReplacement, &InsertedReplacements); |
| 269 | } |
| 270 | return InsertedReplacements; |
| 271 | } |
| 272 | |
| 273 | } // namespace |
| 274 | |
| 275 | std::unique_ptr<clang::ASTConsumer> |
| 276 | ClangMoveAction::CreateASTConsumer(clang::CompilerInstance &Compiler, |
| 277 | StringRef /*InFile*/) { |
| 278 | Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique<FindAllIncludes>( |
| 279 | &Compiler.getSourceManager(), &MoveTool)); |
| 280 | return MatchFinder.newASTConsumer(); |
| 281 | } |
| 282 | |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 283 | ClangMoveTool::ClangMoveTool( |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 284 | const MoveDefinitionSpec &MoveSpec, |
| 285 | std::map<std::string, tooling::Replacements> &FileToReplacements, |
| 286 | llvm::StringRef OriginalRunningDirectory, llvm::StringRef FallbackStyle) |
| 287 | : Spec(MoveSpec), FileToReplacements(FileToReplacements), |
| 288 | OriginalRunningDirectory(OriginalRunningDirectory), |
| 289 | FallbackStyle(FallbackStyle) { |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 290 | Spec.Name = llvm::StringRef(Spec.Name).ltrim(':'); |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 291 | if (!Spec.NewHeader.empty()) |
| 292 | CCIncludes.push_back("#include \"" + Spec.NewHeader + "\"\n"); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void ClangMoveTool::registerMatchers(ast_matchers::MatchFinder *Finder) { |
| 296 | std::string FullyQualifiedName = "::" + Spec.Name; |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 297 | auto InOldHeader = isExpansionInFile( |
| 298 | MakeAbsolutePath(OriginalRunningDirectory, Spec.OldHeader)); |
| 299 | auto InOldCC = isExpansionInFile( |
| 300 | MakeAbsolutePath(OriginalRunningDirectory, Spec.OldCC)); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 301 | auto InOldFiles = anyOf(InOldHeader, InOldCC); |
| 302 | auto InMovedClass = |
| 303 | hasDeclContext(cxxRecordDecl(hasName(FullyQualifiedName))); |
| 304 | |
| 305 | // Match moved class declarations. |
| 306 | auto MovedClass = cxxRecordDecl( |
| 307 | InOldFiles, hasName(FullyQualifiedName), isDefinition(), |
| 308 | hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl()))); |
| 309 | Finder->addMatcher(MovedClass.bind("moved_class"), this); |
| 310 | |
| 311 | // Match moved class methods (static methods included) which are defined |
| 312 | // outside moved class declaration. |
| 313 | Finder->addMatcher(cxxMethodDecl(InOldFiles, |
| 314 | ofClass(hasName(FullyQualifiedName)), |
| 315 | isDefinition()) |
| 316 | .bind("class_method"), |
| 317 | this); |
| 318 | |
| 319 | // Match static member variable definition of the moved class. |
| 320 | Finder->addMatcher(varDecl(InMovedClass, InOldCC, isDefinition()) |
| 321 | .bind("class_static_var_decl"), |
| 322 | this); |
| 323 | |
| 324 | auto inAnonymousNamespace = hasParent(namespaceDecl(isAnonymous())); |
| 325 | // Match functions/variables definitions which are defined in anonymous |
| 326 | // namespace in old cc. |
| 327 | Finder->addMatcher( |
| 328 | namedDecl(anyOf(functionDecl(isDefinition()), varDecl(isDefinition())), |
| 329 | inAnonymousNamespace) |
| 330 | .bind("decls_in_anonymous_ns"), |
| 331 | this); |
| 332 | |
| 333 | // Match static functions/variabale definitions in old cc. |
| 334 | Finder->addMatcher( |
| 335 | namedDecl(anyOf(functionDecl(isDefinition(), unless(InMovedClass), |
Haojian Wu | ef247cb | 2016-09-27 08:01:04 +0000 | [diff] [blame] | 336 | isStaticStorageClass(), InOldCC), |
| 337 | varDecl(isDefinition(), unless(InMovedClass), |
| 338 | isStaticStorageClass(), InOldCC))) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 339 | .bind("static_decls"), |
| 340 | this); |
| 341 | |
| 342 | // Match forward declarations in old header. |
| 343 | Finder->addMatcher( |
| 344 | cxxRecordDecl(unless(anyOf(isImplicit(), isDefinition())), InOldHeader) |
| 345 | .bind("fwd_decl"), |
| 346 | this); |
| 347 | } |
| 348 | |
| 349 | void ClangMoveTool::run(const ast_matchers::MatchFinder::MatchResult &Result) { |
| 350 | if (const auto *CMD = |
| 351 | Result.Nodes.getNodeAs<clang::CXXMethodDecl>("class_method")) { |
| 352 | // Skip inline class methods. isInline() ast matcher doesn't ignore this |
| 353 | // case. |
| 354 | if (!CMD->isInlined()) { |
| 355 | MovedDecls.emplace_back(CMD, &Result.Context->getSourceManager()); |
| 356 | RemovedDecls.push_back(MovedDecls.back()); |
| 357 | } |
| 358 | } else if (const auto *VD = Result.Nodes.getNodeAs<clang::VarDecl>( |
| 359 | "class_static_var_decl")) { |
| 360 | MovedDecls.emplace_back(VD, &Result.Context->getSourceManager()); |
| 361 | RemovedDecls.push_back(MovedDecls.back()); |
| 362 | } else if (const auto *class_decl = |
| 363 | Result.Nodes.getNodeAs<clang::CXXRecordDecl>("moved_class")) { |
| 364 | MovedDecls.emplace_back(class_decl, &Result.Context->getSourceManager()); |
| 365 | RemovedDecls.push_back(MovedDecls.back()); |
| 366 | } else if (const auto *FWD = |
| 367 | Result.Nodes.getNodeAs<clang::CXXRecordDecl>("fwd_decl")) { |
| 368 | // Skip all forwad declarations which appear after moved class declaration. |
| 369 | if (RemovedDecls.empty()) |
| 370 | MovedDecls.emplace_back(FWD, &Result.Context->getSourceManager()); |
| 371 | } else if (const auto *FD = Result.Nodes.getNodeAs<clang::NamedDecl>( |
| 372 | "decls_in_anonymous_ns")) { |
| 373 | MovedDecls.emplace_back(FD, &Result.Context->getSourceManager()); |
| 374 | } else if (const auto *ND = |
| 375 | Result.Nodes.getNodeAs<clang::NamedDecl>("static_decls")) { |
| 376 | MovedDecls.emplace_back(ND, &Result.Context->getSourceManager()); |
| 377 | } |
| 378 | } |
| 379 | |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 380 | void ClangMoveTool::addIncludes(llvm::StringRef IncludeHeader, |
| 381 | bool IsAngled, |
| 382 | llvm::StringRef SearchPath, |
| 383 | llvm::StringRef FileName, |
| 384 | const SourceManager& SM) { |
| 385 | auto AbsoluteSearchPath = MakeAbsolutePath(SM, SearchPath); |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 386 | // FIXME: Add old.h to the new.cc/h when the new target has dependencies on |
| 387 | // old.h/c. For instance, when moved class uses another class defined in |
| 388 | // old.h, the old.h should be added in new.h. |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 389 | if (MakeAbsolutePath(OriginalRunningDirectory, Spec.OldHeader) == |
| 390 | MakeAbsolutePath(AbsoluteSearchPath, IncludeHeader)) |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 391 | return; |
| 392 | |
| 393 | std::string IncludeLine = |
| 394 | IsAngled ? ("#include <" + IncludeHeader + ">\n").str() |
| 395 | : ("#include \"" + IncludeHeader + "\"\n").str(); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 396 | |
| 397 | std::string AbsolutePath = MakeAbsolutePath(SM, FileName); |
| 398 | if (MakeAbsolutePath(OriginalRunningDirectory, Spec.OldHeader) == |
| 399 | AbsolutePath) { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 400 | HeaderIncludes.push_back(IncludeLine); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 401 | } else if (MakeAbsolutePath(OriginalRunningDirectory, Spec.OldCC) == |
| 402 | AbsolutePath) { |
Haojian Wu | daf4cb8 | 2016-09-23 13:28:38 +0000 | [diff] [blame] | 403 | CCIncludes.push_back(IncludeLine); |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 404 | } |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void ClangMoveTool::removeClassDefinitionInOldFiles() { |
| 408 | for (const auto &MovedDecl : RemovedDecls) { |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 409 | const auto &SM = *MovedDecl.SM; |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame^] | 410 | auto Range = GetFullRange(&SM, MovedDecl.Decl); |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 411 | clang::tooling::Replacement RemoveReplacement( |
Haojian Wu | 9abbeaa | 2016-10-06 08:59:24 +0000 | [diff] [blame^] | 412 | *MovedDecl.SM, clang::CharSourceRange::getCharRange( |
| 413 | Range.getBegin(), Range.getEnd()), |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 414 | ""); |
| 415 | std::string FilePath = RemoveReplacement.getFilePath().str(); |
| 416 | addOrMergeReplacement(RemoveReplacement, &FileToReplacements[FilePath]); |
Haojian Wu | 253d596 | 2016-10-06 08:29:32 +0000 | [diff] [blame] | 417 | |
| 418 | llvm::StringRef Code = |
| 419 | SM.getBufferData(SM.getFileID(MovedDecl.Decl->getLocation())); |
| 420 | format::FormatStyle Style = |
| 421 | format::getStyle("file", FilePath, FallbackStyle); |
| 422 | auto CleanReplacements = format::cleanupAroundReplacements( |
| 423 | Code, FileToReplacements[FilePath], Style); |
| 424 | |
| 425 | if (!CleanReplacements) { |
| 426 | llvm::errs() << llvm::toString(CleanReplacements.takeError()) << "\n"; |
| 427 | continue; |
| 428 | } |
| 429 | FileToReplacements[FilePath] = *CleanReplacements; |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | void ClangMoveTool::moveClassDefinitionToNewFiles() { |
| 434 | std::vector<MovedDecl> NewHeaderDecls; |
| 435 | std::vector<MovedDecl> NewCCDecls; |
| 436 | for (const auto &MovedDecl : MovedDecls) { |
Haojian Wu | d2a6d7b | 2016-10-04 09:05:31 +0000 | [diff] [blame] | 437 | if (isInHeaderFile(*MovedDecl.SM, MovedDecl.Decl, OriginalRunningDirectory, |
| 438 | Spec.OldHeader)) |
Haojian Wu | 357ef99 | 2016-09-21 13:18:19 +0000 | [diff] [blame] | 439 | NewHeaderDecls.push_back(MovedDecl); |
| 440 | else |
| 441 | NewCCDecls.push_back(MovedDecl); |
| 442 | } |
| 443 | |
| 444 | if (!Spec.NewHeader.empty()) |
| 445 | FileToReplacements[Spec.NewHeader] = createInsertedReplacements( |
| 446 | HeaderIncludes, NewHeaderDecls, Spec.NewHeader); |
| 447 | if (!Spec.NewCC.empty()) |
| 448 | FileToReplacements[Spec.NewCC] = |
| 449 | createInsertedReplacements(CCIncludes, NewCCDecls, Spec.NewCC); |
| 450 | } |
| 451 | |
| 452 | void ClangMoveTool::onEndOfTranslationUnit() { |
| 453 | if (RemovedDecls.empty()) |
| 454 | return; |
| 455 | removeClassDefinitionInOldFiles(); |
| 456 | moveClassDefinitionToNewFiles(); |
| 457 | } |
| 458 | |
| 459 | } // namespace move |
| 460 | } // namespace clang |