diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -27,6 +27,7 @@ #include "llvm/Support/VersionTuple.h" #include "llvm/Target/TargetOptions.h" #include +#include #include #include #include @@ -489,6 +490,11 @@ // to the contrary. virtual unsigned GetDefaultDwarfVersion() const { return 4; } + // Some toolchains may have different restrictions on the DWARF version and + // may need to adjust it. E.g. NVPTX may need to enforce DWARF2 even when host + // compilation uses DWARF5. + virtual unsigned getMaxDwarfVersion() const { return UINT_MAX; } + // True if the driver should assume "-fstandalone-debug" // in the absence of an option specifying otherwise, // provided that debugging was requested in the first place. diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3852,6 +3852,8 @@ if (GDwarfN) if (auto ExplicitVersion = DwarfVersionNum(GDwarfN->getSpelling())) DWARFVersion = ExplicitVersion; + // Clamp effective DWARF version to the max supported by the toolchain. + DWARFVersion = std::min(DWARFVersion, TC.getMaxDwarfVersion()); } // -gline-directives-only supported only for the DWARF debug info. @@ -3916,8 +3918,8 @@ // we emit an error. const Arg *A = Args.getLastArg(options::OPT_gembed_source); if (DWARFVersion < 5) - D.Diag(diag::err_drv_argument_only_allowed_with) - << A->getAsString(Args) << "-gdwarf-5"; + D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target) + << A->getAsString(Args) << TC.getTripleString(); else if (checkDebugInfoOption(A, Args, D, TC)) CmdArgs.push_back("-gembed-source"); } diff --git a/clang/lib/Driver/ToolChains/Cuda.h b/clang/lib/Driver/ToolChains/Cuda.h --- a/clang/lib/Driver/ToolChains/Cuda.h +++ b/clang/lib/Driver/ToolChains/Cuda.h @@ -185,6 +185,8 @@ const llvm::opt::ArgList &Args) const override; unsigned GetDefaultDwarfVersion() const override { return 2; } + // NVPTX supports only DWARF2. + unsigned getMaxDwarfVersion() const override { return 2; } const ToolChain &HostTC; CudaInstallationDetector CudaInstallation; diff --git a/clang/test/Driver/cuda-unsupported-debug-options.cu b/clang/test/Driver/cuda-unsupported-debug-options.cu --- a/clang/test/Driver/cuda-unsupported-debug-options.cu +++ b/clang/test/Driver/cuda-unsupported-debug-options.cu @@ -18,5 +18,7 @@ // CHECK-NOT: debug information option '{{.*}}' is not supported for target 'x86 // CHECK: "-triple" "nvptx64-nvidia-cuda" // CHECK-NOT: {{-compress-debug|-fdebug-info-for-profiling|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}} +// Make sure we do not see any dwarf version other than 2, regardless of what's used on the host side. +// CHECK-NOT: {{-dwarf-version=[^2]}} // CHECK: "-triple" "x86_64 // CHECK-SAME: {{-compress-debug|-fdebug-info-for-profiling|split-dwarf|lldb|codeview|module-format|embed-source|debug-info-macro|gnu-pubnames|generate-arange-section|generate-type-units}} diff --git a/clang/test/Driver/debug-options.c b/clang/test/Driver/debug-options.c --- a/clang/test/Driver/debug-options.c +++ b/clang/test/Driver/debug-options.c @@ -361,9 +361,9 @@ // RUN: %clang -### -gdwarf-2 -gno-embed-source %s 2>&1 | FileCheck -check-prefix=NOGEMBED_2 %s // // GEMBED_5: "-gembed-source" -// GEMBED_2: error: invalid argument '-gembed-source' only allowed with '-gdwarf-5' +// GEMBED_2: warning: debug information option '-gembed-source' is not supported for target // NOGEMBED_5-NOT: "-gembed-source" -// NOGEMBED_2-NOT: error: invalid argument '-gembed-source' only allowed with '-gdwarf-5' +// NOGEMBED_2-NOT: warning: debug information option '-gembed-source' is not supported for target // // RUN: %clang -### -g -fno-eliminate-unused-debug-types -c %s 2>&1 \ // RUN: | FileCheck -check-prefix=DEBUG_UNUSED_TYPES %s