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