[clang-tools-extra] d6a468d - [clang-tidy] adding "--config-file=<file-path>" to specify custom config file.

Dmitry Polukhin via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 04:01:48 PST 2020


Author: Hiral Oza
Date: 2020-11-03T11:59:46Z
New Revision: d6a468d622b2d48c40c47290aa54d6d910c5a6bf

URL: https://github.com/llvm/llvm-project/commit/d6a468d622b2d48c40c47290aa54d6d910c5a6bf
DIFF: https://github.com/llvm/llvm-project/commit/d6a468d622b2d48c40c47290aa54d6d910c5a6bf.diff

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

Let clang-tidy to read config from specified file.
Example:
$ clang-tidy --config-file=/some/path/myTidyConfig --list-checks --
...this will read config from '/some/path/myTidyConfig'.

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

May speed-up tidy runtime since now it will just look-up <file-path>
instead of searching ".clang-tidy" in parent-dir(s).

Directly specifying config path helps setting build dependencies.

Thanks to @DmitryPolukhin for valuable suggestion. This patch now propose
change only in ClangTidyMain.cpp.

Reviewed By: DmitryPolukhin

Differential Revision: https://reviews.llvm.org/D89936

Added: 
    clang-tools-extra/test/clang-tidy/infrastructure/Inputs/config-file/config-file
    clang-tools-extra/test/clang-tidy/infrastructure/config-file.cpp

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index e248c04ea570..e0465665d6a0 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -168,6 +168,16 @@ each source file in its parent directories.
 )"),
                                    cl::init(""), cl::cat(ClangTidyCategory));
 
+static cl::opt<std::string> ConfigFile("config-file", cl::desc(R"(
+Specify the path of .clang-tidy or custom config file:
+ e.g. --config-file=/some/path/myTidyConfigFile
+This option internally works exactly the same way as
+ --config option after reading specified config file.
+Use either --config-file or --config, not both.
+)"),
+                                       cl::init(""),
+                                       cl::cat(ClangTidyCategory));
+
 static cl::opt<bool> DumpConfig("dump-config", cl::desc(R"(
 Dumps configuration in the YAML format to
 stdout. This option can be used along with a
@@ -302,19 +312,41 @@ static std::unique_ptr<ClangTidyOptionsProvider> createOptionsProvider(
   if (UseColor.getNumOccurrences() > 0)
     OverrideOptions.UseColor = UseColor;
 
-  if (!Config.empty()) {
-    if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
-            parseConfiguration(Config)) {
+  auto LoadConfig = [&](StringRef Configuration)
+      -> std::unique_ptr<ClangTidyOptionsProvider> {
+    llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
+        parseConfiguration(Configuration);
+    if (ParsedConfig)
       return std::make_unique<ConfigOptionsProvider>(
           GlobalOptions,
           ClangTidyOptions::getDefaults().mergeWith(DefaultOptions, 0),
           *ParsedConfig, OverrideOptions, std::move(FS));
-    } else {
-      llvm::errs() << "Error: invalid configuration specified.\n"
-                   << ParsedConfig.getError().message() << "\n";
+    llvm::errs() << "Error: invalid configuration specified.\n"
+                 << ParsedConfig.getError().message() << "\n";
+    return nullptr;
+  };
+
+  if (ConfigFile.getNumOccurrences() > 0) {
+    if (Config.getNumOccurrences() > 0) {
+      llvm::errs() << "Error: --config-file and --config 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() << "Error: can't read config-file '" << ConfigFile
+                   << "': " << EC.message() << "\n";
+      return nullptr;
+    }
+
+    return LoadConfig((*Text)->getBuffer());
   }
+
+  if (Config.getNumOccurrences() > 0)
+    return LoadConfig(Config);
+
   return std::make_unique<FileOptionsProvider>(GlobalOptions, DefaultOptions,
                                                 OverrideOptions, std::move(FS));
 }

diff  --git a/clang-tools-extra/test/clang-tidy/infrastructure/Inputs/config-file/config-file b/clang-tools-extra/test/clang-tidy/infrastructure/Inputs/config-file/config-file
new file mode 100644
index 000000000000..23bb65e0155b
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/Inputs/config-file/config-file
@@ -0,0 +1 @@
+Checks: "-*,hicpp-uppercase-literal-suffix"

diff  --git a/clang-tools-extra/test/clang-tidy/infrastructure/config-file.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/config-file.cpp
new file mode 100644
index 000000000000..49028d198f75
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/config-file.cpp
@@ -0,0 +1,2 @@
+// RUN: clang-tidy -config-file=%S/Inputs/config-file/config-file -dump-config -- | FileCheck %s -check-prefix=CHECK-BASE
+// CHECK-BASE: Checks: {{.*}}hicpp-uppercase-literal-suffix


        


More information about the cfe-commits mailing list