diff options
| author | Alexander Kornienko <alexfh@google.com> | 2017-04-06 13:41:29 +0000 | 
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2017-04-06 13:41:29 +0000 | 
| commit | 2561320f80bae9ef2ee4548001ce5b41c8396324 (patch) | |
| tree | 650dbf488b9ee25acc45b1a1cc70988280dc3739 | |
| parent | a9832134597af6f5104806385445cdeb1f9901ed (diff) | |
| download | bcm5719-llvm-2561320f80bae9ef2ee4548001ce5b41c8396324.tar.gz bcm5719-llvm-2561320f80bae9ef2ee4548001ce5b41c8396324.zip | |
[clang-tidy] Add FormatStyle configuration option.
llvm-svn: 299649
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidy.cpp | 30 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidy.h | 14 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h | 2 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidyOptions.cpp | 3 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidyOptions.h | 14 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | 19 | 
6 files changed, 48 insertions, 34 deletions
| diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp index 2da9e142957..d8f2212d7b8 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -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 getCheckOptions(const ClangTidyOptions &Options) {    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<ClangTidyOptionsProvider> OptionsProvider,    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. diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h index 017864879df..8d4d6c95a5a 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.h +++ b/clang-tools-extra/clang-tidy/ClangTidy.h @@ -224,12 +224,10 @@ ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options);  ///  /// \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<ClangTidyOptionsProvider> OptionsProvider,  /// \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. diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h index eb831a180fc..728347cb238 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -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(); } diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp index 8d7665e115d..83f57e5a6bf 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp @@ -89,6 +89,7 @@ template <> struct MappingTraits<ClangTidyOptions> {      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::getDefaults() {    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 ClangTidyOptions &Other) const {    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); diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.h b/clang-tools-extra/clang-tidy/ClangTidyOptions.h index a3924435b68..d3f4de2070d 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.h +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.h @@ -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() diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp index 95275f619db..1114ff3d142 100644 --- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp +++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp @@ -289,6 +289,7 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {    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<ClangTidyOptionsProvider> createOptionsProvider() {      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 char **argv) {    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 char **argv) {    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 char **argv) {    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 char **argv) {    }    if (!Quiet) { -    printStats(Stats); +    printStats(Context.getStats());      if (DisableFixes)        llvm::errs()            << "Found compiler errors, but -fix-errors was not specified.\n" | 

