[clang-tools-extra] r207532 - Add ClangTidyOptions to encapsulate all clang-tidy options.

Alexander Kornienko alexfh at google.com
Tue Apr 29 08:20:11 PDT 2014


Author: alexfh
Date: Tue Apr 29 10:20:10 2014
New Revision: 207532

URL: http://llvm.org/viewvc/llvm-project?rev=207532&view=rev
Log:
Add ClangTidyOptions to encapsulate all clang-tidy options.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D3544

Added:
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidy.h
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h
    clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
    clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h

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=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Tue Apr 29 10:20:10 2014
@@ -285,24 +285,21 @@ void ClangTidyCheck::setName(StringRef N
   CheckName = Name.str();
 }
 
-std::vector<std::string> getCheckNames(StringRef EnableChecksRegex,
-                                       StringRef DisableChecksRegex) {
+std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
   SmallVector<ClangTidyError, 8> Errors;
-  clang::tidy::ClangTidyContext Context(&Errors, EnableChecksRegex,
-                                        DisableChecksRegex);
+  clang::tidy::ClangTidyContext Context(&Errors, Options);
   ClangTidyASTConsumerFactory Factory(Context);
   return Factory.getCheckNames();
 }
 
-void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex,
+void runClangTidy(const ClangTidyOptions &Options,
                   const tooling::CompilationDatabase &Compilations,
                   ArrayRef<std::string> Ranges,
                   SmallVectorImpl<ClangTidyError> *Errors) {
   // FIXME: Ranges are currently full files. Support selecting specific
   // (line-)ranges.
   ClangTool Tool(Compilations, Ranges);
-  clang::tidy::ClangTidyContext Context(Errors, EnableChecksRegex,
-                                        DisableChecksRegex);
+  clang::tidy::ClangTidyContext Context(Errors, Options);
   ClangTidyDiagnosticConsumer DiagConsumer(Context);
 
   Tool.setDiagnosticConsumer(&DiagConsumer);

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=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.h Tue Apr 29 10:20:10 2014
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H
 
 #include "ClangTidyDiagnosticConsumer.h"
+#include "ClangTidyOptions.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceManager.h"
@@ -115,11 +116,10 @@ private:
 
 /// \brief Fills the list of check names that are enabled when the provided
 /// filters are applied.
-std::vector<std::string> getCheckNames(StringRef EnableChecksRegex,
-                                       StringRef DisableChecksRegex);
+std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
 
 /// \brief Run a set of clang-tidy checks on a set of files.
-void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex,
+void runClangTidy(const ClangTidyOptions &Options,
                   const tooling::CompilationDatabase &Compilations,
                   ArrayRef<std::string> Ranges,
                   SmallVectorImpl<ClangTidyError> *Errors);

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Tue Apr 29 10:20:10 2014
@@ -18,6 +18,7 @@
 
 #include "ClangTidyDiagnosticConsumer.h"
 
+#include "ClangTidyOptions.h"
 #include "clang/Frontend/DiagnosticRenderer.h"
 #include "llvm/ADT/SmallString.h"
 
@@ -113,19 +114,17 @@ ClangTidyMessage::ClangTidyMessage(Strin
 ClangTidyError::ClangTidyError(StringRef CheckName)
     : CheckName(CheckName) {}
 
-ChecksFilter::ChecksFilter(StringRef EnableChecksRegex,
-                           StringRef DisableChecksRegex)
-    : EnableChecks(EnableChecksRegex), DisableChecks(DisableChecksRegex) {}
+ChecksFilter::ChecksFilter(const ClangTidyOptions &Options)
+    : EnableChecks(Options.EnableChecksRegex),
+      DisableChecks(Options.DisableChecksRegex) {}
 
 bool ChecksFilter::isCheckEnabled(StringRef Name) {
   return EnableChecks.match(Name) && !DisableChecks.match(Name);
 }
 
 ClangTidyContext::ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
-                                   StringRef EnableChecksRegex,
-                                   StringRef DisableChecksRegex)
-    : Errors(Errors), DiagEngine(nullptr),
-      Filter(EnableChecksRegex, DisableChecksRegex) {}
+                                   const ClangTidyOptions &Options)
+    : Errors(Errors), DiagEngine(nullptr), Filter(Options) {}
 
 DiagnosticBuilder ClangTidyContext::diag(
     StringRef CheckName, SourceLocation Loc, StringRef Description,

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=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.h Tue Apr 29 10:20:10 2014
@@ -28,6 +28,8 @@ class CompilationDatabase;
 
 namespace tidy {
 
+struct ClangTidyOptions;
+
 /// \brief A message from a clang-tidy check.
 ///
 /// Note that this is independent of a \c SourceManager.
@@ -59,7 +61,7 @@ struct ClangTidyError {
 /// \brief Filters checks by name.
 class ChecksFilter {
 public:
-  ChecksFilter(StringRef EnableChecksRegex, StringRef DisableChecksRegex);
+  ChecksFilter(const ClangTidyOptions& Options);
   bool isCheckEnabled(StringRef Name);
 
 private:
@@ -79,7 +81,7 @@ private:
 class ClangTidyContext {
 public:
   ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
-                   StringRef EnableChecksRegex, StringRef DisableChecksRegex);
+                   const ClangTidyOptions &Options);
 
   /// \brief Report any errors detected using this method.
   ///

Added: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h?rev=207532&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h (added)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h Tue Apr 29 10:20:10 2014
@@ -0,0 +1,26 @@
+//===--- ClangTidyOptions.h - clang-tidy ------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H
+
+namespace clang {
+namespace tidy {
+
+/// \brief Contains options for clang-tidy.
+struct ClangTidyOptions {
+  ClangTidyOptions() : EnableChecksRegex(".*") {}
+  std::string EnableChecksRegex;
+  std::string DisableChecksRegex;
+};
+
+} // end namespace tidy
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_OPTIONS_H

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=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Tue Apr 29 10:20:10 2014
@@ -49,18 +49,21 @@ static cl::opt<bool> ListChecks("list-ch
 int main(int argc, const char **argv) {
   CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
 
+  clang::tidy::ClangTidyOptions Options;
+  Options.EnableChecksRegex = Checks;
+  Options.DisableChecksRegex = DisableChecks;
+
   // FIXME: Allow using --list-checks without positional arguments.
   if (ListChecks) {
     llvm::outs() << "Enabled checks:";
-    for (auto CheckName : clang::tidy::getCheckNames(Checks, DisableChecks))
+    for (auto CheckName : clang::tidy::getCheckNames(Options))
       llvm::outs() << "\n    " << CheckName;
     llvm::outs() << "\n\n";
     return 0;
   }
 
   SmallVector<clang::tidy::ClangTidyError, 16> Errors;
-  clang::tidy::runClangTidy(Checks, DisableChecks,
-                            OptionsParser.getCompilations(),
+  clang::tidy::runClangTidy(Options, OptionsParser.getCompilations(),
                             OptionsParser.getSourcePathList(), &Errors);
   clang::tidy::handleErrors(Errors, Fix);
 

Modified: clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h?rev=207532&r1=207531&r2=207532&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ClangTidyTest.h Tue Apr 29 10:20:10 2014
@@ -43,7 +43,7 @@ template <typename T>
 std::string runCheckOnCode(StringRef Code,
                            SmallVectorImpl<ClangTidyError> &Errors) {
   T Check;
-  ClangTidyContext Context(&Errors, ".*", "");
+  ClangTidyContext Context(&Errors, ClangTidyOptions());
   ClangTidyDiagnosticConsumer DiagConsumer(Context);
   Check.setContext(&Context);
   std::vector<std::string> ArgCXX11(1, "-std=c++11");
@@ -59,10 +59,8 @@ std::string runCheckOnCode(StringRef Cod
     return "";
   DiagConsumer.finish();
   tooling::Replacements Fixes;
-  for (SmallVector<ClangTidyError, 16>::const_iterator I = Errors.begin(),
-                                                       E = Errors.end();
-       I != E; ++I)
-    Fixes.insert(I->Fix.begin(), I->Fix.end());
+  for (const ClangTidyError &Error : Errors)
+    Fixes.insert(Error.Fix.begin(), Error.Fix.end());
   return tooling::applyAllReplacements(Code, Fixes);
 }
 





More information about the cfe-commits mailing list