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";
89 int main(
int argc,
char **argv) {
90 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
93 cl::ParseCommandLineOptions(argc, argv);
95 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
96 DiagnosticsEngine Diagnostics(
97 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), DiagOpts.get());
100 auto FormatStyleOrError = format::getStyle(FormatStyleOpt, FormatStyleConfig,
101 format::DefaultFallbackStyle);
102 if (!FormatStyleOrError) {
103 llvm::errs() <<
llvm::toString(FormatStyleOrError.takeError()) <<
"\n";
120 errs() <<
"Trouble iterating over directory '" <<
Directory 121 <<
"': " << ErrorCode.message() <<
"\n";
127 std::unique_ptr<ScopedFileRemover> Remover;
128 if (RemoveTUReplacementFiles)
129 Remover.reset(
new ScopedFileRemover(TUFiles, Diagnostics));
131 FileManager Files((FileSystemOptions()));
132 SourceManager SM(Diagnostics, Files);
138 tooling::ApplyChangesSpec Spec;
141 Spec.Format = DoFormat ? tooling::ApplyChangesSpec::kAll
142 : tooling::ApplyChangesSpec::kNone;
144 for (
const auto &FileChange : Changes) {
145 const FileEntry *
Entry = FileChange.first;
146 StringRef
FileName = Entry->getName();
147 llvm::Expected<std::string> NewFileData =
148 applyChanges(FileName, FileChange.second, Spec, Diagnostics);
156 llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::F_None);
158 llvm::errs() <<
"Could not open " << FileName <<
" for writing\n";
161 FileStream << *NewFileData;
Some operations such as code completion produce a set of candidates.
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
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)
llvm::Expected< std::string > applyChanges(StringRef File, const std::vector< tooling::AtomicChange > &Changes, const tooling::ApplyChangesSpec &Spec, DiagnosticsEngine &Diagnostics)
Apply AtomicChange on File and rewrite it.
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[]
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs, FileToChangesMap &FileChanges, clang::SourceManager &SM)
Deduplicate, check for conflicts, and extract all Replacements stored in TUs.
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.
llvm::DenseMap< const clang::FileEntry *, std::vector< tooling::AtomicChange > > FileToChangesMap
Map mapping file name to a set of AtomicChange targeting that file.
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))