mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:30:14 +01:00
There's currently a bug with empty files, in that it initializes SourceBuffer with an invalid StringRef that results in a crash. That got me looking at the std::optional TODO, but the issue is that there are really three states: - Buffered - mmapped (not buffered) - Moved out of (no longer initialized) Technically an optional could work if we initialize the buffer on move out, indicating the mmap is gone. But the mode setup felt better to me. And then this also adds the size check. Which is really how I started looking at this.
43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/semantics/semantics.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <optional>
|
|
|
|
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
|
#include "toolchain/lexer/tokenized_buffer.h"
|
|
#include "toolchain/parser/parse_tree.h"
|
|
#include "toolchain/source/source_buffer.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
class ParseTreeTest : public ::testing::Test {
|
|
protected:
|
|
auto Analyze(llvm::Twine t) -> Semantics {
|
|
source_buffer.emplace(std::move(*SourceBuffer::CreateFromText(t.str())));
|
|
tokenized_buffer = TokenizedBuffer::Lex(*source_buffer, consumer);
|
|
parse_tree = ParseTree::Parse(*tokenized_buffer, consumer);
|
|
return Semantics::Analyze(*parse_tree, consumer);
|
|
}
|
|
|
|
std::optional<SourceBuffer> source_buffer;
|
|
std::optional<TokenizedBuffer> tokenized_buffer;
|
|
std::optional<ParseTree> parse_tree;
|
|
DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
|
|
};
|
|
|
|
TEST_F(ParseTreeTest, Empty) {
|
|
// TODO: Validate the returned Semantics object.
|
|
Analyze("");
|
|
ASSERT_FALSE(parse_tree->HasErrors());
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|