rymiel created this revision. rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay, curdeius. Herald added a project: All. rymiel requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D134052 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -728,6 +728,16 @@ 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" Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1110,7 +1110,7 @@ !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; }
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -728,6 +728,16 @@ 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" Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1110,7 +1110,7 @@ !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; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits