Index: clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp =================================================================== --- clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp +++ clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp @@ -48,6 +48,10 @@ "Use -style to choose formatting style.\n"), cl::cat(FormattingCategory)); +static cl::opt ForceOverwriteReadOnly( + "force", cl::desc("Overwrite read-only files when applying replacements\n"), + cl::init(false), cl::cat(ReplacementCategory)); + // FIXME: Consider making the default behaviour for finding a style // configuration file to start the search anew for every file being changed to // handle situations where the style is different for different parts of a @@ -152,6 +156,13 @@ // Write new file to disk std::error_code EC; + if (ForceOverwriteReadOnly) { + using namespace llvm::sys::fs; + if (auto ErrorOrPerms = getPermissions(FileName)) { + perms P = ErrorOrPerms.get(); + setPermissions(FileName, P | all_write); + } + } llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::OF_None); if (EC) { llvm::errs() << "Could not open " << FileName << " for writing\n";