355 Commits

Author SHA1 Message Date
James Y Knight
c7f3437507
NFC: Clean up of IntrusiveRefCntPtr construction from raw pointers. (#151545)
Handles clang::DiagnosticsEngine and clang::DiagnosticIDs.

For DiagnosticIDs, this mostly migrates from `new DiagnosticIDs` to
convenience method `DiagnosticIDs::create()`.

Part of cleanup https://github.com/llvm/llvm-project/issues/151026
2025-07-31 15:07:35 -04:00
James Y Knight
9ddbb478ce
NFC: Clean up construction of IntrusiveRefCntPtr from raw pointers for llvm::vfs::FileSystem. (#151407)
This switches to `makeIntrusiveRefCnt<FileSystem>` where creating a new
object, and to passing/returning by `IntrusiveRefCntPtr<FileSystem>`
instead of `FileSystem*` or `FileSystem&`, when dealing with existing
objects.

Part of cleanup #151026.
2025-07-31 09:57:13 -04:00
Owen Pan
da283b54d9 Revert "[clang-format] Fix an off-by-1 bug with -length option (#143302)"
This reverts commit 1fae5918b3d6fbed8ce6d8a2edf31bdf304ca8db because it may
break VSCode.

Closes #146036
2025-07-14 22:42:12 -07:00
Owen Pan
1fae5918b3
[clang-format] Fix an off-by-1 bug with -length option (#143302)
Also validate the argument value.

Fixes #56245
2025-06-13 00:45:52 -07:00
Owen Pan
f6eaa2b00c
Reland "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp" (#143477)
Reapply https://github.com/llvm/llvm-project/pull/143236 and fix the bug
reported in
https://github.com/llvm/llvm-project/pull/143236#issuecomment-2957102180.
2025-06-12 08:29:49 -07:00
Jorge Gorbe Moya
3c02150f02 Revert "[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp (#143236)"
This reverts commit 897ccddcc30cacdfe04ddd622986b50ec963eabc. It
introduced a bug when formatting multiple files in one go. When a
shorter file is passed after a longer one, a stale length from the
previous file seems to be used, triggering an "invalid length (...) is
outside the file" error.
2025-06-09 14:42:32 -07:00
Owen Pan
897ccddcc3
[clang-format][NFC] Clean up fillRanges() in ClangFormat.cpp (#143236) 2025-06-07 20:59:53 -07:00
Jan Svoboda
13e1a2cb22 Reapply "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit e2a885537f11f8d9ced1c80c2c90069ab5adeb1d. Build failures were fixed right away and reverting the original commit without the fixes breaks the build again.
2025-05-22 12:52:03 -07:00
Kazu Hirata
e2a885537f Revert "[clang] Remove intrusive reference count from DiagnosticOptions (#139584)"
This reverts commit 9e306ad4600c4d3392c194a8be88919ee758425c.

Multiple builtbot failures have been reported:
https://github.com/llvm/llvm-project/pull/139584
2025-05-22 12:44:20 -07:00
Jan Svoboda
9e306ad460
[clang] Remove intrusive reference count from DiagnosticOptions (#139584)
The `DiagnosticOptions` class is currently intrusively
reference-counted, which makes reasoning about its lifetime very
difficult in some cases. For example, `CompilerInvocation` owns the
`DiagnosticOptions` instance (wrapped in `llvm::IntrusiveRefCntPtr`) and
only exposes an accessor returning `DiagnosticOptions &`. One would
think this gives `CompilerInvocation` exclusive ownership of the object,
but that's not the case:

```c++
void shareOwnership(CompilerInvocation &CI) {
  llvm::IntrusiveRefCntPtr<DiagnosticOptions> CoOwner = &CI.getDiagnosticOptions();
  // ...
}
```

This is a perfectly valid pattern that is being actually used in the
codebase.

I would like to ensure the ownership of `DiagnosticOptions` by
`CompilerInvocation` is guaranteed to be exclusive. This can be
leveraged for a copy-on-write optimization later on. This PR changes
usages of `DiagnosticOptions` across `clang`, `clang-tools-extra` and
`lldb` to not be intrusively reference-counted.
2025-05-22 12:33:52 -07:00
Owen Pan
0dfdf7efbf
[clang-format] Handle raw string literals containing JSON code (#140666)
Fix #65400
2025-05-20 19:15:57 -07:00
Owen Pan
5ddcd765db
[clang-format][NFC] Upgrade SortIncludes option to a struct (#140497)
This allows adding other suboptions e.g. IgnoreExtension in #137840.
2025-05-19 01:32:17 -07:00
Selim Keles
0c0b5abff7
[clang-format] Handle spaces in filename paths in clang-format-diff.py (#135779)
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-18 19:42:22 -07:00
Owen Pan
72540dbe01 [clang-format][NFC] Reformat git-clang-format 2025-04-10 19:07:44 -07:00
Tim Clephas
c893f1d18d
Treat ipynb as json (#135137)
Fixes #110727
2025-04-10 20:40:43 +02:00
Wenju He
923da2b843
[clang-format] Add 'cl' to enable OpenCL kernel file formatting (#134529)
There are many .cl files in llvm repo. It would be great that
clang-format can support it.
2025-04-10 01:23:46 -07:00
Owen Pan
4b86a7f386 [clang-format] Update the minimum python version requirement
See https://llvm.org/docs/GettingStarted.html#software

Fix #131456
2025-03-16 16:42:19 -07:00
Ron Gut
d0170424b7
[emacs][clang-format] Suppress reference to free variable errors (#126254)
vc-git-program is referenced but not defined -- vc-git provides the
definition.
2025-02-07 11:04:24 -05:00
goldsteinn
32be90db26
[emacs][clang-format] Add elisp API for clang-format on git diffs (#112792)
New proposed function `clang-format-vc-diff`.

It is the same as calling `clang-format-region` on all diffs between
the content of a buffer-file and the content of the file at git
revision HEAD. This is essentially the same thing as:
    `git-clang-format -f {filename}`
If the current buffer is saved.

The motivation is many project (LLVM included) both have code that is
non-compliant with there clang-format style and disallow unrelated
format diffs in PRs. This means users can't just run
`clang-format-buffer` on the buffer they are working on, and need to
manually go through all the regions by hand to get them
formatted. This is both an error prone and annoying workflow.
2025-02-04 17:17:43 -06:00
Owen Pan
e25c556abe [clang-format][NFC] Reformat git-clang-format with black -l80 2024-12-05 23:18:24 -08:00
Owen Pan
fb2cbc00e0 [clang-format] Reformat Python script git-clang-format with black 2024-12-05 20:26:02 -08:00
Jim B
56ab56c857
[clang-format] Add support for .cjs as JavaScript file extension (#118188)
Node uses `.cjs` as an extension for 'common javascript' modules.
2024-12-03 09:03:51 -08:00
루밀LuMir
6c7b988e64
[clang-format] Add mjs to default_extensions in git-clang-format (#117730) 2024-11-26 22:56:25 -08:00
Owen Pan
b9731a479c [clang-format][doc] Minor cleanup 2024-11-24 23:15:39 -08:00
Nagy-Egri Máté Ferenc
362273d143
[clang-format] Fix path expansion inside git-clang-format.bat (#114078)
The trampoline script used on Windows (due to the absence of shebang
support) doesn't properly expand the path to the Python script, as it
leaves out the drive letter.

Functionally equivalent reproducer in action
```
PS C:\Users\mate> gc (gcm git-clang-formatish.bat).Source
@ECHO OFF
echo "%~pn0" %*
PS C:\Users\mate> git-clang-formatish
"\Users\mate\git-clang-formatish"
```

Adding `d` to the variable modifiers [as per the
docs](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/for)
the drive letter is added. Even in the magical cases when it works.

(I couldn't reproduce, but I suspect it's only tested from some
bash/cygwin variant, where the path becomes `/c/Program Files/...`, but
the drive letter is needed. Without it, I also observed cases when used
via `git clang-format` (without the inital dash) it tries to infer the
drive letter based on the current working directory. In that case it
fails to find `D:\Program Files\LLVM\bin\clang-format.exe`, which
naturally fails, because `Program Files` is on `C:`)
2024-10-30 00:51:11 -07:00
Owen Pan
801f3a5400
[clang-format] Print the names of unfound files in error messages (#113640)
Also fix the return status when `-i` is used with reading from stdin.

Fixes #113631.
2024-10-25 19:29:21 -07:00
Simon Pilgrim
f24c1dd08e Fix MSVC "signed/unsigned mismatch" warning. NFC. 2024-10-25 18:11:28 +01:00
kakkoko
efd8938d57
[clang-format] Fix working -assume-filename with .clang-format-ignore (#113100)
Fixes #113099.

---------

Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-10-24 18:13:08 -07:00
Campbell Barton
093d4db2f3
Add "clang-format-on-save-mode" minor mode to clang-format.el (#104533)
Add an minor mode which can be optionally used to run clang-format on
save.

Formatting before saving works well and is convenient to avoid having to
remember to manually run clang format.

I've written this as it's own package but it's probably better if the
functionality is supported by clang-format.el.
See: https://github.com/melpa/melpa/pull/8762
2024-10-19 22:09:06 +02:00
Owen Pan
85df28180b
[clang-format] Fix a bug that always returns error for JSON (#112839)
Fixes #108556.
2024-10-18 21:10:00 -07:00
Owen Pan
12c0823d67
[clang-format] Handle spaces in file paths in git-clang-format.bat (#107041)
This patch is provided by @jeliebig.

Fixes #107017.
2024-09-03 19:06:40 -07:00
c8ef
4f4bd41f70
[NFC] Fix typos (#106817)
Fixes #106761.
2024-08-31 19:12:57 -07:00
Danial Klimkin
158ba731c9
Fix clang-format-test.el past 625841c (#106398) 2024-08-28 16:00:50 +02:00
MarcoFalke
fac484ccc6
[clang-format-diff.py][NFC] Document _stderr as unused
The stderr in the result tuple is always None and unused in this
context.

Document it with an underscore.
2024-08-27 11:07:17 +02:00
magic-akari
625841c3be
[clang-format] Use double hyphen for multiple-letter flags (#100978)
- Closes: #100974
2024-08-21 18:22:21 +02:00
Owen Pan
986bc3d071
[clang-format] Fix a serious bug in git clang-format -f (#102629)
With the --force (or -f) option, git-clang-format wipes out input files
excluded by a .clang-format-ignore file if they have unstaged changes.

This patch adds a hidden clang-format option --list-ignored that lists
such excluded files for git-clang-format to filter out.

Fixes #102459.
2024-08-10 13:31:35 -07:00
Owen Pan
9fa17fea3c
[clang-format] Handle parenthesized list in RemoveParentheses (#100852)
Also, reformat clang-format source to remove redundant parentheses
enclosing single list items.

Fixes #100768.
2024-08-02 01:29:05 -07:00
Nour
c967c7e022
[clang-format][NFC] Remove an else after a return statement (#94548) 2024-06-06 18:36:33 -07:00
Owen Pan
1c58208d89 [clang-format][NFC] Remove redundnat llvm::, clang::, etc. 2024-05-20 20:32:18 -07:00
Owen Pan
b11a6607cb [clang-format][NFC] Reformat with 18.1.5 2024-05-15 21:04:46 -07:00
Sean Perry
e22ce615fe
[z/OS] treat text files as text files so auto-conversion is done (#90128)
To support auto-conversion on z/OS text files need to be opened as text files. These changes will fix a number of LIT failures due to text files not being converted to the internal code page.

update a number of tools so they open the text files as text files
add support in the cat.py to open a text file as a text file (Windows will continue to treat all files as binary so new lines are handled correctly)
add env var definitions to enable auto-conversion in the lit config file.
2024-05-01 10:39:41 -04:00
Jing Wang
6dbd4bb3b4
Add txtpb to the list of supported TextProto extensions (#88355)
According to
https://protobuf.dev/reference/protobuf/textformat-spec/#text-format-files,
txtpb is the canonical extension

Co-authored-by: Jing Wang <99jingw@gmail.com>
2024-04-12 21:41:06 +02:00
Owen Pan
d9e3e11ae5
[clang-format] Exit clang-format-diff only after all diffs are printed (#86776)
See
https://github.com/llvm/llvm-project/pull/70883#issuecomment-2020811077.
2024-03-27 21:23:37 -07:00
Roberto Bampi
a29e9e32c5
[clang-format] Add --fail-on-incomplete-format. (#84346)
At the moment clang-format will return exit code 0 on incomplete
results. In scripts it would sometimes be useful if clang-format would
instead fail in those cases, signalling that there was something wrong
with the code being formatted.

---------

Co-authored-by: Björn Schäpers <github@hazardy.de>
Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-03-21 01:29:25 -07:00
Owen Pan
b2082a9817 Revert "[clang-format][NFC] Delete 100+ redundant #include lines in .cpp files"
This reverts commit b92d6dd704d789240685a336ad8b25a9f381b4cc. See
github.com/llvm/llvm-project/commit/b92d6dd704d7#commitcomment-139992444

We should use a tool like Visual Studio to clean up the headers.
2024-03-19 21:28:22 -07:00
Owen Pan
b92d6dd704 [clang-format][NFC] Delete 100+ redundant #include lines in .cpp files 2024-03-16 22:24:11 -07:00
Owen Pan
5ac784d1d0 Revert "[clang-format][NFC] Don't use clang-format style in config files"
This reverts commit c69ec700adec315b3daa55742f2ef655242fa297, which has been
obsolete since commit dec63221d56e because the clang-format style was added
in version 18.
2024-03-16 02:39:37 -07:00
Owen Pan
8f6e13e6da
[clang-format] Fix a regression in dumping the config (#80628)
Commit d813af73f70f addressed a regression introduced by commit
3791b3fca6ea
but caused `clang-format -dump-config` to "hang".

This patch reverts changes to ClangFormat.cpp by both commits and
reworks the cleanup.

Fixes #80621.
2024-02-07 21:35:43 -08:00
Ben Hamilton (Ben Gertzfield)
d813af73f7
[Format] Fix detection of languages when reading from stdin (#79051)
The code cleanup in #74794 accidentally broke detection of languages by
reading file content from stdin, e.g. via `clang-format -dump-config - <
/path/to/filename`.

This PR adds unit and integration tests to reproduce the issue and adds
a fix.

Fixes: #79023
2024-01-23 13:32:41 -07:00
darkfeline
5b4abae763
[emacs] Fix Emacs library formatting (#76110)
This makes it easier to ship/install these using the builtin Emacs
package format (in particular, a Version is required).
2024-01-10 13:14:21 +01:00