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