aboutsummaryrefslogtreecommitdiff
path: root/clang/tools/clang-format/clang-format-diff.py
AgeCommit message (Collapse)AuthorFilesLines
2025-04-18[clang-format] Handle spaces in filename paths in clang-format-diff.py (#135779)Selim Keles1-1/+1
This PR resolves an issue in clang-format-diff.py where filenames containing spaces were not correctly extracted from Git diffs. Due to the previous regex implementation, filenames were being truncated, causing the script to fail when processing diffs with such filenames. Fixes #135619.
2025-04-10Treat ipynb as json (#135137)Tim Clephas1-1/+1
Fixes #110727
2024-08-27[clang-format-diff.py][NFC] Document _stderr as unusedMarcoFalke1-1/+1
The stderr in the result tuple is always None and unused in this context. Document it with an underscore.
2024-08-21[clang-format] Use double hyphen for multiple-letter flags (#100978)magic-akari1-4/+4
- Closes: #100974
2024-03-27[clang-format] Exit clang-format-diff only after all diffs are printed (#86776)Owen Pan1-3/+7
See https://github.com/llvm/llvm-project/pull/70883#issuecomment-2020811077.
2023-12-05[clang-format][NFC] Use `prog` in clang-format-diff.py (#74399)Owen Pan1-3/+1
This is a minor improvement to #73491.
2023-11-28Improve clang-format-diff help output (#73491)serge-sans-paille1-3/+6
It is quite common to symlink clang-format-diff.py to clang-format-diff, and in that case the help output still refers to the .py version. Compute it instead to work in both setup.
2023-11-08[clang-format] Add ability for clang-format-diff to exit with non-0 status ↵Conrad Donahue1-0/+1
(#70883) This patch adds the ability for the clang-format-diff script to exit with a non-zero status if it detects that formatting changes are necessary. This makes it easier to use clang-format-diff as part of a DevOps pipeline, since you could add a stage to run clang-format-diff and fail if the formatting needs to be fixed.
2023-08-24[clang-format] Add Verilog suffixes to the scriptssstwcw1-2/+2
I decided not to wait for D149088 because it was taking a long time. The capture group in the regexp was changed to non-capturing. It doesn't need to be captured. Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D154467
2023-05-23[NFC][Py Reformat] Reformat python files in clang and clang-tools-extraTobias Hieta1-115/+155
This is an ongoing series of commits that are reformatting our Python code. Reformatting is done with `black`. If you end up having problems merging this commit because you have made changes to a python file, the best way to handle that is to run git checkout --ours <yourfile> and then reformat it with black. If you run into any problems, post to discourse about it and we will try to help. RFC Thread below: https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style Reviewed By: MatzeB Differential Revision: https://reviews.llvm.org/D150761
2023-03-09Revert "[clang-format] Add -j to clang-format-diff to speed up formatting"Nikita Popov1-66/+40
This reverts commit d14e7ee3d17cfa60d44256d742c10e9949a6048f. Makes clang_format_diff.py always fail with: Traceback (most recent call last): File "/home/npopov/repos/llvm-project/clang/tools/clang-format/clang-format-diff.py", line 177, in <module> main() File "/home/npopov/repos/llvm-project/clang/tools/clang-format/clang-format-diff.py", line 174, in main process_subprocess_result(proc, args) File "/home/npopov/repos/llvm-project/clang/tools/clang-format/clang-format-diff.py", line 42, in process_subprocess_result with open(filename) as f: ^^^^^^^^ NameError: name 'filename' is not defined
2023-03-02[clang-format] Add -j to clang-format-diff to speed up formattingSean Maher1-40/+66
This patch changes the implementation of clang-format-diff.py to start up many clang-format processes in parallel in order to speed up clang-format-diff.py by several orders of magnitude on large patches. Differential Revision: https://reviews.llvm.org/D141230
2023-02-27[clang-format-diff] Correctly parse start-of-file diffsTamir Duberstein1-3/+10
Handle the case where the diff is a pure removal of lines. Before this change start_line would end up as 0 which is rejected by clang-format. Submitting on behalf of @tamird. Differential Revision: https://reviews.llvm.org/D144291
2021-12-14[clang-format] add support for cppm filesmydeveloperday1-2/+2
C++20 Modules current style is to assign a new file suffix .cppm. https://github.com/llvm/llvm-project/issues/52658 Ensure git-clang-format can handle that as a default extension Reviewed By: ChuanqiXu, HazardyKnusperkeks, curdeius Differential Revision: https://reviews.llvm.org/D115625 Fixes: #52658
2021-10-08[clang-format-diff] Fix missing formatting for zero length git diff linesZequan Wu1-3/+5
If we only delete lines that are outer block statements (if, while, etc), clang-format-diff.py can't format the statements inside the block statements. An example to repro: 1. Delete the if statment at line 118 in llvm/lib/CodeGen/Analysis.cpp. 2. Run `git diff -U0 --no-color HEAD^ | clang/tools/clang-format/clang-format-diff.py -i -p1` It fails to format the statement after if. Differential Revision: https://reviews.llvm.org/D111273
2021-09-09[clang_format] Add fallback-style flag to clang-format-diff.pyHaowei Wu1-0/+7
This change adds fallback-style flag to clang-format-diff.py Differential Revision: https://reviews.llvm.org/D109550
2021-06-26[clang-format] Add basic support for formatting JSONmydeveloperday1-1/+1
I find as I develop I'm moving between many different languages C++,C#,JavaScript all the time. As I move between the file types I like to keep `clang-format` as my formatting tool of choice. (hence why I initially added C# support in {D58404}) I know those other languages have their own tools but I have to learn them all, and I have to work out how to configure them, and they may or may not have integration into my IDE or my source code integration. I am increasingly finding that I'm editing additional JSON files as part of my daily work and my editor and git commit hooks are just not setup to go and run [[ https://stedolan.github.io/jq/ | jq ]], So I tend to go to [[ https://jsonformatter.curiousconcept.com/ | JSON Formatter ]] and copy and paste back and forth. To get nicely formatted JSON. This is a painful process and I'd like a new one that causes me much less friction. This has come up from time to time: {D10543} https://stackoverflow.com/questions/35856565/clang-format-a-json-file https://bugs.llvm.org/show_bug.cgi?id=18699 I would like to stop having to do that and have formatting JSON as a first class clang-format support `Language` (even if it has minimal style settings at present). This revision adds support for formatting JSON using the inbuilt JSON serialization library of LLVM, With limited control at present only over the indentation level This adds an additional Language into the .clang-format file to separate the settings from your other supported languages. Reviewed By: HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D93528
2021-04-22Update shebang for clang-format-diff script to python3.Paula Toth1-1/+1
Different distributions have different strategies migrating the `python` symlink. Debian and its derivatives provide `python-is-python2` and `python-is-python3`. If neither is installed, the user gets no `/usr/bin/python`. The clang-format-diff script and consequently `arc diff` can thus fail with a python not found error. Since we require python greater than 3.6 as part of llvm prerequisites (https://llvm.org/docs/GettingStarted.html#software), let's go ahead and update this shebang. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D100968
2021-03-05[clang-format] Improve clang-format-diff.py error messageDavid Spickett1-5/+13
Previously if we couldn't run the clang-format command for some reason, you'd get an unhelpful error message: ``` OSError: [Errno 2] No such file or directory ``` Which doesn't tell you what was happening to cause this. Catch the error and add the command we were attempting to run: ``` RuntimeError: Failed to run "<...>/clang-food <...>" - No such file or directory" RuntimeError: Failed to run "<...>/clang-format <...>" - Permission denied" ``` Reviewed By: krasimir Differential Revision: https://reviews.llvm.org/D98032
2020-09-18[clang-format] Recognize "hxx" as a C++ header in clang-format-diff.pyMiklos Vajna1-2/+2
And shift "proto" to the next line to avoid a too long line. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D87931
2020-05-07[clang-format] [PR45126] Help text is missing all available formatsmydeveloperday1-2/+2
Summary: https://bugs.llvm.org/show_bug.cgi?id=45126 GNU and Microsoft styles are built in supported styles but are not displayed in the help text Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D79372
2020-05-06[NFC] Improve doc string to mention that paths in diff are used as-isChristopher Tetreault1-1/+5
Summary: Add --relative to the suggested git-diff one liner. If the user does not pass this argument, then git will produce a diff with the path relative to the repository root. If the user's working directory is not the repository root, then clang-format will complain that the file is not found. The --relative argument makes git produce a diff with the files relative to the working directory. Add note to doc string to warn users about the fact that filenames embedded in the diff are used as-is with no attempts to "do what they mean, not what they say" Reviewers: djasper, alexfh, efriedma, klimek, thakis Reviewed By: efriedma Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79054
2020-04-13[clang-format] A Minor change to clang-format-diff.pymydeveloperday1-1/+1
Summary: Testing for None should use the 'is' operator. Reviewed By: MyDeveloperDay Patch By: eagleoflqj Tags: #clang-format Differential Revision: https://reviews.llvm.org/D77974
2019-09-24[clang-format] [PR36858] Add missing .hh and .cs extensions from python ↵Paul Hoad1-2/+2
support utilities Summary: https://bugs.llvm.org/show_bug.cgi?id=36858 identifies .hh as a missing C++ header extension file while making this change I realized there was no support for .cs files which were added recently Reviewers: pseyfert, klimek, owenpan Reviewed By: klimek Subscribers: cfe-commits Tags: #clang-tools-extra, #clang Differential Revision: https://reviews.llvm.org/D67949 llvm-svn: 372760
2019-07-23Improve clang-format-diff help outputNico Weber1-8/+4
The description in clang-format-diff.py is more useful than the one in `clang-format-diff -h`, so use the same description in both places. Differential Revision: https://reviews.llvm.org/D64998 llvm-svn: 366828
2019-02-11[tools] Fix python DeprecationWarning: invalid escape sequenceSerge Guelton1-2/+2
The python documentation says "it’s highly recommended that you use raw strings for all but the simplest expressions." (https://docs.python.org/3/library/re.html) So do that with the attached patch generated by sed -i -e "s/re.search('/re.search(r'/g" $(git grep -l 're.search(') The warning can be seen in e.g. python3.7: $ python3.7 -Wd >>> import re; re.search('\s', '') <stdin>:1: DeprecationWarning: invalid escape sequence \s Commited on behalf of Marco Falke. Differential Revision: https://reviews.llvm.org/D57528 llvm-svn: 353707
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2019-01-03Portable Python script across Python versionSerge Guelton1-4/+5
StringIO is obsoleted in Python3, replaced by io.BytesIO or io.StringIO depending on the use. Differential Revision: https://reviews.llvm.org/D55196 llvm-svn: 350318
2018-12-18Portable Python script across Python versionSerge Guelton1-1/+1
Make scripts more future-proof by importing most __future__ stuff. Differential Revision: https://reviews.llvm.org/D55208 llvm-svn: 349504
2018-12-18Portable Python script across Python versionSerge Guelton1-0/+1
Using from __future__ import print_function it is possible to have a compatible behavior of `print(...)` across Python version. Differential Revision: https://reviews.llvm.org/D55213 llvm-svn: 349454
2018-08-03clang-format-diff: Make it work with python3 tooKrasimir Georgiev1-10/+15
Summary: It is not necessary, but would be nice if the script run on python3 as well (as opposed to only python2, which is going to be deprecated https://pythonclock.org/) Contributed by MarcoFalke! Reviewers: krasimir Reviewed By: krasimir Subscribers: lebedev.ri, sammccall, cfe-commits Differential Revision: https://reviews.llvm.org/D48098 llvm-svn: 338839
2016-12-03Add the --no-color option to the git call in the doc when using ↵Sylvestre Ledru1-1/+1
clang-format-diff llvm-svn: 288605
2016-01-20clang-format-diff: Replace hard-code default for clang-format binary with flag.Daniel Jasper1-9/+6
Patch by Kwasi Mensah, thank you. llvm-svn: 258328
2015-10-07clang-format: Add include sorting capabilities to sublime, emacs andDaniel Jasper1-0/+4
clang-format-diff.py. llvm-svn: 249567
2015-03-11clang-format: Recognize the .ts (TypeScript) extension as JavaScript.Daniel Jasper1-1/+1
Patch by Martin Probst. Thank you. llvm-svn: 231926
2014-12-08clang-format: Make clang-format-diff.py format java files.Daniel Jasper1-1/+1
llvm-svn: 223685
2014-11-14clang-format: Give clang-format-diff.py a -v option.Daniel Jasper1-0/+4
With it, it prints the file being formatted. Apparently people are formatting thousands of files and some progress indication is helpful. llvm-svn: 221990
2014-09-02Use /usr/bin/env python instead of /usr/bin/python.Ed Schouten1-1/+1
On operating systems like the BSDs, it is typically the case that /usr/bin/python does not exist. We should therefore use /usr/bin/env instead. This is also done in various other scripts in tools/. llvm-svn: 216945
2014-05-14clang-format: Add clang-format-diff usage examples for SVN.Daniel Jasper1-1/+2
llvm-svn: 208766
2014-01-21clang-format: Enable formatting for .proto and .protodevel files.Daniel Jasper1-1/+2
Support for protocol buffer files seems complete enough. llvm-svn: 199750
2013-12-19Fix usage description of clang-format-diff.py.Daniel Jasper1-2/+2
llvm-svn: 197668
2013-12-18clang-format-diff.py: fix -regex/-iregex matchingAlp Toker1-4/+4
While debating the finer points of file extension matching, we somehow missed the bigger problem that the current code will match anything starting with the default or user-specified pattern (e.g. lit.site.cfg.in). Fix this by doing what find(1) does, implicitly wrapping the pattern with ^$. llvm-svn: 197608
2013-12-18clang-format-diff.py: add the OpenCL file extensionAlp Toker1-1/+1
It's handled correctly as a C-family language. llvm-svn: 197542
2013-12-16Added -iregex for case-insensitive regex to filter file names.Alexander Kornienko1-5/+13
Summary: -regex and -iregex both mimic options of the find utility. Made the default list of extensions case-insensitive, so that it's not only C and CPP extensions are accepted in upper case. Reviewers: djasper Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D2415 llvm-svn: 197378
2013-12-10clang-format-diff.py: Support -regex filter and more filename extensionsAlp Toker1-4/+5
Add support for more filename extensions based on the list in the clang plus JavaScript. Also adds a -regex option so users can override defaults if they have unusual file extensions or want to format everything in the diff. Keeping with tradition the flag is modelled on Unix conventions, this time matching the semantics of find(1). llvm-svn: 196917
2013-12-05clang-format-diff.py: pass through errors to stderr, not stdoutAlp Toker1-5/+2
Also use write() for unified diff output to avoid further processing by the print function (e.g. trailing newline). llvm-svn: 196484
2013-12-04clang-format-diff.py: Fix 'beintroduced' in help outputAlp Toker1-1/+1
Also update docs to reflect recently changed -i inplace edit behaviour. llvm-svn: 196336
2013-10-11Changed clang-format-diff.py to output diff by default. Added -i option to ↵Alexander Kornienko1-3/+22
apply changes to files instead. Summary: "svn diff|clang-format-diff.py" will just output the diff. Now it's possible to use: svn diff|clang-format-diff.py|patch -p0 as an equivalent to: svn diff|clang-format-diff.py -i ;) Reviewers: djasper Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1840 llvm-svn: 192505
2013-10-08clang-format: Don't exit with failure on empty files.Daniel Jasper1-1/+2
Also let clang-format-diff.py detect errors based on clang-format's return code. Otherwise messages like "Can't find usable .clang-format, falling back to LLVM style" can make it fail, which might be undesired. Patch by Alp Toker. Thank you! llvm-svn: 192184
2013-10-02clang-format: Fix clang-format-diff.py according to diff specification.Daniel Jasper1-2/+5
Patch by Alp Toker. Many thanks! Original descriptions: clang-format-diff incorrectly modifies unchanged lines due to an error in diff parsing. The unified diff format has a default line change count of 1, and 0 may be specified to indicate that no lines have been added. This patch updates the parser to accurately reflect the diff specification. This also has the benefit of stabilising the operation so it will produce the same output when run multiple times on the same changeset, which was previously not the case. No tests added because this script is not currently tested (though we should look into that!) llvm-svn: 191820