[PATCH] D89936: [clang-tidy] adding "--clang-tidy-config=<file-path>" to specify custom config file

Hiral via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 27 07:13:02 PDT 2020


Hiralo updated this revision to Diff 300980.
Hiralo added a comment.

With updated patch now changes are only in ClangTidyMain.cpp.
Thanks to @DmitryPolukhin for valuable suggestion :)

Added '--config-file' option to specify custom config file.
ClangTidyMain.cpp reads ConfigFile into string and then assigned read data to 'Config' i.e. makes like '--config' code flow internally.

Ref comments https://reviews.llvm.org/D89936#inline-839224

Thank you.
-Hiral


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89936/new/

https://reviews.llvm.org/D89936

Files:
  clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp


Index: clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -73,6 +73,14 @@
 )"),
                                    cl::init(""), cl::cat(ClangTidyCategory));
 
+static cl::opt<std::string> ConfigFile("config-file", cl::desc(R"(
+Specify full path of .clang-tidy or custom config file.
+For example, --config-file=/some/path/myTidyConfig
+Note: this option is mutually exclusive to --config and --checks options.
+)"),
+                                       cl::init(""),
+                                       cl::cat(ClangTidyCategory));
+
 static cl::opt<std::string> WarningsAsErrors("warnings-as-errors", cl::desc(R"(
 Upgrades warnings to errors. Same format as
 '-checks'.
@@ -279,6 +287,7 @@
 
   ClangTidyOptions DefaultOptions;
   DefaultOptions.Checks = DefaultChecks;
+  DefaultOptions.ConfigFile = "";
   DefaultOptions.WarningsAsErrors = "";
   DefaultOptions.HeaderFilterRegex = HeaderFilter;
   DefaultOptions.SystemHeaders = SystemHeaders;
@@ -291,6 +300,8 @@
   ClangTidyOptions OverrideOptions;
   if (Checks.getNumOccurrences() > 0)
     OverrideOptions.Checks = Checks;
+  if (ConfigFile.getNumOccurrences() > 0)
+    OverrideOptions.ConfigFile = ConfigFile;
   if (WarningsAsErrors.getNumOccurrences() > 0)
     OverrideOptions.WarningsAsErrors = WarningsAsErrors;
   if (HeaderFilter.getNumOccurrences() > 0)
@@ -302,6 +313,30 @@
   if (UseColor.getNumOccurrences() > 0)
     OverrideOptions.UseColor = UseColor;
 
+  if (!ConfigFile.empty()) {
+    if (!Config.empty()) {
+      llvm::errs() << "Error: --config-file and --config are mutually "
+                      "exclusive. Specify only one.\n";
+      return nullptr;
+    }
+
+    if (!Checks.empty()) {
+      llvm::errs() << "Error: --config-file and --checks are mutually "
+                      "exclusive. Specify only one.\n";
+      return nullptr;
+    }
+
+    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
+        llvm::MemoryBuffer::getFile(ConfigFile.c_str());
+    if (std::error_code EC = Text.getError()) {
+      llvm::errs() << "Can't read config-file '" << ConfigFile
+                   << "': " << EC.message() << "\n";
+      return nullptr;
+    }
+
+    Config.assign((*Text)->getBuffer());
+  }
+
   if (!Config.empty()) {
     if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
             parseConfiguration(Config)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89936.300980.patch
Type: text/x-patch
Size: 2528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201027/feb4da4f/attachment-0001.bin>


More information about the cfe-commits mailing list