1 //===-- ClangApplyReplacementsMain.cpp - Main file for the tool -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file provides the main function for the
11 /// clang-apply-replacements tool.
13 //===----------------------------------------------------------------------===//
15 #include "clang-apply-replacements/Tooling/ApplyReplacements.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/Version.h"
20 #include "clang/Format/Format.h"
21 #include "clang/Rewrite/Core/Rewriter.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/CommandLine.h"
27 using namespace clang
;
28 using namespace clang::replace
;
30 static cl::opt
<std::string
> Directory(cl::Positional
, cl::Required
,
31 cl::desc("<Search Root Directory>"));
33 static cl::OptionCategory
ReplacementCategory("Replacement Options");
34 static cl::OptionCategory
FormattingCategory("Formatting Options");
36 const cl::OptionCategory
*VisibleCategories
[] = {&ReplacementCategory
,
39 static cl::opt
<bool> RemoveTUReplacementFiles(
40 "remove-change-desc-files",
41 cl::desc("Remove the change description files regardless of successful\n"
42 "merging/replacing."),
43 cl::init(false), cl::cat(ReplacementCategory
));
45 static cl::opt
<bool> IgnoreInsertConflict(
46 "ignore-insert-conflict",
47 cl::desc("Ignore insert conflict and keep running to fix."),
48 cl::init(false), cl::cat(ReplacementCategory
));
50 static cl::opt
<bool> DoFormat(
52 cl::desc("Enable formatting of code changed by applying replacements.\n"
53 "Use -style to choose formatting style.\n"),
54 cl::cat(FormattingCategory
));
56 // FIXME: Consider making the default behaviour for finding a style
57 // configuration file to start the search anew for every file being changed to
58 // handle situations where the style is different for different parts of a
61 static cl::opt
<std::string
> FormatStyleConfig(
63 cl::desc("Path to a directory containing a .clang-format file\n"
64 "describing a formatting style to use for formatting\n"
65 "code when -style=file.\n"),
66 cl::init(""), cl::cat(FormattingCategory
));
68 static cl::opt
<std::string
>
69 FormatStyleOpt("style", cl::desc(format::StyleOptionHelpDescription
),
70 cl::init("LLVM"), cl::cat(FormattingCategory
));
73 // Helper object to remove the TUReplacement and TUDiagnostic (triggered by
74 // "remove-change-desc-files" command line option) when exiting current scope.
75 class ScopedFileRemover
{
77 ScopedFileRemover(const TUReplacementFiles
&Files
,
78 clang::DiagnosticsEngine
&Diagnostics
)
79 : TURFiles(Files
), Diag(Diagnostics
) {}
81 ~ScopedFileRemover() { deleteReplacementFiles(TURFiles
, Diag
); }
84 const TUReplacementFiles
&TURFiles
;
85 clang::DiagnosticsEngine
&Diag
;
89 static void printVersion(raw_ostream
&OS
) {
90 OS
<< "clang-apply-replacements version " CLANG_VERSION_STRING
<< "\n";
93 int main(int argc
, char **argv
) {
94 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories
));
96 cl::SetVersionPrinter(printVersion
);
97 cl::ParseCommandLineOptions(argc
, argv
);
99 IntrusiveRefCntPtr
<DiagnosticOptions
> DiagOpts(new DiagnosticOptions());
100 DiagnosticsEngine
Diagnostics(
101 IntrusiveRefCntPtr
<DiagnosticIDs
>(new DiagnosticIDs()), DiagOpts
.get());
103 // Determine a formatting style from options.
104 auto FormatStyleOrError
= format::getStyle(FormatStyleOpt
, FormatStyleConfig
,
105 format::DefaultFallbackStyle
);
106 if (!FormatStyleOrError
) {
107 llvm::errs() << llvm::toString(FormatStyleOrError
.takeError()) << "\n";
110 format::FormatStyle FormatStyle
= std::move(*FormatStyleOrError
);
113 TUReplacementFiles TUFiles
;
115 std::error_code ErrorCode
=
116 collectReplacementsFromDirectory(Directory
, TURs
, TUFiles
, Diagnostics
);
121 collectReplacementsFromDirectory(Directory
, TUDs
, TUFiles
, Diagnostics
);
124 errs() << "Trouble iterating over directory '" << Directory
125 << "': " << ErrorCode
.message() << "\n";
129 // Remove the TUReplacementFiles (triggered by "remove-change-desc-files"
130 // command line option) when exiting main().
131 std::unique_ptr
<ScopedFileRemover
> Remover
;
132 if (RemoveTUReplacementFiles
)
133 Remover
.reset(new ScopedFileRemover(TUFiles
, Diagnostics
));
135 FileManager
Files((FileSystemOptions()));
136 SourceManager
SM(Diagnostics
, Files
);
138 FileToChangesMap Changes
;
139 if (!mergeAndDeduplicate(TURs
, TUDs
, Changes
, SM
, IgnoreInsertConflict
))
142 tooling::ApplyChangesSpec Spec
;
144 Spec
.Style
= FormatStyle
;
145 Spec
.Format
= DoFormat
? tooling::ApplyChangesSpec::kAll
146 : tooling::ApplyChangesSpec::kNone
;
148 for (const auto &FileChange
: Changes
) {
149 const FileEntry
*Entry
= FileChange
.first
;
150 StringRef FileName
= Entry
->getName();
151 llvm::Expected
<std::string
> NewFileData
=
152 applyChanges(FileName
, FileChange
.second
, Spec
, Diagnostics
);
154 errs() << llvm::toString(NewFileData
.takeError()) << "\n";
158 // Write new file to disk
160 llvm::raw_fd_ostream
FileStream(FileName
, EC
, llvm::sys::fs::OF_None
);
162 llvm::errs() << "Could not open " << FileName
<< " for writing\n";
165 FileStream
<< *NewFileData
;