diff options
Diffstat (limited to 'llvm/unittests')
-rw-r--r-- | llvm/unittests/ADT/STLExtrasTest.cpp | 49 | ||||
-rw-r--r-- | llvm/unittests/CAS/CMakeLists.txt | 1 | ||||
-rw-r--r-- | llvm/unittests/CAS/OnDiskDataAllocatorTest.cpp | 66 | ||||
-rw-r--r-- | llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp | 10 | ||||
-rw-r--r-- | llvm/unittests/IR/DebugInfoTest.cpp | 76 | ||||
-rw-r--r-- | llvm/unittests/IR/ManglerTest.cpp | 3 | ||||
-rw-r--r-- | llvm/unittests/Option/CMakeLists.txt | 6 | ||||
-rw-r--r-- | llvm/unittests/Option/OptionMarshallingTest.cpp | 5 | ||||
-rw-r--r-- | llvm/unittests/Option/OptionSubCommandsTest.cpp | 252 | ||||
-rw-r--r-- | llvm/unittests/Option/SubCommandOpts.td | 16 | ||||
-rw-r--r-- | llvm/unittests/Support/GlobPatternTest.cpp | 72 |
11 files changed, 548 insertions, 8 deletions
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index 5020acd..47469983 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -14,6 +14,7 @@ #include <array> #include <climits> #include <cstddef> +#include <functional> #include <initializer_list> #include <iterator> #include <list> @@ -1658,6 +1659,54 @@ TEST(STLExtrasTest, Accumulate) { EXPECT_EQ(accumulate(V1, 10), std::accumulate(V1.begin(), V1.end(), 10)); EXPECT_EQ(accumulate(drop_begin(V1), 7), std::accumulate(V1.begin() + 1, V1.end(), 7)); + + EXPECT_EQ(accumulate(V1, 2, std::multiplies<>{}), 240); +} + +TEST(STLExtrasTest, SumOf) { + EXPECT_EQ(sum_of(std::vector<int>()), 0); + EXPECT_EQ(sum_of(std::vector<int>(), 1), 1); + std::vector<int> V1 = {1, 2, 3, 4, 5}; + static_assert(std::is_same_v<decltype(sum_of(V1)), int>); + static_assert(std::is_same_v<decltype(sum_of(V1, 1)), int>); + EXPECT_EQ(sum_of(V1), 15); + EXPECT_EQ(sum_of(V1, 1), 16); + + std::vector<float> V2 = {1.0f, 2.0f, 4.0f}; + static_assert(std::is_same_v<decltype(sum_of(V2)), float>); + static_assert(std::is_same_v<decltype(sum_of(V2), 1.0f), float>); + static_assert(std::is_same_v<decltype(sum_of(V2), 1.0), double>); + EXPECT_EQ(sum_of(V2), 7.0f); + EXPECT_EQ(sum_of(V2, 1.0f), 8.0f); + + // Make sure that for a const argument the return value is non-const. + const std::vector<float> V3 = {1.0f, 2.0f}; + static_assert(std::is_same_v<decltype(sum_of(V3)), float>); + EXPECT_EQ(sum_of(V3), 3.0f); +} + +TEST(STLExtrasTest, ProductOf) { + EXPECT_EQ(product_of(std::vector<int>()), 1); + EXPECT_EQ(product_of(std::vector<int>(), 0), 0); + EXPECT_EQ(product_of(std::vector<int>(), 1), 1); + std::vector<int> V1 = {1, 2, 3, 4, 5}; + static_assert(std::is_same_v<decltype(product_of(V1)), int>); + static_assert(std::is_same_v<decltype(product_of(V1, 1)), int>); + EXPECT_EQ(product_of(V1), 120); + EXPECT_EQ(product_of(V1, 1), 120); + EXPECT_EQ(product_of(V1, 2), 240); + + std::vector<float> V2 = {1.0f, 2.0f, 4.0f}; + static_assert(std::is_same_v<decltype(product_of(V2)), float>); + static_assert(std::is_same_v<decltype(product_of(V2), 1.0f), float>); + static_assert(std::is_same_v<decltype(product_of(V2), 1.0), double>); + EXPECT_EQ(product_of(V2), 8.0f); + EXPECT_EQ(product_of(V2, 4.0f), 32.0f); + + // Make sure that for a const argument the return value is non-const. + const std::vector<float> V3 = {1.0f, 2.0f}; + static_assert(std::is_same_v<decltype(product_of(V3)), float>); + EXPECT_EQ(product_of(V3), 2.0f); } struct Foo; diff --git a/llvm/unittests/CAS/CMakeLists.txt b/llvm/unittests/CAS/CMakeLists.txt index 0f8fcb9..ee40e6c 100644 --- a/llvm/unittests/CAS/CMakeLists.txt +++ b/llvm/unittests/CAS/CMakeLists.txt @@ -8,6 +8,7 @@ add_llvm_unittest(CASTests ActionCacheTest.cpp CASTestConfig.cpp ObjectStoreTest.cpp + OnDiskDataAllocatorTest.cpp OnDiskTrieRawHashMapTest.cpp ProgramTest.cpp ) diff --git a/llvm/unittests/CAS/OnDiskDataAllocatorTest.cpp b/llvm/unittests/CAS/OnDiskDataAllocatorTest.cpp new file mode 100644 index 0000000..966fa03 --- /dev/null +++ b/llvm/unittests/CAS/OnDiskDataAllocatorTest.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/CAS/OnDiskDataAllocator.h" +#include "llvm/CAS/MappedFileRegionArena.h" +#include "llvm/Config/llvm-config.h" +#include "llvm/Support/Alignment.h" +#include "llvm/Testing/Support/Error.h" +#include "llvm/Testing/Support/SupportHelpers.h" + +#if LLVM_ENABLE_ONDISK_CAS + +using namespace llvm; +using namespace llvm::cas; + +TEST(OnDiskDataAllocatorTest, Allocate) { + unittest::TempDir Temp("data-allocator", /*Unique=*/true); + constexpr size_t MB = 1024u * 1024u; + + std::optional<OnDiskDataAllocator> Allocator; + ASSERT_THAT_ERROR(OnDiskDataAllocator::create( + Temp.path("allocator"), "data", /*MaxFileSize=*/MB, + /*NewFileInitialSize=*/std::nullopt) + .moveInto(Allocator), + Succeeded()); + + // Allocate. + { + for (size_t Size = 1; Size < 16; ++Size) { + OnDiskDataAllocator::OnDiskPtr P; + ASSERT_THAT_ERROR(Allocator->allocate(Size).moveInto(P), Succeeded()); + EXPECT_TRUE( + isAligned(MappedFileRegionArena::getAlign(), P.getOffset().get())); + } + } + + // Out of space. + { + OnDiskDataAllocator::OnDiskPtr P; + ASSERT_THAT_ERROR(Allocator->allocate(MB).moveInto(P), Failed()); + } + + // Check size and capacity. + { + ASSERT_EQ(Allocator->capacity(), MB); + ASSERT_LE(Allocator->size(), MB); + } + + // Get. + { + OnDiskDataAllocator::OnDiskPtr P; + ASSERT_THAT_ERROR(Allocator->allocate(32).moveInto(P), Succeeded()); + ArrayRef<char> Data; + ASSERT_THAT_ERROR(Allocator->get(P.getOffset(), 16).moveInto(Data), + Succeeded()); + ASSERT_THAT_ERROR(Allocator->get(P.getOffset(), 1025).moveInto(Data), + Failed()); + } +} + +#endif // LLVM_ENABLE_ONDISK_CAS diff --git a/llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp b/llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp index 7bedfe4..6034c70 100644 --- a/llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp +++ b/llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp @@ -71,7 +71,7 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) { std::optional<FileOffset> Offset; std::optional<MutableArrayRef<char>> Data; { - std::optional<OnDiskTrieRawHashMap::pointer> Insertion; + std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion; ASSERT_THAT_ERROR(Trie1->insert({Hash0, Data0v1}).moveInto(Insertion), Succeeded()); EXPECT_EQ(Hash0, (*Insertion)->Hash); @@ -128,7 +128,7 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) { // Recover from an offset. { - OnDiskTrieRawHashMap::const_pointer Recovered; + OnDiskTrieRawHashMap::ConstOnDiskPtr Recovered; ASSERT_THAT_ERROR(Trie1->recoverFromFileOffset(*Offset).moveInto(Recovered), Succeeded()); ASSERT_TRUE(Recovered); @@ -140,14 +140,14 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) { // Recover from a bad offset. { FileOffset BadOffset(1); - OnDiskTrieRawHashMap::const_pointer Recovered; + OnDiskTrieRawHashMap::ConstOnDiskPtr Recovered; ASSERT_THAT_ERROR( Trie1->recoverFromFileOffset(BadOffset).moveInto(Recovered), Failed()); } // Insert another thing. { - std::optional<OnDiskTrieRawHashMap::pointer> Insertion; + std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion; ASSERT_THAT_ERROR(Trie1->insert({Hash1, Data1}).moveInto(Insertion), Succeeded()); EXPECT_EQ(Hash1, (*Insertion)->Hash); @@ -210,7 +210,7 @@ TEST(OnDiskTrieRawHashMapTest, OutOfSpace) { auto Hash0 = ArrayRef(Hash0Bytes); constexpr StringLiteral Data0v1Bytes = "data0.v1"; ArrayRef<char> Data0v1 = ArrayRef(Data0v1Bytes.data(), Data0v1Bytes.size()); - std::optional<OnDiskTrieRawHashMap::pointer> Insertion; + std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion; ASSERT_THAT_ERROR(Trie->insert({Hash0, Data0v1}).moveInto(Insertion), Failed()); } diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp index 03333d5..475e0a9 100644 --- a/llvm/unittests/IR/DebugInfoTest.cpp +++ b/llvm/unittests/IR/DebugInfoTest.cpp @@ -1250,6 +1250,82 @@ TEST(MetadataTest, DbgVariableRecordConversionRoutines) { EXPECT_EQ(DVI2->getExpression(), Expr2); } +TEST(MetadataTest, InlinedAtMethodsWithMultipleLevels) { + LLVMContext C; + + // Create IR with 3 levels of inlining: + // main() calls inline1() which calls inline2() which calls inline3() + // We'll test from the perspective of code in inline3() + std::unique_ptr<Module> M = parseIR(C, R"( + define void @main() !dbg !10 { + ret void, !dbg !20 + } + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!2} + + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1) + !1 = !DIFile(filename: "test.c", directory: "/test") + !2 = !{i32 2, !"Debug Info Version", i32 3} + + ; Subprograms for each function in the call chain + !10 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 100, unit: !0) + !11 = distinct !DISubprogram(name: "inline1", scope: !1, file: !1, line: 200, unit: !0) + !12 = distinct !DISubprogram(name: "inline2", scope: !1, file: !1, line: 300, unit: !0) + !13 = distinct !DISubprogram(name: "inline3", scope: !1, file: !1, line: 400, unit: !0) + + ; Location in inline3 (line 401), inlined at location !21 + !20 = !DILocation(line: 401, column: 5, scope: !13, inlinedAt: !21) + + ; Location in inline2 (line 301) where inline3 was called, inlined at !22 + !21 = !DILocation(line: 301, column: 10, scope: !12, inlinedAt: !22) + + ; Location in inline1 (line 201) where inline2 was called, inlined at !23 + !22 = !DILocation(line: 201, column: 15, scope: !11, inlinedAt: !23) + + ; Location in main (line 101) where inline1 was called (no more inlinedAt) + !23 = !DILocation(line: 101, column: 3, scope: !10) + )"); + + ASSERT_TRUE(M); + + Function *MainFunc = M->getFunction("main"); + ASSERT_TRUE(MainFunc); + Instruction &RetInst = MainFunc->getEntryBlock().front(); + + // Use getDebugLoc() to get the location from the ret instruction. + const DILocation *InnermostLoc = RetInst.getDebugLoc().get(); + ASSERT_TRUE(InnermostLoc); + + // Test getScope() - should return the immediate scope (inline3). + DILocalScope *ImmediateScope = InnermostLoc->getScope(); + ASSERT_TRUE(ImmediateScope); + EXPECT_TRUE(isa<DISubprogram>(ImmediateScope)); + EXPECT_EQ(cast<DISubprogram>(ImmediateScope)->getName(), "inline3"); + + // Test getInlinedAt() - should return the next level in the inlining chain. + const DILocation *NextLevel = InnermostLoc->getInlinedAt(); + ASSERT_TRUE(NextLevel); + EXPECT_EQ(NextLevel->getLine(), 301u); + EXPECT_EQ(cast<DISubprogram>(NextLevel->getScope())->getName(), "inline2"); + + // Test getInlinedAtLocation() - should return the outermost location. + const DILocation *OutermostLoc = InnermostLoc->getInlinedAtLocation(); + ASSERT_TRUE(OutermostLoc); + EXPECT_EQ(OutermostLoc->getLine(), 101u); + EXPECT_EQ(OutermostLoc->getColumn(), 3u); + EXPECT_EQ(OutermostLoc->getInlinedAt(), nullptr); + EXPECT_EQ(cast<DISubprogram>(OutermostLoc->getScope())->getName(), "main"); + + // Test getInlinedAtScope() - should return the scope of the outermost + // location. + DILocalScope *InlinedAtScope = InnermostLoc->getInlinedAtScope(); + ASSERT_TRUE(InlinedAtScope); + EXPECT_TRUE(isa<DISubprogram>(InlinedAtScope)); + EXPECT_EQ(cast<DISubprogram>(InlinedAtScope)->getName(), "main"); + EXPECT_EQ(InlinedAtScope, OutermostLoc->getScope()); +} + // Test that the hashing function for DISubprograms representing methods produce // the same result after replacing their scope (the type containing the // subprogram) from a temporary DIType with the permanent one. diff --git a/llvm/unittests/IR/ManglerTest.cpp b/llvm/unittests/IR/ManglerTest.cpp index bced6ff..bb0b3ed 100644 --- a/llvm/unittests/IR/ManglerTest.cpp +++ b/llvm/unittests/IR/ManglerTest.cpp @@ -243,6 +243,9 @@ TEST(ManglerTest, Arm64EC) { // public: int __cdecl Wrapper<struct A>::GetValue(struct WW<struct // A>::Z)const "?GetValue@?$Wrapper@UA@@@@$$hQEBAHUZ@?$WW@UA@@@@@Z", + + // MD5 symbol + "??@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@$$h@", }; for (const auto &Arm64ECName : Arm64ECNames) { diff --git a/llvm/unittests/Option/CMakeLists.txt b/llvm/unittests/Option/CMakeLists.txt index 7be4300..5fefb5e 100644 --- a/llvm/unittests/Option/CMakeLists.txt +++ b/llvm/unittests/Option/CMakeLists.txt @@ -4,11 +4,15 @@ set(LLVM_LINK_COMPONENTS ) set(LLVM_TARGET_DEFINITIONS Opts.td) - tablegen(LLVM Opts.inc -gen-opt-parser-defs) + +set(LLVM_TARGET_DEFINITIONS SubCommandOpts.td) +tablegen(LLVM SubCommandOpts.inc -gen-opt-parser-defs) + add_public_tablegen_target(OptsTestTableGen) add_llvm_unittest(OptionTests OptionParsingTest.cpp OptionMarshallingTest.cpp + OptionSubCommandsTest.cpp ) diff --git a/llvm/unittests/Option/OptionMarshallingTest.cpp b/llvm/unittests/Option/OptionMarshallingTest.cpp index 005144b..15917cc 100644 --- a/llvm/unittests/Option/OptionMarshallingTest.cpp +++ b/llvm/unittests/Option/OptionMarshallingTest.cpp @@ -29,8 +29,9 @@ static const OptionWithMarshallingInfo MarshallingTable[] = { #define OPTION_WITH_MARSHALLING( \ PREFIX_TYPE, PREFIXED_NAME_OFFSET, ID, KIND, GROUP, ALIAS, ALIASARGS, \ FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, VALUES, \ - SHOULD_PARSE, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, IMPLIED_CHECK, \ - IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, TABLE_INDEX) \ + SUBCOMMANDIDS_OFFSET, SHOULD_PARSE, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, \ + IMPLIED_CHECK, IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, \ + TABLE_INDEX) \ {PREFIXED_NAME_OFFSET, #KEYPATH, #IMPLIED_CHECK, #IMPLIED_VALUE}, #include "Opts.inc" #undef OPTION_WITH_MARSHALLING diff --git a/llvm/unittests/Option/OptionSubCommandsTest.cpp b/llvm/unittests/Option/OptionSubCommandsTest.cpp new file mode 100644 index 0000000..e31a326 --- /dev/null +++ b/llvm/unittests/Option/OptionSubCommandsTest.cpp @@ -0,0 +1,252 @@ +//===- unittest/Support/OptionParsingTest.cpp - OptTable tests ------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/STLExtras.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/OptTable.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace llvm::opt; + +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +#endif + +namespace { +enum ID { + OPT_INVALID = 0, +#define OPTION(PREFIXES, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \ + VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \ + VALUES, SUBCOMMANDIDS_OFFSET) \ + OPT_##ID, +#include "SubCommandOpts.inc" +#undef OPTION +}; +#define OPTTABLE_STR_TABLE_CODE +#include "SubCommandOpts.inc" +#undef OPTTABLE_STR_TABLE_CODE + +#define OPTTABLE_PREFIXES_TABLE_CODE +#include "SubCommandOpts.inc" +#undef OPTTABLE_PREFIXES_TABLE_CODE + +#define OPTTABLE_SUBCOMMAND_IDS_TABLE_CODE +#include "SubCommandOpts.inc" +#undef OPTTABLE_SUBCOMMAND_IDS_TABLE_CODE + +#define OPTTABLE_SUBCOMMANDS_CODE +#include "SubCommandOpts.inc" +#undef OPTTABLE_SUBCOMMANDS_CODE + +static constexpr OptTable::Info InfoTable[] = { +#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__), +#include "SubCommandOpts.inc" +#undef OPTION +}; + +class TestOptSubCommandTable : public GenericOptTable { +public: + TestOptSubCommandTable(bool IgnoreCase = false) + : GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable, + /*IgnoreCase=*/false, OptionSubCommands, + OptionSubCommandIDsTable) {} +}; + +// Test fixture +template <typename T> class OptSubCommandTableTest : public ::testing::Test {}; + +// Test both precomputed and computed OptTables with the same suite of tests. +using OptSubCommandTableTestTypes = ::testing::Types<TestOptSubCommandTable>; + +TYPED_TEST_SUITE(OptSubCommandTableTest, OptSubCommandTableTestTypes, ); + +TYPED_TEST(OptSubCommandTableTest, SubCommandParsing) { + TypeParam T; + unsigned MAI, MAC; + + std::string ErrMsg; + raw_string_ostream RSO1(ErrMsg); + + auto HandleMultipleSubcommands = [&](ArrayRef<StringRef> SubCommands) { + ErrMsg.clear(); + RSO1 << "Multiple subcommands passed\n"; + for (auto SC : SubCommands) + RSO1 << "\n" << SC; + }; + + auto HandleOtherPositionals = [&](ArrayRef<StringRef> Positionals) { + ErrMsg.clear(); + RSO1 << "Unregistered positionals passed\n"; + for (auto SC : Positionals) + RSO1 << "\n" << SC; + }; + + { + // Test case 1: Toplevel option, no subcommand + const char *Args[] = {"-version"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + EXPECT_TRUE(AL.hasArg(OPT_version)); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + EXPECT_TRUE(SC.empty()); + EXPECT_FALSE(AL.hasArg(OPT_uppercase)); + EXPECT_FALSE(AL.hasArg(OPT_lowercase)); + } + + { + // Test case 2: Subcommand 'foo' with its valid options + const char *Args[] = {"foo", "-uppercase"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + EXPECT_EQ(SC, "foo"); + EXPECT_TRUE(AL.hasArg(OPT_uppercase)); + EXPECT_FALSE(AL.hasArg(OPT_lowercase)); + EXPECT_FALSE(AL.hasArg(OPT_version)); + EXPECT_EQ(std::string::npos, ErrMsg.find("Multiple subcommands passed")) + << "Did not expect error message as this is a valid use case."; + EXPECT_EQ(std::string::npos, ErrMsg.find("Unregistered positionals passed")) + << "Did not expect error message as this is a valid use case."; + } + + { + // Test case 3: Check valid use of subcommand which follows a valid + // subcommand option. + const char *Args[] = {"-uppercase", "foo"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + EXPECT_EQ(SC, "foo"); + EXPECT_TRUE(AL.hasArg(OPT_uppercase)); + EXPECT_FALSE(AL.hasArg(OPT_lowercase)); + EXPECT_FALSE(AL.hasArg(OPT_version)); + EXPECT_EQ(std::string::npos, ErrMsg.find("Multiple subcommands passed")) + << "Did not expect error message as this is a valid use case."; + EXPECT_EQ(std::string::npos, ErrMsg.find("Unregistered positionals passed")) + << "Did not expect error message as this is a valid use case."; + } + + { + // Test case 4: Check invalid use of passing multiple subcommands. + const char *Args[] = {"-uppercase", "foo", "bar"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + // No valid subcommand should be returned as this is an invalid invocation. + EXPECT_TRUE(SC.empty()); + // Expect the multiple subcommands error message. + EXPECT_NE(std::string::npos, ErrMsg.find("Multiple subcommands passed")); + EXPECT_NE(std::string::npos, ErrMsg.find("foo")); + EXPECT_NE(std::string::npos, ErrMsg.find("bar")); + EXPECT_EQ(std::string::npos, ErrMsg.find("Unregistered positionals passed")) + << "Did not expect error message as this is a valid use case."; + } + + { + // Test case 5: Check invalid use of passing unregistered subcommands. + const char *Args[] = {"foobar"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + // No valid subcommand should be returned as this is an invalid invocation. + EXPECT_TRUE(SC.empty()); + // Expect the unregistered subcommands error message. + EXPECT_NE(std::string::npos, + ErrMsg.find("Unregistered positionals passed")); + EXPECT_NE(std::string::npos, ErrMsg.find("foobar")); + } + + { + // Test case 6: Check invalid use of a valid subcommand which follows a + // valid subcommand option but the option is not registered with the given + // subcommand. + const char *Args[] = {"-lowercase", "bar"}; + InputArgList AL = T.ParseArgs(Args, MAI, MAC); + StringRef SC = AL.getSubCommand( + T.getSubCommands(), HandleMultipleSubcommands, HandleOtherPositionals); + auto HandleSubCommandArg = [&](ID OptionType) { + if (!AL.hasArg(OptionType)) + return false; + auto O = T.getOption(OptionType); + if (!O.isRegisteredSC(SC)) { + ErrMsg.clear(); + RSO1 << "Option [" << O.getName() << "] is not valid for SubCommand [" + << SC << "]\n"; + return false; + } + return true; + }; + EXPECT_EQ(SC, "bar"); // valid subcommand + EXPECT_TRUE(AL.hasArg(OPT_lowercase)); // valid option + EXPECT_FALSE(HandleSubCommandArg(OPT_lowercase)); + EXPECT_NE( + std::string::npos, + ErrMsg.find("Option [lowercase] is not valid for SubCommand [bar]")); + } +} + +TYPED_TEST(OptSubCommandTableTest, SubCommandHelp) { + TypeParam T; + std::string Help; + raw_string_ostream RSO(Help); + + // Toplevel help + T.printHelp(RSO, "Test Usage String", "OverviewString"); + EXPECT_NE(std::string::npos, Help.find("OVERVIEW:")); + EXPECT_NE(std::string::npos, Help.find("OverviewString")); + EXPECT_NE(std::string::npos, Help.find("USAGE:")); + EXPECT_NE(std::string::npos, Help.find("Test Usage String")); + EXPECT_NE(std::string::npos, Help.find("SUBCOMMANDS:")); + EXPECT_NE(std::string::npos, Help.find("foo")); + EXPECT_NE(std::string::npos, Help.find("bar")); + EXPECT_NE(std::string::npos, Help.find("HelpText for SubCommand foo.")); + EXPECT_NE(std::string::npos, Help.find("HelpText for SubCommand bar.")); + EXPECT_NE(std::string::npos, Help.find("OPTIONS:")); + EXPECT_NE(std::string::npos, Help.find("--help")); + EXPECT_NE(std::string::npos, Help.find("-version")); + // uppercase is not a global option and should not be shown. + EXPECT_EQ(std::string::npos, Help.find("-uppercase")); + + // Help for subcommand foo + Help.clear(); + StringRef SC1 = "foo"; + T.printHelp(RSO, "Test Usage String", "OverviewString", false, false, + Visibility(), SC1); + EXPECT_NE(std::string::npos, Help.find("OVERVIEW:")); + EXPECT_NE(std::string::npos, Help.find("OverviewString")); + // SubCommand "foo" definition for tablegen has NO dedicated usage string so + // not expected to see USAGE. + EXPECT_EQ(std::string::npos, Help.find("USAGE:")); + EXPECT_NE(std::string::npos, Help.find("HelpText for SubCommand foo.")); + EXPECT_NE(std::string::npos, Help.find("-uppercase")); + EXPECT_NE(std::string::npos, Help.find("-lowercase")); + EXPECT_EQ(std::string::npos, Help.find("-version")); + EXPECT_EQ(std::string::npos, Help.find("SUBCOMMANDS:")); + + // Help for subcommand bar + Help.clear(); + StringRef SC2 = "bar"; + T.printHelp(RSO, "Test Usage String", "OverviewString", false, false, + Visibility(), SC2); + EXPECT_NE(std::string::npos, Help.find("OVERVIEW:")); + EXPECT_NE(std::string::npos, Help.find("OverviewString")); + // SubCommand "bar" definition for tablegen has a dedicated usage string. + EXPECT_NE(std::string::npos, Help.find("USAGE:")); + EXPECT_NE(std::string::npos, Help.find("Subcommand bar <options>")); + EXPECT_NE(std::string::npos, Help.find("HelpText for SubCommand bar.")); + EXPECT_NE(std::string::npos, Help.find("-uppercase")); + // lowercase is not an option for bar and should not be shown. + EXPECT_EQ(std::string::npos, Help.find("-lowercase")); + // version is a global option and should not be shown. + EXPECT_EQ(std::string::npos, Help.find("-version")); +} +} // end anonymous namespace diff --git a/llvm/unittests/Option/SubCommandOpts.td b/llvm/unittests/Option/SubCommandOpts.td new file mode 100644 index 0000000..b9750da --- /dev/null +++ b/llvm/unittests/Option/SubCommandOpts.td @@ -0,0 +1,16 @@ +include "llvm/Option/OptParser.td" + +def sc_foo : SubCommand<"foo", "HelpText for SubCommand foo.">; + +def sc_bar : SubCommand<"bar", "HelpText for SubCommand bar.", + "Subcommand bar <options>">; + +def help : Flag<["--"], "help">, HelpText<"Subcommand <subcommand> <options>">; + +def version : Flag<["-"], "version">, HelpText<"Display the version number">; + +def uppercase : Flag<["-"], "uppercase", [sc_foo, sc_bar]>, + HelpText<"Print in uppercase">; + +def lowercase : Flag<["-"], "lowercase", [sc_foo]>, + HelpText<"Print in lowercase">; diff --git a/llvm/unittests/Support/GlobPatternTest.cpp b/llvm/unittests/Support/GlobPatternTest.cpp index e4f1025..58fd767 100644 --- a/llvm/unittests/Support/GlobPatternTest.cpp +++ b/llvm/unittests/Support/GlobPatternTest.cpp @@ -257,6 +257,78 @@ TEST_F(GlobPatternTest, NUL) { } } +TEST_F(GlobPatternTest, PrefixSuffix) { + auto Pat = GlobPattern::create(""); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("", Pat->prefix()); + EXPECT_EQ("", Pat->suffix()); + + Pat = GlobPattern::create("abcd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("abcd", Pat->prefix()); + EXPECT_EQ("", Pat->suffix()); + + Pat = GlobPattern::create("*abcd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("", Pat->prefix()); + EXPECT_EQ("abcd", Pat->suffix()); + + Pat = GlobPattern::create("abcd*"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("abcd", Pat->prefix()); + EXPECT_EQ("", Pat->suffix()); + + Pat = GlobPattern::create("ab*cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); + + Pat = GlobPattern::create("ab?cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); + + Pat = GlobPattern::create("ab[n]cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); + + Pat = GlobPattern::create("ab{}cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); + + Pat = GlobPattern::create("ab{cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); + + Pat = GlobPattern::create("ab]cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab]cd", Pat->prefix()); + EXPECT_EQ("", Pat->suffix()); + + Pat = GlobPattern::create("ab\\cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("d", Pat->suffix()); + + Pat = GlobPattern::create("ab\\\\cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("d", Pat->suffix()); + + Pat = GlobPattern::create("ab?cd?"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("ab", Pat->prefix()); + EXPECT_EQ("", Pat->suffix()); + + Pat = GlobPattern::create("?ab?cd"); + ASSERT_TRUE((bool)Pat); + EXPECT_EQ("", Pat->prefix()); + EXPECT_EQ("cd", Pat->suffix()); +} + TEST_F(GlobPatternTest, Pathological) { std::string P, S(40, 'a'); StringRef Pieces[] = {"a*", "[ba]*", "{b*,a*}*"}; |