compile_helper.cpp 2.3 KB

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