[clang-tidy] Make format style customizable

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
This commit is contained in:
Jonas Devlieghere 2016-11-30 18:06:42 +00:00
parent 30ed5467a4
commit 7319184946
4 changed files with 24 additions and 12 deletions

View File

@ -88,13 +88,13 @@ private:
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);
}
@ -196,8 +196,7 @@ public:
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);
@ -248,6 +247,7 @@ private:
unsigned TotalFixes;
unsigned AppliedFixes;
unsigned WarningsAsErrors;
StringRef FormatStyle;
};
class ClangTidyASTConsumer : public MultiplexConsumer {
@ -538,8 +538,8 @@ runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
}
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();

View File

@ -235,9 +235,10 @@ runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
// 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.

View File

@ -49,9 +49,9 @@ Configuration files:
)");
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 '-'
@ -120,6 +120,13 @@ well.
)"),
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.
@ -386,7 +393,8 @@ static int clangTidyMain(int argc, const char **argv) {
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;

View File

@ -179,6 +179,9 @@ An overview of all the command-line options:
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