summaryrefslogtreecommitdiff
path: root/clang/lib/Format/UnwrappedLineFormatter.cpp
AgeCommit message (Collapse)Author
2025-11-05[clang-format] Fix brace wrapping for Java records (#164711)Hannes Braun
The brace wrapping for Java records should now behave similar to classes. Before, opening braces for Java records were always placed in the same line as the record definition.
2025-10-02[clang-format][NFC] Introduce isNoneOf (#161021)Björn Schäpers
And apply throughout the code base.
2025-09-14[clang-format] Add IndentPPDirectives Leave option (#139750)Gedare Bloom
Allow an option to leave preprocessor directive indenting as-is. This simplifies handling mixed styles of CPP directive indentation. Fixes #38511
2025-08-29[clang-format] Allow short function body on a single line (#151428)闫立栋
Fix #145161
2025-08-17[clang-format] Add SpaceInEmptyBraces option (#153765)owenca
Also set it to SIEB_Always for WebKit style. Closes #85525. Closes #93635.
2025-07-06[clang-format][NFC] Replace size() with empty() (#147164)Owen Pan
2025-07-05[clang-format] Propagate `LeadingEmptyLinesAffected` when joining lines ↵Eric Li
(#146761) Before this commit, when `LineJoiner` joins a line with affected leading whitespace, it would drop the knowledge of this entirely. However, when the `AffectedRangeManager` is computing the affected lines, the leading empty whitespace lines are potentially considered for non-first tokens in the `AnnotatedLine`. This causes a discrepancy in behavior when an `AnnotatedLine` is put together from joining multiple lines versus when it is not. We change `LineJoiner::join` to follow `AffectedRangeManager`'s logic, considering the leading whitespace when determining `Affected` for a token. https://github.com/llvm/llvm-project/blob/a63f57262898588b576d66e5fd79c0aa64b35f2d/clang/lib/Format/AffectedRangeManager.cpp#L111-L130 Fixes #138942.
2025-05-19[clang-format] Merge short inline function in macro definition body (#140366)Owen Pan
Fix #62356
2025-04-18[clang-format] Fix a bug in BWACS_MultiLine (#136281)Owen Pan
Fixes #136266
2025-04-16[clang-format] Fix a bug in BWACS_MultiLine (#135906)Owen Pan
Fix #51940
2025-04-12[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466)Owen Pan
Also remove redundant name qualifiers format::, FormatStyle::, and LanguageKind::.
2025-04-09[clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) (#134973)Owen Pan
This allows simplification of code that checks if a token is an Objective-C keyword. Also, delete the following in UnwrappedLineParser::parseStructuralElement(): - an else-after-break in the tok::at case - the copypasted code in the tok::objc_autoreleasepool case
2025-04-05[clang-format] Merge inline short functions for BS_Whitesmiths (#134473)Owen Pan
Fix #126747
2025-03-06[clang-format] Remove special handling of C++ access specifiers in C (#129983)Owen Pan
This effectively reverts d1aed486efc6d35a81ca4acbabb4203c4b91cda9 because of #129426.
2025-02-14Revert "[NFC] Avoid potential null dereference." (#127228)schittir
Reverts llvm/llvm-project#126872 The commit, though harmless, is unnecessary.
2025-02-13[clang-format] Support BraceWrapping.AfterNamespace with ↵Galen Elias
AllowShortNamespacesOnASingleLine (#123010) AllowShortNamespacesOnASingleLine assumes that there is no newline before the namespace brace, however, there is no actual reason this shouldn't be compatible with BraceWrapping.AfterNamespace = true. This is a little tricky in the implementation because UnwrappedLineFormatter works on lines, so being flexible about the offsets is awkward. Not sure if there is a better pattern for combining the 'AllowShort' options with the various configurations of BraceWrapping, but this seemed mostly reasonable. Really, it would almost be preferable to just pattern match on the direct token stream, rather than the AnnotatedLines, but I'm not seeing a straightforward way to do that. --------- Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-02-12[NFC] Avoid potential null dereference. (#126872)schittir
Add a null check.
2025-01-19[clang-format] Improve brace wrapping and add an option to control ↵Sirraide
indentation of `export { ... }` (#110381) `export { ... }` blocks can get a bit long, so I thought it would make sense to have an option that makes it so their contents are not indented (basically the same argument as for namespaces). This is based on the `NamespaceIndentation` option, except that there is no option to control the behaviour of `export` blocks when nested because nesting them doesn’t really make sense. Additionally, brace wrapping of short `export { ... }` blocks is now controlled by the `AllowShortBlocksOnASingleLine` option. There is no separate option just for `export` blocks because you can just write e.g. `export int x;` instead of `export { int x; }`. This closes #121723.
2025-01-09[clang-format] Stop fixing indentation on namespace closing brace (#122234)Owen Pan
Fixes #119790.
2025-01-04[clang-format] Add `TT_CompoundRequirementLBrace` for better annotation ↵Owen Pan
(#121539) Also, add `ST_CompoundRequirement` to help annotating */&/&& in compound requirement expressions as `TT_BinaryOperator`. Fixes #121471.
2025-01-04[clang-format][NFC] Replace SmallVectorImpl with ArrayRef (#121621)Owen Pan
2025-01-02[clang-format] Add option WrapNamespaceBodyWithNewlines (#106145)dmasloff
It wraps the body of namespace with additional newlines, turning this code: ``` namespace N { int function(); } ``` into the following: ``` namespace N { int function(); } ``` --------- Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-12-30[clang-format] Add `AllowShortNamespacesOnASingleLine` option (#105597)Galen Elias
This fixes #101363 which is a resurrection of a previously opened but never completed review: https://reviews.llvm.org/D11851 The feature is to allow code like the following not to be broken across multiple lines: ``` namespace foo { class bar; } namespace foo { namespace bar { class baz; } } ``` Code like this is commonly used for forward declarations, which are ideally kept compact. This is also apparently the format that include-what-you-use will insert for forward declarations. Also, fix an off-by-one error in `CompactNamespaces` code. For nested namespaces with 3 or more namespaces, it was incorrectly compacting lines which were 1 or two spaces over the `ColumnLimit`, leading to incorrect formatting results.
2024-06-30[clang-format] Add option to remove leading blank lines (#91221)sstwcw
The options regarding which blank lines are kept are also aggregated. The new option is `KeepEmptyLines`. This patch was initially part of 9267f8f19a2e502e. I neglected to check the server builds before I added it. It broke clangd. Jie Fu fixed the problem in 4c91b49bab0728d4. I was unaware of it. I thought the main branch was still broken. I reverted the first patch in 70cfece24d6cbb57. It broke his fix. He reverted it in c69ea04fb9738db2. Now the feature is added again including the fix.
2024-06-25Revert "[clang-format] Add option to remove leading blank lines (#91221)"sstwcw
This reverts commit 9267f8f19a2e502ef5a216c0d52b352b3699d399. I changed a formatter option. I forgot to update other components that depend on the formatter when the option name changed.
2024-06-25[clang-format] Add option to remove leading blank lines (#91221)sstwcw
The options regarding which blank lines are kept are also aggregated. The new option is `KeepEmptyLines`.
2024-06-16[clang-format] Handle AttributeMacro before access modifiers (#95634)Owen Pan
Closes #95094.
2024-05-06[clang-format] Handle Java switch expressions (#91112)Owen Pan
Also adds AllowShortCaseExpressionOnASingleLine option and AlignCaseArrows suboption of AlignConsecutiveShortCaseStatements. Fixes #55903.
2024-04-10[clang-format] Don't merge a short block for SBS_Never (#88238)Owen Pan
Also fix unit tests. Fixes #87484.
2024-03-19Revert "[clang-format][NFC] Delete 100+ redundant #include lines in .cpp files"Owen Pan
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-16[clang-format][NFC] Delete 100+ redundant #include lines in .cpp filesOwen Pan
2024-02-23[clang-format][NFC] Enable RemoveSemicolon for clang-format style (#82735)Owen Pan
Also insert separators for decimal integers longer than 4 digits.
2024-01-15[clang-format] Stop aligning the to continuation lines (#76378)sstwcw
Some unwrapped lines are marked as continuations of the previous lines, for example the ports in a Verilog module header. Previously, if the first line following the ports line was changed, and git-clang-format was run, the changed line would be indented by an extra continuation indentation.
2024-01-11[ClangFormat] Fix formatting bugs. (#76245)r4nt
1. There are multiple calls to addFakeParenthesis; move the guard to not assign fake parenthesis into the function to make sure we cover all calls. 2. MustBreakBefore can be set on a token in two cases: either during unwrapped line parsing, or later, during token annotation. We must keep the latter, but reset the former. 3. Added a test to document that the intended behavior of preferring not to break between a return type and a function identifier. For example, with MOCK_METHOD(r, n, a)=r n a, the code MOCK_METHOD(void, f, (int a, int b)) should prefer the same breaks as the expanded void f(int a, int b).
2023-12-06[clang-format] Handle merging functions containing only a block comment (#74651)Owen Pan
Fixed #41854.
2023-12-06[clang-format][NFC] Refactor getting first/last non-comment of line (#74570)Owen Pan
2023-11-29[clang-format] Finalize children after formatting them (#73753)Owen Pan
This would also fix the overlapping replacements below: ``` $ clang-format a( #else #endif ) = []() { )} The new replacement overlaps with an existing replacement. New replacement: <stdin>: 38:+7:" " Existing replacement: <stdin>: 38:+7:" " ``` Fixed #73487.
2023-10-25[clang-format] AllowShortCompoundRequirementOnASingleLineBackl1ght
clang-format brace wrapping did not take requires into consideration, compound requirements will be affected by BraceWrapping.AfterFunction. Closes #59412. Differential Revision: https://reviews.llvm.org/D139834
2023-09-05[clang-format] Fix segmentation fault when formatting nested namespacesArkadiy Yudintsev
Fixing the clang-format crash with the segmentation fault error when formatting code with nested namespaces. Fixes #64701. Differential Revision: https://reviews.llvm.org/D158363
2023-08-24[clang-format][NFC] Replace !is() with isNot()Owen Pan
Differential Revision: https://reviews.llvm.org/D158571
2023-07-18[clang-format] Fix indent for selective formattingSedenion
The problem was that the LevelIndentTracker remembered the indentation level of previous deeper levels when leaving a scope. Afterwards, when it entered again a deeper level, it blindly reused the previous indentation level. In case the --lines option was used such that the previous deeper level was not formatted, that previous level was whatever happened to be there in the source code. The formatter simply believed it. This is fixed by letting the LevelIndentTracker forget the previous deeper levels when stepping out of them (=> change in LevelIndentTracker::nextLine()). Note that this used to be the case until LLVM 14.0.6, but was changed in https://reviews.llvm.org/D129064 (#56352) to fix a crash. Our commit here essentially reverts that crash fix. It was incorrect/incomplete. Fixes #58464. Fixes #59178. Fixes #62799. Differential Revision: https://reviews.llvm.org/D151047
2023-07-18[clang-format] Refactoring and asserts in LevelIndentTracker. (NFC)Sedenion
adjustToUnmodifiedLine: The code does something only for non-PP-directives. This is now reflected by putting the if-check to the top. This also ensures that the assert() there is executed only if IndentForLevel is actually accessed. getIndent(): assert valid index into IndentForLevel. Added explanation regarding the intention of IndentForLevel. Differential Revision: https://reviews.llvm.org/D155094
2023-07-11[clang-format][NFC] Remove redundant parentheses in the source codeOwen Pan
Reformat the source code with RemoveParentheses set to ReturnStatement.
2023-06-21[clang-format] Don't finalize #if, #else, #endif, etc.Owen Pan
Don't finalize a preprocessor branch directive if it's the first token of an annotated line. See the rationale at https://reviews.llvm.org/D150057#inline-1449546. Fixes #63379 Differential Revision: https://reviews.llvm.org/D153243
2023-06-16Reland [clang-format] Fix overlapping whitespace replacements before PPDirectiveOwen Pan
If the first token of an annotated line already has a computed Newlines, reuse it to avoid potential overlapping whitespace replacements before preprocessor branching directives. Fixes #62892. Differential Revision: https://reviews.llvm.org/D151954
2023-06-13Reland [clang-format] Add the KeepEmptyLinesAtEOF optionOwen Pan
2023-06-09[clang-format] Add test case for issue 63170Paul Kirth
After https://reviews.llvm.org/D151954 we've noticed some issues w/ clang-format behavior, as outlined in https://github.com/llvm/llvm-project/issues/63170. Valid C/C++ files, that were previously accepted, are now rejected by clang-format, emitting the message: "The new replacement overlaps with an existing replacement." This reverts commit 4b9764959dc4b8783e18747c1742ab164e4bc4ee and d2627cf88d2553a4c2e850430bdb908a4b7d2e52, which depends on it. Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D152473
2023-06-08Revert "Revert "[clang-format] Fix overlapping replacements before ↵Leonard Chan
PPDirectives"" This reverts commit 94e75469597f197f9c4b45baa6c8a576c78dbd02. Apparently I broke some builders with the original revert: http://45.33.8.238/linux/109159/step_7.txt
2023-06-07Revert "[clang-format] Fix overlapping replacements before PPDirectives"Leonard Chan
This reverts commit 4b9764959dc4b8783e18747c1742ab164e4bc4ee. Reverting since this causes clang-formtat to incorrectly fall into an error path yet return a zero exit code despite not formatting the file at all. See https://github.com/llvm/llvm-project/issues/63170
2023-06-07[clang-format] Add the KeepEmptyLinesAtEOF optionOwen Pan
Adds an option KeepEmptyLinesAtEOF to keep empty lines (up to MaxEmptyLinesToKeep) before EOF. This remedies the probably unintentional change in behavior introduced in 3d3ea84a4f8f, which started to always remove empty lines before EOF. Fixes #56054. Fixes #63150. Differential Revision: https://reviews.llvm.org/D152305