Skip to content

Commit

Permalink
[Clang][Lex] Fix parsing of nested requirement to prevent flowing off…
Browse files Browse the repository at this point in the history
… the end of token stream (#73691)

Currently when parsing a nested requirement we attempt to balance parens
if we have a parameter list. This will fail in some cases of ill-formed
code and keep going until we fall off the token stream and crash. This
fixes the hand parsing by using SkipUntil which will properly flag if we
don't find the expected tokens.

Fixes: #73112
  • Loading branch information
shafik committed Nov 30, 2023
1 parent 770dc47 commit 0233a13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ Bug Fixes to C++ Support
completes (except deduction guides). Fixes:
(`#59827 <https://github.com/llvm/llvm-project/issues/59827>`_)

- Fix crash when parsing nested requirement. Fixes:
(`#73112 <https://github.com/llvm/llvm-project/issues/73112>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Parse/ParseExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3635,10 +3635,12 @@ ExprResult Parser::ParseRequiresExpression() {
auto Res = TryParseParameterDeclarationClause();
if (Res != TPResult::False) {
// Skip to the closing parenthesis
// FIXME: Don't traverse these tokens twice (here and in
// TryParseParameterDeclarationClause).
unsigned Depth = 1;
while (Depth != 0) {
bool FoundParen = SkipUntil(tok::l_paren, tok::r_paren,
SkipUntilFlags::StopBeforeMatch);
if (!FoundParen)
break;
if (Tok.is(tok::l_paren))
Depth++;
else if (Tok.is(tok::r_paren))
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Parser/cxx2a-concepts-requires-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,11 @@ template <int N>
requires requires {
typename BitInt<N>; // ok
} using r44 = void;

namespace GH73112 {
void f() {
requires { requires(int; } // expected-error {{expected ')'}} \
// expected-error {{expected expression}} \
// expected-note {{to match this '('}}
}
}

0 comments on commit 0233a13

Please sign in to comment.