summaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTestSelective.cpp
AgeCommit message (Collapse)Author
2025-07-21[clang-format][NFC] Clean up around StringRef initializations (#149765)Owen Pan
Consistently use `constexpr StringRef Code("string literal");`.
2025-07-20[clang-format][NFC] Delete redundant type names after FormatStyle::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-01-09[clang-format] Stop fixing indentation on namespace closing brace (#122234)Owen Pan
Fixes #119790.
2024-05-20[clang-format][NFC] Remove redundnat llvm::, clang::, etc.Owen Pan
2023-10-27[clang-format][NFC] Remove more extraneous newlines in unit testsOwen Pan
Also removed a duplicate test case.
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-05-23Revert "[clang-format] Fix indentation for selective formatting"Owen Pan
This reverts commit 72ab89e3197cc1bee3b9774edb504690e3e43ed0. Reverted due to bots failures e.g. https://lab.llvm.org/buildbot/#/builders/139/builds/41339.
2023-05-23[clang-format] Fix indentation 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 the previous indentation level. In case of the --lines option configured 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://github.com/llvm/llvm-project/issues/56352 to fix a crash. Our commit here essentially reverts that crash fix. It seemed to have been incorrect. The proper fix is to set the AnnotedLine::Level of joined lines correctly (=> change in LineJoiner::join()). See https://github.com/llvm/llvm-project/issues/59178#issuecomment-1542637781 for some more details. Fixes #58464. Fixes #59178. Differential Revision: https://reviews.llvm.org/D151047
2022-07-07[clang-format] Avoid crash in LevelIndentTracker.Marek Kurdej
Fixes https://github.com/llvm/llvm-project/issues/56352. Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D129064
2022-02-17[clang-format] Fix wrong assertion with non-negative shift when aligning tokens.Marek Kurdej
Fixes https://github.com/llvm/llvm-project/issues/53880.
2020-05-02[clang-format] NFC - clang-format the FormatTestsmydeveloperday
Summary: Ensure the clang-format unit tests are themselves clang-formatted Having areas of the llvm code which are clang-format clean, give us more areas to run new clang-format binaries on ensuring we haven't broken anything. It seems to me we SHOULD have this clang-formatted at a minimum, otherwise how can we expect others to use clang-format if we "don't eat our own dogfood", also if the tests are dependent on the formatting of the code then that would also be bad! Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D79204
2019-03-13[clang-format] [PR25010] AllowShortIfStatementsOnASingleLine not working if ↵Paul Hoad
an "else" statement is present Summary: Addressing: PR25010 - https://bugs.llvm.org/show_bug.cgi?id=25010 Code like: ``` if(true) var++; else { var--; } ``` is reformatted to be ``` if (true) var++; else { var--; } ``` Even when `AllowShortIfStatementsOnASingleLine` is true The following revision comes from a +1'd suggestion in the PR to support AllowShortIfElseStatementsOnASingleLine This suppresses the clause prevents the merging of the if when there is a compound else Reviewers: klimek, djasper, JonasToth, alexfh, krasimir, reuk Reviewed By: reuk Subscribers: reuk, Higuoxing, jdoerfert, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D59087 llvm-svn: 356031
2019-03-13Revert "[clang-format] [PR25010] AllowShortIfStatementsOnASingleLine not ↵Paul Hoad
working if an "else" statement is present" This reverts commit b358cbb9b78389e20f7be36e1a98e26515c3ecce. llvm-svn: 356030
2019-03-13[clang-format] [PR25010] AllowShortIfStatementsOnASingleLine not working if ↵Paul Hoad
an "else" statement is present Summary: Addressing: PR25010 - https://bugs.llvm.org/show_bug.cgi?id=25010 Code like: ``` if(true) var++; else { var--; } ``` is reformatted to be ``` if (true) var++; else { var--; } ``` Even when `AllowShortIfStatementsOnASingleLine` is true The following revision comes from a +1'd suggestion in the PR to support AllowShortIfElseStatementsOnASingleLine This suppresses the clause prevents the merging of the if when there is a compound else Reviewers: klimek, djasper, JonasToth, alexfh, krasimir, reuk Reviewed By: reuk Subscribers: reuk, Higuoxing, jdoerfert, cfe-commits Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D59087 llvm-svn: 356029
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth
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
2018-05-15[clang] Update uses of DEBUG macro to LLVM_DEBUG.Nicola Zaghen
The DEBUG() macro is very generic so it might clash with other projects. The renaming was done as follows: - git grep -l 'DEBUG' | xargs sed -i 's/\bDEBUG\s\?(/LLVM_DEBUG(/g' - git diff -U0 master | ../clang/tools/clang-format/clang-format-diff.py -i -p1 -style LLVM Explicitly avoided changing the strings in the clang-format tests. Differential Revision: https://reviews.llvm.org/D44975 llvm-svn: 332350
2018-04-23Format closing braces when reformatting the line containing the opening brace.Manuel Klimek
This required a couple of yaks to be shaved: 1. MatchingOpeningBlockLineIndex was misused to also store the closing index; instead, use a second variable, as this doesn't work correctly for "} else {". 2. We needed to change the API of AffectedRangeManager to not use iterators; we always passed in begin / end for the whole container before, so there was no mismatch in generality. 3. We need an extra check to discontinue formatting at the top level, as we now sometimes change the indent of the closing brace, but want to bail out immediately afterwards, for example: void f() { if (a) { } void g(); Previously: void f() { if (a) { } void g(); Now: void f() { if (a) { } void g(); Differential Revision: https://reviews.llvm.org/D45726 llvm-svn: 330573
2017-05-19[clang-format] Handle trailing comment sections in import statement linesKrasimir Georgiev
Summary: This patch updates the handling of multiline trailing comment sections in import statement lines to make it more consistent with the case in general. This includes updating the parsing logic to collect the trailing comment sections and the formatting logic to not insert escaped newlines at the end of comment lines in import statement lines. Specifically, before this patch this code: ``` #include <a> // line 1 // line 2 ``` will be turned into two unwrapped lines, whereas this code: ``` int i; // line 1 // line 2 ``` is turned into a single unwrapped line, enabling reflowing across comments. An example where the old behaviour is bad is when partially formatting the lines 3 to 4 of this code: ``` #include <a> // line 1 // line 2 int i; ``` which gets turned into: ``` #include <a> // line 1 // line 2 int i; ``` because the two comment lines were independent and the indent was copied. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D33351 llvm-svn: 303415
2017-05-08[clang-format] Convert AlignEscapedNewlinesLeft to an enum, addingDaniel Jasper
DontAlign This converts the clang-format option AlignEscapedNewlinesLeft from a boolean to an enum, named AlignEscapedNewlines, with options Left (prev. true), Right (prev. false), and a new option DontAlign. When set to DontAlign, the backslashes are placed just after the last token in each line: #define EXAMPLE \ do { \ int x = aaaaa; \ int b; \ int dddddddddd; \ } while (0) Patch by jtbandes. Thank you! llvm-svn: 302428
2017-04-21[clang-format] Replace IncompleteFormat by a struct with LineKrasimir Georgiev
Summary: This patch replaces the boolean IncompleteFormat that is used to notify the client if an unrecoverable syntax error occurred by a struct that also contains a line number. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D32298 llvm-svn: 300985
2017-01-25[clang-format] Implement comment reflowing.Krasimir Georgiev
Summary: This presents a version of the comment reflowing with less mutable state inside the comment breakable token subclasses. The state has been pushed into the driving breakProtrudingToken method. For this, the API of BreakableToken is enriched by the methods getSplitBefore and getLineLengthAfterSplitBefore. Reviewers: klimek Reviewed By: klimek Subscribers: djasper, klimek, mgorny, cfe-commits, ioeric Differential Revision: https://reviews.llvm.org/D28764 llvm-svn: 293055
2016-07-11Make tooling::applyAllReplacements return llvm::Expected<string> instead of ↵Eric Liu
empty string to indicate potential error. Summary: return llvm::Expected<> to carry error status and error information. This is the first step towards introducing "Error" into tooling::Replacements. Reviewers: djasper, klimek Subscribers: ioeric, klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21601 llvm-svn: 275062
2016-03-17clang-format: [JS] Make requoting of JavaScript string literals onlyDaniel Jasper
change affected ranges. llvm-svn: 263713
2016-02-29clang-format: Don't format unrelated nested blocks.Daniel Jasper
With this change: SomeFunction( [] { int i; return i; // Format this line. }, [] { return 2; // Don't "fix" this. }); llvm-svn: 262216
2016-01-09clang-format: Improve selective comment formatting.Daniel Jasper
Starting here: int x; // Format this line only. int xx; // int xxxxx; // Before: int x; // Format this line only. int xx; // int xxxxx; // After: int x; // Format this line only. int xx; // int xxxxx; // llvm-svn: 257258
2015-11-02clang-format: Simplify and improve stop condition for formattingDaniel Jasper
unaffected lines with incorrect initial indent. Starting from: namespace { int i; // There shouldn't be indentation here. int j; // <- call clang-format on this line. } Before: namespace { int i; int j; } After: namespace { int i; int j; } llvm-svn: 251824
2015-11-01clang-format: Be slightly more cautious when formatting subsequent lines ↵Daniel Jasper
after a change. With r251474, clang-format could indent the entire rest of the file, if there is a missing closing brace, e.g. while writing code in an editor. Summary: With this change, clang-format stops formatting when either it leaves the current scope or when it comes back to the initial scope after going into a nested one. Reviewers: klimek Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D14213 llvm-svn: 251760
2015-10-28clang-format: When a line is formatted, also format subsequence lines if ↵Daniel Jasper
their indent is off. Summary: This is especially important so that if a change is solely inserting a block around a few statements, clang-format-diff.py will still clean up and add indentation to the inner parts. Reviewers: klimek Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D14105 llvm-svn: 251474
2015-06-15clang-format: NFC. Move testing of selective formatting to a separate file.Daniel Jasper
This is a first step for splitting the huge FormatTest.cpp into separate files to make it easier to find specific tests. llvm-svn: 239730