[PATCH] D151315: [clangd] Add a switch to specify a default clangd configuration file

Thilo Vörtler via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 24 04:11:04 PDT 2023


voertler created this revision.
voertler added reviewers: dgoldman, kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
voertler requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

This patch allows it to set a default configuration as discussed in issue https://github.com/clangd/clangd/issues/845 by adding a startup option --default-config.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151315

Files:
  clang-tools-extra/clangd/tool/ClangdMain.cpp


Index: clang-tools-extra/clangd/tool/ClangdMain.cpp
===================================================================
--- clang-tools-extra/clangd/tool/ClangdMain.cpp
+++ clang-tools-extra/clangd/tool/ClangdMain.cpp
@@ -317,7 +317,6 @@
 RetiredFlag<bool> InlayHints("inlay-hints");
 RetiredFlag<bool> FoldingRanges("folding-ranges");
 
-
 opt<int> LimitResults{
     "limit-results",
     cat(Features),
@@ -497,7 +496,8 @@
     desc(
         "Read user and project configuration from YAML files.\n"
         "Project config is from a .clangd file in the project directory.\n"
-        "User config is from clangd/config.yaml in the following directories:\n"
+        "User config is from clangd/config.yaml in the following directories \n"
+        "(unless specified with --default-config):\n"
         "\tWindows: %USERPROFILE%\\AppData\\Local\n"
         "\tMac OS: ~/Library/Preferences/\n"
         "\tOthers: $XDG_CONFIG_HOME, usually ~/.config\n"
@@ -505,6 +505,15 @@
     init(true),
 };
 
+opt<std::string> DefaultConfig{
+    "default-config",
+    cat(Misc),
+    desc("Path to a default clangd configuration file. A clangd user and "
+         "project configuration has a higher priority (requires "
+         "--enable-config) "),
+    init(""),
+};
+
 opt<bool> UseDirtyHeaders{"use-dirty-headers", cat(Misc),
                           desc("Use files open in the editor when parsing "
                                "headers instead of reading from the disk"),
@@ -945,6 +954,21 @@
     } else {
       elog("Couldn't determine user config file, not loading");
     }
+    if (DefaultConfig.getNumOccurrences()) {
+      llvm::SmallString<256> DefaultConfigPath;
+      if (auto Error = llvm::sys::fs::real_path(
+              DefaultConfig, DefaultConfigPath, /*expand_tilde=*/true)) {
+        elog("Couldn't determine default config file {0}: {1} , not loading",
+             DefaultConfig, Error.message());
+      } else {
+        vlog("Default config file is {0}", DefaultConfigPath);
+        // Give lowest priority to default config 
+        ProviderStack.insert(
+            ProviderStack.begin(),
+            config::Provider::fromYAMLFile(DefaultConfigPath, /*Directory=*/"",
+                                           TFS, /*Trusted=*/true));
+      }
+    }
   }
   ProviderStack.push_back(std::make_unique<FlagsConfigProvider>());
   std::vector<const config::Provider *> ProviderPointers;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151315.525101.patch
Type: text/x-patch
Size: 2434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230524/a555e12a/attachment.bin>


More information about the cfe-commits mailing list