xref: /llvm-project/llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp (revision f7872cdce1102a5c12633dad462a1d9763a578d3)
1 //===- llvm/unittest/Support/CommandLineInit/CommandLineInitTest.cpp ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Check if preset options in libSupport -- e.g., "help", "version", etc. --
11 /// are correctly initialized and registered before getRegisteredOptions is
12 /// invoked.
13 ///
14 /// Most code here comes from llvm/utils/unittest/UnitTestMain/TestMain.cpp,
15 /// except that llvm::cl::ParseCommandLineOptions() call is removed.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Support/CommandLine.h"
20 #include "gtest/gtest.h"
21 
22 #if defined(_WIN32)
23 #include <windows.h>
24 #if defined(_MSC_VER)
25 #include <crtdbg.h>
26 #endif
27 #endif
28 
29 using namespace llvm;
30 
main(int argc,char ** argv)31 int main(int argc, char **argv) {
32   testing::InitGoogleTest(&argc, argv);
33 
34 #if defined(_WIN32)
35   // Disable all of the possible ways Windows conspires to make automated
36   // testing impossible.
37   ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
38 #if defined(_MSC_VER)
39   ::_set_error_mode(_OUT_TO_STDERR);
40   _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
41   _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
42   _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
43   _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
44   _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
45   _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
46 #endif
47 #endif
48 
49   return RUN_ALL_TESTS();
50 }
51 
TEST(CommandLineInitTest,GetPresetOptions)52 TEST(CommandLineInitTest, GetPresetOptions) {
53   StringMap<cl::Option *> &Map =
54       cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
55 
56   for (auto *Str :
57        {"help", "help-hidden", "help-list", "help-list-hidden", "version"})
58     EXPECT_EQ(Map.count(Str), (size_t)1)
59         << "Could not get preset option `" << Str << '`';
60 }
61