compile_helper.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/testing/compile_helper.h"
  5. namespace Carbon::Testing {
  6. auto CompileHelper::GetTokenizedBuffer(llvm::StringRef text,
  7. DiagnosticConsumer* consumer)
  8. -> Lex::TokenizedBuffer& {
  9. auto& source = GetSourceBuffer(text);
  10. value_store_storage_.emplace_front();
  11. token_storage_.push_front(Lex::Lex(value_store_storage_.front(), source,
  12. consumer ? *consumer : consumer_));
  13. return token_storage_.front();
  14. }
  15. auto CompileHelper::GetTokenizedBufferWithSharedValueStore(llvm::StringRef text)
  16. -> std::pair<Lex::TokenizedBuffer&, SharedValueStores&> {
  17. auto& tokens = GetTokenizedBuffer(text);
  18. return {tokens, value_store_storage_.front()};
  19. }
  20. auto CompileHelper::GetTree(llvm::StringRef text) -> Parse::Tree& {
  21. auto& tokens = GetTokenizedBuffer(text);
  22. tree_storage_.push_front(Parse::Parse(tokens, consumer_,
  23. /*vlog_stream=*/nullptr));
  24. return tree_storage_.front();
  25. }
  26. auto CompileHelper::GetTreeAndSubtrees(llvm::StringRef text)
  27. -> Parse::TreeAndSubtrees& {
  28. auto& tree = GetTree(text);
  29. tree_and_subtrees_storage_.push_front(
  30. Parse::TreeAndSubtrees(token_storage_.front(), tree));
  31. return tree_and_subtrees_storage_.front();
  32. }
  33. auto CompileHelper::GetTokenizedBufferWithTreeAndSubtrees(llvm::StringRef text)
  34. -> std::pair<Lex::TokenizedBuffer&, Parse::TreeAndSubtrees&> {
  35. auto& tree_and_subtrees = GetTreeAndSubtrees(text);
  36. return {token_storage_.front(), tree_and_subtrees};
  37. }
  38. auto CompileHelper::GetSourceBuffer(llvm::StringRef text) -> SourceBuffer& {
  39. std::string filename = llvm::formatv("test{0}.carbon", ++file_index_);
  40. CARBON_CHECK(fs_.addFile(filename, /*ModificationTime=*/0,
  41. llvm::MemoryBuffer::getMemBuffer(text)));
  42. source_storage_.push_front(
  43. std::move(*SourceBuffer::MakeFromFile(fs_, filename, consumer_)));
  44. return source_storage_.front();
  45. }
  46. } // namespace Carbon::Testing