Index: .gitattributes =================================================================== --- /dev/null +++ .gitattributes @@ -0,0 +1 @@ +*.png binary Index: clang-tidy/ClangTidy.h =================================================================== --- clang-tidy/ClangTidy.h +++ clang-tidy/ClangTidy.h @@ -224,12 +224,10 @@ /// /// \param Profile if provided, it enables check profile collection in /// MatchFinder, and will contain the result of the profile. -ClangTidyStats -runClangTidy(std::unique_ptr OptionsProvider, - const tooling::CompilationDatabase &Compilations, - ArrayRef InputFiles, - std::vector *Errors, - ProfileData *Profile = nullptr); +void runClangTidy(clang::tidy::ClangTidyContext &Context, + const tooling::CompilationDatabase &Compilations, + ArrayRef 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 @@ /// \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 &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. Index: clang-tidy/ClangTidy.cpp =================================================================== --- clang-tidy/ClangTidy.cpp +++ clang-tidy/ClangTidy.cpp @@ -89,13 +89,13 @@ 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(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 @@ 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 @@ DiagnosticsEngine Diags; SourceManager SourceMgr; llvm::StringMap FileReplacements; + ClangTidyContext &Context; bool ApplyFixes; unsigned TotalFixes; unsigned AppliedFixes; unsigned WarningsAsErrors; - StringRef FormatStyle; }; class ClangTidyASTConsumer : public MultiplexConsumer { @@ -471,13 +472,10 @@ return Factory.getCheckOptions(); } -ClangTidyStats -runClangTidy(std::unique_ptr OptionsProvider, - const CompilationDatabase &Compilations, - ArrayRef InputFiles, - std::vector *Errors, ProfileData *Profile) { +void runClangTidy(clang::tidy::ClangTidyContext &Context, + const CompilationDatabase &Compilations, + ArrayRef 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 @@ ActionFactory Factory(Context); Tool.run(&Factory); - *Errors = Context.getErrors(); - return Context.getStats(); } -void handleErrors(const std::vector &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. Index: clang-tidy/ClangTidyDiagnosticConsumer.h =================================================================== --- clang-tidy/ClangTidyDiagnosticConsumer.h +++ clang-tidy/ClangTidyDiagnosticConsumer.h @@ -162,7 +162,7 @@ const ClangTidyStats &getStats() const { return Stats; } /// \brief Returns all collected errors. - const std::vector &getErrors() const { return Errors; } + ArrayRef getErrors() const { return Errors; } /// \brief Clears collected errors. void clearErrors() { Errors.clear(); } Index: clang-tidy/ClangTidyOptions.h =================================================================== --- clang-tidy/ClangTidyOptions.h +++ clang-tidy/ClangTidyOptions.h @@ -75,6 +75,20 @@ /// \brief Turns on temporary destructor-based analysis. llvm::Optional 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 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() Index: clang-tidy/ClangTidyOptions.cpp =================================================================== --- clang-tidy/ClangTidyOptions.cpp +++ clang-tidy/ClangTidyOptions.cpp @@ -89,6 +89,7 @@ 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 @@ 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 @@ 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); Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -35,12 +35,13 @@ option, command-line option takes precedence. The effective configuration can be inspected using -dump-config: - $ clang-tidy -dump-config - -- + $ clang-tidy -dump-config --- Checks: '-*,some-check' WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false + FormatStyle: none User: user CheckOptions: - key: some-check.SomeOption @@ -131,6 +132,8 @@ - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. +This option overrides the 'FormatStyle` option in +.clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory)); @@ -289,6 +292,7 @@ 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 +309,8 @@ OverrideOptions.SystemHeaders = SystemHeaders; if (AnalyzeTemporaryDtors.getNumOccurrences() > 0) OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors; + if (FormatStyle.getNumOccurrences() > 0) + OverrideOptions.FormatStyle = FormatStyle; if (!Config.empty()) { if (llvm::ErrorOr ParsedConfig = @@ -327,7 +333,8 @@ CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory, cl::ZeroOrMore); - auto OptionsProvider = createOptionsProvider(); + auto OwningOptionsProvider = createOptionsProvider(); + auto *OptionsProvider = OwningOptionsProvider.get(); if (!OptionsProvider) return 1; @@ -396,10 +403,10 @@ ProfileData Profile; - std::vector 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 Errors = Context.getErrors(); bool FoundErrors = std::find_if(Errors.begin(), Errors.end(), [](const ClangTidyError &E) { return E.DiagLevel == ClangTidyError::Error; @@ -410,8 +417,7 @@ 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 +430,7 @@ } if (!Quiet) { - printStats(Stats); + printStats(Context.getStats()); if (DisableFixes) llvm::errs() << "Found compiler errors, but -fix-errors was not specified.\n" Index: docs/clang-tidy/index.rst =================================================================== --- docs/clang-tidy/index.rst +++ docs/clang-tidy/index.rst @@ -169,6 +169,8 @@ - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. + This option overrides the 'FormatStyle` option in + .clang-tidy file, if any. -header-filter= - Regular expression matching the names of the headers to output diagnostics from. Diagnostics @@ -195,9 +197,6 @@ printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. - -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 @@ -233,12 +232,13 @@ option, command-line option takes precedence. The effective configuration can be inspected using -dump-config: - $ clang-tidy -dump-config - -- + $ clang-tidy -dump-config --- Checks: '-*,some-check' WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false + FormatStyle: none User: user CheckOptions: - key: some-check.SomeOption Index: test/clang-tidy/misc-forwarding-reference-overload.cpp =================================================================== --- test/clang-tidy/misc-forwarding-reference-overload.cpp +++ test/clang-tidy/misc-forwarding-reference-overload.cpp @@ -1,8 +1,8 @@ -// RUN: %check_clang_tidy %s misc-forwarding-reference-overload %t +// RUN: %check_clang_tidy %s misc-forwarding-reference-overload %t -- -- -std=c++14 namespace std { template -struct enable_if {}; +struct enable_if { typedef T type; }; template struct enable_if { typedef T type; };