17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/DiagnosticOptions.h" 19 #include "clang/Basic/SourceManager.h" 20 #include "clang/Basic/Version.h" 21 #include "clang/Format/Format.h" 22 #include "clang/Rewrite/Core/Rewriter.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/Support/CommandLine.h" 28 using namespace clang;
31 static cl::opt<std::string>
Directory(cl::Positional, cl::Required,
32 cl::desc(
"<Search Root Directory>"));
41 "remove-change-desc-files",
42 cl::desc(
"Remove the change description files regardless of successful\n" 43 "merging/replacing."),
48 cl::desc(
"Enable formatting of code changed by applying replacements.\n" 49 "Use -style to choose formatting style.\n"),
50 cl::cat(FormattingCategory));
59 cl::desc(
"Path to a directory containing a .clang-format file\n" 60 "describing a formatting style to use for formatting\n" 61 "code when -style=file.\n"),
62 cl::init(
""), cl::cat(FormattingCategory));
64 static cl::opt<std::string>
65 FormatStyleOpt(
"style", cl::desc(format::StyleOptionHelpDescription),
66 cl::init(
"LLVM"), cl::cat(FormattingCategory));
71 class ScopedFileRemover {
74 clang::DiagnosticsEngine &Diagnostics)
75 : TURFiles(Files), Diag(Diagnostics) {}
81 clang::DiagnosticsEngine &Diag;
86 OS <<
"clang-apply-replacements version " CLANG_VERSION_STRING <<
"\n";
105 Rewriter &Rewrites, std::string &Result) {
106 if (Replacements.empty())
112 SourceManager &SM = Rewrites.getSourceMgr();
113 FileManager &Files = SM.getFileManager();
115 StringRef FileName = Replacements.begin()->getFilePath();
116 const clang::FileEntry *
Entry = Files.getFile(FileName);
117 assert(Entry &&
"Expected an existing file");
118 FileID ID = SM.translateFile(Entry);
119 assert(ID.isValid() &&
"Expected a valid FileID");
120 const RewriteBuffer *Buffer = Rewrites.getRewriteBufferFor(ID);
121 Result = std::string(Buffer->begin(), Buffer->end());
141 std::string &Result, DiagnosticsEngine &Diagnostics) {
142 FileManager Files((FileSystemOptions()));
143 SourceManager SM(Diagnostics, Files);
144 Rewriter Rewrites(SM, LangOptions());
169 const StringRef FileData, std::string &FormattedFileData,
171 DiagnosticsEngine &Diagnostics) {
172 assert(!Replacements.empty() &&
"Need at least one replacement");
176 StringRef FileName = Replacements.begin()->getFilePath();
177 tooling::Replacements R =
178 format::reformat(FormatStyle, FileData, Ranges, FileName);
182 std::vector<tooling::Replacement> FormattingReplacements;
183 std::copy(R.begin(), R.end(), back_inserter(FormattingReplacements));
185 if (FormattingReplacements.empty()) {
186 FormattedFileData = FileData;
190 FileManager Files((FileSystemOptions()));
191 SourceManager SM(Diagnostics, Files);
192 SM.overrideFileContents(Files.getFile(FileName),
193 llvm::MemoryBuffer::getMemBufferCopy(FileData));
194 Rewriter Rewrites(SM, LangOptions());
196 return getRewrittenData(FormattingReplacements, Rewrites, FormattedFileData);
199 int main(
int argc,
char **argv) {
200 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
203 cl::ParseCommandLineOptions(argc, argv);
205 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
206 DiagnosticsEngine Diagnostics(
207 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), DiagOpts.get());
212 auto FormatStyleOrError =
213 format::getStyle(FormatStyleOpt, FormatStyleConfig,
"LLVM");
214 if (!FormatStyleOrError) {
215 llvm::errs() <<
llvm::toString(FormatStyleOrError.takeError()) <<
"\n";
218 FormatStyle = *FormatStyleOrError;
224 std::error_code ErrorCode =
233 errs() <<
"Trouble iterating over directory '" <<
Directory 234 <<
"': " << ErrorCode.message() <<
"\n";
240 std::unique_ptr<ScopedFileRemover> Remover;
241 if (RemoveTUReplacementFiles)
242 Remover.reset(
new ScopedFileRemover(TUFiles, Diagnostics));
244 FileManager Files((FileSystemOptions()));
245 SourceManager SM(Diagnostics, Files);
253 Rewriter ReplacementsRewriter(SM, LangOptions());
255 for (
const auto &FileAndReplacements : GroupedReplacements) {
258 if (FileAndReplacements.second.empty())
261 std::string NewFileData;
262 StringRef FileName = FileAndReplacements.first->getName();
265 errs() <<
"Failed to apply replacements to " << FileName <<
"\n";
272 FormatStyle, Diagnostics)) {
273 errs() <<
"Failed to apply reformatting replacements for " << FileName
280 llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_None);
282 llvm::errs() <<
"Could not open " << FileName <<
" for writing\n";
286 FileStream << NewFileData;
llvm::DenseMap< const clang::FileEntry *, std::vector< clang::tooling::Replacement > > FileToReplacementsMap
Map mapping file name to Replacements targeting that file.
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
static bool applyFormatting(const std::vector< tooling::Replacement > &Replacements, const StringRef FileData, std::string &FormattedFileData, const format::FormatStyle &FormatStyle, DiagnosticsEngine &Diagnostics)
Apply code formatting to all places where replacements were made.
static cl::OptionCategory FormattingCategory("Formatting Options")
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static cl::opt< bool > RemoveTUReplacementFiles("remove-change-desc-files", cl::desc("Remove the change description files regardless of successful\ "merging/replacing."), cl::init(false), cl::cat(ReplacementCategory))
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
static void printVersion(raw_ostream &OS)
std::vector< clang::tooling::Range > RangeVector
Collection of source ranges.
RangeVector calculateChangedRanges(const std::vector< clang::tooling::Replacement > &Replacements)
Given a collection of Replacements for a single file, produces a list of source ranges that enclose t...
static cl::OptionCategory ReplacementCategory("Replacement Options")
static cl::opt< std::string > FormatStyleOpt("style", cl::desc(format::StyleOptionHelpDescription), cl::init("LLVM"), cl::cat(FormattingCategory))
static cl::opt< bool > DoFormat("format", cl::desc("Enable formatting of code changed by applying replacements.\ "Use -style to choose formatting style.\"), cl::cat(FormattingCategory))
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TUReplacements &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics)
Recursively descends through a directory structure rooted at Directory and attempts to deserialize *...
const cl::OptionCategory * VisibleCategories[]
static bool getRewrittenData(const std::vector< tooling::Replacement > &Replacements, Rewriter &Rewrites, std::string &Result)
Convenience function to get rewritten content for Filename from Rewrites.
int main(int argc, char **argv)
This file provides the interface for deduplicating, detecting conflicts in, and applying collections ...
std::vector< clang::tooling::TranslationUnitDiagnostics > TUDiagnostics
Collection of TranslationUniDiagnostics.
static cl::opt< std::string > FormatStyleConfig("style-config", cl::desc("Path to a directory containing a .clang-format file\ "describing a formatting style to use for formatting\" "code when -style=file.\"), cl::init(""), cl::cat(FormattingCategory))
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
bool applyAllReplacements(const std::vector< tooling::Replacement > &Replaces, Rewriter &Rewrite)
bool mergeAndDeduplicate(const TUReplacements &TUs, FileToReplacementsMap &GroupedReplacements, clang::SourceManager &SM)
Deduplicate, check for conflicts, and apply all Replacements stored in TUs.
static bool applyReplacements(const std::vector< tooling::Replacement > &Replacements, std::string &Result, DiagnosticsEngine &Diagnostics)
Apply Replacements and return the new file contents.
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"(
Style for formatting code around applied fixes:
- 'none' (default) turns off formatting
- 'file' (literally 'file', not a placeholder)
uses .clang-format file in the closest parent
directory
- '{ <json> }' specifies options inline, e.g.
-format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
- 'llvm', 'google', 'webkit', 'mozilla'
See clang-format documentation for the up-to-date
information about formatting styles and options.
This option overrides the 'FormatStyle` option in
.clang-tidy file, if any.
)"), cl::init("none"), cl::cat(ClangTidyCategory))