aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/Format.cpp
AgeCommit message (Collapse)AuthorFilesLines
6 days[clang-format] Correctly handle backward compatibility of C headers (#159908)owenca1-38/+53
This in effect reverts 05fb8408de23c3ccb6125b6886742177755bd757 and 7e1a88b9d1431e263258e3ff0f729c1fdce342d3, the latter of which erroneously changed the behavior of formatting `ObjC` header files when both the default and `ObjC` styles were absent. Now the previous behavior of treating that as an error is restored. Fixes #158704
9 days[clang-format] Add AllowBreakBeforeQtProperty option (#159909)owenca1-0/+3
The test cases are adapted from #131605.
11 days[clang-format][NFC] Add is_sorted() assertion for binary_search()Owen Pan1-0/+1
2025-09-14[clang-format] Handle C digit separators (#158418)owenca1-0/+1
Fixes #158413
2025-09-14[clang-format] Add IndentPPDirectives Leave option (#139750)Gedare Bloom1-0/+1
Allow an option to leave preprocessor directive indenting as-is. This simplifies handling mixed styles of CPP directive indentation. Fixes #38511
2025-09-12[clang-format] Add an option to format numeric literal case (#151590)Andy MacGregor1-0/+29
Some languages have the flexibility to use upper or lower case characters interchangeably in integer and float literal definitions. I'd like to be able to enforce a consistent case style in one of my projects, so I added this clang-format style option to control it. With this .clang-format configuration: ```yaml NumericLiteralCaseStyle: UpperCasePrefix: Never UpperCaseHexDigit: Always UpperCaseSuffix: Never ``` This line of code: ```C unsigned long long 0XdEaDbEeFUll; ``` gets reformatted into this line of code: ```C unsigned long long 0xDEAFBEEFull; ``` ----- I'm new to this project, so please let me know if I missed something in the process. I modeled this PR from [IntegerLiteralSeparatorFixer](https://reviews.llvm.org/D140543)
2025-08-17[clang-format] Add SpaceInEmptyBraces option (#153765)owenca1-3/+21
Also set it to SIEB_Always for WebKit style. Closes #85525. Closes #93635.
2025-07-28[clang-format] Fix a bug in `DerivePointerAlignment: true` (#150744)Owen Pan1-17/+29
This effectively reverts a4d4859dc70c046ad928805ddeaf8fa101793394 which didn't fix the problem that `int*,` was not counted as "Left" alignment. Fixes #150327
2025-07-25[clang-format] Google Style: disable DerivePointerAlignment. (#149602)James Y Knight1-1/+1
The [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions) is being changed to specify that spaces should go after the asterisk/ampersand, rather than permitting either before or after on a file-by-file basis. The new requirement is: > When referring to a pointer or reference (variable declarations or > definitions, arguments, return types, template parameters, etc.), > you must not place a space before the asterisk/ampersand. Use a > space to separate the type from the declared name (if present). The [Google ObjC style](https://google.github.io/styleguide/objcguide.html) is silent on this matter, but the de-facto style is not being modified at this time. So, keep DerivePointerAlignment enabled for ObjC language mode.
2025-07-24[clang-format] Fix a bug in `DerivePointerAlignment: true` (#150387)Owen Pan1-2/+3
Fixes #150327
2025-07-24[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)Owen Pan1-0/+1
Closes #149971
2025-07-17[clang-format] Add IgnoreExtension to SortIncludes (#137840)Daan De Meyer1-16/+29
Sorting by stem gives nicer results when various header file names are substrings of other header file names. For example, a CLI application with a main header named analyze.h and an analyze-xxx.h header for each subcommand currently will always put analyze.h last after all the analyze-xxx.h headers, but putting analyze.h first instead is arguably nicer to read. TLDR; Instead of ``` #include "analyze-blame.h" #include "analyze.h" ``` You'd get ``` #include "analyze.h" #include "analyze-blame.h" ``` Let's allow sorting by stem instead of full path by adding IgnoreExtension to SortIncludes.
2025-07-13[clang-format] Add MacrosSkippedByRemoveParentheses option (#148345)Owen Pan1-0/+2
This allows RemoveParentheses to skip the invocations of function-like macros. Fixes #68354. Fixes #147780.
2025-07-11Follow up on #147623Owen Pan1-1/+0
2025-07-09Address a handful of C4146 compiler warnings where literals can be replaced ↵Alex Sepkowski1-10/+17
with std::numeric_limits (#147623) This PR addresses instances of compiler warning C4146 that can be replaced with std::numeric_limits. Specifically, these are cases where a literal such as '-1ULL' was used to assign a value to a uint64_t variable. The intent is much cleaner if we use the appropriate std::numeric_limits value<Type>::max() for these cases. Addresses #147439
2025-07-06[clang-format][NFC] Replace size() with empty() (#147164)Owen Pan1-1/+1
2025-05-28[clang-format] Handle .h files for LK_C and LK_ObjC (#141714)Owen Pan1-5/+13
Fix #137792
2025-05-20[clang-format] Handle raw string literals containing JSON code (#140666)Owen Pan1-2/+4
Fix #65400
2025-05-19[clang-format][NFC] Upgrade SortIncludes option to a struct (#140497)Owen Pan1-12/+22
This allows adding other suboptions e.g. IgnoreExtension in #137840.
2025-05-16[clang] Use llvm::binary_search (NFC) (#140216)Kazu Hirata1-3/+2
2025-05-07[clang-format] Add SpaceAfterOperatorKeyword option (#137610)Filip Milosevic1-0/+3
Add SpaceAfterOperatorKeyword option to clang-format
2025-04-30Reland [clang-format] Add OneLineFormatOffRegex option (#137577)Owen Pan1-0/+1
2025-04-30Revert "[clang-format] Add OneLineFormatOffRegex option (#137577)"Owen Pan1-1/+0
This reverts commit b8bb1ccb4f9126d1bc9817be24e17f186a75a08b which triggered an assertion failure in CodeGenTest.TestNonAlterTest.
2025-04-29[clang-format] Add OneLineFormatOffRegex option (#137577)Owen Pan1-0/+1
Close #54334
2025-04-19[clang] Use llvm::unique (NFC) (#136469)Kazu Hirata1-9/+9
2025-04-18[clang-format] Fix a crash in EnumTrailingComma (#135903)Owen Pan1-6/+11
Fix #135819
2025-04-12[clang-format] Wrap and indent lambda braces in GNU style (#135479)Owen Pan1-1/+1
Fix #133135
2025-04-12[clang-format][NFC] Add isJava() and isTextProto() in FormatStyle (#135466)Owen Pan1-7/+6
Also remove redundant name qualifiers format::, FormatStyle::, and LanguageKind::.
2025-04-10Treat ipynb as json (#135137)Tim Clephas1-1/+3
Fixes #110727
2025-04-09[clang-format][NFC] Add FormatToken::is(tok::ObjCKeywordKind) (#134973)Owen Pan1-1/+1
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] Set C11 instead of C17 for LK_C (#134472)Owen Pan1-1/+1
Fix #134453
2025-03-30[clang-format] Add an option for editing enum trailing commas (#133576)Owen Pan1-36/+81
Also refactor the code that removes/replaces a token.
2025-03-27[clang-format] Allow `Language: Cpp` for C files (#133033)Owen Pan1-4/+14
Fix #132832
2025-03-14[clang-format] Add support for absl nullability macros (#130346)Jan Voung1-0/+4
Add support for formatting w/ absl nullability macros (https://github.com/abseil/abseil-cpp/blob/c52afac4f87ef76e6293b84874e5126a62be1f15/absl/base/nullability.h#L237). Example at https://godbolt.org/z/PYv19M1Gj input: ``` std::vector<int* _Nonnull> x; std::vector<int* absl_nonnull> y; ``` orig output: ``` std::vector<int* _Nonnull> x; std::vector<int * absl_nonnull> y; ``` new output: ``` std::vector<int* _Nonnull> x; std::vector<int* absl_nonnull> y; ``` credit to @ymand for the original patch
2025-03-03[clang-format] Lex C++ only keywords as identifiers in C (#129426)Owen Pan1-17/+25
Fix #128847
2025-03-03[clang-format] Don't sort includes for C# (#129369)Owen Pan1-1/+2
Fixes #106194
2025-02-27[clang-format] Change BracedInitializerIndentWidth to int (#128988)Owen Pan1-1/+1
Fixes #108526
2025-02-23[clang-format] Add the C language instead of treating it like C++ (#128287)Owen Pan1-1/+11
Closes #128120
2025-02-21[clang-format] Allow specifying the language for `.h` files (#128122)Owen Pan1-0/+31
Closes #128119
2025-02-07[clang-format] Add BinPackLongBracedList style option (#112482)Gedare Bloom1-0/+2
The use of Cpp11BracedListStyle with BinPackArguments=False avoids bin packing until reaching a hard-coded limit of 20 items. This is an arbitrary choice. Introduce a new style option to allow disabling this limit.
2025-02-06[clang-format] Add BreakBeforeTemplateCloser option (#118046)leijurv1-0/+3
In clang-format, multiline templates have the `>` on the same line as the last parameter: ```c++ template < typename Foo, typename Bar> void foo() { ``` I would like to add an option to put the `>` on the next line, like this: ```c++ template < typename Foo, typename Bar > void foo() { ``` An example of a large project that uses this style is NVIDIA's CUTLASS, here is an example: https://github.com/NVIDIA/cutlass/blob/main/include/cutlass/epilogue/dispatch_policy.hpp#L149-L156 My reasoning is that it reminds me of this style of braces: ```c++ if (foo()) { bar(); baz();} ``` Most people agree this is better: ```c++ if (foo()) { bar(); baz(); } ``` --------- Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-27[clang-format] Add style option `PenaltyBreakBeforeMemberAccess` (#118409)Gedare Bloom1-0/+3
The penalty for breaking before a member access is hard-coded to 150. Add a configuration option to allow setting it. --------- Co-authored-by: Owen Pan <owenpiano@gmail.com>
2025-01-21[clang-format] Rename ExportBlockIndentation -> IndentExportBlock (#123493)Sirraide1-2/+2
This renames the `ExportBlockIndentation` option and adds a config parse test, as requested in #110381.
2025-01-19[clang-format] Improve brace wrapping and add an option to control ↵Sirraide1-0/+2
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-04[clang-format][NFC] Replace SmallVectorImpl with ArrayRef (#121621)Owen Pan1-4/+4
2025-01-02[clang-format] Add option WrapNamespaceBodyWithNewlines (#106145)dmasloff1-0/+15
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>
2025-01-01[clang-format] Add `VariableTemplates` option (#121318)Owen Pan1-0/+1
Closes #120148.
2024-12-30[clang-format] Add `AllowShortNamespacesOnASingleLine` option (#105597)Galen Elias1-0/+3
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-12-24[clang-format] Skip line splices when sorting C++ includes (#120680)Owen Pan1-2/+9
Fixes #109864.
2024-12-03[clang-format] Add support for `.cjs` as JavaScript file extension (#118188)Jim B1-0/+1
Node uses `.cjs` as an extension for 'common javascript' modules.