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 | 6d5350c | 2013-09-24 18:14:54 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Version.h" |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 21 | #include "clang/Format/Format.h" |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame] | 22 | #include "clang/Rewrite/Core/Rewriter.h" |
Edwin Vane | 6d5350c | 2013-09-24 18:14:54 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
| 24 | #include "llvm/ADT/StringSet.h" |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace clang; |
| 29 | using namespace clang::replace; |
| 30 | |
| 31 | static cl::opt<std::string> Directory(cl::Positional, cl::Required, |
| 32 | cl::desc("<Search Root Directory>")); |
| 33 | |
Chris Bieneman | fc4fd52 | 2015-01-28 22:45:26 +0000 | [diff] [blame] | 34 | static cl::OptionCategory ReplacementCategory("Replacement Options"); |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 35 | static cl::OptionCategory FormattingCategory("Formatting Options"); |
| 36 | |
Chris Bieneman | fc4fd52 | 2015-01-28 22:45:26 +0000 | [diff] [blame] | 37 | const cl::OptionCategory *VisibleCategories[] = {&ReplacementCategory, |
| 38 | &FormattingCategory}; |
| 39 | |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 40 | static cl::opt<bool> RemoveTUReplacementFiles( |
| 41 | "remove-change-desc-files", |
| 42 | cl::desc("Remove the change description files regardless of successful\n" |
| 43 | "merging/replacing."), |
Chris Bieneman | fc4fd52 | 2015-01-28 22:45:26 +0000 | [diff] [blame] | 44 | cl::init(false), cl::cat(ReplacementCategory)); |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 45 | |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 46 | |
| 47 | static cl::opt<bool> DoFormat( |
| 48 | "format", |
| 49 | cl::desc("Enable formatting of code changed by applying replacements.\n" |
| 50 | "Use -style to choose formatting style.\n"), |
| 51 | cl::cat(FormattingCategory)); |
| 52 | |
| 53 | // FIXME: Consider making the default behaviour for finding a style |
| 54 | // configuration file to start the search anew for every file being changed to |
| 55 | // handle situations where the style is different for different parts of a |
| 56 | // project. |
| 57 | |
| 58 | static cl::opt<std::string> FormatStyleConfig( |
| 59 | "style-config", |
| 60 | cl::desc("Path to a directory containing a .clang-format file\n" |
| 61 | "describing a formatting style to use for formatting\n" |
| 62 | "code when -style=file.\n"), |
| 63 | cl::init(""), cl::cat(FormattingCategory)); |
| 64 | |
| 65 | static cl::opt<std::string> |
| 66 | FormatStyleOpt("style", cl::desc(format::StyleOptionHelpDescription), |
| 67 | cl::init("LLVM"), cl::cat(FormattingCategory)); |
Edwin Vane | 6d5350c | 2013-09-24 18:14:54 +0000 | [diff] [blame] | 68 | |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 69 | // Helper object to remove the TUReplacement files (triggered by |
| 70 | // "remove-change-desc-files" command line option) when exiting current scope. |
| 71 | class ScopedFileRemover { |
| 72 | public: |
| 73 | ScopedFileRemover(const TUReplacementFiles &Files, |
| 74 | clang::DiagnosticsEngine &Diagnostics) |
| 75 | : TURFiles(Files), Diag(Diagnostics) {} |
| 76 | |
| 77 | ~ScopedFileRemover() { |
| 78 | deleteReplacementFiles(TURFiles, Diag); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | const TUReplacementFiles &TURFiles; |
| 83 | clang::DiagnosticsEngine &Diag; |
| 84 | }; |
| 85 | |
Edwin Vane | 6d5350c | 2013-09-24 18:14:54 +0000 | [diff] [blame] | 86 | void printVersion() { |
| 87 | outs() << "clang-apply-replacements version " CLANG_VERSION_STRING << "\n"; |
| 88 | } |
| 89 | |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 90 | /// \brief Convenience function to get rewritten content for \c Filename from |
| 91 | /// \c Rewrites. |
| 92 | /// |
| 93 | /// \pre Replacements[i].getFilePath() == Replacements[i+1].getFilePath(). |
| 94 | /// \post Replacements.empty() -> Result.empty() |
| 95 | /// |
| 96 | /// \param[in] Replacements Replacements to apply |
| 97 | /// \param[in] Rewrites Rewriter to use to apply replacements. |
| 98 | /// \param[out] Result Contents of the file after applying replacements if |
| 99 | /// replacements were provided. |
| 100 | /// |
| 101 | /// \returns \li true if all replacements were applied successfully. |
| 102 | /// \li false if at least one replacement failed to apply. |
| 103 | static bool |
| 104 | getRewrittenData(const std::vector<tooling::Replacement> &Replacements, |
| 105 | Rewriter &Rewrites, std::string &Result) { |
| 106 | if (Replacements.empty()) return true; |
| 107 | |
| 108 | if (!tooling::applyAllReplacements(Replacements, Rewrites)) |
| 109 | return false; |
| 110 | |
| 111 | SourceManager &SM = Rewrites.getSourceMgr(); |
| 112 | FileManager &Files = SM.getFileManager(); |
| 113 | |
| 114 | StringRef FileName = Replacements.begin()->getFilePath(); |
| 115 | const clang::FileEntry *Entry = Files.getFile(FileName); |
| 116 | assert(Entry && "Expected an existing file"); |
| 117 | FileID ID = SM.translateFile(Entry); |
| 118 | assert(!ID.isInvalid() && "Expected a valid FileID"); |
| 119 | const RewriteBuffer *Buffer = Rewrites.getRewriteBufferFor(ID); |
| 120 | Result = std::string(Buffer->begin(), Buffer->end()); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /// \brief Apply \c Replacements and return the new file contents. |
| 126 | /// |
| 127 | /// \pre Replacements[i].getFilePath() == Replacements[i+1].getFilePath(). |
| 128 | /// \post Replacements.empty() -> Result.empty() |
| 129 | /// |
| 130 | /// \param[in] Replacements Replacements to apply. |
| 131 | /// \param[out] Result Contents of the file after applying replacements if |
| 132 | /// replacements were provided. |
| 133 | /// \param[in] Diagnostics For diagnostic output. |
| 134 | /// |
| 135 | /// \returns \li true if all replacements applied successfully. |
| 136 | /// \li false if at least one replacement failed to apply. |
| 137 | bool applyReplacements(const std::vector<tooling::Replacement> &Replacements, |
| 138 | std::string &Result, |
| 139 | DiagnosticsEngine &Diagnostics) { |
| 140 | FileManager Files((FileSystemOptions())); |
| 141 | SourceManager SM(Diagnostics, Files); |
| 142 | Rewriter Rewrites(SM, LangOptions()); |
| 143 | |
| 144 | return getRewrittenData(Replacements, Rewrites, Result); |
| 145 | } |
| 146 | |
| 147 | /// \brief Apply code formatting to all places where replacements were made. |
| 148 | /// |
| 149 | /// \pre !Replacements.empty(). |
| 150 | /// \pre Replacements[i].getFilePath() == Replacements[i+1].getFilePath(). |
| 151 | /// \pre Replacements[i].getOffset() <= Replacements[i+1].getOffset(). |
| 152 | /// |
| 153 | /// \param[in] Replacements Replacements that were made to the file. Provided |
| 154 | /// to indicate where changes were made. |
| 155 | /// \param[in] FileData The contents of the file \b after \c Replacements have |
| 156 | /// been applied. |
| 157 | /// \param[out] FormattedFileData The contents of the file after reformatting. |
Edwin Vane | 2fb10d1 | 2013-10-01 13:21:39 +0000 | [diff] [blame] | 158 | /// \param[in] FormatStyle Style to apply. |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 159 | /// \param[in] Diagnostics For diagnostic output. |
| 160 | /// |
| 161 | /// \returns \li true if reformatting replacements were all successfully |
| 162 | /// applied. |
| 163 | /// \li false if at least one reformatting replacement failed to apply. |
| 164 | bool applyFormatting(const std::vector<tooling::Replacement> &Replacements, |
| 165 | const StringRef FileData, |
| 166 | std::string &FormattedFileData, |
| 167 | const format::FormatStyle &FormatStyle, |
| 168 | DiagnosticsEngine &Diagnostics) { |
| 169 | assert(!Replacements.empty() && "Need at least one replacement"); |
| 170 | |
| 171 | RangeVector Ranges = calculateChangedRanges(Replacements); |
| 172 | |
| 173 | StringRef FileName = Replacements.begin()->getFilePath(); |
| 174 | tooling::Replacements R = |
| 175 | format::reformat(FormatStyle, FileData, Ranges, FileName); |
| 176 | |
| 177 | // FIXME: Remove this copy when tooling::Replacements is implemented as a |
| 178 | // vector instead of a set. |
| 179 | std::vector<tooling::Replacement> FormattingReplacements; |
| 180 | std::copy(R.begin(), R.end(), back_inserter(FormattingReplacements)); |
| 181 | |
| 182 | if (FormattingReplacements.empty()) { |
| 183 | FormattedFileData = FileData; |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | FileManager Files((FileSystemOptions())); |
| 188 | SourceManager SM(Diagnostics, Files); |
David Blaikie | 715c98a | 2014-08-27 20:54:50 +0000 | [diff] [blame] | 189 | SM.overrideFileContents(Files.getFile(FileName), |
| 190 | llvm::MemoryBuffer::getMemBufferCopy(FileData)); |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 191 | Rewriter Rewrites(SM, LangOptions()); |
| 192 | |
| 193 | return getRewrittenData(FormattingReplacements, Rewrites, FormattedFileData); |
| 194 | } |
| 195 | |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 196 | int main(int argc, char **argv) { |
Chris Bieneman | fc4fd52 | 2015-01-28 22:45:26 +0000 | [diff] [blame] | 197 | cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories)); |
Edwin Vane | 6d5350c | 2013-09-24 18:14:54 +0000 | [diff] [blame] | 198 | |
| 199 | cl::SetVersionPrinter(&printVersion); |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 200 | cl::ParseCommandLineOptions(argc, argv); |
| 201 | |
| 202 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions()); |
| 203 | DiagnosticsEngine Diagnostics( |
| 204 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), |
Alp Toker | 573583e | 2014-07-05 03:04:33 +0000 | [diff] [blame] | 205 | DiagOpts.get()); |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 206 | |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 207 | // Determine a formatting style from options. |
| 208 | format::FormatStyle FormatStyle; |
| 209 | if (DoFormat) |
Alexander Kornienko | 3daaf32 | 2013-12-02 15:22:30 +0000 | [diff] [blame] | 210 | FormatStyle = format::getStyle(FormatStyleOpt, FormatStyleConfig, "LLVM"); |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 211 | |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 212 | TUReplacements TUs; |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 213 | TUReplacementFiles TURFiles; |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 214 | |
Rafael Espindola | c7f0d23 | 2014-06-12 22:08:48 +0000 | [diff] [blame] | 215 | std::error_code ErrorCode = |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 216 | collectReplacementsFromDirectory(Directory, TUs, TURFiles, Diagnostics); |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 217 | |
| 218 | if (ErrorCode) { |
| 219 | errs() << "Trouble iterating over directory '" << Directory |
| 220 | << "': " << ErrorCode.message() << "\n"; |
Edwin Vane | c0f00b7 | 2013-10-05 12:15:58 +0000 | [diff] [blame] | 221 | return 1; |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 224 | // Remove the TUReplacementFiles (triggered by "remove-change-desc-files" |
| 225 | // command line option) when exiting main(). |
Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 226 | std::unique_ptr<ScopedFileRemover> Remover; |
Tareq A. Siraj | 1175552 | 2013-08-26 19:58:59 +0000 | [diff] [blame] | 227 | if (RemoveTUReplacementFiles) |
| 228 | Remover.reset(new ScopedFileRemover(TURFiles, Diagnostics)); |
| 229 | |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 230 | FileManager Files((FileSystemOptions())); |
| 231 | SourceManager SM(Diagnostics, Files); |
| 232 | |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 233 | FileToReplacementsMap GroupedReplacements; |
Edwin Vane | 1106ef2 | 2013-08-22 13:40:32 +0000 | [diff] [blame] | 234 | if (!mergeAndDeduplicate(TUs, GroupedReplacements, SM)) |
| 235 | return 1; |
| 236 | |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 237 | Rewriter ReplacementsRewriter(SM, LangOptions()); |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame] | 238 | |
Benjamin Kramer | ab7cefc | 2014-09-09 13:53:51 +0000 | [diff] [blame] | 239 | for (const auto &FileAndReplacements : GroupedReplacements) { |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 240 | // This shouldn't happen but if a file somehow has no replacements skip to |
| 241 | // next file. |
Benjamin Kramer | ab7cefc | 2014-09-09 13:53:51 +0000 | [diff] [blame] | 242 | if (FileAndReplacements.second.empty()) |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 243 | continue; |
| 244 | |
Benjamin Kramer | ab7cefc | 2014-09-09 13:53:51 +0000 | [diff] [blame] | 245 | std::string NewFileData; |
| 246 | const char *FileName = FileAndReplacements.first->getName(); |
| 247 | if (!applyReplacements(FileAndReplacements.second, NewFileData, |
| 248 | Diagnostics)) { |
| 249 | errs() << "Failed to apply replacements to " << FileName << "\n"; |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | |
| 253 | // Apply formatting if requested. |
Benjamin Kramer | ab7cefc | 2014-09-09 13:53:51 +0000 | [diff] [blame] | 254 | if (DoFormat && |
| 255 | !applyFormatting(FileAndReplacements.second, NewFileData, NewFileData, |
| 256 | FormatStyle, Diagnostics)) { |
| 257 | errs() << "Failed to apply reformatting replacements for " << FileName |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 258 | << "\n"; |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | // Write new file to disk |
Rafael Espindola | b14bd53 | 2014-08-25 18:17:00 +0000 | [diff] [blame] | 263 | std::error_code EC; |
Nikola Smiljanic | 76c21b5 | 2014-12-09 02:57:56 +0000 | [diff] [blame] | 264 | llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_None); |
Rafael Espindola | b14bd53 | 2014-08-25 18:17:00 +0000 | [diff] [blame] | 265 | if (EC) { |
Benjamin Kramer | ab7cefc | 2014-09-09 13:53:51 +0000 | [diff] [blame] | 266 | llvm::errs() << "Could not open " << FileName << " for writing\n"; |
Edwin Vane | 59d93af | 2013-09-30 13:59:21 +0000 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | |
| 270 | FileStream << NewFileData; |
| 271 | } |
Edwin Vane | f18633c | 2013-08-28 17:19:10 +0000 | [diff] [blame] | 272 | |
| 273 | return 0; |
Edwin Vane | 3bf14ce | 2013-08-22 13:07:14 +0000 | [diff] [blame] | 274 | } |