Adds actual config file reading to the clang-format utility.
Configuration file name is .clang-format. It is looked up for each input file
in its parent directories starting from immediate one. First found .clang-format
file is used. When using standard input, .clang-format is searched starting from
the current directory.
Added -dump-config option to easily create configuration files.
Details
- Reviewers
klimek djasper - Commits
- rL181589: Config file support for clang-format, part 2.
Diff Detail
Event Timeline
tools/clang-format/ClangFormat.cpp | ||
---|---|---|
78–79 | Not really following this, but (a) why not SmallString, and (b) why not llvm::sys::path::append? |
I like where this is going, found a few nits.
tools/clang-format/ClangFormat.cpp | ||
---|---|---|
79 | Is there a path-separator-agnostic way of doing this? "/" should be OK on Windows, but not prefered. You could run it through llvm::sys::path::native or llvm::sys::path::append. | |
80–81 | Initialize IsFile or handle error return from is_regular_file. IsFile will be uninitialized if the function fails. | |
89 | I think that basing the YAML parser on text content will cause diagnostics not to contain the filename. Unfortunately it looks like the smart stuff in YAMLTraits can only work based on content, not a buffer. YAMLParser's Stream class takes a MemoryBuffer and pulls filename for diagnostics from its buffer identifier, usually a filename. You may want to provoke a diagnostic by feeding the parser malformed YAML, and see what the context is. I think the default is "YAML: ". | |
93 | Remove this from stderr? Wasn't there a debug stream added to clang-format at some point? |
Use llvm::sys::path::append + SmallString. Print .clang-format file name on error. Output BasedOnStyle in -dump-config.
Looks good!
Did you try provoking a syntax error? Unless it's been short-circuited, I think the YAML parsing framework will print a diagnostic before your manual llvm::errs() output, but I don't have time to rebuild and check right now.
lib/Format/Format.cpp | ||
---|---|---|
65–73 | Maybe move this into clang::format::isPredefinedStyle(const FormatStyle& Style)? That way you don't have to re-define the predefined style names here too. | |
70 | IIRC, the YAML parser doesn't take "#" comments, only JavaScript-style "//", but I'm not 100% sure. Did you try roundtripping a dumped configuration? |
I've tried, of course. And my message goes after YAML I/O's, but this way
it's still informative enough for diagnostic purposes. And I thought we can
come back to this once YAML I/O has better support for this.
Now I have an answer about reasons to ignore errors from is_regular_file ;)
It should be enough to know that we can't read the file. In particular, we don't want "No such file or directory" to be treated as an error:
Error quering /.../work/llvm/tools/clang/.clang-format: No such file or directory Error quering /.../work/llvm/tools/.clang-format: No such file or directory YAML:1:1: error: not a mapping akljsdflj ^~~~~~~~~ Error reading /.../work/llvm/.clang-format: Invalid argument Error quering /.../work/.clang-format: No such file or directory Error quering /.../.clang-format: No such file or directory ... Error quering /.clang-format: No such file or directory Can't find usable .clang-format, using LLVM style
So I removed error handling again, now output for a file with invalid contents looks better:
YAML:1:1: error: not a mapping akljsdflj ^~~~~~~~~ Error reading /.../work/llvm/.clang-format: Invalid argument Can't find usable .clang-format, using LLVM style
lib/Format/Format.cpp | ||
---|---|---|
70 | There's a round-trip test, and it passes for me. |
Any reason to put an extra newline here?