Index: clang-tidy/ClangTidy.h =================================================================== --- clang-tidy/ClangTidy.h +++ clang-tidy/ClangTidy.h @@ -235,8 +235,9 @@ // 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. -void handleErrors(const std::vector &Errors, bool Fix, +/// Errors containing fixes are automatically applied and reformatted. If no +/// clang-format configuration file the given \P FormatStyle is used. +void handleErrors(const std::vector &Errors, bool Fix, StringRef FormatStyle, unsigned &WarningsAsErrorsCount); /// \brief Serializes replacements into YAML and writes them to the specified Index: clang-tidy/ClangTidy.cpp =================================================================== --- clang-tidy/ClangTidy.cpp +++ clang-tidy/ClangTidy.cpp @@ -88,13 +88,13 @@ class ErrorReporter { public: - ErrorReporter(bool ApplyFixes) + ErrorReporter(bool ApplyFixes, StringRef FormatStyle) : Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()), DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)), Diags(IntrusiveRefCntPtr(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); } @@ -196,8 +196,7 @@ 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 CleanReplacements = format::cleanupAroundReplacements(Code, FileAndReplacements.second, Style); @@ -248,6 +247,7 @@ unsigned TotalFixes; unsigned AppliedFixes; unsigned WarningsAsErrors; + StringRef FormatStyle; }; class ClangTidyASTConsumer : public MultiplexConsumer { @@ -538,8 +538,8 @@ } void handleErrors(const std::vector &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(); Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -49,9 +49,9 @@ )"); -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 Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' @@ -120,6 +120,13 @@ )"), cl::init(false), cl::cat(ClangTidyCategory)); +static cl::opt 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 ListChecks("list-checks", cl::desc(R"( List all enabled checks and exit. Use with -checks=* to list all available checks. @@ -386,7 +393,8 @@ 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; Index: docs/clang-tidy/index.rst =================================================================== --- docs/clang-tidy/index.rst +++ docs/clang-tidy/index.rst @@ -179,6 +179,9 @@ List all enabled checks and exit. Use with -checks=* to list all available checks. -p= - Build path + -style= - + 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= - Upgrades warnings to errors. Same format as