-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adopt clang-format for all files? #2876
Comments
Is it possible to just clang-format new lines?
|
Possibly, but we are likely to have to tweak the existing style rules (because there may not be existing clang-format knobs to match everything), and we already have some older code violating things, and it's more complicated to set up editors and scripts to only format changes -- it seems simpler to get entire files in a consistent state. Xref other project experiences such as https://engineering.mongodb.com/post/succeeding-with-clangformat-part-2-the-big-reformat/ |
Maybe we could make use of the clang-format-diff.py script: https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/clang-format-diff.py It just formats the changed lines in a diff. To format a commit, I use something like
With clang-format and clang-format-diff.py being in the PATH |
This patch adds a first version of a clang-format style definition for DynamoRIO. I think it is relatively close to the style guide, but please let me know of any other rules that should be accounted for. I have re-formatted 3 files from the aarch64 backend. We do not have to include them in the final commit, but I thought they would be useful to illustrate the impact of the style rules. Issue #2876 Change-Id: I03c3f3530159b9a770909f5554cf13b30ae45da0
FTR here are the settings I used a year ago though I did not finish tweaking them:
Cpp11 should be fine now. One issue is clang versioning: it moves rapidly and always seems a pain to deal with, having to get the latest version all the time. AlwaysBreakAfterReturnType was not added until after at least 3.5. We might consider placing a clang-format binary in the repo: that is probably the best way to go to ensure all machines have the right version for our settings. |
This patch adds a first version of a clang-format style definition for DynamoRIO. I think it is relatively close to the style guide, but I expect some more tweaking. Issue #2876
This patch adds a first version of a clang-format style definition for DynamoRIO. I think it is relatively close to the style guide, but I expect some more tweaking. Issue #2876
Starting to get into the final details here:
|
http://apt.llvm.org/ provides apt repositories for various ubuntu versions (including Trusty) which provides clang-format packages (either clang-format-6.0 or clang-format-7.0 (current LLVM trunk) |
I'm considering putting this in place this weekend. I think even if someone has a large outstanding diff it shouldn't be too painful to merge as it's all whitespace changes and clang-format will do the merge work for you. |
I'm using clang-format 6.0 and am seeing a number of what look like bugs in clang-format as I don't see any competing rule that would cause this behavior. Most of these are found by the vera++ checks, some by visual scanning the diff. Here are some examples: __cdecl confuses it?
Macro-generated-name confuses it:
Single-char operator name confuses it??
Greater-than-90-char lines:
I tried increasing PenaltyExcessCharacter but it's already really high. I |
I want to exclude some files from clang-format-diff.py without marking them
I can't find any simple way to do it. I see references online to a ".clang-format-ignore" file but it seem to be Maybe we could exclude those files from the diff generated to pass to Going to punt for now. Maybe on changes to those the author temporarily |
More clang-format bugs, including an interesting one: having a C++ token in C code seems to cause the parser to mess up. Try formatting this .h file with
Add an
Ditto for These are correctly inside |
I'm about ready to put in the big change. Some things I'm not 100% happy with but overall I am pleased and I think this will improve readability and make contributing and reviewing simpler. I'm pasting my exclusion notes. There is some overlap with prior comments above. *** TODO put exclusions in place I guess we should use the off-on blocks for things like line length in Maybe abandon line-length exclusion for at least some of those files listed Going through them:
**** DONE what about third_party/? => not formatting; want to ignore in travis but unclear how **** DONE what about ext/drsyms/libelftc/include/? => not formatting; want to ignore in travis but unclear how Avoid indenting for that? Not sure how w/o turning off formatting for **** DONE asm code inside .c files => exclude it Don't want this:
I did:
**** DONE configure.cmake.h and dr_api.h ${var} => exclude it
**** DONE mcxtx.h avoid indent? => exclude it mcxtx.h is one example.
***** TODO (i#3092): [cleanup] refactor public interface headers and eliminate patchwork exports via genapi.pl fragment.h: no indent Looks like just the two globals*.h fail to find the include guard. https://reviews.llvm.org/D35955
This seems to trigger it:
So we have:
But:
This also:
These are inside 'ifdndef __cplusplus', but the clang parser must be This is the one in globals.h:
tools.h: "true" again. Fixed by including c_defines.h instead. ext/drgui/drgui_tool_interface.h: small file: living w/ it. ***** DONE make a small c_defines.h to isolate **** DONE don't want to indent inside API_EXPORT_ONLY => genapi.pl removes it
I have genapi.pl removing it.
And then looking at include/dr_defines.h. **** DONE don't want to indent inside CLIENT_INTERFACE in instrument_api.h => genapi.pl removes it **** DONE DR_LOG_* indented => removed if 0 hack no longer needed **** TODO genapi.pl indent removal not perfect: some issues In dr_ir_macros.h:
Prob just going to live with it and let i#3092 clean it up. **** DONE clang-format bug: no break after type for single-char operators => excluded ipc_reader.cpp:
I tried **** DONE clang-format bug: no break after type for __decl => excluded This isn't right -- __cdecl confuses it??
**** DONE clang-format bug: no break after type for macro-wrapped func name => excluded
**** DONE clang-format bug: >90-char lines! => excluded
Some are caused by comment alignment:
I tried increasing PenaltyExcessCharacter but it's already really high. I **** DONE clang-format bug: mis-parses read_and_verify_dr_marker_common => excluded Gets confused by braces in ifdefs and gets the indentation all wrong in the **** DONE clang-format bug: turns define into macro func => excluded
**** DONE clang-format bug: threw out macro line continuations Just for this macro in core/win32/eventlog.c:
If I manually move the "do" to the next line it then works. ?!? **** DONE L_EXPAND_LEVEL fails on multi-line strings
It fits though: a holdover from the failed include guard, b/c clang-format **** DONE clang-format bug: messes up some inline asm => excluded It is nearly always correct but in hooker-ntdll.c and several other tests
|
Massive clang-format of every C or C++ source file (*.h, *.c, *.cpp), except for: + third_party/ + ext/drsysm/libelftc/include/ + ext/drsyms/demangle.cc This is a single, large commit by design to present a single history disruption point and bring the code base into a consistent format. clang-format version 6.0 was used. Tweaks the clang-format rules to indent 4 after pre-processor hashes, to allow single-line case labels, and align trailing comments. Disables the pp_indent vera style check as we changed the indent rule and clang-format now covers it. Leaves the other checks, even though some are redundant (they found clang-format errors). Moves C++ token defines from globals_shared.h and globals.h to a new header, core/lib/c_defines.h, to avoid a clang-format parsing error where it fails to identify the include guard. Adds genapi.pl removal of extra indentation inside API_EXPORT_ONLY and CLIENT_INTERFACE regions. Adds exclusions around large regions we don't want to format, in these files: + core/win32/syscallx.h + core/arch/x86/decode_table.c + core/arch/arm/table_a32_pred.c + core/arch/arm/table_a32_unpred.c + core/arch/arm/table_t32_16.c + core/arch/arm/table_t32_16_it.c + core/arch/arm/table_t32_base.c + core/arch/arm/table_t32_coproc.c + core/arch/arm/table_encode.c Adds smaller exclusions to work around clang-format bugs: + Several missing break-after-return-type + Several >90-char lines + Misc scattered issues, all listed in #2876 Issue: #2876
Massive clang-format of every C or C++ source file (*.h, *.c, *.cpp), except for: + third_party/ + ext/drsysm/libelftc/include/ + ext/drsyms/demangle.cc This is a single, large commit by design to present a single history disruption point and bring the code base into a consistent format. clang-format version 6.0 was used. Tweaks the clang-format rules to indent 4 after pre-processor hashes, to allow single-line case labels, and align trailing comments. Disables the pp_indent vera style check as we changed the indent rule and clang-format now covers it. Leaves the other checks, even though some are redundant (they found clang-format errors). Moves C++ token defines from globals_shared.h and globals.h to a new header, core/lib/c_defines.h, to avoid a clang-format parsing error where it fails to identify the include guard. Adds genapi.pl removal of extra indentation inside API_EXPORT_ONLY and CLIENT_INTERFACE regions. Adds exclusions around large regions we don't want to format, in these files: + core/win32/syscallx.h + core/arch/x86/decode_table.c + core/arch/arm/table_a32_pred.c + core/arch/arm/table_a32_unpred.c + core/arch/arm/table_t32_16.c + core/arch/arm/table_t32_16_it.c + core/arch/arm/table_t32_base.c + core/arch/arm/table_t32_coproc.c + core/arch/arm/table_encode.c Adds smaller exclusions to work around clang-format bugs: + Several missing break-after-return-type + Several >90-char lines + Misc scattered issues, all listed in #2876 Issue: #2876
Adds checking of diff formatting to runsuite.cmake. If clang-format-diff{,.py} is found, it is used to check the formatting, and any format change is a fatal error. On Travis, not finding the program is a fatal error. Adds installation of clang-format-6.0 from apt.llvm.org on Travis. Issue: #2876
I'm having trouble installing clang-format-6.0 on Travis. Whether I do this:
Or this:
I hit this error:
What's weird is that searching github shows several other projects installing in one of these two ways onto Travis and succeeding. So far I can't tell what's different about their setup vs ours. |
Hm it's weird that the clang-format-6.0 package for trusty requires libstdc++6 (4.9), but it looks like trusty only ships version 4.8. Maybe installing gcc-4.9 from https://wiki.ubuntu.com/ToolChain#PPA_packages would do the trick? |
I just saw that http://apt.llvm.org/ recommends using |
Thanks, I did see another project using the source ubuntu-toolchain-r-test: I'll try that. Mac is also broken: https://travis-ci.org/DynamoRIO/dynamorio/jobs/401354219. I will fix it. |
I set up the format check as part of the 64-bit clang, so the final Travis build, to avoid adding more time to the ones running tests. A failure looks like this: https://travis-ci.org/DynamoRIO/dynamorio/jobs/401568760
|
Running the checks only in the one clang build means you can see build failures simultaneously, w/o all builds being blocked up front w/ format errors. |
Fixes the Mac build which was failing on a C++ comment inside configure.h. Issue: #2876
Fixes the Mac build which was failing on a C++ comment inside configure.h. Issue: #2876
There was a double parenthesis causing clang-format to fail to format core/optionsx.h. We fix that here. Issue: #2876
There was a double parenthesis causing clang-format to fail to format core/optionsx.h. We fix that here. Issue: #2876
As part of adding lint style checks in #2369 we considered using clang-format but balked at reformatting every single file and thus adding a history hurdle. But as more contributions come in with style issues and as I see other projects successfully use clang-format to simplify things, I lean more and more toward biting the bullet and reformatting everything and then adding a pre-commit check.
The text was updated successfully, but these errors were encountered: