12e4a20fdSEduardo Caldas //===- TreeTestBase.h -----------------------------------------------------===// 2d17437d2SEduardo Caldas // 3d17437d2SEduardo Caldas // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4d17437d2SEduardo Caldas // See https://llvm.org/LICENSE.txt for license information. 5d17437d2SEduardo Caldas // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6d17437d2SEduardo Caldas // 7d17437d2SEduardo Caldas //===----------------------------------------------------------------------===// 8d17437d2SEduardo Caldas // 9d17437d2SEduardo Caldas // This file provides the test infrastructure for syntax trees. 10d17437d2SEduardo Caldas // 11d17437d2SEduardo Caldas //===----------------------------------------------------------------------===// 12d17437d2SEduardo Caldas 132e4a20fdSEduardo Caldas #ifndef LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H 142e4a20fdSEduardo Caldas #define LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H 152e4a20fdSEduardo Caldas 16d17437d2SEduardo Caldas #include "clang/Basic/LLVM.h" 17d17437d2SEduardo Caldas #include "clang/Frontend/CompilerInvocation.h" 18d17437d2SEduardo Caldas #include "clang/Testing/TestClangConfig.h" 19d17437d2SEduardo Caldas #include "clang/Tooling/Syntax/Nodes.h" 20263dcf45SHaojian Wu #include "clang/Tooling/Syntax/TokenBufferTokenManager.h" 21d17437d2SEduardo Caldas #include "clang/Tooling/Syntax/Tokens.h" 229c2e708fSEduardo Caldas #include "clang/Tooling/Syntax/Tree.h" 23d17437d2SEduardo Caldas #include "llvm/ADT/StringRef.h" 24d4934eb5SSam McCall #include "llvm/Support/ScopedPrinter.h" 25*3432f4bfSJordan Rupprecht #include "llvm/Testing/Annotations/Annotations.h" 26d4934eb5SSam McCall #include "gmock/gmock.h" 27d17437d2SEduardo Caldas #include "gtest/gtest.h" 28d17437d2SEduardo Caldas 29d17437d2SEduardo Caldas namespace clang { 30d17437d2SEduardo Caldas namespace syntax { 31d17437d2SEduardo Caldas class SyntaxTreeTest : public ::testing::Test, 32d17437d2SEduardo Caldas public ::testing::WithParamInterface<TestClangConfig> { 33d17437d2SEduardo Caldas protected: 34d17437d2SEduardo Caldas // Build a syntax tree for the code. 352e4a20fdSEduardo Caldas TranslationUnit *buildTree(StringRef Code, 362e4a20fdSEduardo Caldas const TestClangConfig &ClangConfig); 37d17437d2SEduardo Caldas 38d17437d2SEduardo Caldas /// Finds the deepest node in the tree that covers exactly \p R. 39d17437d2SEduardo Caldas /// FIXME: implement this efficiently and move to public syntax tree API. 402e4a20fdSEduardo Caldas syntax::Node *nodeByRange(llvm::Annotations::Range R, syntax::Node *Root); 41d17437d2SEduardo Caldas 42d17437d2SEduardo Caldas // Data fields. 43d17437d2SEduardo Caldas IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 44d17437d2SEduardo Caldas IntrusiveRefCntPtr<DiagnosticsEngine> Diags = 45d17437d2SEduardo Caldas new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get()); 46d17437d2SEduardo Caldas IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS = 47d17437d2SEduardo Caldas new llvm::vfs::InMemoryFileSystem; 48d17437d2SEduardo Caldas IntrusiveRefCntPtr<FileManager> FileMgr = 49d17437d2SEduardo Caldas new FileManager(FileSystemOptions(), FS); 50d17437d2SEduardo Caldas IntrusiveRefCntPtr<SourceManager> SourceMgr = 51d17437d2SEduardo Caldas new SourceManager(*Diags, *FileMgr); 52d17437d2SEduardo Caldas std::shared_ptr<CompilerInvocation> Invocation; 53d17437d2SEduardo Caldas // Set after calling buildTree(). 54d17437d2SEduardo Caldas std::unique_ptr<syntax::TokenBuffer> TB; 55263dcf45SHaojian Wu std::unique_ptr<syntax::TokenBufferTokenManager> TM; 56d17437d2SEduardo Caldas std::unique_ptr<syntax::Arena> Arena; 57d17437d2SEduardo Caldas }; 58c01d28dcSEduardo Caldas 59c01d28dcSEduardo Caldas std::vector<TestClangConfig> allTestClangConfigs(); 60d4934eb5SSam McCall 61d4934eb5SSam McCall MATCHER_P(role, R, "") { 62d4934eb5SSam McCall if (arg.getRole() == R) 63d4934eb5SSam McCall return true; 64d4934eb5SSam McCall *result_listener << "role is " << llvm::to_string(arg.getRole()); 65d4934eb5SSam McCall return false; 66d4934eb5SSam McCall } 67d4934eb5SSam McCall 68d17437d2SEduardo Caldas } // namespace syntax 69d17437d2SEduardo Caldas } // namespace clang 702e4a20fdSEduardo Caldas #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H 71