[clang] 8dab452 - [clang-format] Disallow requires clauses to become function declarations
Emilia Dreamer via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 18 18:06:23 PDT 2022
Author: Emilia Dreamer
Date: 2022-09-19T04:04:31+03:00
New Revision: 8dab452740007e03c963b349e716068eac2b25a0
URL: https://github.com/llvm/llvm-project/commit/8dab452740007e03c963b349e716068eac2b25a0
DIFF: https://github.com/llvm/llvm-project/commit/8dab452740007e03c963b349e716068eac2b25a0.diff
LOG: [clang-format] Disallow requires clauses to become function declarations
There already exists logic to disallow requires *expressions* to be
treated as function declarations, but this expands it to include
requires *clauses*, when they happen to also be parenthesized.
Previously, in the following case:
```
template <typename T>
requires(Foo<T>)
T foo();
```
The line with the requires clause was actually being considered as the
line with the function declaration due to the parentheses, and the
*real* function declaration on the next line became a trailing
annotation
(Together with https://reviews.llvm.org/D134049) Fixes https://github.com/llvm/llvm-project/issues/56213
Reviewed By: HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D134052
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a81bbb99f5d9e..e530120fa4454 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1110,7 +1110,7 @@ class AnnotatingParser {
!Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
!Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen) &&
(!Tok->Previous ||
- !Tok->Previous->isOneOf(tok::kw___attribute,
+ !Tok->Previous->isOneOf(tok::kw___attribute, TT_RequiresClause,
TT_LeadingJavaAnnotation))) {
Line.MightBeFunctionDecl = true;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f3007f85e91b0..d27c176415c83 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -737,6 +737,16 @@ TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
BaseTokenCount = 26;
TestRequires(__LINE__);
+ BaseCode = "template<typename T>\n"
+ "T foo();";
+ ConstrainedCode = "template<typename T>\n"
+ " requires(Foo<T>)\n"
+ "T foo();";
+ BaseTokenCount = 11;
+ RequiresTokenCount = 7;
+ PrefixTokenCount = 5;
+ TestRequires(__LINE__);
+
BaseCode = "template<typename T>\n"
"Bar(T) -> Bar<typename T::I>;";
ConstrainedCode = "template<typename T>\n"
More information about the cfe-commits
mailing list