clang-format: Add -assume-filename option for editor integrations.
With -style=file, clang-format now starts to search for a .clang-format
file starting at the file given with -assume-filename if it reads from
stdin. Otherwise, it would start searching from the current directory,
which is not helpful for editor integrations.
Also changed vim, emacs and sublime integrations to actually make use of
this flag.
This fixes llvm.org/PR17072.
git-svn-id: svn://svn.chromium.org/llvm-project/cfe/trunk/tools/clang-format@190691 0b72dbe1-c17f-4bc7-b9db-2b4152be0356
diff --git a/ClangFormat.cpp b/ClangFormat.cpp
index 592d46a..71dcfc5 100644
--- a/ClangFormat.cpp
+++ b/ClangFormat.cpp
@@ -73,6 +73,14 @@
"parameters, e.g.:\n"
" -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""),
cl::init("file"), cl::cat(ClangFormatCategory));
+
+static cl::opt<std::string>
+AssumeFilename("assume-filename",
+ cl::desc("When reading from stdin, clang-format assumes this\n"
+ "filename to look for a style config file (with\n"
+ "-style=file)."),
+ cl::cat(ClangFormatCategory));
+
static cl::opt<bool> Inplace("i",
cl::desc("Inplace edit <file>s, if specified."),
cl::cat(ClangFormatCategory));
@@ -126,11 +134,15 @@
return Style;
}
+ if (FileName == "-")
+ FileName = AssumeFilename;
SmallString<128> Path(FileName);
llvm::sys::fs::make_absolute(Path);
- for (StringRef Directory = llvm::sys::path::parent_path(Path);
+ for (StringRef Directory = Path;
!Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
+ if (!llvm::sys::fs::is_directory(Directory))
+ continue;
SmallString<128> ConfigFile(Directory);
llvm::sys::path::append(ConfigFile, ".clang-format");