Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
clang/unittests/libclang/TestUtils.h
//===- unittests/libclang/TestUtils.h -------------------------------------===// | //===- unittests/libclang/TestUtils.h -------------------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#ifndef LLVM_CLANG_TEST_TESTUTILS_H | #ifndef LLVM_CLANG_TEST_TESTUTILS_H | ||||
#define LLVM_CLANG_TEST_TESTUTILS_H | #define LLVM_CLANG_TEST_TESTUTILS_H | ||||
#include "clang-c/Index.h" | #include "clang-c/Index.h" | ||||
#include "llvm/ADT/StringRef.h" | #include "llvm/ADT/StringRef.h" | ||||
#include "llvm/Support/FileSystem.h" | #include "llvm/Support/FileSystem.h" | ||||
#include "llvm/Support/Path.h" | #include "llvm/Support/Path.h" | ||||
#include "gtest/gtest.h" | |||||
#include <fstream> | #include <fstream> | ||||
#include <functional> | |||||
#include <memory> | #include <memory> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#include "gtest/gtest.h" | |||||
class LibclangParseTest : public ::testing::Test { | class LibclangParseTest : public ::testing::Test { | ||||
std::set<std::string> Files; | // std::greater<> to remove files before their parent dirs in TearDown(). | ||||
std::set<std::string, std::greater<>> Files; | |||||
typedef std::unique_ptr<std::string> fixed_addr_string; | typedef std::unique_ptr<std::string> fixed_addr_string; | ||||
std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents; | std::map<fixed_addr_string, fixed_addr_string> UnsavedFileContents; | ||||
public: | public: | ||||
std::string TestDir; | std::string TestDir; | ||||
bool RemoveTestDirRecursivelyDuringTeardown = false; | |||||
CXIndex Index; | CXIndex Index; | ||||
CXTranslationUnit ClangTU; | CXTranslationUnit ClangTU; | ||||
unsigned TUFlags; | unsigned TUFlags; | ||||
std::vector<CXUnsavedFile> UnsavedFiles; | std::vector<CXUnsavedFile> UnsavedFiles; | ||||
void SetUp() override { | void SetUp() override { | ||||
llvm::SmallString<256> Dir; | llvm::SmallString<256> Dir; | ||||
ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir)); | ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("libclang-test", Dir)); | ||||
TestDir = std::string(Dir.str()); | TestDir = std::string(Dir.str()); | ||||
TUFlags = CXTranslationUnit_DetailedPreprocessingRecord | | TUFlags = CXTranslationUnit_DetailedPreprocessingRecord | | ||||
clang_defaultEditingTranslationUnitOptions(); | clang_defaultEditingTranslationUnitOptions(); | ||||
Index = clang_createIndex(0, 0); | Index = clang_createIndex(0, 0); | ||||
ClangTU = nullptr; | ClangTU = nullptr; | ||||
} | } | ||||
void TearDown() override { | void TearDown() override { | ||||
clang_disposeTranslationUnit(ClangTU); | clang_disposeTranslationUnit(ClangTU); | ||||
clang_disposeIndex(Index); | clang_disposeIndex(Index); | ||||
namespace fs = llvm::sys::fs; | |||||
for (const std::string &Path : Files) | for (const std::string &Path : Files) | ||||
llvm::sys::fs::remove(Path); | EXPECT_FALSE(fs::remove(Path, /*IgnoreNonExisting=*/false)); | ||||
llvm::sys::fs::remove(TestDir); | if (RemoveTestDirRecursivelyDuringTeardown) | ||||
EXPECT_FALSE(fs::remove_directories(TestDir, /*IgnoreErrors=*/false)); | |||||
else | |||||
EXPECT_FALSE(fs::remove(TestDir, /*IgnoreNonExisting=*/false)); | |||||
} | } | ||||
void WriteFile(std::string &Filename, const std::string &Contents) { | void WriteFile(std::string &Filename, const std::string &Contents) { | ||||
if (!llvm::sys::path::is_absolute(Filename)) { | if (!llvm::sys::path::is_absolute(Filename)) { | ||||
llvm::SmallString<256> Path(TestDir); | llvm::SmallString<256> Path(TestDir); | ||||
llvm::sys::path::append(Path, Filename); | namespace path = llvm::sys::path; | ||||
for (auto FileI = path::begin(Filename), FileEnd = path::end(Filename); | |||||
FileI != FileEnd; ++FileI) { | |||||
ASSERT_NE(*FileI, "."); | |||||
path::append(Path, *FileI); | |||||
Files.emplace(Path.str()); | |||||
} | |||||
Filename = std::string(Path.str()); | Filename = std::string(Path.str()); | ||||
Files.insert(Filename); | |||||
} | } | ||||
llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename)); | llvm::sys::fs::create_directories(llvm::sys::path::parent_path(Filename)); | ||||
std::ofstream OS(Filename); | std::ofstream OS(Filename); | ||||
OS << Contents; | OS << Contents; | ||||
assert(OS.good()); | assert(OS.good()); | ||||
} | } | ||||
void MapUnsavedFile(std::string Filename, const std::string &Contents) { | void MapUnsavedFile(std::string Filename, const std::string &Contents) { | ||||
if (!llvm::sys::path::is_absolute(Filename)) { | if (!llvm::sys::path::is_absolute(Filename)) { | ||||
Show All 37 Lines |