[clang] [clang-format] Fix clang-format issue with 'new' and 'delete' keywords in C files (PR #85470)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 15 14:37:39 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: None (scythris)
<details>
<summary>Changes</summary>
This resolves an issue in clang-format where `new` and `delete` were incorrectly formatted as keywords in C files. The fix modifies `TokenAnnotator::spaceRequiredBetween` to handle `new` and `delete` when used as identifiers for function pointers in structs in C code.
---
Full diff: https://github.com/llvm/llvm-project/pull/85470.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+1-1)
- (modified) clang/unittests/Format/FormatTest.cpp (+5)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e464c2b5731a35..c58204ce33ae52 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4607,7 +4607,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
spaceRequiredBeforeParens(Right);
}
- if (!Left.Previous || Left.Previous->isNot(tok::period)) {
+ if (!Left.Previous || !Left.Previous->isOneOf(tok::period, tok::arrow)) {
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fc367a7a5a8981..410e08ea589692 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -11449,6 +11449,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
"void delete(link p);",
"void new (link p);\n"
"void delete (link p);");
+
+ verifyFormat("{ p->delete(); }\n"
+ "{ p->new(); }",
+ "{ p->delete (); }\n"
+ "{ p->new (); }");
FormatStyle AfterPlacementOperator = getLLVMStyle();
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
``````````
</details>
https://github.com/llvm/llvm-project/pull/85470
More information about the cfe-commits
mailing list