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"
30static cl::opt<std::string>
Directory(cl::Positional, cl::Required,
31 cl::desc(
"<Search Root Directory>"));
40 "remove-change-desc-files",
41 cl::desc(
"Remove the change description files regardless of successful\n"
42 "merging/replacing."),
46 "ignore-insert-conflict",
47 cl::desc(
"Ignore insert conflict and keep running to fix."),
52 cl::desc(
"Enable formatting of code changed by applying replacements.\n"
53 "Use -style to choose formatting style.\n"),
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"),
68static cl::opt<std::string>
75class ScopedFileRemover {
85 clang::DiagnosticsEngine &Diag;
90 OS <<
"clang-apply-replacements version " CLANG_VERSION_STRING <<
"\n";
93int main(
int argc,
char **argv) {
97 cl::ParseCommandLineOptions(argc, argv);
99 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
new DiagnosticOptions());
101 IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs()), DiagOpts.get());
105 format::DefaultFallbackStyle);
106 if (!FormatStyleOrError) {
107 llvm::errs() << llvm::toString(FormatStyleOrError.takeError()) <<
"\n";
110 format::FormatStyle
FormatStyle = std::move(*FormatStyleOrError);
115 std::error_code ErrorCode =
124 errs() <<
"Trouble iterating over directory '" <<
Directory
125 <<
"': " << ErrorCode.message() <<
"\n";
131 std::unique_ptr<ScopedFileRemover> Remover;
133 Remover.reset(
new ScopedFileRemover(TUFiles,
Diagnostics));
135 FileManager Files((FileSystemOptions()));
142 tooling::ApplyChangesSpec Spec;
144 Spec.Format =
DoFormat ? tooling::ApplyChangesSpec::kAll
145 : tooling::ApplyChangesSpec::kNone;
148 for (
const auto &FileChange :
Changes) {
149 FileEntryRef
Entry = FileChange.first;
151 llvm::Expected<std::string> NewFileData =
154 errs() << llvm::toString(NewFileData.takeError()) <<
"\n";
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;
This file provides the interface for deduplicating, detecting conflicts in, and applying collections ...
static cl::OptionCategory FormattingCategory("Formatting Options")
const cl::OptionCategory * VisibleCategories[]
static cl::opt< std::string > FormatStyleOpt("style", cl::desc(format::StyleOptionHelpDescription), cl::init("LLVM"), cl::cat(FormattingCategory))
int main(int argc, char **argv)
static cl::opt< bool > DoFormat("format", cl::desc("Enable formatting of code changed by applying replacements.\n" "Use -style to choose formatting style.\n"), cl::cat(FormattingCategory))
static cl::opt< std::string > FormatStyleConfig("style-config", cl::desc("Path to a directory containing a .clang-format file\n" "describing a formatting style to use for formatting\n" "code when -style=file.\n"), cl::init(""), cl::cat(FormattingCategory))
static void printVersion(raw_ostream &OS)
static cl::opt< bool > IgnoreInsertConflict("ignore-insert-conflict", cl::desc("Ignore insert conflict and keep running to fix."), cl::init(false), cl::cat(ReplacementCategory))
static cl::OptionCategory ReplacementCategory("Replacement Options")
static cl::opt< bool > RemoveTUReplacementFiles("remove-change-desc-files", cl::desc("Remove the change description files regardless of successful\n" "merging/replacing."), cl::init(false), cl::cat(ReplacementCategory))
static cl::opt< std::string > FormatStyle("format-style", 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))
llvm::StringRef Directory
WantDiagnostics Diagnostics
llvm::DenseMap< clang::FileEntryRef, std::vector< tooling::AtomicChange > > FileToChangesMap
Map mapping file name to a set of AtomicChange targeting that file.
bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs, FileToChangesMap &FileChanges, clang::SourceManager &SM, bool IgnoreInsertConflict=false)
Deduplicate, check for conflicts, and extract all Replacements stored in TUs.
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TranslationUnits &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics)=delete
Recursively descends through a directory structure rooted at Directory and attempts to deserialize *....
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
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.
std::vector< clang::tooling::TranslationUnitDiagnostics > TUDiagnostics
Collection of TranslationUniDiagnostics.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.