Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 1 | //===-- ClangReplaceMain.cpp - Main file for clang-replace tool -----------===// |
| 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 | /// \file |
| 11 | /// \brief This file provides the main function for the clang-replace tool. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ApplyReplacements.h" |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticOptions.h" |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame^] | 19 | #include "clang/Rewrite/Core/Rewriter.h" |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace clang; |
| 24 | using namespace clang::replace; |
| 25 | |
| 26 | static cl::opt<std::string> Directory(cl::Positional, cl::Required, |
| 27 | cl::desc("<Search Root Directory>")); |
| 28 | |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 29 | static cl::opt<bool> RemoveTUReplacementFiles( |
| 30 | "remove-change-desc-files", |
| 31 | cl::desc("Remove the change description files regardless of successful\n" |
| 32 | "merging/replacing."), |
| 33 | cl::init(false)); |
| 34 | |
| 35 | // Helper object to remove the TUReplacement files (triggered by |
| 36 | // "remove-change-desc-files" command line option) when exiting current scope. |
| 37 | class ScopedFileRemover { |
| 38 | public: |
| 39 | ScopedFileRemover(const TUReplacementFiles &Files, |
| 40 | clang::DiagnosticsEngine &Diagnostics) |
| 41 | : TURFiles(Files), Diag(Diagnostics) {} |
| 42 | |
| 43 | ~ScopedFileRemover() { |
| 44 | deleteReplacementFiles(TURFiles, Diag); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | const TUReplacementFiles &TURFiles; |
| 49 | clang::DiagnosticsEngine &Diag; |
| 50 | }; |
| 51 | |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 52 | int main(int argc, char **argv) { |
| 53 | cl::ParseCommandLineOptions(argc, argv); |
| 54 | |
| 55 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions()); |
| 56 | DiagnosticsEngine Diagnostics( |
| 57 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
| 58 | DiagOpts.getPtr()); |
| 59 | |
| 60 | TUReplacements TUs; |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 61 | TUReplacementFiles TURFiles; |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 62 | |
| 63 | error_code ErrorCode = |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 64 | collectReplacementsFromDirectory(Directory, TUs, TURFiles, Diagnostics); |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 65 | |
| 66 | if (ErrorCode) { |
| 67 | errs() << "Trouble iterating over directory '" << Directory |
| 68 | << "': " << ErrorCode.message() << "\n"; |
| 69 | return false; |
| 70 | } |
| 71 | |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 72 | // Remove the TUReplacementFiles (triggered by "remove-change-desc-files" |
| 73 | // command line option) when exiting main(). |
| 74 | OwningPtr<ScopedFileRemover> Remover; |
| 75 | if (RemoveTUReplacementFiles) |
| 76 | Remover.reset(new ScopedFileRemover(TURFiles, Diagnostics)); |
| 77 | |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 78 | FileManager Files((FileSystemOptions())); |
| 79 | SourceManager SM(Diagnostics, Files); |
| 80 | |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 81 | FileToReplacementsMap GroupedReplacements; |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 82 | if (!mergeAndDeduplicate(TUs, GroupedReplacements, SM)) |
| 83 | return 1; |
| 84 | |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame^] | 85 | Rewriter DestRewriter(SM, LangOptions()); |
| 86 | if (!applyReplacements(GroupedReplacements, DestRewriter)) { |
| 87 | errs() << "Failed to apply all replacements. No changes made.\n"; |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 88 | return 1; |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame^] | 89 | } |
| 90 | |
| 91 | if (!writeFiles(DestRewriter)) |
| 92 | return 1; |
| 93 | |
| 94 | return 0; |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 95 | } |