teemperor created this revision.
teemperor added reviewers: labath, JDevlieghere.
Herald added subscribers: lldb-commits, abidh, MaskRay, emaste.
Herald added a reviewer: martong.
Herald added a reviewer: espindola.
Herald added a reviewer: shafik.
Herald added a project: LLDB.
teemperor added a comment.
Herald added a subscriber: rnkovacs.

I only converted like 40% of our tests to the new system as I first wanted to 
get some feedback before I do all the refactoring. Also I'm open to ideas how 
to name the member (currently I just use `subsystems` without the `m_` prefix 
as it's not really a real member with any contents).


Many of our tests need to initialize certain subsystems/plugins of LLDB such as
`FileSystem` or `HostInfo` by calling their static `Initialize` functions 
before the
test starts and then calling `::Terminate` after the test is done (in reverse 
order).
This adds a lot of error-prone boilerplate code to our testing code.

This patch adds a RAII called SubsystemRAII that ensures that we always call
::Initialize and then call ::Terminate after the test is done (and that the 
Terminate
calls are always in the reverse order of the ::Initialize calls). It also gets 
rid of
all of the boilerplate that we had for these calls.

Per-fixture initialization is still not very nice with this approach as it would
require some kind of static unique_ptr that gets manually assigned/reseted
from the gtest SetUpTestCase/TearDownTestCase functions. Because of that
I changed all per-fixture setup to now do per-test setup which can be done
by just having the SubsystemRAII as a member of the test fixture. This change 
doesn't
influence our normal test runtime as LIT anyway runs each test case separately
(and the Initialize/Terminate calls are anyway not very expensive). It will 
however
make running all tests in a single executable slightly slower.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D71630

Files:
  lldb/unittests/Core/MangledTest.cpp
  lldb/unittests/Editline/EditlineTest.cpp
  lldb/unittests/Expression/ClangParserTest.cpp
  lldb/unittests/Expression/CppModuleConfigurationTest.cpp
  lldb/unittests/Expression/DWARFExpressionTest.cpp
  lldb/unittests/Host/HostInfoTest.cpp
  lldb/unittests/Interpreter/TestCompletion.cpp
  lldb/unittests/Language/Highlighting/HighlighterTest.cpp
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
  lldb/unittests/ObjectFile/PECOFF/TestPECallFrameInfo.cpp
  lldb/unittests/Symbol/TestClangASTImporter.cpp
  lldb/unittests/Symbol/TestDWARFCallFrameInfo.cpp
  lldb/unittests/Symbol/TestLineEntry.cpp
  lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
  lldb/unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp
  lldb/unittests/Target/ModuleCacheTest.cpp
  lldb/unittests/TestingSupport/SubsystemRAII.h

Index: lldb/unittests/TestingSupport/SubsystemRAII.h
===================================================================
--- /dev/null
+++ lldb/unittests/TestingSupport/SubsystemRAII.h
@@ -0,0 +1,59 @@
+//===- SubsystemRAII.h ------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SUBSYSTEMRAII_H
+#define LLDB_UNITTESTS_TESTINGSUPPORT_SUBSYSTEMRAII_H
+
+namespace lldb_private {
+
+template <typename... T> class SubsystemRAII {};
+
+/// RAII for initializing and deinitializing LLDB subsystems.
+///
+/// This RAII takes care of calling the Initialize and Terminate functions for
+/// the subsystems specified by its template arguments. The ::Initialize
+/// functions are called on construction for each subsystem template parameter
+/// in the order in which they are passed as template parameters.
+/// The ::Terminate functions are called in the reverse order at destruction
+/// time.
+///
+/// Constructing this RAII in a scope like this:
+///
+///   @code{.cpp}
+///   {
+///     SubsystemRAII<FileSystem, HostInfo> Subsystems;
+///     DoingTestWork();
+///   }
+///   @endcode
+///
+/// is equivalent to the following code:
+///
+///   @code{.cpp}
+///   {
+///     FileSystem::Initialize();
+///     HostInfo::Initialize();
+///
+///     DoingTestWork();
+///
+///     FileSystem::Terminate();
+///     HostInfo::Terminate();
+///   }
+///   @endcode
+template <typename T, typename... Ts> class SubsystemRAII<T, Ts...> {
+  /// RAII for a single subsystem.
+  template <typename SubT> struct SubsystemRAIICase {
+    SubsystemRAIICase() { SubT::Initialize(); }
+    ~SubsystemRAIICase() { SubT::Terminate(); }
+  };
+
+  SubsystemRAIICase<T> CurrentSubsystem;
+  SubsystemRAII<Ts...> RemainingSubsystems;
+};
+} // namespace lldb_private
+
+#endif // LLDB_UNITTESTS_TESTINGSUPPORT_SUBSYSTEMRAII_H
Index: lldb/unittests/Target/ModuleCacheTest.cpp
===================================================================
--- lldb/unittests/Target/ModuleCacheTest.cpp
+++ lldb/unittests/Target/ModuleCacheTest.cpp
@@ -6,6 +6,7 @@
 
 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/ModuleSpec.h"
@@ -20,23 +21,21 @@
 namespace {
 
 class ModuleCacheTest : public testing::Test {
-public:
-  static void SetUpTestCase();
+  SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab>
+      subsystems;
 
-  static void TearDownTestCase();
+public:
+  void SetUp() override;
 
 protected:
-  static FileSpec s_cache_dir;
-  static std::string s_test_executable;
+  FileSpec s_cache_dir;
+  std::string s_test_executable;
 
   void TryGetAndPut(const FileSpec &cache_dir, const char *hostname,
                     bool expect_download);
 };
 }
 
-FileSpec ModuleCacheTest::s_cache_dir;
-std::string ModuleCacheTest::s_test_executable;
-
 static const char dummy_hostname[] = "dummy_hostname";
 static const char dummy_remote_dir[] = "bin";
 static const char module_name[] = "TestModule.so";
@@ -66,23 +65,11 @@
   return spec;
 }
 
-void ModuleCacheTest::SetUpTestCase() {
-  FileSystem::Initialize();
-  HostInfo::Initialize();
-  ObjectFileELF::Initialize();
-  SymbolFileSymtab::Initialize();
-
+void ModuleCacheTest::SetUp() {
   s_cache_dir = HostInfo::GetProcessTempDir();
   s_test_executable = GetInputFilePath(module_name);
 }
 
-void ModuleCacheTest::TearDownTestCase() {
-  SymbolFileSymtab::Terminate();
-  ObjectFileELF::Terminate();
-  HostInfo::Terminate();
-  FileSystem::Terminate();
-}
-
 static void VerifyDiskState(const FileSpec &cache_dir, const char *hostname) {
   FileSpec uuid_view = GetUuidView(cache_dir);
   EXPECT_TRUE(FileSystem::Instance().Exists(uuid_view))
@@ -108,8 +95,8 @@
 
   Status error = mc.GetAndPut(
       cache_dir, hostname, module_spec,
-      [&download_called](const ModuleSpec &module_spec,
-                         const FileSpec &tmp_download_file_spec) {
+      [&download_called, this](const ModuleSpec &module_spec,
+                               const FileSpec &tmp_download_file_spec) {
         download_called = true;
         EXPECT_STREQ(GetDummyRemotePath().GetCString(),
                      module_spec.GetFileSpec().GetCString());
Index: lldb/unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp
===================================================================
--- lldb/unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp
+++ lldb/unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp
@@ -20,6 +20,7 @@
 #include "Plugins/SymbolFile/DWARF/DWARFDebugAbbrev.h"
 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
 #include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Core/Address.h"
 #include "lldb/Core/Module.h"
@@ -34,34 +35,17 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/StreamString.h"
 
-
-
 using namespace lldb;
 using namespace lldb_private;
 
 class SymbolFileDWARFTests : public testing::Test {
+  SubsystemRAII<FileSystem, HostInfo, ObjectFilePECOFF, SymbolFileDWARF,
+                ClangASTContext, SymbolFilePDB>
+      subsystems;
+
 public:
   void SetUp() override {
-// Initialize and TearDown the plugin every time, so we get a brand new
-// AST every time so that modifications to the AST from each test don't
-// leak into the next test.
-FileSystem::Initialize();
-HostInfo::Initialize();
-ObjectFilePECOFF::Initialize();
-SymbolFileDWARF::Initialize();
-ClangASTContext::Initialize();
-SymbolFilePDB::Initialize();
-
-m_dwarf_test_exe = GetInputFilePath("test-dwarf.exe");
-  }
-
-  void TearDown() override {
-    SymbolFilePDB::Terminate();
-    ClangASTContext::Initialize();
-    SymbolFileDWARF::Terminate();
-    ObjectFilePECOFF::Terminate();
-    HostInfo::Terminate();
-    FileSystem::Terminate();
+    m_dwarf_test_exe = GetInputFilePath("test-dwarf.exe");
   }
 
 protected:
Index: lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
===================================================================
--- lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
+++ lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp
@@ -6,26 +6,17 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "gmock/gmock.h"
-#include "gtest/gtest.h"
-
 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
 #include "Plugins/SymbolFile/DWARF/DWARFDIE.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
 
 using namespace lldb;
 using namespace lldb_private;
 
 class DWARFASTParserClangTests : public testing::Test {
-public:
-  void SetUp() override {
-    FileSystem::Initialize();
-    ClangASTContext::Initialize();
-  }
-
-  void TearDown() override {
-    ClangASTContext::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, ClangASTContext> subsystems;
 };
 
 namespace {
Index: lldb/unittests/Symbol/TestLineEntry.cpp
===================================================================
--- lldb/unittests/Symbol/TestLineEntry.cpp
+++ lldb/unittests/Symbol/TestLineEntry.cpp
@@ -14,6 +14,7 @@
 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Symbol/ClangASTContext.h"
 
@@ -31,17 +32,13 @@
 using namespace lldb;
 
 class LineEntryTest : public testing::Test {
+  SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO, SymbolFileDWARF,
+                ClangASTContext>
+      subsystem;
+
 public:
   void SetUp() override;
 
-  void TearDown() override {
-    ClangASTContext::Terminate();
-    SymbolFileDWARF::Terminate();
-    ObjectFileMachO::Terminate();
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
-
 protected:
   llvm::Expected<LineEntry> GetLineEntryForLine(uint32_t line);
   llvm::Optional<TestFile> m_file;
@@ -49,11 +46,6 @@
 };
 
 void LineEntryTest::SetUp() {
-  FileSystem::Initialize();
-  HostInfo::Initialize();
-  ObjectFileMachO::Initialize();
-  SymbolFileDWARF::Initialize();
-  ClangASTContext::Initialize();
   auto ExpectedFile = TestFile::fromYamlFile("inlined-functions.yaml");
   ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
   m_file.emplace(std::move(*ExpectedFile));
Index: lldb/unittests/Symbol/TestDWARFCallFrameInfo.cpp
===================================================================
--- lldb/unittests/Symbol/TestDWARFCallFrameInfo.cpp
+++ lldb/unittests/Symbol/TestDWARFCallFrameInfo.cpp
@@ -12,6 +12,7 @@
 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
 #include "Plugins/Process/Utility/RegisterContext_x86.h"
 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 
 #include "lldb/Core/Module.h"
@@ -32,20 +33,8 @@
 using namespace lldb;
 
 class DWARFCallFrameInfoTest : public testing::Test {
-public:
-  void SetUp() override {
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-    ObjectFileELF::Initialize();
-    SymbolFileSymtab::Initialize();
-  }
-
-  void TearDown() override {
-    SymbolFileSymtab::Terminate();
-    ObjectFileELF::Terminate();
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab>
+      subsystems;
 
 protected:
   void TestBasic(DWARFCallFrameInfo::Type type, llvm::StringRef symbol);
Index: lldb/unittests/Symbol/TestClangASTImporter.cpp
===================================================================
--- lldb/unittests/Symbol/TestClangASTImporter.cpp
+++ lldb/unittests/Symbol/TestClangASTImporter.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "TestingSupport/SubsystemRAII.h"
 #include "gtest/gtest.h"
 
 #include "lldb/Host/FileSystem.h"
@@ -22,16 +23,7 @@
 using namespace lldb_private;
 
 class TestClangASTImporter : public testing::Test {
-public:
-  static void SetUpTestCase() {
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-  }
-
-  static void TearDownTestCase() {
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, HostInfo> Subsystems;
 
 protected:
   std::unique_ptr<ClangASTContext> createAST() {
Index: lldb/unittests/ObjectFile/PECOFF/TestPECallFrameInfo.cpp
===================================================================
--- lldb/unittests/ObjectFile/PECOFF/TestPECallFrameInfo.cpp
+++ lldb/unittests/ObjectFile/PECOFF/TestPECallFrameInfo.cpp
@@ -11,6 +11,7 @@
 
 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
 #include "Plugins/Process/Utility/lldb-x86-register-enums.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 
 #include "lldb/Core/Module.h"
@@ -22,16 +23,7 @@
 using namespace lldb;
 
 class PECallFrameInfoTest : public testing::Test {
-public:
-  void SetUp() override {
-    FileSystem::Initialize();
-    ObjectFilePECOFF::Initialize();
-  }
-
-  void TearDown() override {
-    ObjectFilePECOFF::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, ObjectFilePECOFF> subsystems;
 
 protected:
   void GetUnwindPlan(addr_t file_addr, UnwindPlan &plan) const;
@@ -127,12 +119,12 @@
 # 03 60          UOP_PushNonVol(0) RSI(6), offset in prolog is 3
 # 02 30          UOP_PushNonVol(0) RBX(3), offset in prolog is 2
 # Corresponding prolog:
-# 00    push    rbx
-# 02    push    rsi
-# 03    push    rdi
-# 04    push    r14
-# 06    push    r15
-# 08    sub     rsp, 20h
+# 00    push    rbx
+# 02    push    rsi
+# 03    push    rdi
+# 04    push    r14
+# 06    push    r15
+# 08    sub     rsp, 20h
 
 # Unwind info at 0x2010:
 # 21 05 02 00    Has chained info, prolog size = 5, unwind codes size is 2 words, no frame register
@@ -157,17 +149,17 @@
 # 08 C0          UOP_PushNonVol(0) R12(0xC), offset in prolog is 8
 # 06 50          UOP_PushNonVol(0) RBP(5), offset in prolog is 6
 # Corresponding prolog:
-# 00    mov     [rsp+8], rcx
-# 05    push    rbp
-# 06    push    r12
-# 08    push    r13
-# 0A    push    r14
-# 0C    push    r15
-# 0E    sub     rsp, 2F0h
-# 15    lea     rbp, [rsp+30h]
-# 1A    mov     [rbp+2F8h], rbx
-# 21    mov     [rbp+300h], rsi
-# 28    mov     [rbp+308h], rdi
+# 00    mov     [rsp+8], rcx
+# 05    push    rbp
+# 06    push    r12
+# 08    push    r13
+# 0A    push    r14
+# 0C    push    r15
+# 0E    sub     rsp, 2F0h
+# 15    lea     rbp, [rsp+30h]
+# 1A    mov     [rbp+2F8h], rbx
+# 21    mov     [rbp+300h], rsi
+# 28    mov     [rbp+308h], rdi
 
   - Name:            .pdata
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===================================================================
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -9,6 +9,7 @@
 
 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/ModuleSpec.h"
@@ -29,22 +30,8 @@
 using namespace lldb;
 
 class ObjectFileELFTest : public testing::Test {
-public:
-  void SetUp() override {
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-    ObjectFileELF::Initialize();
-    SymbolFileSymtab::Initialize();
-  }
-
-  void TearDown() override {
-    SymbolFileSymtab::Terminate();
-    ObjectFileELF::Terminate();
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
-
-protected:
+  SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab>
+      subsystems;
 };
 
 TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
@@ -297,4 +284,4 @@
 
   auto entry_point_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
   ASSERT_EQ(entry_point_addr.GetAddressClass(), AddressClass::eCode);
-}
\ No newline at end of file
+}
Index: lldb/unittests/Language/Highlighting/HighlighterTest.cpp
===================================================================
--- lldb/unittests/Language/Highlighting/HighlighterTest.cpp
+++ lldb/unittests/Language/Highlighting/HighlighterTest.cpp
@@ -14,33 +14,18 @@
 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
 #include "Plugins/Language/ObjC/ObjCLanguage.h"
 #include "Plugins/Language/ObjCPlusPlus/ObjCPlusPlusLanguage.h"
+#include "TestingSupport/SubsystemRAII.h"
 
 using namespace lldb_private;
 
 namespace {
 class HighlighterTest : public testing::Test {
-public:
-  static void SetUpTestCase();
-  static void TearDownTestCase();
+  SubsystemRAII<FileSystem, CPlusPlusLanguage, ObjCLanguage,
+                ObjCPlusPlusLanguage>
+      subsystems;
 };
 } // namespace
 
-void HighlighterTest::SetUpTestCase() {
-  // The HighlighterManager uses the language plugins under the hood, so we
-  // have to initialize them here for our test process.
-  FileSystem::Initialize();
-  CPlusPlusLanguage::Initialize();
-  ObjCLanguage::Initialize();
-  ObjCPlusPlusLanguage::Initialize();
-}
-
-void HighlighterTest::TearDownTestCase() {
-  CPlusPlusLanguage::Terminate();
-  ObjCLanguage::Terminate();
-  ObjCPlusPlusLanguage::Terminate();
-  FileSystem::Terminate();
-}
-
 static std::string getName(lldb::LanguageType type) {
   HighlighterManager m;
   return m.getHighlighterFor(type, "").GetName().str();
Index: lldb/unittests/Interpreter/TestCompletion.cpp
===================================================================
--- lldb/unittests/Interpreter/TestCompletion.cpp
+++ lldb/unittests/Interpreter/TestCompletion.cpp
@@ -15,6 +15,7 @@
 #include "gtest/gtest.h"
 
 #include "TestingSupport/MockTildeExpressionResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/FileSystem.h"
@@ -29,6 +30,8 @@
 namespace {
 
 class CompletionTest : public testing::Test {
+  SubsystemRAII<FileSystem> subsystems;
+
 protected:
   /// Unique temporary directory in which all created filesystem entities must
   /// be placed. It is removed at the end of the test suite.
@@ -56,8 +59,6 @@
   SmallString<128> FileBaz;
 
   void SetUp() override {
-    FileSystem::Initialize();
-
     // chdir back into the original working dir this test binary started with.
     // A previous test may have have changed the working dir.
     ASSERT_NO_ERROR(fs::set_current_path(OriginalWorkingDir));
@@ -100,7 +101,6 @@
 
   void TearDown() override {
     ASSERT_NO_ERROR(fs::remove_directories(BaseDir));
-    FileSystem::Terminate();
   }
 
   static bool HasEquivalentFile(const Twine &Path, const StringList &Paths) {
Index: lldb/unittests/Host/HostInfoTest.cpp
===================================================================
--- lldb/unittests/Host/HostInfoTest.cpp
+++ lldb/unittests/Host/HostInfoTest.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/HostInfo.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/lldb-defines.h"
@@ -17,15 +18,7 @@
 
 namespace {
 class HostInfoTest : public ::testing::Test {
-public:
-  void SetUp() override {
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-  }
-  void TearDown() override {
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, HostInfo> subsystems;
 };
 } // namespace
 
Index: lldb/unittests/Expression/DWARFExpressionTest.cpp
===================================================================
--- lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -9,6 +9,7 @@
 #include "lldb/Expression/DWARFExpression.h"
 #include "../../source/Plugins/SymbolFile/DWARF/DWARFUnit.h"
 #include "../../source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/Section.h"
 #include "lldb/Core/Value.h"
@@ -119,6 +120,7 @@
 /// Helper class that can construct a module from YAML and evaluate
 /// DWARF expressions on it.
 class YAMLModuleTester {
+  SubsystemRAII<FileSystem> subsystems;
   llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_sections_map;
   lldb::ModuleSP m_module_sp;
   lldb::ObjectFileSP m_objfile_sp;
@@ -128,8 +130,6 @@
 public:
   /// Parse the debug info sections from the YAML description.
   YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple) {
-    FileSystem::Initialize();
-
     auto sections_map = llvm::DWARFYAML::EmitDebugSections(yaml_data, true);
     if (!sections_map)
       return;
@@ -153,7 +153,6 @@
     if (dwarf_unit)
       m_dwarf_unit = dwarf_unit.get();
   }
-  ~YAMLModuleTester() { FileSystem::Terminate(); }
   DWARFUnitSP GetDwarfUnit() { return m_dwarf_unit; }
 
   // Evaluate a raw DWARF expression.
Index: lldb/unittests/Expression/CppModuleConfigurationTest.cpp
===================================================================
--- lldb/unittests/Expression/CppModuleConfigurationTest.cpp
+++ lldb/unittests/Expression/CppModuleConfigurationTest.cpp
@@ -8,6 +8,7 @@
 
 #include "Plugins/ExpressionParser/Clang/CppModuleConfiguration.h"
 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 
@@ -18,16 +19,7 @@
 
 namespace {
 struct CppModuleConfigurationTest : public testing::Test {
-  static void SetUpTestCase() {
-    // Getting the resource directory uses those subsystems, so we should
-    // initialize them.
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-  }
-  static void TearDownTestCase() {
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, HostInfo> subsystems;
 };
 } // namespace
 
Index: lldb/unittests/Expression/ClangParserTest.cpp
===================================================================
--- lldb/unittests/Expression/ClangParserTest.cpp
+++ lldb/unittests/Expression/ClangParserTest.cpp
@@ -9,6 +9,7 @@
 #include "clang/Basic/Version.h"
 
 #include "Plugins/ExpressionParser/Clang/ClangHost.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
@@ -20,14 +21,7 @@
 
 namespace {
 struct ClangHostTest : public testing::Test {
-  static void SetUpTestCase() {
-    FileSystem::Initialize();
-    HostInfo::Initialize();
-  }
-  static void TearDownTestCase() {
-    HostInfo::Terminate();
-    FileSystem::Terminate();
-  }
+  SubsystemRAII<FileSystem, HostInfo> subsystems;
 };
 } // namespace
 
Index: lldb/unittests/Editline/EditlineTest.cpp
===================================================================
--- lldb/unittests/Editline/EditlineTest.cpp
+++ lldb/unittests/Editline/EditlineTest.cpp
@@ -20,6 +20,7 @@
 #include <memory>
 #include <thread>
 
+#include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Host/Editline.h"
 #include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Pipe.h"
@@ -242,7 +243,7 @@
 }
 
 class EditlineTestFixture : public ::testing::Test {
-private:
+  SubsystemRAII<FileSystem> subsystems;
   EditlineAdapter _el_adapter;
   std::shared_ptr<std::thread> _sp_output_thread;
 
@@ -253,8 +254,6 @@
   }
 
   void SetUp() override {
-    FileSystem::Initialize();
-
     // Validate the editline adapter.
     EXPECT_TRUE(_el_adapter.IsValid());
     if (!_el_adapter.IsValid())
@@ -269,8 +268,6 @@
     _el_adapter.CloseInput();
     if (_sp_output_thread)
       _sp_output_thread->join();
-
-    FileSystem::Terminate();
   }
 
   EditlineAdapter &GetEditlineAdapter() { return _el_adapter; }
Index: lldb/unittests/Core/MangledTest.cpp
===================================================================
--- lldb/unittests/Core/MangledTest.cpp
+++ lldb/unittests/Core/MangledTest.cpp
@@ -8,6 +8,7 @@
 
 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
 #include "TestingSupport/TestUtilities.h"
 
 #include "lldb/Core/Mangled.h"
@@ -58,10 +59,8 @@
 }
 
 TEST(MangledTest, NameIndexes_FindFunctionSymbols) {
-  FileSystem::Initialize();
-  HostInfo::Initialize();
-  ObjectFileELF::Initialize();
-  SymbolFileSymtab::Initialize();
+  SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab>
+      Subsystems;
 
   auto ExpectedFile = TestFile::fromYaml(R"(
 --- !ELF
@@ -251,9 +250,4 @@
   EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod));
   EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase));
   EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod));
-
-  SymbolFileSymtab::Terminate();
-  ObjectFileELF::Terminate();
-  HostInfo::Terminate();
-  FileSystem::Terminate();
 }
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to