Skip to content

Commit 68b939f

Browse files
authored
[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
1 parent 345c482 commit 68b939f

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,6 +2003,12 @@ void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
20032003
// Print out the install directory.
20042004
OS << "InstalledDir: " << Dir << '\n';
20052005

2006+
// Print the build config if it's non-default.
2007+
// Intended to help LLVM developers understand the configs of compilers
2008+
// they're investigating.
2009+
if (!llvm::cl::getCompilerBuildConfig().empty())
2010+
llvm::cl::printBuildConfig(OS);
2011+
20062012
// If configuration files were used, print their paths.
20072013
for (auto ConfigFile : ConfigFiles)
20082014
OS << "Configuration file: " << ConfigFile << '\n';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# REQUIRES: asserts
2+
# RUN: %clang --version 2>&1 | FileCheck %s
3+
4+
# CHECK: clang version
5+
# When assertions are enabled, we should have a build configuration line that reflects that
6+
# CHECK: Build config: {{.*}}+assertions

llvm/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ option (LLVM_BUILD_EXTERNAL_COMPILER_RT
799799
option (LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
800800
"Show target and host info when tools are invoked with --version." ON)
801801

802+
option(LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
803+
"Show the optional build config flags when tools are invoked with --version." ON)
804+
802805
# You can configure which libraries from LLVM you want to include in the
803806
# shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
804807
# list of LLVM components. All component names handled by llvm-config are valid.

llvm/include/llvm/Config/config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@
290290
/* Whether tools show host and target info when invoked with --version */
291291
#cmakedefine01 LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
292292

293+
/* Whether tools show optional build config flags when invoked with --version */
294+
#cmakedefine01 LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
295+
293296
/* Define if libxml2 is supported on this platform. */
294297
#cmakedefine LLVM_ENABLE_LIBXML2 ${LLVM_ENABLE_LIBXML2}
295298

llvm/include/llvm/Support/CommandLine.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,16 @@ void PrintVersionMessage();
20022002
/// \param Categorized if true print options in categories
20032003
void PrintHelpMessage(bool Hidden = false, bool Categorized = false);
20042004

2005+
/// An array of optional enabled settings in the LLVM build configuration,
2006+
/// which may be of interest to compiler developers. For example, includes
2007+
/// "+assertions" if assertions are enabled. Used by printBuildConfig.
2008+
ArrayRef<StringRef> getCompilerBuildConfig();
2009+
2010+
/// Prints the compiler build configuration.
2011+
/// Designed for compiler developers, not compiler end-users.
2012+
/// Intended to be used in --version output when enabled.
2013+
void printBuildConfig(raw_ostream &OS);
2014+
20052015
//===----------------------------------------------------------------------===//
20062016
// Public interface for accessing registered options.
20072017
//

llvm/lib/Support/CommandLine.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,52 @@ void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
27342734
CommonOptions->CategorizedHiddenPrinter.printHelp();
27352735
}
27362736

2737+
ArrayRef<StringRef> cl::getCompilerBuildConfig() {
2738+
static const StringRef Config[] = {
2739+
// Placeholder to ensure the array always has elements, since it's an
2740+
// error to have a zero-sized array. Slice this off before returning.
2741+
"",
2742+
// Actual compiler build config feature list:
2743+
#if LLVM_IS_DEBUG_BUILD
2744+
"+unoptimized",
2745+
#endif
2746+
#ifndef NDEBUG
2747+
"+assertions",
2748+
#endif
2749+
#ifdef EXPENSIVE_CHECKS
2750+
"+expensive-checks",
2751+
#endif
2752+
#if __has_feature(address_sanitizer)
2753+
"+asan",
2754+
#endif
2755+
#if __has_feature(dataflow_sanitizer)
2756+
"+dfsan",
2757+
#endif
2758+
#if __has_feature(hwaddress_sanitizer)
2759+
"+hwasan",
2760+
#endif
2761+
#if __has_feature(memory_sanitizer)
2762+
"+msan",
2763+
#endif
2764+
#if __has_feature(thread_sanitizer)
2765+
"+tsan",
2766+
#endif
2767+
#if __has_feature(undefined_behavior_sanitizer)
2768+
"+ubsan",
2769+
#endif
2770+
};
2771+
return ArrayRef(Config).drop_front(1);
2772+
}
2773+
2774+
// Utility function for printing the build config.
2775+
void cl::printBuildConfig(raw_ostream &OS) {
2776+
#if LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
2777+
OS << "Build config: ";
2778+
llvm::interleaveComma(cl::getCompilerBuildConfig(), OS);
2779+
OS << '\n';
2780+
#endif
2781+
}
2782+
27372783
/// Utility function for printing version number.
27382784
void cl::PrintVersionMessage() {
27392785
CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);

0 commit comments

Comments
 (0)