1 //===- unittests/libclang/TestUtils.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 #ifndef LLVM_CLANG_TEST_TESTUTILS_H
10 #define LLVM_CLANG_TEST_TESTUTILS_H
12 #include "clang-c/Index.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
21 #include "gtest/gtest.h"
23 class LibclangParseTest
: public ::testing::Test
{
24 std::set
<std::string
> Files
;
25 typedef std::unique_ptr
<std::string
> fixed_addr_string
;
26 std::map
<fixed_addr_string
, fixed_addr_string
> UnsavedFileContents
;
30 CXTranslationUnit ClangTU
;
32 std::vector
<CXUnsavedFile
> UnsavedFiles
;
34 void SetUp() override
{
35 llvm::SmallString
<256> Dir
;
36 ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir
));
37 TestDir
= std::string(Dir
.str());
38 TUFlags
= CXTranslationUnit_DetailedPreprocessingRecord
|
39 clang_defaultEditingTranslationUnitOptions();
40 Index
= clang_createIndex(0, 0);
43 void TearDown() override
{
44 clang_disposeTranslationUnit(ClangTU
);
45 clang_disposeIndex(Index
);
46 for (const std::string
&Path
: Files
)
47 llvm::sys::fs::remove(Path
);
48 llvm::sys::fs::remove(TestDir
);
50 void WriteFile(std::string
&Filename
, const std::string
&Contents
) {
51 if (!llvm::sys::path::is_absolute(Filename
)) {
52 llvm::SmallString
<256> Path(TestDir
);
53 llvm::sys::path::append(Path
, Filename
);
54 Filename
= std::string(Path
.str());
55 Files
.insert(Filename
);
57 llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename
));
58 std::ofstream
OS(Filename
);
62 void MapUnsavedFile(std::string Filename
, const std::string
&Contents
) {
63 if (!llvm::sys::path::is_absolute(Filename
)) {
64 llvm::SmallString
<256> Path(TestDir
);
65 llvm::sys::path::append(Path
, Filename
);
66 Filename
= std::string(Path
.str());
68 auto it
= UnsavedFileContents
.insert(std::make_pair(
69 fixed_addr_string(new std::string(Filename
)),
70 fixed_addr_string(new std::string(Contents
))));
71 UnsavedFiles
.push_back({
72 it
.first
->first
->c_str(), // filename
73 it
.first
->second
->c_str(), // contents
74 it
.first
->second
->size() // length
78 void Traverse(const F
&TraversalFunctor
) {
79 CXCursor TuCursor
= clang_getTranslationUnitCursor(ClangTU
);
80 std::reference_wrapper
<const F
> FunctorRef
= std::cref(TraversalFunctor
);
81 clang_visitChildren(TuCursor
,
82 &TraverseStateless
<std::reference_wrapper
<const F
>>,
86 template<typename TState
>
87 static CXChildVisitResult
TraverseStateless(CXCursor cx
, CXCursor parent
,
89 TState
*State
= static_cast<TState
*>(data
);
90 return State
->get()(cx
, parent
);
94 #endif // LLVM_CLANG_TEST_TESTUTILS_H