compile_helper.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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(
  16. llvm::StringRef text, DiagnosticConsumer* consumer)
  17. -> std::pair<Lex::TokenizedBuffer&, SharedValueStores&> {
  18. auto& tokens = GetTokenizedBuffer(text, consumer);
  19. return {tokens, value_store_storage_.front()};
  20. }
  21. auto CompileHelper::GetTree(llvm::StringRef text) -> Parse::Tree& {
  22. auto& tokens = GetTokenizedBuffer(text);
  23. tree_storage_.push_front(Parse::Parse(tokens, consumer_,
  24. /*vlog_stream=*/nullptr));
  25. return tree_storage_.front();
  26. }
  27. auto CompileHelper::GetTreeAndSubtrees(llvm::StringRef text)
  28. -> Parse::TreeAndSubtrees& {
  29. auto& tree = GetTree(text);
  30. tree_and_subtrees_storage_.push_front(
  31. Parse::TreeAndSubtrees(token_storage_.front(), tree));
  32. return tree_and_subtrees_storage_.front();
  33. }
  34. auto CompileHelper::GetTokenizedBufferWithTreeAndSubtrees(llvm::StringRef text)
  35. -> std::pair<Lex::TokenizedBuffer&, Parse::TreeAndSubtrees&> {
  36. auto& tree_and_subtrees = GetTreeAndSubtrees(text);
  37. return {token_storage_.front(), tree_and_subtrees};
  38. }
  39. auto CompileHelper::GetSourceBuffer(llvm::StringRef text) -> SourceBuffer& {
  40. std::string filename = llvm::formatv("test{0}.carbon", ++file_index_);
  41. CARBON_CHECK(fs_.addFile(filename, /*ModificationTime=*/0,
  42. llvm::MemoryBuffer::getMemBuffer(text)));
  43. source_storage_.push_front(
  44. std::move(*SourceBuffer::MakeFromFile(fs_, filename, consumer_)));
  45. return source_storage_.front();
  46. }
  47. } // namespace Carbon::Testing