[clang-tools-extra] r299649 - [clang-tidy] Add FormatStyle configuration option.
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 6 06:41:29 PDT 2017
Author: alexfh
Date: Thu Apr 6 08:41:29 2017
New Revision: 299649
URL: http://llvm.org/viewvc/llvm-project?rev=299649&view=rev
Log:
[clang-tidy] Add FormatStyle configuration option.
Modified:
clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
clang-tools-extra/trunk/clang-tidy/ClangTidy.h
clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Thu Apr 6 08:41:29 2017
@@ -89,13 +89,13 @@ private:
class ErrorReporter {
public:
- ErrorReporter(bool ApplyFixes, StringRef FormatStyle)
+ ErrorReporter(ClangTidyContext &Context, bool ApplyFixes)
: 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), FormatStyle(FormatStyle) {
+ SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
+ TotalFixes(0), AppliedFixes(0), WarningsAsErrors(0) {
DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
DiagPrinter->BeginSourceFile(LangOpts);
}
@@ -196,7 +196,8 @@ public:
continue;
}
StringRef Code = Buffer.get()->getBuffer();
- auto Style = format::getStyle(FormatStyle, File, "none");
+ auto Style = format::getStyle(
+ *Context.getOptionsForFile(File).FormatStyle, File, "none");
if (!Style) {
llvm::errs() << llvm::toString(Style.takeError()) << "\n";
continue;
@@ -255,11 +256,11 @@ private:
DiagnosticsEngine Diags;
SourceManager SourceMgr;
llvm::StringMap<Replacements> FileReplacements;
+ ClangTidyContext &Context;
bool ApplyFixes;
unsigned TotalFixes;
unsigned AppliedFixes;
unsigned WarningsAsErrors;
- StringRef FormatStyle;
};
class ClangTidyASTConsumer : public MultiplexConsumer {
@@ -471,13 +472,10 @@ ClangTidyOptions::OptionMap getCheckOpti
return Factory.getCheckOptions();
}
-ClangTidyStats
-runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
- const CompilationDatabase &Compilations,
- ArrayRef<std::string> InputFiles,
- std::vector<ClangTidyError> *Errors, ProfileData *Profile) {
+void runClangTidy(clang::tidy::ClangTidyContext &Context,
+ const CompilationDatabase &Compilations,
+ ArrayRef<std::string> InputFiles, ProfileData *Profile) {
ClangTool Tool(Compilations, InputFiles);
- clang::tidy::ClangTidyContext Context(std::move(OptionsProvider));
// Add extra arguments passed by the clang-tidy command-line.
ArgumentsAdjuster PerFileExtraArgumentsInserter =
@@ -545,20 +543,18 @@ runClangTidy(std::unique_ptr<ClangTidyOp
ActionFactory Factory(Context);
Tool.run(&Factory);
- *Errors = Context.getErrors();
- return Context.getStats();
}
-void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix,
- StringRef FormatStyle, unsigned &WarningsAsErrorsCount) {
- ErrorReporter Reporter(Fix, FormatStyle);
+void handleErrors(ClangTidyContext &Context, bool Fix,
+ unsigned &WarningsAsErrorsCount) {
+ ErrorReporter Reporter(Context, Fix);
vfs::FileSystem &FileSystem =
*Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
if (!InitialWorkingDir)
llvm::report_fatal_error("Cannot get current working path.");
- for (const ClangTidyError &Error : Errors) {
+ for (const ClangTidyError &Error : Context.getErrors()) {
if (!Error.BuildDirectory.empty()) {
// By default, the working directory of file system is the current
// clang-tidy running directory.
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.h?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.h Thu Apr 6 08:41:29 2017
@@ -224,12 +224,10 @@ ClangTidyOptions::OptionMap getCheckOpti
///
/// \param Profile if provided, it enables check profile collection in
/// MatchFinder, and will contain the result of the profile.
-ClangTidyStats
-runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
- const tooling::CompilationDatabase &Compilations,
- ArrayRef<std::string> InputFiles,
- std::vector<ClangTidyError> *Errors,
- ProfileData *Profile = nullptr);
+void runClangTidy(clang::tidy::ClangTidyContext &Context,
+ const tooling::CompilationDatabase &Compilations,
+ ArrayRef<std::string> InputFiles,
+ ProfileData *Profile = nullptr);
// FIXME: This interface will need to be significantly extended to be useful.
// FIXME: Implement confidence levels for displaying/fixing errors.
@@ -237,8 +235,8 @@ runClangTidy(std::unique_ptr<ClangTidyOp
/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
/// 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,
- StringRef FormatStyle, unsigned &WarningsAsErrorsCount);
+void handleErrors(ClangTidyContext &Context, bool Fix,
+ unsigned &WarningsAsErrorsCount);
/// \brief Serializes replacements into YAML and writes them to the specified
/// output stream.
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h Thu Apr 6 08:41:29 2017
@@ -162,7 +162,7 @@ public:
const ClangTidyStats &getStats() const { return Stats; }
/// \brief Returns all collected errors.
- const std::vector<ClangTidyError> &getErrors() const { return Errors; }
+ ArrayRef<ClangTidyError> getErrors() const { return Errors; }
/// \brief Clears collected errors.
void clearErrors() { Errors.clear(); }
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp Thu Apr 6 08:41:29 2017
@@ -89,6 +89,7 @@ template <> struct MappingTraits<ClangTi
IO.mapOptional("WarningsAsErrors", Options.WarningsAsErrors);
IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex);
IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
+ IO.mapOptional("FormatStyle", Options.FormatStyle);
IO.mapOptional("User", Options.User);
IO.mapOptional("CheckOptions", NOpts->Options);
IO.mapOptional("ExtraArgs", Options.ExtraArgs);
@@ -109,6 +110,7 @@ ClangTidyOptions ClangTidyOptions::getDe
Options.HeaderFilterRegex = "";
Options.SystemHeaders = false;
Options.AnalyzeTemporaryDtors = false;
+ Options.FormatStyle = "none";
Options.User = llvm::None;
for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
E = ClangTidyModuleRegistry::end();
@@ -148,6 +150,7 @@ ClangTidyOptions::mergeWith(const ClangT
overrideValue(Result.HeaderFilterRegex, Other.HeaderFilterRegex);
overrideValue(Result.SystemHeaders, Other.SystemHeaders);
overrideValue(Result.AnalyzeTemporaryDtors, Other.AnalyzeTemporaryDtors);
+ overrideValue(Result.FormatStyle, Other.FormatStyle);
overrideValue(Result.User, Other.User);
mergeVectors(Result.ExtraArgs, Other.ExtraArgs);
mergeVectors(Result.ExtraArgsBefore, Other.ExtraArgsBefore);
Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h Thu Apr 6 08:41:29 2017
@@ -75,6 +75,20 @@ struct ClangTidyOptions {
/// \brief Turns on temporary destructor-based analysis.
llvm::Optional<bool> AnalyzeTemporaryDtors;
+ /// \brief Format code around applied fixes with clang-format using this
+ /// style.
+ ///
+ /// Can be one of:
+ /// * 'none' - don't format code around applied fixes;
+ /// * 'llvm', 'google', 'mozilla' or other predefined clang-format style
+ /// names;
+ /// * 'file' - use the .clang-format file in the closest parent directory of
+ /// each source file;
+ /// * '{inline-formatting-style-in-yaml-format}'.
+ ///
+ /// See clang-format documentation for more about configuring format style.
+ llvm::Optional<std::string> FormatStyle;
+
/// \brief Specifies the name or e-mail of the user running clang-tidy.
///
/// This option is used, for example, to place the correct user name in TODO()
Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=299649&r1=299648&r2=299649&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Thu Apr 6 08:41:29 2017
@@ -289,6 +289,7 @@ static std::unique_ptr<ClangTidyOptionsP
DefaultOptions.HeaderFilterRegex = HeaderFilter;
DefaultOptions.SystemHeaders = SystemHeaders;
DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
+ DefaultOptions.FormatStyle = FormatStyle;
DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
// USERNAME is used on Windows.
if (!DefaultOptions.User)
@@ -305,6 +306,8 @@ static std::unique_ptr<ClangTidyOptionsP
OverrideOptions.SystemHeaders = SystemHeaders;
if (AnalyzeTemporaryDtors.getNumOccurrences() > 0)
OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
+ if (FormatStyle.getNumOccurrences() > 0)
+ OverrideOptions.FormatStyle = FormatStyle;
if (!Config.empty()) {
if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
@@ -327,7 +330,8 @@ static int clangTidyMain(int argc, const
CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory,
cl::ZeroOrMore);
- auto OptionsProvider = createOptionsProvider();
+ auto OwningOptionsProvider = createOptionsProvider();
+ auto *OptionsProvider = OwningOptionsProvider.get();
if (!OptionsProvider)
return 1;
@@ -396,10 +400,10 @@ static int clangTidyMain(int argc, const
ProfileData Profile;
- std::vector<ClangTidyError> Errors;
- ClangTidyStats Stats =
- runClangTidy(std::move(OptionsProvider), OptionsParser.getCompilations(),
- PathList, &Errors, EnableCheckProfile ? &Profile : nullptr);
+ ClangTidyContext Context(std::move(OwningOptionsProvider));
+ runClangTidy(Context, OptionsParser.getCompilations(), PathList,
+ EnableCheckProfile ? &Profile : nullptr);
+ ArrayRef<ClangTidyError> Errors = Context.getErrors();
bool FoundErrors =
std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) {
return E.DiagLevel == ClangTidyError::Error;
@@ -410,8 +414,7 @@ static int clangTidyMain(int argc, const
unsigned WErrorCount = 0;
// -fix-errors implies -fix.
- handleErrors(Errors, (FixErrors || Fix) && !DisableFixes, FormatStyle,
- WErrorCount);
+ handleErrors(Context, (FixErrors || Fix) && !DisableFixes, WErrorCount);
if (!ExportFixes.empty() && !Errors.empty()) {
std::error_code EC;
@@ -424,7 +427,7 @@ static int clangTidyMain(int argc, const
}
if (!Quiet) {
- printStats(Stats);
+ printStats(Context.getStats());
if (DisableFixes)
llvm::errs()
<< "Found compiler errors, but -fix-errors was not specified.\n"
More information about the cfe-commits
mailing list