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

It's useful to have some significant build options visible in the
version when investigating problems with a specific compiler artifact.
This makes it easy to see if assertions, expensive checks, sanitizers,
etc. are enabled when checking a compiler version.

Example config line output:
Build configuration: +unoptimized, +assertions, +asan, +ubsan
This commit is contained in:
Cassie Jones 2024-04-05 13:01:09 -04:00 committed by GitHub
parent 345c4822e4
commit 68b939f931
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 74 additions and 0 deletions

View File

@ -2003,6 +2003,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';

View File

@ -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

View File

@ -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.

View File

@ -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}

View File

@ -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.
//

View File

@ -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);