[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-dev
Subject:    [cfe-dev] Fix for clang-tidy to allow using -list-checks without positional parameters
From:       don hinton <hintonda () gmail ! com>
Date:       2015-08-08 23:38:05
Message-ID: CAL2T-v7RNJr84Uq86n1hLF6i9=XkK3f=7KNBG6Ci0w=jbPd0Sg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi:

This is a trivial fix, but I just wanted to start working with clang
tooling and figured this was a good way to get my feet wet.

I've attached two patch files, one for clang::tooling::CommonOptionsParser
which adds a new ctor taking an cl::NumOccurrences parameter for the
SourcePaths option.   It defaults to current behavior, so users don't need
to make any changes.

The second patch is for clangTidyMain.  It just adds a dummy file if
pathlist is empty, and maintains all current logic.

thanks...
don

[Attachment #5 (text/html)]

<div dir="ltr">Hi:<div><br></div><div>This is a trivial fix, but I just wanted to \
start working with clang tooling and figured this was a good way to get my feet \
wet.</div><div><br></div><div>I&#39;ve attached two patch files, one for \
clang::tooling::CommonOptionsParser which adds a new ctor taking an \
cl::NumOccurrences parameter for the SourcePaths option.    It defaults to current \
behavior, so users don&#39;t need to make any changes.</div><div><br></div><div>The \
second patch is for clangTidyMain.   It just adds a dummy file if pathlist is empty, \
and maintains all current \
logic.</div><div><br></div><div>thanks...</div><div>don</div><div><br></div><div><br></div></div>



["tooling.diff" (text/plain)]

diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h
index c23dc92..167caac 100644
--- a/include/clang/Tooling/CommonOptionsParser.h
+++ b/include/clang/Tooling/CommonOptionsParser.h
@@ -72,6 +72,22 @@ public:
   /// This constructor exits program in case of error.
   CommonOptionsParser(int &argc, const char **argv,
                       llvm::cl::OptionCategory &Category,
+                      const char *Overview = nullptr)
+    : CommonOptionsParser (argc, argv, Category, llvm::cl::OneOrMore, Overview) {}
+
+  /// \brief Parses command-line, initializes a compilation database.
+  ///
+  /// This constructor can change argc and argv contents, e.g. consume
+  /// command-line options used for creating FixedCompilationDatabase.
+  ///
+  /// All options not belonging to \p Category become hidden.
+  ///
+  /// I also allows calls to set the required number of positional parameters.
+  ///
+  /// This constructor exits program in case of error.
+  CommonOptionsParser(int &argc, const char **argv,
+                      llvm::cl::OptionCategory &Category,
+                      enum llvm::cl::NumOccurrencesFlag NumRequiredPositional,
                       const char *Overview = nullptr);
 
   /// Returns a reference to the loaded compilations database.
diff --git a/lib/Tooling/CommonOptionsParser.cpp b/lib/Tooling/CommonOptionsParser.cpp
index adae178..a25b810 100644
--- a/lib/Tooling/CommonOptionsParser.cpp
+++ b/lib/Tooling/CommonOptionsParser.cpp
@@ -94,6 +94,7 @@ private:
 
 CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
                                          cl::OptionCategory &Category,
+                                         enum llvm::cl::NumOccurrencesFlag NumRequiredPositional,
                                          const char *Overview) {
   static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
@@ -101,7 +102,7 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
                                         cl::Optional, cl::cat(Category));
 
   static cl::list<std::string> SourcePaths(
-      cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore,
+      cl::Positional, cl::desc("<source0> [... <sourceN>]"), NumRequiredPositional,
       cl::cat(Category));
 
   static cl::list<std::string> ArgsAfter(
@@ -120,6 +121,8 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
                                                                    argv));
   cl::ParseCommandLineOptions(argc, argv, Overview);
   SourcePathList = SourcePaths;
+  if (NumRequiredPositional == cl::ZeroOrMore && SourcePathList.empty())
+    return;
   if (!Compilations) {
     std::string ErrorMessage;
     if (!BuildPath.empty()) {

["clang-tidy.diff" (text/plain)]

diff --git a/clang-tidy/tool/ClangTidyMain.cpp b/clang-tidy/tool/ClangTidyMain.cpp
index a6d5e8b..0109f10 100644
--- a/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tidy/tool/ClangTidyMain.cpp
@@ -262,17 +262,20 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider() {
 }
 
 static int clangTidyMain(int argc, const char **argv) {
-  CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory);
+  CommonOptionsParser OptionsParser(argc, argv, ClangTidyCategory, cl::ZeroOrMore);
 
   auto OptionsProvider = createOptionsProvider();
   if (!OptionsProvider)
     return 1;
 
-  std::string FileName = OptionsParser.getSourcePathList().front();
+  std::string FileName ("dummy.cpp");
+  auto pathlist = OptionsParser.getSourcePathList();
+  if (!pathlist.empty()) {
+    FileName = OptionsParser.getSourcePathList().front();
+  }
   ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName);
   std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
 
-  // FIXME: Allow using --list-checks without positional arguments.
   if (ListChecks) {
     llvm::outs() << "Enabled checks:";
     for (auto CheckName : EnabledChecks)

[Attachment #8 (text/plain)]

_______________________________________________
cfe-dev mailing list
cfe-dev@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic