Skip to content

[driver] Make --version show if assertions, etc. are enabled #87585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 5, 2024
Merged
6 changes: 6 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,12 @@ void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
// Print out the install directory.
OS << "InstalledDir: " << Dir << '\n';

// Print the build config if it's non-default.
// Intended to help LLVM developers understand the configs of compilers
// they're investigating.
if (!llvm::cl::getCompilerBuildConfig().empty())
llvm::cl::printBuildConfig(OS);

// If configuration files were used, print their paths.
for (auto ConfigFile : ConfigFiles)
OS << "Configuration file: " << ConfigFile << '\n';
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/version-build-config.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# REQUIRES: asserts
# RUN: %clang --version 2>&1 | FileCheck %s

# CHECK: clang version
# When assertions are enabled, we should have a build configuration line that reflects that
# CHECK: Build config: {{.*}}+assertions
3 changes: 3 additions & 0 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,9 @@ option (LLVM_BUILD_EXTERNAL_COMPILER_RT
option (LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
"Show target and host info when tools are invoked with --version." ON)

option(LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
"Show the optional build config flags when tools are invoked with --version." ON)

# You can configure which libraries from LLVM you want to include in the
# shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
# list of LLVM components. All component names handled by llvm-config are valid.
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Config/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@
/* Whether tools show host and target info when invoked with --version */
#cmakedefine01 LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO

/* Whether tools show optional build config flags when invoked with --version */
#cmakedefine01 LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG

/* Define if libxml2 is supported on this platform. */
#cmakedefine LLVM_ENABLE_LIBXML2 ${LLVM_ENABLE_LIBXML2}

Expand Down
10 changes: 10 additions & 0 deletions llvm/include/llvm/Support/CommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,16 @@ void PrintVersionMessage();
/// \param Categorized if true print options in categories
void PrintHelpMessage(bool Hidden = false, bool Categorized = false);

/// An array of optional enabled settings in the LLVM build configuration,
/// which may be of interest to compiler developers. For example, includes
/// "+assertions" if assertions are enabled. Used by printBuildConfig.
ArrayRef<StringRef> getCompilerBuildConfig();

/// Prints the compiler build configuration.
/// Designed for compiler developers, not compiler end-users.
/// Intended to be used in --version output when enabled.
void printBuildConfig(raw_ostream &OS);

//===----------------------------------------------------------------------===//
// Public interface for accessing registered options.
//
Expand Down
46 changes: 46 additions & 0 deletions llvm/lib/Support/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,52 @@ void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
CommonOptions->CategorizedHiddenPrinter.printHelp();
}

ArrayRef<StringRef> cl::getCompilerBuildConfig() {
static const StringRef Config[] = {
// Placeholder to ensure the array always has elements, since it's an
// error to have a zero-sized array. Slice this off before returning.
"",
// Actual compiler build config feature list:
#if LLVM_IS_DEBUG_BUILD
"+unoptimized",
#endif
#ifndef NDEBUG
"+assertions",
#endif
#ifdef EXPENSIVE_CHECKS
"+expensive-checks",
#endif
#if __has_feature(address_sanitizer)
"+asan",
#endif
#if __has_feature(dataflow_sanitizer)
"+dfsan",
#endif
#if __has_feature(hwaddress_sanitizer)
"+hwasan",
#endif
#if __has_feature(memory_sanitizer)
"+msan",
#endif
#if __has_feature(thread_sanitizer)
"+tsan",
#endif
#if __has_feature(undefined_behavior_sanitizer)
"+ubsan",
#endif
};
return ArrayRef(Config).drop_front(1);
}

// Utility function for printing the build config.
void cl::printBuildConfig(raw_ostream &OS) {
#if LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
OS << "Build config: ";
llvm::interleaveComma(cl::getCompilerBuildConfig(), OS);
OS << '\n';
#endif
}

/// Utility function for printing version number.
void cl::PrintVersionMessage() {
CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);
Expand Down