Skip to content

Commit b431830

Browse files
committed
[Clang][WIP] Increase the default expression nesting limit.
Fixes #94728
1 parent 33e5d01 commit b431830

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ Modified Compiler Flags
169169
the behavior of ``-mtp`` in gcc. This changes the default behavior for ARM targets that provide the ``TPIDRURO`` register as this will be used instead of a call to the ``__aeabi_read_tp``.
170170
Programs that use ``__aeabi_read_tp`` but do not use the ``TPIDRURO`` register must use ``-mtp=soft``. Fixes #123864
171171

172+
- The compiler flag `-fbracket-depth` default value is increased from 256 to 2048. (#GH94728)
173+
172174
Removed Compiler Flags
173175
-------------------------
174176

@@ -264,7 +266,7 @@ Improvements to Clang's diagnostics
264266
as function arguments or return value respectively. Note that
265267
:doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
266268
feature will be default-enabled with ``-Wthread-safety`` in a future release.
267-
- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
269+
- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
268270
except for the case where the operand is an unsigned integer
269271
and throws warning if they are compared with unsigned integers (##18878).
270272
- The ``-Wunnecessary-virtual-specifier`` warning has been added to warn about

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8161,7 +8161,7 @@ def fapply_global_visibility_to_externs : Flag<["-"], "fapply-global-visibility-
81618161
MarshallingInfoFlag<LangOpts<"SetVisibilityForExternDecls">>;
81628162
def fbracket_depth : Separate<["-"], "fbracket-depth">,
81638163
HelpText<"Maximum nesting level for parentheses, brackets, and braces">,
8164-
MarshallingInfoInt<LangOpts<"BracketDepth">, "256">;
8164+
MarshallingInfoInt<LangOpts<"BracketDepth">, "2048">;
81658165
defm const_strings : BoolOption<"f", "const-strings",
81668166
LangOpts<"ConstStrings">, DefaultFalse,
81678167
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Use">,

clang/include/clang/Parse/Parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class Parser : public CodeCompletionHandler {
9191

9292
DiagnosticsEngine &Diags;
9393

94+
StackExhaustionHandler StackHandler;
95+
9496
/// ScopeCache - Cache scopes to reduce malloc traffic.
9597
enum { ScopeCacheSize = 16 };
9698
unsigned NumCachedScopes;
@@ -518,7 +520,7 @@ class Parser : public CodeCompletionHandler {
518520
typedef Sema::FullExprArg FullExprArg;
519521

520522
/// A SmallVector of statements.
521-
typedef SmallVector<Stmt *, 32> StmtVector;
523+
typedef SmallVector<Stmt *, 24> StmtVector;
522524

523525
// Parsing methods.
524526

clang/lib/Parse/ParseStmt.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,19 @@ Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
126126
Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs, GNUOrMSAttrs);
127127
MaybeDestroyTemplateIds();
128128

129-
// Attributes that are left should all go on the statement, so concatenate the
130-
// two lists.
131-
ParsedAttributes Attrs(AttrFactory);
132-
takeAndConcatenateAttrs(CXX11Attrs, GNUOrMSAttrs, Attrs);
129+
CXX11Attrs.takeAllFrom(GNUOrMSAttrs);
130+
if (!CXX11Attrs.Range.getBegin().isValid())
131+
CXX11Attrs.Range.setBegin(GNUOrMSAttrs.Range.getBegin());
132+
if (GNUOrMSAttrs.Range.getEnd().isValid())
133+
CXX11Attrs.Range.setEnd(GNUOrMSAttrs.Range.getEnd());
133134

134-
assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
135+
assert((CXX11Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
135136
"attributes on empty statement");
136137

137-
if (Attrs.empty() || Res.isInvalid())
138+
if (CXX11Attrs.empty() || Res.isInvalid())
138139
return Res;
139140

140-
return Actions.ActOnAttributedStmt(Attrs, Res.get());
141+
return Actions.ActOnAttributedStmt(CXX11Attrs, Res.get());
141142
}
142143

143144
namespace {
@@ -1057,7 +1058,11 @@ StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
10571058
ParseScope CompoundScope(this, ScopeFlags);
10581059

10591060
// Parse the statements in the body.
1060-
return ParseCompoundStatementBody(isStmtExpr);
1061+
StmtResult R;
1062+
StackHandler.runWithSufficientStackSpace(Tok.getLocation(), [&, this]() {
1063+
R = ParseCompoundStatementBody(isStmtExpr);
1064+
});
1065+
return R;
10611066
}
10621067

10631068
/// Parse any pragmas at the start of the compound expression. We handle these
@@ -1222,7 +1227,7 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
12221227
while (Tok.is(tok::kw___label__)) {
12231228
SourceLocation LabelLoc = ConsumeToken();
12241229

1225-
SmallVector<Decl *, 8> DeclsInGroup;
1230+
SmallVector<Decl *, 4> DeclsInGroup;
12261231
while (true) {
12271232
if (Tok.isNot(tok::identifier)) {
12281233
Diag(Tok, diag::err_expected) << tok::identifier;

clang/lib/Parse/Parser.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/AST/DeclTemplate.h"
1818
#include "clang/Basic/DiagnosticParse.h"
1919
#include "clang/Basic/FileManager.h"
20+
#include "clang/Basic/StackExhaustionHandler.h"
2021
#include "clang/Parse/RAIIObjectsForParser.h"
2122
#include "clang/Sema/DeclSpec.h"
2223
#include "clang/Sema/EnterExpressionEvaluationContext.h"
@@ -54,9 +55,10 @@ IdentifierInfo *Parser::getSEHExceptKeyword() {
5455

5556
Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
5657
: PP(pp), PreferredType(pp.isCodeCompletionEnabled()), Actions(actions),
57-
Diags(PP.getDiagnostics()), GreaterThanIsOperator(true),
58-
ColonIsSacred(false), InMessageExpression(false),
59-
TemplateParameterDepth(0), ParsingInObjCContainer(false) {
58+
Diags(PP.getDiagnostics()), StackHandler(Diags),
59+
GreaterThanIsOperator(true), ColonIsSacred(false),
60+
InMessageExpression(false), TemplateParameterDepth(0),
61+
ParsingInObjCContainer(false) {
6062
SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
6163
Tok.startToken();
6264
Tok.setKind(tok::eof);

clang/test/Parser/parser_overflow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: not %clang_cc1 %s -fsyntax-only -DHUGE 2>&1 | FileCheck %s
2-
// RUN: not %clang_cc1 %s -fsyntax-only 2>&1 | FileCheck %s
2+
// RUN: %clang_cc1 %s -fsyntax-only
33
// RUN: not %clang_cc1 %s -fsyntax-only -fbracket-depth 299 2>&1 | FileCheck %s
44
// RUN: %clang_cc1 %s -fsyntax-only -fbracket-depth 300
55
// RUN: not %clang %s -fsyntax-only -fbracket-depth=299 2>&1 | FileCheck %s
@@ -15,5 +15,5 @@ void foo(void) {
1515
#endif
1616
}
1717

18-
// CHECK: fatal error: bracket nesting level exceeded maximum of {{256|299}}
18+
// CHECK: fatal error: bracket nesting level exceeded maximum of {{2048|299}}
1919
// CHECK: note: use -fbracket-depth=N to increase maximum nesting level

0 commit comments

Comments
 (0)