[PATCH] D34252: Add arbitrary file/path support to clang-format style file selection

Dan Ciliske via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 15 15:11:46 PDT 2017


dciliske created this revision.
Herald added a subscriber: klimek.

The Format library has no way to specify a specific file to be used as the style source. It climbs the path looking for ‘.clang_format’. This patch adds the ability to specify a specific file to use for clang Format utilities.

This patch is in direct response to https://bugs.llvm.org//show_bug.cgi?id=28107 (along with my own personal need).

New style argument feature:

  Use -style=file,<filename> to load style
  configuration from an explicitly defined file.


https://reviews.llvm.org/D34252

Files:
  lib/Format/Format.cpp


Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1967,6 +1967,8 @@
     ".clang-format file located in one of the parent\n"
     "directories of the source file (or current\n"
     "directory for stdin).\n"
+    "Use -style=file,<filename> to load style \n"
+    "configuration from an explicitly defined file.\n"
     "Use -style=\"{key: value, ...}\" to set specific\n"
     "parameters, e.g.:\n"
     "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
@@ -2014,8 +2016,44 @@
   }
 
   if (!StyleName.equals_lower("file")) {
+    SmallString<128> ConfigFile(StyleName.substr(5));
+    if (StyleName.startswith_lower("file,")) {
+      DEBUG(llvm::dbgs() << "Trying explicit file" << ConfigFile << "...\n");
+
+      auto Status = FS->status(ConfigFile.str());
+      bool FoundConfigFile =
+        Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
+      if (FoundConfigFile) {
+        llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
+          FS->getBufferForFile(ConfigFile.str());
+        if (std::error_code EC = Text.getError()) {
+          DEBUG(llvm::dbgs() << "Text Error getting contents.\n");
+          return make_string_error(EC.message());
+        }
+        if (std::error_code ec =
+            parseConfiguration(Text.get()->getBuffer(), &Style)) {
+          if (ec == ParseError::Unsuitable) {
+
+            DEBUG(llvm::dbgs() << "Config file error.\n");
+            return make_string_error(
+                "Configuration file does not support " +
+                getLanguageName(Style.Language) + ": " +
+                ConfigFile);
+          }
+          DEBUG(llvm::dbgs() << "Error reading " << ConfigFile << ".\n");
+          return make_string_error("Error reading " + ConfigFile + ": " +
+              ec.message());
+        }
+        DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
+        return Style;
+      }
+      DEBUG(llvm::dbgs() << "Could not find file: " << ConfigFile << "\n");
+      return FallbackStyle;
+    }
     if (!getPredefinedStyle(StyleName, Style.Language, &Style))
       return make_string_error("Invalid value for -style");
+
+    DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile << "\n");
     return Style;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34252.102728.patch
Type: text/x-patch
Size: 2372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170615/2eb753eb/attachment.bin>


More information about the cfe-commits mailing list