compile_helper.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include <string>
  6. #include <utility>
  7. namespace Carbon::Testing {
  8. auto CompileHelper::GetTokenizedBuffer(llvm::StringRef text,
  9. Diagnostics::Consumer* consumer)
  10. -> Lex::TokenizedBuffer& {
  11. auto& source = GetSourceBuffer(text);
  12. value_store_storage_.emplace_front();
  13. token_storage_.push_front(Lex::Lex(value_store_storage_.front(), source,
  14. consumer ? *consumer : consumer_));
  15. return token_storage_.front();
  16. }
  17. auto CompileHelper::GetTokenizedBufferWithSharedValueStore(
  18. llvm::StringRef text, Diagnostics::Consumer* consumer)
  19. -> std::pair<Lex::TokenizedBuffer&, SharedValueStores&> {
  20. auto& tokens = GetTokenizedBuffer(text, consumer);
  21. return {tokens, value_store_storage_.front()};
  22. }
  23. auto CompileHelper::GetTree(llvm::StringRef text) -> Parse::Tree& {
  24. auto& tokens = GetTokenizedBuffer(text);
  25. tree_storage_.push_front(Parse::Parse(tokens, consumer_,
  26. /*vlog_stream=*/nullptr));
  27. return tree_storage_.front();
  28. }
  29. auto CompileHelper::GetTreeAndSubtrees(llvm::StringRef text)
  30. -> Parse::TreeAndSubtrees& {
  31. auto& tree = GetTree(text);
  32. tree_and_subtrees_storage_.push_front(
  33. Parse::TreeAndSubtrees(token_storage_.front(), tree));
  34. return tree_and_subtrees_storage_.front();
  35. }
  36. auto CompileHelper::GetTokenizedBufferWithTreeAndSubtrees(llvm::StringRef text)
  37. -> std::pair<Lex::TokenizedBuffer&, Parse::TreeAndSubtrees&> {
  38. auto& tree_and_subtrees = GetTreeAndSubtrees(text);
  39. return {token_storage_.front(), tree_and_subtrees};
  40. }
  41. auto CompileHelper::GetSourceBuffer(llvm::StringRef text) -> SourceBuffer& {
  42. std::string filename = llvm::formatv("test{0}.carbon", ++file_index_);
  43. CARBON_CHECK(
  44. fs_.addFile(filename, /*ModificationTime=*/0,
  45. llvm::MemoryBuffer::getMemBuffer(
  46. text, filename, /*RequiresNullTerminator=*/false)));
  47. source_storage_.push_front(
  48. std::move(*SourceBuffer::MakeFromFile(fs_, filename, consumer_)));
  49. return source_storage_.front();
  50. }
  51. } // namespace Carbon::Testing