diff options
Diffstat (limited to 'unittests/Basic')
-rw-r--r-- | unittests/Basic/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/Basic/DiagnosticTest.cpp | 49 | ||||
-rw-r--r-- | unittests/Basic/FileManagerTest.cpp | 37 | ||||
-rw-r--r-- | unittests/Basic/SourceManagerTest.cpp | 30 | ||||
-rw-r--r-- | unittests/Basic/VirtualFileSystemTest.cpp | 17 |
5 files changed, 94 insertions, 40 deletions
diff --git a/unittests/Basic/CMakeLists.txt b/unittests/Basic/CMakeLists.txt index b8f69bf357b4..3cb3cb8d3c80 100644 --- a/unittests/Basic/CMakeLists.txt +++ b/unittests/Basic/CMakeLists.txt @@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS add_clang_unittest(BasicTests CharInfoTest.cpp + DiagnosticTest.cpp FileManagerTest.cpp SourceManagerTest.cpp VirtualFileSystemTest.cpp diff --git a/unittests/Basic/DiagnosticTest.cpp b/unittests/Basic/DiagnosticTest.cpp new file mode 100644 index 000000000000..fa2b56e08341 --- /dev/null +++ b/unittests/Basic/DiagnosticTest.cpp @@ -0,0 +1,49 @@ +//===- unittests/Basic/DiagnosticTest.cpp -- Diagnostic engine tests ------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticIDs.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +// Check that DiagnosticErrorTrap works with SuppressAllDiagnostics. +TEST(DiagnosticTest, suppressAndTrap) { + DiagnosticsEngine Diags(new DiagnosticIDs(), + new DiagnosticOptions, + new IgnoringDiagConsumer()); + Diags.setSuppressAllDiagnostics(true); + + { + DiagnosticErrorTrap trap(Diags); + + // Diag that would set UncompilableErrorOccurred and ErrorOccurred. + Diags.Report(diag::err_target_unknown_triple) << "unknown"; + + // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred. + Diags.Report(diag::err_cannot_open_file) << "file" << "error"; + + // Diag that would set FatalErrorOccurred + // (via non-note following a fatal error). + Diags.Report(diag::warn_mt_message) << "warning"; + + EXPECT_TRUE(trap.hasErrorOccurred()); + EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred()); + } + + EXPECT_FALSE(Diags.hasErrorOccurred()); + EXPECT_FALSE(Diags.hasFatalErrorOccurred()); + EXPECT_FALSE(Diags.hasUncompilableErrorOccurred()); + EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred()); +} + +} diff --git a/unittests/Basic/FileManagerTest.cpp b/unittests/Basic/FileManagerTest.cpp index b3bc767949e8..dd8cf2410ad1 100644 --- a/unittests/Basic/FileManagerTest.cpp +++ b/unittests/Basic/FileManagerTest.cpp @@ -10,8 +10,8 @@ #include "clang/Basic/FileManager.h" #include "clang/Basic/FileSystemOptions.h" #include "clang/Basic/FileSystemStatCache.h" -#include "gtest/gtest.h" #include "llvm/Config/llvm-config.h" +#include "gtest/gtest.h" using namespace llvm; using namespace clang; @@ -97,7 +97,7 @@ TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { // FileManager to report "file/directory doesn't exist". This // avoids the possibility of the result of this test being affected // by what's in the real file system. - manager.addStatCache(new FakeStatCache); + manager.addStatCache(llvm::make_unique<FakeStatCache>()); EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir")); @@ -107,7 +107,7 @@ TEST_F(FileManagerTest, NoVirtualDirectoryExistsBeforeAVirtualFileIsAdded) { // When a virtual file is added, all of its ancestors should be created. TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { // Fake an empty real file system. - manager.addStatCache(new FakeStatCache); + manager.addStatCache(llvm::make_unique<FakeStatCache>()); manager.getVirtualFile("virtual/dir/bar.h", 100, 0); EXPECT_EQ(nullptr, manager.getDirectory("virtual/dir/foo")); @@ -124,7 +124,7 @@ TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) { // getFile() returns non-NULL if a real file exists at the given path. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { // Inject fake files into the file system. - FakeStatCache *statCache = new FakeStatCache; + auto statCache = llvm::make_unique<FakeStatCache>(); statCache->InjectDirectory("/tmp", 42); statCache->InjectFile("/tmp/test", 43); @@ -135,7 +135,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { statCache->InjectFile(FileName, 45); #endif - manager.addStatCache(statCache); + manager.addStatCache(std::move(statCache)); const FileEntry *file = manager.getFile("/tmp/test"); ASSERT_TRUE(file != nullptr); @@ -158,7 +158,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) { // getFile() returns non-NULL if a virtual file exists at the given path. TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { // Fake an empty real file system. - manager.addStatCache(new FakeStatCache); + manager.addStatCache(llvm::make_unique<FakeStatCache>()); manager.getVirtualFile("virtual/dir/bar.h", 100, 0); const FileEntry *file = manager.getFile("virtual/dir/bar.h"); @@ -175,11 +175,11 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) { TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { // Inject two fake files into the file system. Different inodes // mean the files are not symlinked together. - FakeStatCache *statCache = new FakeStatCache; + auto statCache = llvm::make_unique<FakeStatCache>(); statCache->InjectDirectory(".", 41); statCache->InjectFile("foo.cpp", 42); statCache->InjectFile("bar.cpp", 43); - manager.addStatCache(statCache); + manager.addStatCache(std::move(statCache)); const FileEntry *fileFoo = manager.getFile("foo.cpp"); const FileEntry *fileBar = manager.getFile("bar.cpp"); @@ -192,10 +192,10 @@ TEST_F(FileManagerTest, getFileReturnsDifferentFileEntriesForDifferentFiles) { // exists at the given path. TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { // Inject a fake foo.cpp into the file system. - FakeStatCache *statCache = new FakeStatCache; + auto statCache = llvm::make_unique<FakeStatCache>(); statCache->InjectDirectory(".", 41); statCache->InjectFile("foo.cpp", 42); - manager.addStatCache(statCache); + manager.addStatCache(std::move(statCache)); // Create a virtual bar.cpp file. manager.getVirtualFile("bar.cpp", 200, 0); @@ -211,11 +211,11 @@ TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) { // getFile() returns the same FileEntry for real files that are aliases. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { // Inject two real files with the same inode. - FakeStatCache *statCache = new FakeStatCache; + auto statCache = llvm::make_unique<FakeStatCache>(); statCache->InjectDirectory("abc", 41); statCache->InjectFile("abc/foo.cpp", 42); statCache->InjectFile("abc/bar.cpp", 42); - manager.addStatCache(statCache); + manager.addStatCache(std::move(statCache)); EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); } @@ -224,11 +224,11 @@ TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) { // corresponding real files that are aliases. TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { // Inject two real files with the same inode. - FakeStatCache *statCache = new FakeStatCache; + auto statCache = llvm::make_unique<FakeStatCache>(); statCache->InjectDirectory("abc", 41); statCache->InjectFile("abc/foo.cpp", 42); statCache->InjectFile("abc/bar.cpp", 42); - manager.addStatCache(statCache); + manager.addStatCache(std::move(statCache)); manager.getVirtualFile("abc/foo.cpp", 100, 0); manager.getVirtualFile("abc/bar.cpp", 200, 0); @@ -236,6 +236,15 @@ TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedVirtualFiles) { EXPECT_EQ(manager.getFile("abc/foo.cpp"), manager.getFile("abc/bar.cpp")); } +TEST_F(FileManagerTest, addRemoveStatCache) { + manager.addStatCache(llvm::make_unique<FakeStatCache>()); + auto statCacheOwner = llvm::make_unique<FakeStatCache>(); + auto *statCache = statCacheOwner.get(); + manager.addStatCache(std::move(statCacheOwner)); + manager.addStatCache(llvm::make_unique<FakeStatCache>()); + manager.removeStatCache(statCache); +} + #endif // !LLVM_ON_WIN32 } // anonymous namespace diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp index 9ea093c6b2b8..1dda54dff14d 100644 --- a/unittests/Basic/SourceManagerTest.cpp +++ b/unittests/Basic/SourceManagerTest.cpp @@ -74,8 +74,8 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { const char *source = "#define M(x) [x]\n" "M(foo)"; - MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); - FileID mainFileID = SourceMgr.createFileID(buf); + std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(source); + FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); SourceMgr.setMainFileID(mainFileID); VoidModuleLoader ModLoader; @@ -127,8 +127,8 @@ TEST_F(SourceManagerTest, getColumnNumber) { "int x;\n" "int y;"; - MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source); - FileID MainFileID = SourceMgr.createFileID(Buf); + std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source); + FileID MainFileID = SourceMgr.createFileID(std::move(Buf)); SourceMgr.setMainFileID(MainFileID); bool Invalid; @@ -186,14 +186,14 @@ TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { "#define CONCAT(X, Y) X##Y\n" "CONCAT(1,1)\n"; - MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header); - MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main); - FileID mainFileID = SourceMgr.createFileID(mainBuf); + std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header); + std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main); + FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf)); SourceMgr.setMainFileID(mainFileID); const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", - headerBuf->getBufferSize(), 0); - SourceMgr.overrideFileContents(headerFile, headerBuf); + HeaderBuf->getBufferSize(), 0); + SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); VoidModuleLoader ModLoader; HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts, @@ -285,13 +285,13 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { "#define INC2 </test-header.h>\n" "#include M(INC2)\n"; - MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header); - MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main); - SourceMgr.setMainFileID(SourceMgr.createFileID(mainBuf)); + std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header); + std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main); + SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf))); const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", - headerBuf->getBufferSize(), 0); - SourceMgr.overrideFileContents(headerFile, headerBuf); + HeaderBuf->getBufferSize(), 0); + SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); VoidModuleLoader ModLoader; HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts, @@ -303,7 +303,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { PP.Initialize(*Target); std::vector<MacroAction> Macros; - PP.addPPCallbacks(new MacroTracker(Macros)); + PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros)); PP.EnterMainSourceFile(); diff --git a/unittests/Basic/VirtualFileSystemTest.cpp b/unittests/Basic/VirtualFileSystemTest.cpp index e7d361e252b4..67beb923d976 100644 --- a/unittests/Basic/VirtualFileSystemTest.cpp +++ b/unittests/Basic/VirtualFileSystemTest.cpp @@ -32,21 +32,15 @@ class DummyFileSystem : public vfs::FileSystem { public: DummyFileSystem() : FSID(getNextFSID()), FileID(0) {} - ErrorOr<vfs::Status> status(const Twine &Path) { + ErrorOr<vfs::Status> status(const Twine &Path) override { std::map<std::string, vfs::Status>::iterator I = FilesAndDirs.find(Path.str()); if (I == FilesAndDirs.end()) return make_error_code(llvm::errc::no_such_file_or_directory); return I->second; } - std::error_code openFileForRead(const Twine &Path, - std::unique_ptr<vfs::File> &Result) { - llvm_unreachable("unimplemented"); - } - std::error_code getBufferForFile(const Twine &Name, - std::unique_ptr<MemoryBuffer> &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true) { + ErrorOr<std::unique_ptr<vfs::File>> + openFileForRead(const Twine &Path) override { llvm_unreachable("unimplemented"); } @@ -539,8 +533,9 @@ public: IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLRawString(StringRef Content, IntrusiveRefCntPtr<vfs::FileSystem> ExternalFS) { - MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Content); - return getVFSFromYAML(Buffer, CountingDiagHandler, this, ExternalFS); + std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBuffer(Content); + return getVFSFromYAML(std::move(Buffer), CountingDiagHandler, this, + ExternalFS); } IntrusiveRefCntPtr<vfs::FileSystem> getFromYAMLString( |