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