• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Support/ScopedPrinter.h"
24 #include "llvm/Testing/Support/Annotations.h"
25 #include "gmock/gmock.h"
26 #include "gtest/gtest.h"
27 
28 namespace clang {
29 namespace syntax {
30 class SyntaxTreeTest : public ::testing::Test,
31                        public ::testing::WithParamInterface<TestClangConfig> {
32 protected:
33   // Build a syntax tree for the code.
34   TranslationUnit *buildTree(StringRef Code,
35                              const TestClangConfig &ClangConfig);
36 
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);
40 
41   // Data fields.
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;
55 };
56 
57 std::vector<TestClangConfig> allTestClangConfigs();
58 
59 MATCHER_P(role, R, "") {
60   if (arg.getRole() == R)
61     return true;
62   *result_listener << "role is " << llvm::to_string(arg.getRole());
63   return false;
64 }
65 
66 } // namespace syntax
67 } // namespace clang
68 #endif // LLVM_CLANG_UNITTESTS_TOOLING_SYNTAX_TREETESTBASE_H
69