[cfe-dev] Fix for clang-tidy to allow using -list-checks without positional parameters

don hinton hintonda at gmail.com
Sat Aug 8 16:38:05 PDT 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150808/d3c1de71/attachment.html>
-------------- next part --------------
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()) {
-------------- next part --------------
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)


More information about the cfe-dev mailing list