1 //===- TreeTestBase.h -----------------------------------------------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides the test infrastructure for syntax trees.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
14 #define LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
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"
31 class SyntaxTreeTest
: public ::testing::Test
,
32 public ::testing::WithParamInterface
<TestClangConfig
> {
34 // Build a syntax tree for the code.
35 TranslationUnit
*buildTree(StringRef Code
,
36 const TestClangConfig
&ClangConfig
);
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
);
43 IntrusiveRefCntPtr
<DiagnosticOptions
> DiagOpts
= new DiagnosticOptions();
44 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
=
45 new DiagnosticsEngine(new DiagnosticIDs
, DiagOpts
.get());
46 IntrusiveRefCntPtr
<llvm::vfs::InMemoryFileSystem
> FS
=
47 new llvm::vfs::InMemoryFileSystem
;
48 IntrusiveRefCntPtr
<FileManager
> FileMgr
=
49 new FileManager(FileSystemOptions(), FS
);
50 IntrusiveRefCntPtr
<SourceManager
> SourceMgr
=
51 new SourceManager(*Diags
, *FileMgr
);
52 std::shared_ptr
<CompilerInvocation
> Invocation
;
53 // Set after calling buildTree().
54 std::unique_ptr
<syntax::TokenBuffer
> TB
;
55 std::unique_ptr
<syntax::TokenBufferTokenManager
> TM
;
56 std::unique_ptr
<syntax::Arena
> Arena
;
59 std::vector
<TestClangConfig
> allTestClangConfigs();
61 MATCHER_P(role
, R
, "") {
62 if (arg
.getRole() == R
)
64 *result_listener
<< "role is " << llvm::to_string(arg
.getRole());
70 #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H