Index: unittests/Interpreter/CMakeLists.txt =================================================================== --- unittests/Interpreter/CMakeLists.txt +++ unittests/Interpreter/CMakeLists.txt @@ -4,7 +4,7 @@ LINK_LIBS lldbInterpreter - lldbUtilityMocks + lldbUtilityHelpers ) target_link_libraries(InterpreterTests Index: unittests/Interpreter/TestCompletion.cpp =================================================================== --- unittests/Interpreter/TestCompletion.cpp +++ unittests/Interpreter/TestCompletion.cpp @@ -12,13 +12,12 @@ #include "lldb/Utility/StringList.h" #include "lldb/Utility/TildeExpressionResolver.h" +#include "unittests/Utility/Helpers/MockTildeExpressionResolver.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" -#include "unittests/Utility/Mocks/MockTildeExpressionResolver.h" - namespace fs = llvm::sys::fs; namespace path = llvm::sys::path; using namespace llvm; Index: unittests/ObjectFile/ELF/CMakeLists.txt =================================================================== --- unittests/ObjectFile/ELF/CMakeLists.txt +++ unittests/ObjectFile/ELF/CMakeLists.txt @@ -6,6 +6,7 @@ lldbPluginObjectFileELF lldbPluginSymbolVendorELF lldbCore + lldbUtilityHelpers ) add_dependencies(ObjectFileELFTests yaml2obj) Index: unittests/ObjectFile/ELF/TestObjectFileELF.cpp =================================================================== --- unittests/ObjectFile/ELF/TestObjectFileELF.cpp +++ unittests/ObjectFile/ELF/TestObjectFileELF.cpp @@ -9,20 +9,18 @@ //===----------------------------------------------------------------------===// #include "Plugins/ObjectFile/ELF/ObjectFileELF.h" +#include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h" #include "lldb/Core/Module.h" #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/Section.h" #include "lldb/Host/HostInfo.h" +#include "unittests/Utility/Helpers/TestUtilities.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" -#include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h" - -extern const char *TestMainArgv0; - using namespace lldb_private; using namespace lldb; @@ -32,10 +30,6 @@ HostInfo::Initialize(); ObjectFileELF::Initialize(); SymbolVendorELF::Initialize(); - - m_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0); - llvm::sys::path::append(m_inputs_folder, "Inputs"); - llvm::sys::fs::make_absolute(m_inputs_folder); } void TearDown() override { @@ -45,7 +39,6 @@ } protected: - llvm::SmallString<128> m_inputs_folder; }; #define ASSERT_NO_ERROR(x) \ @@ -60,9 +53,8 @@ } TEST_F(ObjectFileELFTest, SectionsResolveConsistently) { - llvm::SmallString<128> yaml = m_inputs_folder; - llvm::sys::path::append(yaml, "sections-resolve-consistently.yaml"); - llvm::SmallString<128> obj = m_inputs_folder; + std::string yaml = GetInputFile("sections-resolve-consistently.yaml"); + llvm::SmallString<128> obj; ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( "sections-resolve-consistently-%%%%%%", "obj", obj)); Index: unittests/Process/minidump/CMakeLists.txt =================================================================== --- unittests/Process/minidump/CMakeLists.txt +++ unittests/Process/minidump/CMakeLists.txt @@ -7,6 +7,7 @@ lldbTarget lldbPluginProcessUtility lldbPluginProcessMinidump + lldbUtilityHelpers LINK_COMPONENTS Support ) Index: unittests/Process/minidump/MinidumpParserTest.cpp =================================================================== --- unittests/Process/minidump/MinidumpParserTest.cpp +++ unittests/Process/minidump/MinidumpParserTest.cpp @@ -23,7 +23,7 @@ #include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/FileSpec.h" - +#include "unittests/Utility/Helpers/TestUtilities.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/FileSystem.h" @@ -35,24 +35,14 @@ // C++ includes #include -extern const char *TestMainArgv0; - using namespace lldb_private; using namespace minidump; class MinidumpParserTest : public testing::Test { public: - void SetUp() override { - llvm::StringRef dmp_folder = llvm::sys::path::parent_path(TestMainArgv0); - inputs_folder = dmp_folder; - llvm::sys::path::append(inputs_folder, "Inputs"); - } - void SetUpData(const char *minidump_filename, uint64_t load_size = UINT64_MAX) { - llvm::SmallString<128> filename = inputs_folder; - llvm::sys::path::append(filename, minidump_filename); - + std::string filename = GetInputFile(minidump_filename); auto BufferPtr = DataBufferLLVM::CreateSliceFromPath(filename, load_size, 0); llvm::Optional optional_parser = @@ -62,7 +52,6 @@ ASSERT_GT(parser->GetData().size(), 0UL); } - llvm::SmallString<128> inputs_folder; std::unique_ptr parser; }; Index: unittests/Symbol/CMakeLists.txt =================================================================== --- unittests/Symbol/CMakeLists.txt +++ unittests/Symbol/CMakeLists.txt @@ -6,6 +6,7 @@ LINK_LIBS lldbHost lldbSymbol + lldbUtilityHelpers ) add_dependencies(SymbolTests yaml2obj) Index: unittests/Symbol/TestDWARFCallFrameInfo.cpp =================================================================== --- unittests/Symbol/TestDWARFCallFrameInfo.cpp +++ unittests/Symbol/TestDWARFCallFrameInfo.cpp @@ -16,14 +16,13 @@ #include "lldb/Host/HostInfo.h" #include "lldb/Symbol/DWARFCallFrameInfo.h" #include "lldb/Utility/StreamString.h" +#include "unittests/Utility/Helpers/TestUtilities.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" -extern const char *TestMainArgv0; - using namespace lldb_private; using namespace lldb; @@ -32,10 +31,6 @@ void SetUp() override { HostInfo::Initialize(); ObjectFileELF::Initialize(); - - m_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0); - llvm::sys::path::append(m_inputs_folder, "Inputs"); - llvm::sys::fs::make_absolute(m_inputs_folder); } void TearDown() override { @@ -44,8 +39,6 @@ } protected: - llvm::SmallString<128> m_inputs_folder; - void TestBasic(DWARFCallFrameInfo::Type type, llvm::StringRef symbol); }; @@ -96,9 +89,8 @@ void DWARFCallFrameInfoTest::TestBasic(DWARFCallFrameInfo::Type type, llvm::StringRef symbol) { - llvm::SmallString<128> yaml = m_inputs_folder; - llvm::sys::path::append(yaml, "basic-call-frame-info.yaml"); - llvm::SmallString<128> obj = m_inputs_folder; + std::string yaml = GetInputFile("basic-call-frame-info.yaml"); + llvm::SmallString<128> obj; ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile( "basic-call-frame-info-%%%%%%", "obj", obj)); Index: unittests/SymbolFile/DWARF/CMakeLists.txt =================================================================== --- unittests/SymbolFile/DWARF/CMakeLists.txt +++ unittests/SymbolFile/DWARF/CMakeLists.txt @@ -8,6 +8,7 @@ lldbPluginObjectFilePECOFF lldbPluginSymbolFileDWARF lldbPluginSymbolFilePDB + lldbUtilityHelpers LINK_COMPONENTS Support DebugInfoPDB Index: unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp =================================================================== --- unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp +++ unittests/SymbolFile/DWARF/SymbolFileDWARFTests.cpp @@ -15,6 +15,9 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" +#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h" +#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" +#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h" #include "lldb/Core/Address.h" #include "lldb/Core/ArchSpec.h" #include "lldb/Core/Module.h" @@ -25,12 +28,7 @@ #include "lldb/Symbol/LineTable.h" #include "lldb/Symbol/SymbolVendor.h" #include "lldb/Utility/FileSpec.h" - -#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h" -#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" -#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h" - -extern const char *TestMainArgv0; +#include "unittests/Utility/Helpers/TestUtilities.h" using namespace lldb_private; @@ -46,12 +44,7 @@ ClangASTContext::Initialize(); SymbolFilePDB::Initialize(); - llvm::StringRef exe_folder = llvm::sys::path::parent_path(TestMainArgv0); - llvm::SmallString<128> inputs_folder = exe_folder; - llvm::sys::path::append(inputs_folder, "Inputs"); - - m_dwarf_test_exe = inputs_folder; - llvm::sys::path::append(m_dwarf_test_exe, "test-dwarf.exe"); + m_dwarf_test_exe = GetInputFile("test-dwarf.exe"); } void TearDown() override { @@ -63,12 +56,12 @@ } protected: - llvm::SmallString<128> m_dwarf_test_exe; + std::string m_dwarf_test_exe; }; TEST_F(SymbolFileDWARFTests, TestAbilitiesForDWARF) { // Test that when we have Dwarf debug info, SymbolFileDWARF is used. - FileSpec fspec(m_dwarf_test_exe.c_str(), false); + FileSpec fspec(m_dwarf_test_exe, false); ArchSpec aspec("i686-pc-windows"); lldb::ModuleSP module = std::make_shared(fspec, aspec); Index: unittests/SymbolFile/PDB/CMakeLists.txt =================================================================== --- unittests/SymbolFile/PDB/CMakeLists.txt +++ unittests/SymbolFile/PDB/CMakeLists.txt @@ -8,6 +8,7 @@ lldbPluginObjectFilePECOFF lldbPluginSymbolFileDWARF lldbPluginSymbolFilePDB + lldbUtiltyHelpers LINK_COMPONENTS Support DebugInfoPDB Index: unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp =================================================================== --- unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp +++ unittests/SymbolFile/PDB/SymbolFilePDBTests.cpp @@ -15,6 +15,9 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" +#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h" +#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" +#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h" #include "lldb/Core/Address.h" #include "lldb/Core/ArchSpec.h" #include "lldb/Core/Module.h" @@ -25,10 +28,7 @@ #include "lldb/Symbol/LineTable.h" #include "lldb/Symbol/SymbolVendor.h" #include "lldb/Utility/FileSpec.h" - -#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h" -#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" -#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h" +#include "unittests/Utility/Helpers/TestUtilities.h" #if defined(_MSC_VER) #include "lldb/Host/windows/windows.h" @@ -37,8 +37,6 @@ #include -extern const char *TestMainArgv0; - using namespace lldb_private; class SymbolFilePDBTests : public testing::Test { @@ -61,10 +59,8 @@ llvm::SmallString<128> inputs_folder = exe_folder; llvm::sys::path::append(inputs_folder, "Inputs"); - m_pdb_test_exe = inputs_folder; - m_types_test_exe = inputs_folder; - llvm::sys::path::append(m_pdb_test_exe, "test-pdb.exe"); - llvm::sys::path::append(m_types_test_exe, "test-pdb-types.exe"); + m_pdb_test_exe = GetInputFile("test-pdb.exe"); + m_types_test_exe = GetInputFile("test-pdb-types.exe"); } void TearDown() override { @@ -80,8 +76,8 @@ } protected: - llvm::SmallString<128> m_pdb_test_exe; - llvm::SmallString<128> m_types_test_exe; + std::string m_pdb_test_exe; + std::string m_types_test_exe; bool FileSpecMatchesAsBaseOrFull(const FileSpec &left, const FileSpec &right) const { Index: unittests/Target/CMakeLists.txt =================================================================== --- unittests/Target/CMakeLists.txt +++ unittests/Target/CMakeLists.txt @@ -8,6 +8,7 @@ lldbSymbol lldbUtility lldbPluginObjectFileELF + lldbUtilityHelpers LINK_COMPONENTS Support ) Index: unittests/Target/ModuleCacheTest.cpp =================================================================== --- unittests/Target/ModuleCacheTest.cpp +++ unittests/Target/ModuleCacheTest.cpp @@ -10,8 +10,7 @@ #include "lldb/Host/HostInfo.h" #include "lldb/Symbol/SymbolContext.h" #include "lldb/Target/ModuleCache.h" - -extern const char *TestMainArgv0; +#include "unittests/Utility/Helpers/TestUtilities.h" using namespace lldb_private; using namespace lldb; @@ -26,7 +25,7 @@ protected: static FileSpec s_cache_dir; - static llvm::SmallString<128> s_test_executable; + static std::string s_test_executable; void TryGetAndPut(const FileSpec &cache_dir, const char *hostname, bool expect_download); @@ -34,7 +33,7 @@ } FileSpec ModuleCacheTest::s_cache_dir; -llvm::SmallString<128> ModuleCacheTest::s_test_executable; +std::string ModuleCacheTest::s_test_executable; static const char dummy_hostname[] = "dummy_hostname"; static const char dummy_remote_dir[] = "bin"; @@ -71,10 +70,7 @@ FileSpec tmpdir_spec; HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, s_cache_dir); - - llvm::StringRef exe_folder = llvm::sys::path::parent_path(TestMainArgv0); - s_test_executable = exe_folder; - llvm::sys::path::append(s_test_executable, "Inputs", module_name); + s_test_executable = GetInputFile(module_name); } void ModuleCacheTest::TearDownTestCase() { Index: unittests/Utility/CMakeLists.txt =================================================================== --- unittests/Utility/CMakeLists.txt +++ unittests/Utility/CMakeLists.txt @@ -1,4 +1,4 @@ -add_subdirectory(Mocks) +add_subdirectory(Helpers) add_lldb_unittest(UtilityTests ConstStringTest.cpp @@ -15,7 +15,7 @@ LINK_LIBS lldbUtility - lldbUtilityMocks + lldbUtilityHelpers LINK_COMPONENTS Support ) Index: unittests/Utility/Helpers/CMakeLists.txt =================================================================== --- unittests/Utility/Helpers/CMakeLists.txt +++ unittests/Utility/Helpers/CMakeLists.txt @@ -1,5 +1,7 @@ -add_lldb_library(lldbUtilityMocks +set(EXCLUDE_FROM_ALL ON) +add_lldb_library(lldbUtilityHelpers MockTildeExpressionResolver.cpp + TestUtilities.cpp LINK_LIBS lldbUtility Index: unittests/Utility/Helpers/TestUtilities.h =================================================================== --- /dev/null +++ unittests/Utility/Helpers/TestUtilities.h @@ -0,0 +1,20 @@ +//===- TestUtilities.h ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_UNITTESTS_UTILITY_HELPERS_TESTUTILITIES_H +#define LLDB_UNITTESTS_UTILITY_HELPERS_TESTUTILITIES_H + +#include "llvm/ADT/Twine.h" +#include + +namespace lldb_private { +std::string GetInputFile(llvm::Twine name); +} + +#endif Index: unittests/Utility/Helpers/TestUtilities.cpp =================================================================== --- /dev/null +++ unittests/Utility/Helpers/TestUtilities.cpp @@ -0,0 +1,22 @@ +//===- TestUtilities.cpp ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "TestUtilities.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" + +extern const char *TestMainArgv0; + +std::string lldb_private::GetInputFile(llvm::Twine name) { + llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0); + llvm::sys::fs::make_absolute(result); + llvm::sys::path::append(result, "Inputs", name); + return result.str(); +} Index: unittests/Utility/Mocks/MockTildeExpressionResolver.h =================================================================== --- /dev/null +++ unittests/Utility/Mocks/MockTildeExpressionResolver.h @@ -1,37 +0,0 @@ -//===--------------------- TildeExpressionResolver.h ------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLDB_UNITTESTS_UTILITY_MOCKS_MOCK_TILDE_EXPRESSION_RESOLVER_H -#define LLDB_UNITTESTS_UTILITY_MOCKS_MOCK_TILDE_EXPRESSION_RESOLVER_H - -#include "lldb/Utility/TildeExpressionResolver.h" - -#include "llvm/ADT/SmallString.h" -#include "llvm/ADT/StringMap.h" - -namespace lldb_private { -class MockTildeExpressionResolver : public TildeExpressionResolver { - llvm::StringRef CurrentUser; - llvm::StringMap UserDirectories; - -public: - MockTildeExpressionResolver(llvm::StringRef CurrentUser, - llvm::StringRef HomeDir); - - void AddKnownUser(llvm::StringRef User, llvm::StringRef HomeDir); - void Clear(); - void SetCurrentUser(llvm::StringRef User); - - bool ResolveExact(llvm::StringRef Expr, - llvm::SmallVectorImpl &Output) override; - bool ResolvePartial(llvm::StringRef Expr, llvm::StringSet<> &Output) override; -}; -} - -#endif Index: unittests/Utility/Mocks/MockTildeExpressionResolver.cpp =================================================================== --- /dev/null +++ unittests/Utility/Mocks/MockTildeExpressionResolver.cpp @@ -1,80 +0,0 @@ -//===----------------- MockTildeExpressionResolver.cpp ----------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "MockTildeExpressionResolver.h" -#include "llvm/Support/Path.h" - -using namespace lldb_private; -using namespace llvm; - -MockTildeExpressionResolver::MockTildeExpressionResolver(StringRef CurrentUser, - StringRef HomeDir) - : CurrentUser(CurrentUser) { - UserDirectories.insert(std::make_pair(CurrentUser, HomeDir)); -} - -void MockTildeExpressionResolver::AddKnownUser(StringRef User, - StringRef HomeDir) { - assert(UserDirectories.find(User) == UserDirectories.end()); - UserDirectories.insert(std::make_pair(User, HomeDir)); -} - -void MockTildeExpressionResolver::Clear() { - CurrentUser = StringRef(); - UserDirectories.clear(); -} - -void MockTildeExpressionResolver::SetCurrentUser(StringRef User) { - assert(UserDirectories.find(User) != UserDirectories.end()); - CurrentUser = User; -} - -bool MockTildeExpressionResolver::ResolveExact(StringRef Expr, - SmallVectorImpl &Output) { - Output.clear(); - - assert(!llvm::any_of( - Expr, [](char c) { return llvm::sys::path::is_separator(c); })); - assert(Expr.empty() || Expr[0] == '~'); - Expr = Expr.drop_front(); - if (Expr.empty()) { - auto Dir = UserDirectories[CurrentUser]; - Output.append(Dir.begin(), Dir.end()); - return true; - } - - for (const auto &User : UserDirectories) { - if (User.getKey() != Expr) - continue; - Output.append(User.getValue().begin(), User.getValue().end()); - return true; - } - return false; -} - -bool MockTildeExpressionResolver::ResolvePartial(StringRef Expr, - StringSet<> &Output) { - Output.clear(); - - assert(!llvm::any_of( - Expr, [](char c) { return llvm::sys::path::is_separator(c); })); - assert(Expr.empty() || Expr[0] == '~'); - Expr = Expr.drop_front(); - - SmallString<16> QualifiedName("~"); - for (const auto &User : UserDirectories) { - if (!User.getKey().startswith(Expr)) - continue; - QualifiedName.resize(1); - QualifiedName.append(User.getKey().begin(), User.getKey().end()); - Output.insert(QualifiedName); - } - - return !Output.empty(); -} Index: unittests/Utility/StructuredDataTest.cpp =================================================================== --- unittests/Utility/StructuredDataTest.cpp +++ unittests/Utility/StructuredDataTest.cpp @@ -9,33 +9,16 @@ #include "gtest/gtest.h" +#include "Helpers/TestUtilities.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" #include "llvm/Support/Path.h" -extern const char *TestMainArgv0; - using namespace lldb; using namespace lldb_private; -namespace { - -class StructuredDataTest : public testing::Test { -public: - static void SetUpTestCase() { - s_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0); - llvm::sys::path::append(s_inputs_folder, "Inputs"); - } - -protected: - static llvm::SmallString<128> s_inputs_folder; -}; -} // namespace - -llvm::SmallString<128> StructuredDataTest::s_inputs_folder; - -TEST_F(StructuredDataTest, StringDump) { +TEST(StructuredDataTest, StringDump) { std::pair TestCases[] = { {R"(asdfg)", R"("asdfg")"}, {R"(as"df)", R"("as\"df")"}, @@ -49,14 +32,13 @@ } } -TEST_F(StructuredDataTest, ParseJSONFromFile) { +TEST(StructuredDataTest, ParseJSONFromFile) { Status status; auto object_sp = StructuredData::ParseJSONFromFile( FileSpec("non-existing-file.json", false), status); EXPECT_EQ(nullptr, object_sp); - llvm::SmallString<128> input = s_inputs_folder; - llvm::sys::path::append(input, "StructuredData-basic.json"); + std::string input = GetInputFile("StructuredData-basic.json"); object_sp = StructuredData::ParseJSONFromFile(FileSpec(input, false), status); ASSERT_NE(nullptr, object_sp); Index: unittests/Utility/TildeExpressionResolverTest.cpp =================================================================== --- unittests/Utility/TildeExpressionResolverTest.cpp +++ unittests/Utility/TildeExpressionResolverTest.cpp @@ -1,6 +1,6 @@ #include "gtest/gtest.h" -#include "Mocks/MockTildeExpressionResolver.h" +#include "Helpers/MockTildeExpressionResolver.h" #include "lldb/Utility/TildeExpressionResolver.h" #include "llvm/ADT/SmallString.h"