mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 06:21:04 +01:00
Modify parsing to monitor stack depth and error before overflow (#987)
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "llvm/ADT/Sequence.h"
|
||||
#include "llvm/Support/FormatVariadic.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
#include "toolchain/common/yaml_test_helpers.h"
|
||||
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
||||
@@ -22,6 +23,7 @@
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
using ::testing::AtLeast;
|
||||
using ::testing::ElementsAre;
|
||||
using ::testing::Eq;
|
||||
using ::testing::Ne;
|
||||
@@ -1170,5 +1172,25 @@ TEST_F(ParseTreeTest, ParenMatchRegression) {
|
||||
MatchFileEnd()}));
|
||||
}
|
||||
|
||||
TEST_F(ParseTreeTest, RecursionLimit) {
|
||||
std::string code = "fn Foo() { return ";
|
||||
code.append(10000, '(');
|
||||
code.append(10000, ')');
|
||||
code += "; }";
|
||||
TokenizedBuffer tokens = GetTokenizedBuffer(code);
|
||||
ASSERT_FALSE(tokens.HasErrors());
|
||||
Testing::MockDiagnosticConsumer consumer;
|
||||
// Recursion might be exceeded multiple times due to quirks in parse tree
|
||||
// handling; we only need to be sure it's hit at least once for test
|
||||
// correctness.
|
||||
EXPECT_CALL(
|
||||
consumer,
|
||||
HandleDiagnostic(DiagnosticMessage(llvm::formatv(
|
||||
"Exceeded recursion limit ({0})", ParseTree::StackDepthLimit))))
|
||||
.Times(AtLeast(1));
|
||||
ParseTree tree = ParseTree::Parse(tokens, consumer);
|
||||
EXPECT_TRUE(tree.HasErrors());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
|
||||
Reference in New Issue
Block a user