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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides the test infrastructure for syntax trees.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
14 #define LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
15 
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/Testing/Support/Annotations.h"
24 #include "gtest/gtest.h"
25 
26 namespace clang {
27 namespace syntax {
28 class SyntaxTreeTest : public ::testing::Test,
29                        public ::testing::WithParamInterface<TestClangConfig> {
30 protected:
31   // Build a syntax tree for the code.
32   TranslationUnit *buildTree(StringRef Code,
33                              const TestClangConfig &ClangConfig);
34 
35   ::testing::AssertionResult treeDumpEqual(StringRef Code, StringRef Tree);
36 
37   ::testing::AssertionResult
38   treeDumpEqualOnAnnotations(StringRef CodeWithAnnotations,
39                              ArrayRef<StringRef> TreeDumps);
40   /// Finds the deepest node in the tree that covers exactly \p R.
41   /// FIXME: implement this efficiently and move to public syntax tree API.
42   syntax::Node *nodeByRange(llvm::Annotations::Range R, syntax::Node *Root);
43 
44   // Data fields.
45   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
46   IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
47       new DiagnosticsEngine(new DiagnosticIDs, DiagOpts.get());
48   IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS =
49       new llvm::vfs::InMemoryFileSystem;
50   IntrusiveRefCntPtr<FileManager> FileMgr =
51       new FileManager(FileSystemOptions(), FS);
52   IntrusiveRefCntPtr<SourceManager> SourceMgr =
53       new SourceManager(*Diags, *FileMgr);
54   std::shared_ptr<CompilerInvocation> Invocation;
55   // Set after calling buildTree().
56   std::unique_ptr<syntax::TokenBuffer> TB;
57   std::unique_ptr<syntax::Arena> Arena;
58 };
59 } // namespace syntax
60 } // namespace clang
61 #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
62