Summary: I came across an outstanding FIXME to make the format style customizable. Inspired by the include fixer, I added an new option `-style` to configure the fallback style in case no clang-format configuration file is found. The default remains "llvm".
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Subscribers: cfe-commits, malcolm.parsons
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D27142
llvm-svn: 288258
class ErrorReporter {
public:
- ErrorReporter(bool ApplyFixes)
+ ErrorReporter(bool ApplyFixes, StringRef FormatStyle)
: Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()),
DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
DiagPrinter),
SourceMgr(Diags, Files), ApplyFixes(ApplyFixes), TotalFixes(0),
- AppliedFixes(0), WarningsAsErrors(0) {
+ AppliedFixes(0), WarningsAsErrors(0), FormatStyle(FormatStyle) {
DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
DiagPrinter->BeginSourceFile(LangOpts);
}
continue;
}
StringRef Code = Buffer.get()->getBuffer();
- // FIXME: Make the style customizable.
- format::FormatStyle Style = format::getStyle("file", File, "LLVM");
+ format::FormatStyle Style = format::getStyle("file", File, FormatStyle);
llvm::Expected<Replacements> CleanReplacements =
format::cleanupAroundReplacements(Code, FileAndReplacements.second,
Style);
unsigned TotalFixes;
unsigned AppliedFixes;
unsigned WarningsAsErrors;
+ StringRef FormatStyle;
};
class ClangTidyASTConsumer : public MultiplexConsumer {
}
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
- unsigned &WarningsAsErrorsCount) {
- ErrorReporter Reporter(Fix);
+ StringRef FormatStyle, unsigned &WarningsAsErrorsCount) {
+ ErrorReporter Reporter(Fix, FormatStyle);
vfs::FileSystem &FileSystem =
*Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
// FIXME: Implement confidence levels for displaying/fixing errors.
//
/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
-/// Errors containing fixes are automatically applied.
+/// Errors containing fixes are automatically applied and reformatted. If no
+/// clang-format configuration file is found, the given \P FormatStyle is used.
void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
- unsigned &WarningsAsErrorsCount);
+ StringRef FormatStyle, unsigned &WarningsAsErrorsCount);
/// \brief Serializes replacements into YAML and writes them to the specified
/// output stream.
)");
-const char DefaultChecks[] = // Enable these checks by default:
- "clang-diagnostic-*," // * compiler diagnostics
- "clang-analyzer-*"; // * Static Analyzer checks
+const char DefaultChecks[] = // Enable these checks by default:
+ "clang-diagnostic-*," // * compiler diagnostics
+ "clang-analyzer-*"; // * Static Analyzer checks
static cl::opt<std::string> Checks("checks", cl::desc(R"(
Comma-separated list of globs with optional '-'
)"),
cl::init(false), cl::cat(ClangTidyCategory));
+static cl::opt<std::string> FormatStyle("style", cl::desc(R"(
+Fallback style for reformatting after inserting fixes
+if there is no clang-format config file found.
+)"),
+ cl::init("llvm"),
+ cl::cat(ClangTidyCategory));
+
static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
List all enabled checks and exit. Use with
-checks=* to list all available checks.
unsigned WErrorCount = 0;
// -fix-errors implies -fix.
- handleErrors(Errors, (FixErrors || Fix) && !DisableFixes, WErrorCount);
+ handleErrors(Errors, (FixErrors || Fix) && !DisableFixes, FormatStyle,
+ WErrorCount);
if (!ExportFixes.empty() && !Errors.empty()) {
std::error_code EC;
List all enabled checks and exit. Use with
-checks=* to list all available checks.
-p=<string> - Build path
+ -style=<string> -
+ Fallback style for reformatting after inserting fixes
+ if there is no clang-format config file found.
-system-headers - Display the errors from system headers.
-warnings-as-errors=<string> -
Upgrades warnings to errors. Same format as