compile_helper.h 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. #ifndef CARBON_TOOLCHAIN_TESTING_COMPILE_HELPER_H_
  5. #define CARBON_TOOLCHAIN_TESTING_COMPILE_HELPER_H_
  6. #include <forward_list>
  7. #include "llvm/Support/VirtualFileSystem.h"
  8. #include "toolchain/diagnostics/consumer.h"
  9. #include "toolchain/lex/lex.h"
  10. #include "toolchain/parse/parse.h"
  11. #include "toolchain/parse/tree_and_subtrees.h"
  12. #include "toolchain/source/source_buffer.h"
  13. namespace Carbon::Testing {
  14. // A test helper for compile-related functionality.
  15. class CompileHelper {
  16. public:
  17. // Returns the result of lex.
  18. auto GetTokenizedBuffer(llvm::StringRef text,
  19. Diagnostics::Consumer* consumer = nullptr)
  20. -> Lex::TokenizedBuffer&;
  21. // Returns the result of lex along with shared values.
  22. auto GetTokenizedBufferWithSharedValueStore(
  23. llvm::StringRef text, Diagnostics::Consumer* consumer = nullptr)
  24. -> std::pair<Lex::TokenizedBuffer&, SharedValueStores&>;
  25. // Returns the result of parse.
  26. auto GetTree(llvm::StringRef text) -> Parse::Tree&;
  27. // Returns the result of parse (with extra subtree information).
  28. auto GetTreeAndSubtrees(llvm::StringRef text) -> Parse::TreeAndSubtrees&;
  29. // Returns the results of both lex and parse (with extra subtree information).
  30. auto GetTokenizedBufferWithTreeAndSubtrees(llvm::StringRef text)
  31. -> std::pair<Lex::TokenizedBuffer&, Parse::TreeAndSubtrees&>;
  32. private:
  33. // Produces a source buffer for the input text.
  34. auto GetSourceBuffer(llvm::StringRef text) -> SourceBuffer&;
  35. // Diagnostics will be printed to console.
  36. Diagnostics::Consumer& consumer_ = Diagnostics::ConsoleConsumer();
  37. // An index to generate unique filenames.
  38. int file_index_ = 0;
  39. // A filesystem for storing test files.
  40. llvm::vfs::InMemoryFileSystem fs_;
  41. // Storage for various compile artifacts.
  42. std::forward_list<SourceBuffer> source_storage_;
  43. std::forward_list<SharedValueStores> value_store_storage_;
  44. std::forward_list<Lex::TokenizedBuffer> token_storage_;
  45. std::forward_list<Parse::Tree> tree_storage_;
  46. std::forward_list<Parse::TreeAndSubtrees> tree_and_subtrees_storage_;
  47. };
  48. } // namespace Carbon::Testing
  49. #endif // CARBON_TOOLCHAIN_TESTING_COMPILE_HELPER_H_