summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorJonas Devlieghere <jonas@devlieghere.com>2016-11-30 18:06:42 +0000
committerJonas Devlieghere <jonas@devlieghere.com>2016-11-30 18:06:42 +0000
commit7319184946fe9e43647c67ebe3f80be43941b331 (patch)
treeac6fc13a4fe743e9a963099083a9e09e0595afee /clang-tools-extra
parent30ed5467a476e5f09efaf1a11f1d971f05f18adf (diff)
downloadbcm5719-llvm-7319184946fe9e43647c67ebe3f80be43941b331.tar.gz
bcm5719-llvm-7319184946fe9e43647c67ebe3f80be43941b331.zip
[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
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidy.cpp12
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidy.h5
-rw-r--r--clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp16
-rw-r--r--clang-tools-extra/docs/clang-tidy/index.rst3
4 files changed, 24 insertions, 12 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index a03f3fe9663..1d22f7249aa 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -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();
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h
index 41df4d7e582..f30bb209d7b 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.h
+++ b/clang-tools-extra/clang-tidy/ClangTidy.h
@@ -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.
diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 8578ef48f29..fd53c4e6ee2 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -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;
diff --git a/clang-tools-extra/docs/clang-tidy/index.rst b/clang-tools-extra/docs/clang-tidy/index.rst
index 403711a7819..264b81ebb7c 100644
--- a/clang-tools-extra/docs/clang-tidy/index.rst
+++ b/clang-tools-extra/docs/clang-tidy/index.rst
@@ -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
OpenPOWER on IntegriCloud