1167 Commits

Author SHA1 Message Date
Owen Pan
4f093b3166 Revert "[clang-format] Add AlignConsecutiveShortCaseStatements"
This reverts commit 4ba00844174db562d2bd8b15dab6aac605eddf57 due to missing
authorship.
2023-07-24 18:19:38 -07:00
Owen Pan
4ba0084417 [clang-format] Add AlignConsecutiveShortCaseStatements
This adds a new AlignConsecutiveShortCaseStatements option in line with the
existing AlignConsecutive* options , which when
AllowShortCaseLabelsOnASingleLine is enabled will align the tokens after the
case statement's colon. This also adds a AlignCaseColons option to allow
aligning the case label colon itself rather than the token after it.

Fixes #55475.

Differential Revision: https://reviews.llvm.org/D151761
2023-07-24 18:16:21 -07:00
Owen Pan
5c106f7b94 [clang-format] Add TypeNames option to disambiguate types/objects
If a non-keyword identifier is found in TypeNames, then a *, &, or && that
follows it is annotated as TT_PointerOrReference.

Differential Revision: https://reviews.llvm.org/D155273
2023-07-18 14:18:40 -07:00
Owen Pan
3a6a0702c2 [clang-format] Add an option to remove redundant parentheses
Differential Revision: https://reviews.llvm.org/D154484
2023-07-11 16:33:19 -07:00
Sedenion
899c867794 [clang-format] Fixed bad performance with enabled qualifier fixer.
This fixes github issue #57117: If the "QualifierAlignment"
option of clang-format is set to anything else but "Leave", the
"QualifierAlignmentFixer" pass gets enabled. This pass scales
quadratically with the number of preprocessor branches, i.e.
with the number of elements in TokenAnalyzer::UnwrappedLines.
The reason is that QualifierAlignmentFixer::process() generates
the UnwrappedLines, but then QualifierAlignmentFixer::analyze()
calls LeftRightQualifierAlignmentFixer::process() several times
(once for each qualifier) which again each time generates the
UnwrappedLines.

This commit gets rid of this double loop by registering the
individual LeftRightQualifierAlignmentFixer passes directly in
the top most container of passes (local variable "Passes" in
reformat()).
With this change, the original example in the github issue #57117
now takes only around 3s instead of >300s to format.

Since QualifierAlignmentFixer::analyze() got deleted, we also
no longer have the code with the NonNoOpFixes. This causes
replacements that end up not changing anything to appear in the
list of final replacements. There is a unit test to check that
this does not happen: QualifierFixerTest.NoOpQualifierReplacements.
However, it got broken at some point in time. So this commit
fixes the test. To keep the behavior that no no-op replacements
should appear from the qualifier fixer, the corresponding code
from QualifierAlignmentFixer::analyze() was moved to the top
reformat() function. Thus, is now done for **every** replacement
of every formatting pass. If no-op replacements are a problem
for the qualifier fixer, then it seems to be a good idea to
filter them out always.

See
https://github.com/llvm/llvm-project/issues/57117#issuecomment-1546716934
for some more details.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D153228
2023-07-03 11:54:33 +01:00
Owen Pan
829ed96b77 Reland [clang-format] Add the KeepEmptyLinesAtEOF option 2023-06-13 10:02:10 -07:00
Paul Kirth
b6a718016c [clang-format] Add test case for issue 63170
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-09 20:10:19 +00:00
Owen Pan
d2627cf88d [clang-format] Add the KeepEmptyLinesAtEOF option
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
2023-06-07 14:17:58 -07:00
sstwcw
24231df9b8 [clang-format] Parse the Verilog language option in configuration
Reviewed By: HazardyKnusperkeks, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D151632
2023-06-02 14:59:27 +00:00
Ben Hamilton
85670ac868 [Format/ObjC] Support NS_ASSUME_NONNULL_BEGIN and FOUNDATION_EXPORT in ObjC language guesser
This adds to the ObjC language guesser a few more common macros used
in ObjC headers. These can help distinguish ObjC headers which
otherwise lack ObjC types from C++ headers.

Contributed by danblakemore.

Tested: New tests included. Ran unit tests with:
  ```
  % cmake -S llvm -B build -G Ninja && \
    ninja -C build FormatTests && \
    ./build/tools/clang/unittests/Format/FormatTests --gtest_filter="*FormatTestObjC*"

  (snip)
  [----------] 24 tests from FormatTestObjC (265 ms total)

  [----------] Global test environment tear-down
  [==========] 26 tests from 2 test suites ran. (270 ms total)
  [  PASSED  ] 26 tests.
  ```

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D151578
2023-05-30 11:12:03 -06:00
Kazu Hirata
ed1539c6ad Migrate {starts,ends}with_insensitive to {starts,ends}_with_insensitive (NFC)
This patch migrates uses of StringRef::{starts,ends}with_insensitive
to StringRef::{starts,ends}_with_insensitive so that we can use names
similar to those used in std::string_view.

Note that the llvm/ directory has migrated in commit
6c3ea866e93003e16fc55d3b5cedd3bc371d1fde.

I'll post a separate patch to deprecate
StringRef::{starts,ends}with_insensitive.

Differential Revision: https://reviews.llvm.org/D150506
2023-05-16 10:12:42 -07:00
Manna, Soumi
babe8629d7 [NFC][Clang] Fix static analyzer tool remarks about large copies by values in Format.cpp file
Reported by Static Analyzer Tool, Coverity:

Inside "Format.cpp" file, in clang::format::internal::reformat(clang::format::FormatStyle const &, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus *)::[lambda(clang::format::Environment const &) (instance 4)]::operator ()(clang::format::Environment const &): A very large function call parameter exceeding the high threshold is passed by value.

pass_by_value: Capturing variable S of type clang::format::FormatStyle (size 808 bytes) by value, which exceeds the high threshold of 512 bytes

This patch uses original code but with an init capture that does a move instead of a copy.

Reviewed By: tahonermann, erichkeane, MyDeveloperDay, owenpan, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D149647
2023-05-05 18:55:49 -07:00
Jon Phillips
c12aa69a0b [clang-format] Add BracedInitializerIndentWidth option
The option allows users to specify how many columns to use to indent
the contents of initializer lists.

Closes #51070.

Differential Revision: https://reviews.llvm.org/D146101
2023-04-29 00:36:19 -07:00
Manna, Soumi
38ecb9767c [NFC][clang] Fix Coverity bugs with AUTO_CAUSES_COPY
Reported by Coverity:
AUTO_CAUSES_COPY
Unnecessary object copies can affect performance.

1. Inside "ExtractAPIVisitor.h" file, in clang::extractapi::impl::ExtractAPIVisitorBase<<unnamed>::BatchExtractAPIVisitor>::VisitFunctionDecl(clang::FunctionDecl const *): Using the auto keyword without an & causes the copy of an object of type DynTypedNode.

2. Inside "NeonEmitter.cpp" file, in <unnamed>::Intrinsic::Intrinsic(llvm::Record *, llvm::StringRef, llvm::StringRef, <unnamed>::TypeSpec, <unnamed>::TypeSpec, <unnamed>::ClassKind, llvm::ListInit *, <unnamed>::NeonEmitter &, llvm::StringRef, llvm::StringRef, bool, bool): Using the auto keyword without an & causes the copy of an object of type Type.

3. Inside "MicrosoftCXXABI.cpp" file, in <unnamed>::MSRTTIBuilder::getClassHierarchyDescriptor(): Using the auto keyword without an & causes the copy of an object of type MSRTTIClass.

4. Inside "CGGPUBuiltin.cpp" file, in clang::CodeGen::CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(clang::CallExpr const *): Using the auto keyword without an & causes the copy of an object of type CallArg.

5. Inside "SemaDeclAttr.cpp" file, in threadSafetyCheckIsSmartPointer(clang::Sema &, clang::RecordType const *): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.

6. Inside "ComputeDependence.cpp" file, in clang::computeDependence(clang::DesignatedInitExpr *): Using the auto keyword without an & causes the copy of an object of type Designator.

7. Inside "Format.cpp" file, In clang::format::affectsRange(llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int): Using the auto keyword without an & causes the copy of an object of type Range.

Reviewed By: tahonermann

Differential Revision: https://reviews.llvm.org/D149074
2023-04-24 14:52:55 -07:00
Owen Pan
51f6a16646 [clang-format] Hanlde leading whitespaces for JSON files
Fixes #62228.
Fixes #62229.

Differential Revision: https://reviews.llvm.org/D148777
2023-04-20 04:31:55 -07:00
Owen Pan
2bcfff6708 [clang-format] Fix regression with AlignTrailingComments set to true
Fixes #62161.

Differential Revision: https://reviews.llvm.org/D148447
2023-04-16 18:52:17 -07:00
Ben Hamilton
00ea679895 [Format/ObjC] Support NS_ERROR_ENUM in ObjC language guesser
Apple added a new NS_ERROR_ENUM macro to help define enums for
NSError codes.

This updates libformat's Objective-C language-guessing heuristic
to detect the new macro as well as related NSError types.

Tested: New tests added.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D147577
2023-04-07 16:10:52 -06:00
sstwcw
74cc4389f3 [clang-format] Add option for having one port per line in Verilog
We added the option `VerilogBreakBetweenInstancePorts` to put ports on
separate lines in module instantiations.  We made it default to true
because style guides mostly recommend it that way for example:

https://github.com/lowRISC/style-guides/blob/master/VerilogCodingStyle.md#module-instantiation

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D147327
2023-04-04 14:51:22 +00:00
sstwcw
feb585e7d6 [clang-format] Handle Verilog struct literals
Previously `isVerilogIdentifier` was mistaking the apostrophe used in
struct literals as an identifier.  It is fixed.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D147329
2023-04-01 17:10:31 +00:00
Owen Pan
253985d58c [clang-format] Add MinDigits suboptions to IntegerLiteralSeparator
Closes #61209.

Differential Revision: https://reviews.llvm.org/D147111
2023-03-31 17:14:33 -07:00
mydeveloperday
af98f3b1f4 [clang-format] JSON Add ability to add a space before the colon
I've seen a couple of request for extra Json formatting to match prettier capability.

Reviewed By: owenpan

Differential Revision: https://reviews.llvm.org/D147003
2023-03-29 21:28:40 +01:00
Manuel Klimek
01402831aa [clang-format] Add simple macro replacements in formatting.
Add configuration to specify macros.
Macros will be expanded, and the code will be parsed and annotated
in the expanded state. In a second step, the formatting decisions
in the annotated expanded code will be reconstructed onto the
original unexpanded macro call.

Eventually, this will allow to remove special-case code for
various macro options we accumulated over the years in favor of
one principled mechanism.

Differential Revision: https://reviews.llvm.org/D144170
2023-02-24 15:44:24 +00:00
Owen Pan
0ef289e5b2 [clang-format][NFC] Clean up nullptr comparison style
For example, use 'Next' instead of 'Next != nullptr',
and '!Next' instead of 'Next == nullptr'.

Differential Revision: https://reviews.llvm.org/D144355
2023-02-21 02:56:27 -08:00
Backl1ght
c24cdd58a1 [clang-format] PackConstructorInitializers support PCIS_OnlyNextLine
fixes https://github.com/llvm/llvm-project/issues/60241

Differential Revision: https://reviews.llvm.org/D143091
2023-02-06 19:39:05 +08:00
Owen Pan
25e2d0f3c8 [clang-format] Support clang-format on/off line comments as prefix
Closes #60264.

Differential Revision: https://reviews.llvm.org/D142804
2023-02-01 13:07:09 -08:00
Backl1ght
58751f943f [clang-format] SortUsingDeclarations support lexicographic order
fix https://github.com/llvm/llvm-project/issues/59930

Differential Revision: https://reviews.llvm.org/D141694
2023-01-20 21:34:57 +08:00
Kazu Hirata
6ad0788c33 [clang] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 12:31:01 -08:00
Owen Pan
e3eca33594 [clang-format] Replace DeriveLineEnding and UseCRLF with LineEnding
Below is the mapping:
LineEnding  DeriveLineEnding UseCRLF
LF                false       false
CRLF              false       true
DeriveLF          true        false
DeriveCRLF        true        true

Differential Revision: https://reviews.llvm.org/D141654
2023-01-13 15:46:34 -08:00
Owen Pan
2e2aa8bb6d [clang-format] Fix a bug in DerivePointerAlignment fallback
Fixes #59953.

Differential Revision: https://reviews.llvm.org/D141563
2023-01-13 12:58:33 -08:00
Krasimir Georgiev
922c8891d9 Revert "Revert "[clang-format] Add an option for breaking after C++11 attributes""
This reverts commit 879bfe6a979295f834b76df66b19a203b93eed0f.

owenpan@ pointed out on https://reviews.llvm.org/D140956 that this
actually makes the formatting more consistent, so it's not a regression.
2023-01-11 11:30:30 +00:00
Krasimir Georgiev
879bfe6a97 Revert "[clang-format] Add an option for breaking after C++11 attributes"
This reverts commit a28f0747c2f3728bd8a6f64f7c8ba80b4e0cda9f.

It appears that this regresses some function definitions, added an
example as a comment over at https://reviews.llvm.org/D140956.
2023-01-10 09:23:44 +00:00
Owen Pan
2c6ecc9db6 [clang-format] Add an option to insert a newline at EOF if missing
Closes #38042.

Differential Revision: https://reviews.llvm.org/D141035
2023-01-05 15:25:51 -08:00
Owen Pan
a28f0747c2 [clang-format] Add an option for breaking after C++11 attributes
Fixes #45968.
Fixes #54265.
Fixes #58102.

Differential Revision: https://reviews.llvm.org/D140956
2023-01-05 04:08:58 -08:00
Owen Pan
89aad1e6a3 Reland [clang-format] Add an option to format integer literal separators
Previously committed in 46c94e5067b5 which was reverted in f0756e086010
due to a memory bug.

Closes #58949.

Differential Revision: https://reviews.llvm.org/D140543
2022-12-31 17:57:33 -08:00
Vitaly Buka
f0756e0860 Revert "[clang-format] Add an option to format integer literal separators"
Revert "[clang-format] Disable FixRanges in IntegerLiteralSeparatorTest"

Breaks buildbots, details in https://reviews.llvm.org/D140543

This reverts commit 879bd9146a2c9ea395abd7c1ebd0f76f414a4967.
This reverts commit 46c94e5067b5f396c24bb950505c79bc819bd4b8.
2022-12-25 13:04:41 -08:00
Owen Pan
46c94e5067 [clang-format] Add an option to format integer literal separators
Closes #58949.

Differential Revision: https://reviews.llvm.org/D140543
2022-12-24 15:35:17 -08:00
Jordan Rupprecht
5a06334c51 [Format] Capture FormatStyle by value to avoid use-after-free.
Fixes the test failure in 240e29c5015d246de7fb5e4421aa93042fada59b, reported on the D140058 review thread.
2022-12-15 20:38:32 -08:00
Owen Pan
240e29c501 [clang-format][NFC] Turn on some code-changing options one by one
For the code-changing options InsertBraces, RemoveBracesLLVM, and
RemoveSemicolon, turn the option on only when running the token
analyzer pass for it. This improves the run-time and avoids
interference from other options.

Differential Revision: https://reviews.llvm.org/D140058
2022-12-15 19:48:26 -08:00
Owen Pan
0e89f1cca4 [clang-format][NFC] Don't run any passes if Environment::make fails
Differential Revision: https://reviews.llvm.org/D139999
2022-12-14 10:27:05 -08:00
Kazu Hirata
7d3178d76a [Format] Use std::optional in Format.cpp (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09 23:07:59 -08:00
David Goldman
fc46d6e67f [clang][Tooling] Add support for generating #import edits
And make use of this from clangd's CodeComplete and IncludeFixer, although currently they are both restricted only to #include symbols.

Differential Revision: https://reviews.llvm.org/D128677
2022-12-06 13:47:07 -05:00
Kazu Hirata
5891420e68 [clang] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:46 -08:00
Sam McCall
1a8dd74258 [include-cleaner] clang-include-cleaner can print/apply edits
This adds command-line flags to the tool:
+ -print: prints changed source code
+ -print=changes: prints headers added/removed
+ -edit: rewrites code in place
+ -insert=0/-remove=0: disables additions/deletions for the above

These are supported by a couple of new functions dumped into Analysis:
analyze() sits on top of walkUsed and makes used/unused decisions for
Includes. fixIncludes() applies those results to source code.

Differential Revision: https://reviews.llvm.org/D139013
2022-12-02 13:16:58 +01:00
Björn Schäpers
316e259902 [clang-format][NFC] More sorting in getLLVMStyle()
Seems I've missed that.

Amends 41a09a07ce4ddd1e97ce0430d1debe1dcc853890
2022-11-10 22:36:25 +01:00
Anastasiia Lukianenko
f6b252978c [clang-format] Add BreakBeforeInlineASMColon configuration
If true, colons in ASM parameters will be placed after line breaks.

true:
asm volatile("string",
                     :
                     : val);

false:
asm volatile("string", : : val);

Differential Revision: https://reviews.llvm.org/D91950
2022-11-10 22:31:09 +01:00
Björn Schäpers
41a09a07ce [clang-format][NFCish] Alphabetical sort Format.(h|pp)
I've:
- Sorted the members of FormatStyle alphabetical. The enums and structs
  are kept close to the member.
- Sorted the yaml io functions, based on the type they operate on.
- Sorted the initializers in getLLVMStyle(), except that penalities are
  kept at the end.
- Sorted the io of FormatStyle, this changes the --dump-config behavior.
- Moved the deprecated options into the only input case, this also
  changes --dump-config, it does not put the not directly used options
  in the .clang-format anymore.
- Sorted the comparisons in operator==.
- Added WhiteSpaceMacros in operator==, I've not actively looked if all
  other members are compared.
- This showed flawed tests (or in my opinion a flawed io operation, but
  that is another discussion and change).

Differential Revision: https://reviews.llvm.org/D137409
2022-11-08 21:46:15 +01:00
Yusuke Kadowaki
3edc1210a4 [clang-format] Adds a formatter for aligning trailing comments over empty lines
This patch addresses https://github.com/llvm/llvm-project/issues/19756

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D132131
2022-10-30 12:22:39 +00:00
owenca
37e754e580 [clang-format] Insert closing braces after an unaffected line
The token that records the number of closing braces to be inserted
may be on an unaffected line. Extra work is required in order to
actually insert the closing braces after inserting the matching
opening braces of affected lines.

Fixes #58161.

Differential Revision: https://reviews.llvm.org/D136437
2022-10-24 20:00:06 -07:00
Danil Sidoruk
9c422ab7ce
[clang-format] Add option for aligning requires clause body
Adds an option whether requires clause body should be aligned with
the `requires` keyword.
This option is now the default, both without configuration and in LLVM
style.

Fixes https://github.com/llvm/llvm-project/issues/56283

Differential Revision: https://reviews.llvm.org/D129443

Co-authored-by: Emilia Dreamer <emilia@rymiel.space>
2022-10-21 10:42:45 +03:00
mydeveloperday
69e772768e [clang-format] Add support to remove unnecessary semicolons after function definition
Fixes: https://github.com/llvm/llvm-project/issues/58217

This change is to remove extraneous and unnecessary ';' from after a function definition, its off by default and carries the same "code modification" warning as some of our other code manipulating changes.

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D135466
2022-10-09 11:18:16 +01:00