xref: /llvm-project/clang/unittests/Tooling/Syntax/TreeTestBase.h (revision 3432f4bf86e7c77666ae9dede1610ae843dde648)
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/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 
29 namespace clang {
30 namespace syntax {
31 class SyntaxTreeTest : public ::testing::Test,
32                        public ::testing::WithParamInterface<TestClangConfig> {
33 protected:
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   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;
57 };
58 
59 std::vector<TestClangConfig> allTestClangConfigs();
60 
61 MATCHER_P(role, R, "") {
62   if (arg.getRole() == R)
63     return true;
64   *result_listener << "role is " << llvm::to_string(arg.getRole());
65   return false;
66 }
67 
68 } // namespace syntax
69 } // namespace clang
70 #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
71