aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format
AgeCommit message (Collapse)AuthorFilesLines
10 hours[clang-format] Fix a bug in wrapping { after else (#161048)owenca2-21/+25
Fixes #160775
2 days[clang-format] Fix bugs in annotating arrows and square brackets (#160973)owenca2-17/+13
Fixes #160518
2 days[clang-format] Fix qualifier ordering for lines after PP directives (#160731)Ruoyu Zhong1-1/+1
Lines appearing after preprocessor conditional blocks (like `#endif`) were not having their qualifiers reordered by `QualifierOrder`, while lines inside the conditional blocks were processed correctly. The issue was that tokens on lines following preprocessor directives have `MustBreakBefore` = `true`. The qualifier alignment logic was breaking immediately upon encountering any token with `MustBreakBefore` = `true`, preventing analysis of the entire line. The fix allows processing to continue when `MustBreakBefore` = `true` on the first token of a line, since this is expected behavior (the token legitimately starts a new line). Only tokens with `MustBreakBefore` = `true` that appear mid-line will cause the analysis loop to break. Fixes https://github.com/llvm/llvm-project/issues/160487.
5 days[clang-format] Align within the level with Cpp11BracedListStyle disabled ↵sstwcw2-27/+29
(#159140) When the style is `{AlignConsecutiveDeclarations: true, Cpp11BracedListStyle: false}`, the program would sometimes align the lambda body with the outside. Like this. ```C++ const volatile auto result{ []() { const auto something = 1; return 2; } }; ``` This patch stops it. Now the output looks like this. ```C++ const volatile auto result{ []() { const auto something = 1; return 2; } }; ``` Fixes #53699. The problem was with how the `AlignTokenSequence` function tracked levels. The stack was pushed once when a token was more nested than the previous one. It was popped once when a token was less nested than the previous one. With the option `Cpp11BracedListStyle` disabled, the `[` token was deeper than the previous token by 2 levels. Both its indentation level and nesting level were more than that of the previous one. But the stack was only pushed once. The following tokens popped the 2 levels separately. The function treated the body of the lambda expression as on the same level as the outside. The problem is fixed this way. The function `AlignTokens` which calls the function `AlignTokenSequence` already uses a simpler and more robust way to track levels. It remembers the outermost level it should align. It compares the token's level with the outermost level. It does not need a stack. So it is immune to the problem. This patch makes the function `AlignTokenSequence` take as a parameter the indices of the tokens matched by the function `AlignTokens`. This way it does not have to contain the logic again. Now the function `AlignTokenSequence` only aligns tokens already matched by the function `AlignTokens`. It is easy to see that the assertion on line 453 holds. The workaround on line 354 is not needed any more. The test on line 20310 added at the same time as the assertion ensures that the assertion hold. The stack in the function `AlignTokenSequence` is kept for now. It is still used to figure out when things inside a level should move along with the outer level. Since the stack has the problem, the developer plans to move the logic into token annotator. It already uses a stack.
5 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
8 days[clang-format] Add AllowBreakBeforeQtProperty option (#159909)owenca4-2/+31
The test cases are adapted from #131605.
10 days[clang-format][NFC] Add is_sorted() assertion for binary_search()Owen Pan2-0/+2
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 Bloom6-24/+36
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 MacGregor4-0/+239
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-31[clang-format] Fix TableGen nested DAGArg format (#155837)Hirofumi Nakamura1-1/+12
Fixes https://github.com/llvm/llvm-project/issues/154634. Allow inserting space before DAGArg's opener paren when nested.
2025-08-29[clang-format] Allow short function body on a single line (#151428)闫立栋1-4/+7
Fix #145161
2025-08-28[clang-format] Correctly annotate RequiresExpressionLBrace (#155773)owenca1-1/+1
Fixes #155746
2025-08-26[clang-format] Fix a bug in SkipMacroDefinitionBody (#155346)owenca1-5/+2
All comments before the macro definition body should be skipped.
2025-08-22[clang-format] Fix a bug in SkipMacroDefinitionBody (#154787)owenca1-15/+24
Fixes #154683
2025-08-17[clang-format] Add SpaceInEmptyBraces option (#153765)owenca3-14/+35
Also set it to SIEB_Always for WebKit style. Closes #85525. Closes #93635.
2025-08-17[clang-format] Fix a bug in breaking before FunctionDeclarationName (#153924)owenca1-2/+9
Fixes #153891
2025-08-17[clang-format] Allow breaking before bit-field colons (#153529)owenca1-1/+2
Fixes #153448
2025-08-17[clang-format] Don't annotate class property specifiers as StartOfName (#153525)owenca1-0/+3
Fixes #153443
2025-08-11[clang-format] Add functionality of getting info about numeric literals ↵owenca3-0/+95
(#152878)
2025-08-02[clang-format][NFC] Maximize usage of isOneOf() in TokenAnnotator (#151658)Owen Pan2-27/+23
Also make a StringRef literal `static constexpr`.
2025-07-30[clang-format] Disable IntegerLiteralSeparator for C++ before c++14 (#151273)Owen Pan1-4/+7
Fixes #151102
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 Pan2-1/+3
Closes #149971
2025-07-24[clang-format] Stop ctor initializer from being inlined (#150361)Eric Li1-1/+2
The colon in a constructor's initializer list triggers the inlining of a nested block as if it was a conditional operator expression. This prevents line breaks under certain circumstances when the initializer list contains braced initializers, which in turn prevents the line formatter from finding a solution. In this commit we exclude colons that are a constructor initializer colon from consideration of nested block inlining. Fixes #97242. Fixes #81822.
2025-07-21[clang-format][NFC] Clean up around StringRef initializations (#149765)Owen Pan4-9/+9
Consistently use `constexpr StringRef Code("string literal");`.
2025-07-21[clang-format] Remove code related to trigraphs (#148640)sstwcw1-24/+11
When reviewing #147156, the reviewers pointed out that we didn't need to support the trigraph. The code never handled it right. In the debug build, this kind of input caused the assertion in the function `countLeadingWhitespace` to fail. The release build without assertions outputted `?` `?` `/` separated by spaces. ```C #define A ??/ int i; ``` This is because the code in `countLeadingWhitespace` assumed that the underlying lexer recognized the entire `??/` sequence as a single token. In fact, the lexer recognized it as 3 separate tokens. The flag to make the lexer recognize trigraphs was never enabled. This patch enables the flag in the underlying lexer. This way, the program now either turns the trigraph into a single `\` or removes it altogether if the line is short enough. There are operators like the `??=` in C#. So the flag is not enabled for all input languages. Instead the check for the token size is moved from the assert line into the if line. The problem was introduced by my own patch 370bee480139 from about 3 years ago. I added code to count the number of characters in the escape sequence probably just because the block of code used to have a comment saying someone should add the feature. Maybe I forgot to enable assertions when I ran the code. I found the problem because reviewing pull request 145243 made me look at the code again.
2025-07-20[clang-format] Fix a bug in `BreakBeforeBinaryOperators: All` (#149695)Owen Pan1-0/+1
Fixes #149520
2025-07-17[clang-format] Fix a regression of annotating PointerOrReference (#149039)Owen Pan1-3/+7
Fixes #149010
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][NFC] Simplify some logic in BreakableLineCommentSection (#148324)Owen Pan1-6/+4
2025-07-13[clang-format] Add MacrosSkippedByRemoveParentheses option (#148345)Owen Pan7-9/+22
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-10[clang-format] Split line comments separated by backslashes (#147648)Owen Pan1-9/+12
Fixes #147341
2025-07-10[clang-format][NFC] Replace a function with StringRef::contains (#146245)Owen Pan1-13/+1
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] Use `empty()` instead of comparing size() to 0 or 1Owen Pan3-4/+4
2025-07-06[clang-format][NFC] Replace size() with empty() (#147164)Owen Pan5-15/+13
2025-07-05[clang-format] Propagate `LeadingEmptyLinesAffected` when joining lines ↵Eric Li1-1/+3
(#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-06-29[clang-format] Make EndsInComma in ContinuationIndenter consistent (#146256)Owen Pan1-8/+9
2025-06-28[clang-format] Fix a bug in `ReflowComments: Always` (#146202)Owen Pan1-2/+1
Fixes #39150
2025-06-26[clang-format][NFC] Remove `\brief` from comments (#145853)Owen Pan3-15/+14
This was done before in https://reviews.llvm.org/D46320
2025-06-25[clang-format][NFC] Remove a redundant check for nullOwen Pan1-1/+1
Missed by #145686
2025-06-25[clang-format] Handle Trailing Whitespace After Line Continuation (P2223R2) ↵Naveen Seth Hanig1-9/+21
(#145243) Fixes #145226. Implement [P2223R2](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2223r2.pdf) in clang-format to correctly handle cases where a backslash '\\' is followed by trailing whitespace before the newline. Previously, `clang-format` failed to properly detect and handle such cases, leading to misformatted code. With this, `clang-format` matches the behavior already implemented in Clang's lexer and `DependencyDirectivesScanner.cpp`, which allow trailing whitespace after a line continuation in any C++ standard.
2025-06-25Remove unneeded checks for null; NFC (#145686)Aaron Ballman1-4/+1
At the start of the case for `tok::colon`, we do an early return if `Prev` is null. Nothing else within that case modifies the value of `Prev`, so the checks for null are unnecessary and were confusing static analysis tools.
2025-06-25[clang-format] Improve QualifierAlignment in guessing macros (#145468)Owen Pan1-7/+18
Fixes #145388
2025-06-15[clang-format][NFC] Clean up DisallowLineBreaks lambda (#144255)Owen Pan1-36/+42
See also https://github.com/llvm/llvm-project/pull/141576/files#r2141808121
2025-06-14[clang-format] Fix a bug in annotating braces (#144095)Owen Pan1-1/+1
Stop looking for function decls after hitting a BK_BracedInit brace. Fixes #144057.