This diff extends the -style=file option to allow a config file to be specified explicitly. This is useful (for instance) when adding IDE commands to reformat code to a personal style.
Usage: clang-format -style=file:<path/to/config/file> ...
Differential D72326
[clang-format] Add option to explicitly specify a config file zwliew on Jan 7 2020, 5:39 AM. Authored by Tokens
Details This diff extends the -style=file option to allow a config file to be specified explicitly. This is useful (for instance) when adding IDE commands to reformat code to a personal style. Usage: clang-format -style=file:<path/to/config/file> ...
Diff Detail
Event TimelineThere are a very large number of changes, so older changes are hidden. Show Older Changes Comment Actions In principle, I think this is something that might help a lot of people as I've seen people asking for a better mechanism to be able to load a centrally stored .clang-format file. but maybe wait a couple of days for others to comment @klimek , @mitchell-stellar , @sammccall ... Also you need to make full context diffs, plus please add a release note, and because I can't check the full diff, I'm unsure if your Format.h change means we need to regenerate anything in the ClangFormatStyleOptions.rst Comment Actions In this patch, relative paths are relative to the working directory (or at least the current directory of the VFS). This makes sense for command-line args, but if I understand correctly this patch will also allow BasedOnStyle: file:some/path. Is that the case?
Comment Actions Thanks for your time and feedback.
Regarding the tests, I am unsure how to do this currenlty (I'd need to read some docs/other examples). Comment Actions
No, it should not, and I also think it's better not to. I think that all points are addressed now. Looking forward to have your feedback. Comment Actions Nit: please add a release note and regenerate the ClangFormatStyleOptions.rst (if there are any changes because you modified Format.h). Comment Actions Hmm, I tried to run docs/tools/dump_format_style.py, but it fails at two locations (once because a comment in Format.h has two slashes instead of 3, one because of an empty line). After fixing those, it seems that the generated file contains less information than the commited one. This file was probably updated manually as well. Therefore I also did in the updated diff... one should probably fix this separately. Comment Actions In the absence of any other comments, This LGTM, this may help to resolve D68569: [clang-format] Also look for .{ext}.clang-format file, I think I prefer this solution.
Comment Actions It's not more approval that is needed, it's just that someone with commit access (assuming you do not) needs to find the time to commit this. For what it's worth, I'm getting a patch rejection for the 4th block in lib/Format/Format.cpp. It seems the contents of LoadConfigFile need to be updated to reflect the most recent changes, so please rebase against master when you can. Comment Actions Reverted this commit due to an unexpected test failure: ******************** TEST 'Clang :: Format/dump-config-objc.h' FAILED ******************** Script: -- : 'RUN: at line 1'; /b/1/clang-x86_64-debian-fast/llvm.obj/bin/clang-format -dump-config /b/1/clang-x86_64-debian-fast/llvm.src/clang/test/Format/dump-config-objc.h | /b/1/clang-x86_64-debian-fast/llvm.obj/bin/FileCheck /b/1/clang-x86_64-debian-fast/llvm.src/clang/test/Format/dump-config-objc.h -- Exit Code: 1 Command Output (stderr): -- /b/1/clang-x86_64-debian-fast/llvm.src/clang/test/Format/dump-config-objc.h:3:11: error: CHECK: expected string not found in input // CHECK: Language: ObjC ^ <stdin>:1:1: note: scanning from here --- ^ <stdin>:2:1: note: possible intended match here Language: Cpp ^ -- ******************** I don't know enough about this patch in order to determine what the issue is, or how to proceed further. Perhaps @MyDeveloperDay will chime in.
Comment Actions These tests still fail running the following in the build directory (if your build directory is side-by-side with the llvm-project directory): c:/Python37/python ./bin/llvm-lit.py -v ./tools/clang/test/Format $ ./run_format_lit_tests.sh llvm-lit.py: C:\cygwin64\buildareas\clang\build\bin\..\..\llvm-project\llvm\utils\lit\lit\llvm\config.py:343: note: using clang: c:\cygwin64\buildareas\clang\build\bin\clang.exe -- Testing: 21 tests, 12 workers -- PASS: Clang :: Format/cursor.cpp (1 of 21) FAIL: Clang :: Format/dump-config-objc.h (2 of 21) ******************** TEST 'Clang :: Format/dump-config-objc.h' FAILED ******************** Script: -- : 'RUN: at line 1'; c:\cygwin64\buildareas\clang\build\bin\clang-format.exe -dump-config C:\cygwin64\buildareas\clang\llvm-project\clang\test\Format\dump-config-objc.h | c:\cygwin64\buildareas\clang\build\bin\filecheck.exe C:\cygwin64\buildareas\clang\llvm-project\clang\test\Format\dump-config-objc.h -- Exit Code: 1 Command Output (stdout): -- $ ":" "RUN: at line 1" $ "c:\cygwin64\buildareas\clang\build\bin\clang-format.exe" "-dump-config" "C:\cygwin64\buildareas\clang\llvm-project\clang\test\Format\dump-config-objc.h" $ "c:\cygwin64\buildareas\clang\build\bin\filecheck.exe" "C:\cygwin64\buildareas\clang\llvm-project\clang\test\Format\dump-config-objc.h" # command stderr: C:\cygwin64\buildareas\clang\llvm-project\clang\test\Format\dump-config-objc.h:3:11: error: CHECK: expected string not found in input // CHECK: Language: ObjC ^ <stdin>:1:1: note: scanning from here --- ^ <stdin>:2:1: note: possible intended match here Language: Cpp ^ error: command failed with exit status: 1 -- ********************
Comment Actions @tnorth Are you planning on working on this or do you mind if one of us fixes the issues enough to get it over the line. (this was previously accepted and landed but the unit tests fail, for what looks like a fairly minor issue) Comment Actions After rebasing these tests seem to work (on Windows) -- Testing: 24 tests, 16 workers -- PASS: Clang :: Format/dry-run-alias.cpp (1 of 24) PASS: Clang :: Format/remove-duplicate-includes.cpp (2 of 24) PASS: Clang :: Format/disable-format.cpp (3 of 24) PASS: Clang :: Format/cursor.cpp (4 of 24) PASS: Clang :: Format/multiple-inputs.cpp (5 of 24) PASS: Clang :: Format/basic.cpp (6 of 24) PASS: Clang :: Format/incomplete.cpp (7 of 24) PASS: Clang :: Format/line-ranges.cpp (8 of 24) PASS: Clang :: Format/adjust-indent.cpp (9 of 24) UNSUPPORTED: Clang :: Format/style-on-command-line.cpp (10 of 24) PASS: Clang :: Format/ranges.cpp (11 of 24) PASS: Clang :: Format/access-modifiers.cpp (12 of 24) PASS: Clang :: Format/multiple-inputs-error.cpp (13 of 24) PASS: Clang :: Format/dump-config-objc.h (14 of 24) PASS: Clang :: Format/xmloutput.cpp (15 of 24) PASS: Clang :: Format/dump-config-cxx.h (16 of 24) PASS: Clang :: Format/multiple-inputs-inplace.cpp (17 of 24) PASS: Clang :: Format/error-config.cpp (18 of 24) PASS: Clang :: Format/dry-run.cpp (19 of 24) PASS: Clang :: Format/inplace.cpp (20 of 24) PASS: Clang :: Format/dump-config-list-override.cpp (21 of 24) PASS: Clang :: Format/disable-include-sorting.cpp (22 of 24) PASS: Clang :: Format/language-detection.cpp (23 of 24) PASS: Clang :: Format/verbose.cpp (24 of 24) I'm going to Commandeer this revision and see if we can't get this relanded.
Comment Actions The reason I have picked this us was because of: https://twitter.com/bruxisma/status/1462987809879257101 This slightly annoys me because : a) What they were talking about was in my view is disrespectful and inaccurate. I went looking for this review which had previously been accepted and landed, but got reverted because it seemed to fail the tests The original author and the original-original-author has both obviously moved on and dropped it and so it didn't get fixed. I don't like wasting all that effort, especailly if we are going to get grief for it. So I rebased the review so we can land it again, (and checked both the unit tests and lit tests) I hope we don't have to go around the houses on this too much.
Comment Actions Hi there, Comment Actions Ping! Could we see this previously approved patch over the line I’d like to use it to build a regression suit of code and format files without the need for every test to be in its own directory Comment Actions Hi, I'd like to help to get this patch accepted and merged. I have a few suggestions/questions below, and I can help make any changes to the patch if needed!
Comment Actions On further thought, the logic for loadConfigFile() looks incomplete. It does not properly handle BasedOnStyle: InheritParentConfig. In fact, the logic for -style=file can be pulled out and merged into loadConfigFile(). I'll look into making this change, and possibly refactoring the code to reduce code duplication. Comment Actions Support inheritance from parent configs via BasedOnStyle: InheritParentConfig I made the following changes:
Please have a look and let me know your thoughts, thanks! Also, this is my first time using Phabricator, so let me know if I did anything wrong. Side question: Is there a reason that the number of children configs in the fallback case is limited to 1 (that is, when all the configs have BasedOnStyle: InheritParentConfig set)?
Comment Actions Do you plan to refactor something for this review, or do you think you are done? Then I will look at it again as a whole. Comment Actions I'm going to try refactoring the code for loading and parsing the config file into a separate function. I'll update the diff in no longer than a day. Comment Actions I am done. Please review the latest changes. Thanks! I made the following changes:
Comment Actions LGTM but please wait for the green light from other involved reviewers.
Comment Actions Addressed the comments on the previous diff:
Comment Actions Thanks for the review. I've moved the unrelated change to https://reviews.llvm.org/D116371 instead. Comment Actions Do you have commit access, or do you need someone to commit this and your other changes? If the latter we need a name and an email for the commit. Then someone will come along and commit it. Comment Actions I don't think I have commit access, so I would be grateful if someone helps me do it. Name: Email: |
I think two backticks are missing here.