Skip to content

Commit 486b601

Browse files
zsrkmynMaskRay
authored andcommitted
[Support] Initialize common options in getRegisteredOptions
This allows users accessing options in libSupport before invoking `cl::ParseCommandLineOptions`, and also matches the behavior before D105959. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D106334
1 parent 8385de1 commit 486b601

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

llvm/lib/Support/CommandLine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,7 @@ void cl::AddExtraVersionPrinter(VersionPrinterTy func) {
26332633
}
26342634

26352635
StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) {
2636+
initCommonOptions();
26362637
auto &Subs = GlobalParser->RegisteredSubCommands;
26372638
(void)Subs;
26382639
assert(is_contained(Subs, &Sub));

llvm/unittests/Support/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ if(NOT LLVM_INTEGRATED_CRT_ALLOC)
127127
# The test doesn't pass when using a custom allocator, PR47881.
128128
add_subdirectory(DynamicLibrary)
129129
endif()
130+
131+
add_subdirectory(CommandLineInit)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
set(test_name CommandLineInitTests)
2+
set(test_suite UnitTests)
3+
4+
# We don't call add_llvm_unittest() here, because the function automatically
5+
# links the test against TestMain.cpp, in which main() function calls
6+
# llvm::cl::ParseCommandLineOptions, and it makes the test always pass.
7+
# The following code mainly comes from `add_unittest` in
8+
# llvm/cmake/modules/AddLLVM.cmake, except that gtest_main is excluded from
9+
# target_link_libraries to prevent the test linking against TestMain.cpp.
10+
11+
if (NOT LLVM_BUILD_TESTS)
12+
set(EXCLUDE_FROM_ALL ON)
13+
endif()
14+
15+
if (SUPPORTS_VARIADIC_MACROS_FLAG)
16+
list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
17+
endif ()
18+
# Some parts of gtest rely on this GNU extension, don't warn on it.
19+
if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG)
20+
list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments")
21+
endif()
22+
23+
list(APPEND LLVM_LINK_COMPONENTS Support)
24+
25+
add_llvm_executable(${test_name}
26+
IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH
27+
CommandLineInitTest.cpp)
28+
29+
target_link_libraries(${test_name} PRIVATE gtest)
30+
31+
add_dependencies(${test_suite} ${test_name})
32+
33+
set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
34+
set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
35+
36+
get_target_property(test_suite_folder ${test_suite} FOLDER)
37+
if (test_suite_folder)
38+
set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
39+
endif ()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
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+
52+
TEST(CommandLineInitTest, GetPresetOptions) {
53+
StringMap<cl::Option *> &Map =
54+
cl::getRegisteredOptions(*cl::TopLevelSubCommand);
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+
}

0 commit comments

Comments
 (0)