This is an archive of the discontinued LLVM Phabricator instance.

Config file support for clang-format, part 2.
ClosedPublic

Authored by alexfh on May 7 2013, 11:19 AM.

Details

Summary

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.

Diff Detail

Event Timeline

jordan_rose added inline comments.May 7 2013, 1:51 PM
tools/clang-format/ClangFormat.cpp
78–79

Not really following this, but (a) why not SmallString, and (b) why not llvm::sys::path::append?

kimgr added a comment.May 7 2013, 2:01 PM

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?

alexfh updated this revision to Unknown Object (????).May 8 2013, 4:43 AM

Use llvm::sys::path::append + SmallString. Print .clang-format file name on error. Output BasedOnStyle in -dump-config.

kimgr added a comment.May 9 2013, 12:55 AM

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?

alexfh added a comment.May 9 2013, 1:17 AM

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.

klimek added inline comments.May 10 2013, 1:54 AM
lib/Format/Format.cpp
49

Any reason to put an extra newline here?

67

Use llvm-style loop?

tools/clang-format/ClangFormat.cpp
83

Any reason to ignore the error?

alexfh updated this revision to Unknown Object (????).May 10 2013, 4:32 AM

Addressed comments.

klimek accepted this revision.May 10 2013, 4:36 AM

LG

tools/clang-format/ClangFormat.cpp
85

querying

alexfh updated this revision to Unknown Object (????).May 10 2013, 4:55 AM

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
alexfh added inline comments.May 10 2013, 4:57 AM
lib/Format/Format.cpp
70

There's a round-trip test, and it passes for me.

alexfh closed this revision.May 10 2013, 4:57 AM