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/Tokens.h"
21 #include "clang/Tooling/Syntax/Tree.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/ScopedPrinter.h"
24 #include "llvm/Testing/Support/Annotations.h"
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
30 class SyntaxTreeTest
: public ::testing::Test
,
31 public ::testing::WithParamInterface
<TestClangConfig
> {
33 // Build a syntax tree for the code.
34 TranslationUnit
*buildTree(StringRef Code
,
35 const TestClangConfig
&ClangConfig
);
37 /// Finds the deepest node in the tree that covers exactly \p R.
38 /// FIXME: implement this efficiently and move to public syntax tree API.
39 syntax::Node
*nodeByRange(llvm::Annotations::Range R
, syntax::Node
*Root
);
42 IntrusiveRefCntPtr
<DiagnosticOptions
> DiagOpts
= new DiagnosticOptions();
43 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
=
44 new DiagnosticsEngine(new DiagnosticIDs
, DiagOpts
.get());
45 IntrusiveRefCntPtr
<llvm::vfs::InMemoryFileSystem
> FS
=
46 new llvm::vfs::InMemoryFileSystem
;
47 IntrusiveRefCntPtr
<FileManager
> FileMgr
=
48 new FileManager(FileSystemOptions(), FS
);
49 IntrusiveRefCntPtr
<SourceManager
> SourceMgr
=
50 new SourceManager(*Diags
, *FileMgr
);
51 std::shared_ptr
<CompilerInvocation
> Invocation
;
52 // Set after calling buildTree().
53 std::unique_ptr
<syntax::TokenBuffer
> TB
;
54 std::unique_ptr
<syntax::Arena
> Arena
;
57 std::vector
<TestClangConfig
> allTestClangConfigs();
59 MATCHER_P(role
, R
, "") {
60 if (arg
.getRole() == R
)
62 *result_listener
<< "role is " << llvm::to_string(arg
.getRole());
68 #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H