diff options
Diffstat (limited to 'clang/unittests/libclang/LibclangTest.cpp')
-rw-r--r-- | clang/unittests/libclang/LibclangTest.cpp | 165 |
1 files changed, 164 insertions, 1 deletions
diff --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp index 11a729f..18d0fc1 100644 --- a/clang/unittests/libclang/LibclangTest.cpp +++ b/clang/unittests/libclang/LibclangTest.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "TestUtils.h" #include "clang-c/Index.h" #include "clang-c/Rewrite.h" #include "llvm/ADT/StringRef.h" @@ -14,7 +15,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" -#include "TestUtils.h" +#include <cstring> #include <fstream> #include <functional> #include <map> @@ -355,6 +356,168 @@ TEST(libclang, ModuleMapDescriptor) { clang_ModuleMapDescriptor_dispose(MMD); } +TEST_F(LibclangParseTest, GlobalOptions) { + EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index), CXGlobalOpt_None); +} + +class LibclangIndexOptionsTest : public LibclangParseTest { + virtual void AdjustOptions(CXIndexOptions &Opts) {} + +protected: + void CreateIndex() override { + CXIndexOptions Opts; + memset(&Opts, 0, sizeof(Opts)); + Opts.Size = sizeof(CXIndexOptions); + AdjustOptions(Opts); + Index = clang_createIndexWithOptions(&Opts); + ASSERT_TRUE(Index); + } +}; + +TEST_F(LibclangIndexOptionsTest, GlobalOptions) { + EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index), CXGlobalOpt_None); +} + +class LibclangIndexingEnabledIndexOptionsTest + : public LibclangIndexOptionsTest { + void AdjustOptions(CXIndexOptions &Opts) override { + Opts.ThreadBackgroundPriorityForIndexing = CXChoice_Enabled; + } +}; + +TEST_F(LibclangIndexingEnabledIndexOptionsTest, GlobalOptions) { + EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index), + CXGlobalOpt_ThreadBackgroundPriorityForIndexing); +} + +class LibclangIndexingDisabledEditingEnabledIndexOptionsTest + : public LibclangIndexOptionsTest { + void AdjustOptions(CXIndexOptions &Opts) override { + Opts.ThreadBackgroundPriorityForIndexing = CXChoice_Disabled; + Opts.ThreadBackgroundPriorityForEditing = CXChoice_Enabled; + } +}; + +TEST_F(LibclangIndexingDisabledEditingEnabledIndexOptionsTest, GlobalOptions) { + EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index), + CXGlobalOpt_ThreadBackgroundPriorityForEditing); +} + +class LibclangBothEnabledIndexOptionsTest : public LibclangIndexOptionsTest { + void AdjustOptions(CXIndexOptions &Opts) override { + Opts.ThreadBackgroundPriorityForIndexing = CXChoice_Enabled; + Opts.ThreadBackgroundPriorityForEditing = CXChoice_Enabled; + } +}; + +TEST_F(LibclangBothEnabledIndexOptionsTest, GlobalOptions) { + EXPECT_EQ(clang_CXIndex_getGlobalOptions(Index), + CXGlobalOpt_ThreadBackgroundPriorityForAll); +} + +class LibclangPreambleStorageTest : public LibclangParseTest { + std::string Main = "main.cpp"; + +protected: + std::string PreambleDir; + void InitializePreambleDir() { + llvm::SmallString<128> PathBuffer(TestDir); + llvm::sys::path::append(PathBuffer, "preambles"); + namespace fs = llvm::sys::fs; + ASSERT_FALSE(fs::create_directory(PathBuffer, false, fs::perms::owner_all)); + + PreambleDir = static_cast<std::string>(PathBuffer); + FilesAndDirsToRemove.insert(PreambleDir); + } + +public: + void CountPreamblesInPreambleDir(int PreambleCount) { + // For some reason, the preamble is not created without '\n' before `int`. + WriteFile(Main, "\nint main() {}"); + + TUFlags |= CXTranslationUnit_CreatePreambleOnFirstParse; + ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, + nullptr, 0, TUFlags); + + int FileCount = 0; + + namespace fs = llvm::sys::fs; + std::error_code EC; + for (fs::directory_iterator File(PreambleDir, EC), FileEnd; + File != FileEnd && !EC; File.increment(EC)) { + ++FileCount; + + EXPECT_EQ(File->type(), fs::file_type::regular_file); + + const auto Filename = llvm::sys::path::filename(File->path()); + EXPECT_EQ(Filename.size(), std::strlen("preamble-%%%%%%.pch")); + EXPECT_TRUE(Filename.startswith("preamble-")); + EXPECT_TRUE(Filename.endswith(".pch")); + + const auto Status = File->status(); + ASSERT_TRUE(Status); + if (false) { + // The permissions assertion below fails, because the .pch.tmp file is + // created with default permissions and replaces the .pch file along + // with its permissions. Therefore the permissions set in + // TempPCHFile::create() don't matter in the end. + EXPECT_EQ(Status->permissions(), fs::owner_read | fs::owner_write); + } + } + + EXPECT_EQ(FileCount, PreambleCount); + } +}; + +class LibclangNotOverriddenPreambleStoragePathTest + : public LibclangPreambleStorageTest { +protected: + void CreateIndex() override { + InitializePreambleDir(); + LibclangPreambleStorageTest::CreateIndex(); + } +}; + +class LibclangSetPreambleStoragePathTest : public LibclangPreambleStorageTest { + virtual const char *PreambleStoragePath() = 0; + +protected: + void CreateIndex() override { + InitializePreambleDir(); + + CXIndexOptions Opts = {sizeof(CXIndexOptions)}; + Opts.PreambleStoragePath = PreambleStoragePath(); + Index = clang_createIndexWithOptions(&Opts); + ASSERT_TRUE(Index); + } +}; + +class LibclangNullPreambleStoragePathTest + : public LibclangSetPreambleStoragePathTest { + const char *PreambleStoragePath() override { return nullptr; } +}; +class LibclangEmptyPreambleStoragePathTest + : public LibclangSetPreambleStoragePathTest { + const char *PreambleStoragePath() override { return ""; } +}; +class LibclangPreambleDirPreambleStoragePathTest + : public LibclangSetPreambleStoragePathTest { + const char *PreambleStoragePath() override { return PreambleDir.c_str(); } +}; + +TEST_F(LibclangNotOverriddenPreambleStoragePathTest, CountPreambles) { + CountPreamblesInPreambleDir(0); +} +TEST_F(LibclangNullPreambleStoragePathTest, CountPreambles) { + CountPreamblesInPreambleDir(0); +} +TEST_F(LibclangEmptyPreambleStoragePathTest, CountPreambles) { + CountPreamblesInPreambleDir(0); +} +TEST_F(LibclangPreambleDirPreambleStoragePathTest, CountPreambles) { + CountPreamblesInPreambleDir(1); +} + TEST_F(LibclangParseTest, AllSkippedRanges) { std::string Header = "header.h", Main = "main.cpp"; WriteFile(Header, |