[flang-commits] [clang] [flang] [flang][driver] Honour the toolchain default DWARF version (PR #217610)
via flang-commits
flang-commits at lists.llvm.org
Thu Aug 20 06:07:33 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-driver
Author: Abid Qadeer (abidh)
<details>
<summary>Changes</summary>
The driver only rendered -dwarf-version= when the user named a version explicitly with -gdwarf-N. With plain -g the option was omitted, and the backend fell back to dwarf::DWARF_VERSION. As a result `flang -g` always produced DWARF 4 while `clang -g` produced the toolchain default for the same target..
The fix is to generate the "-dwarf-version" flag when either the debug information is enabled or an explicit -gdwarf-N is given.
---
Full diff: https://github.com/llvm/llvm-project/pull/217610.diff
2 Files Affected:
- (modified) clang/lib/Driver/ToolChains/Flang.cpp (+8-1)
- (modified) flang/test/Driver/flang-dwarf-version.f90 (+41)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index a48e41159f367..64d07fd2f1955 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -248,7 +248,14 @@ void Flang::addDebugOptions(const llvm::opt::ArgList &Args, const JobAction &JA,
DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
}
addDebugInfoKind(CmdArgs, DebugInfoKind);
- if (hasDwarfNArg) {
+ // Pass the DWARF version on when debug information is being generated, or
+ // when -gdwarf-N names a version. Leaving it out means the version stays
+ // unset and the backend falls back to dwarf::DWARF_VERSION (4) instead of
+ // honouring toolchain default like clang does.
+ //
+ // Note that both conditions are needed to match clang for cases like
+ // "-gdwarf-5 -g0".
+ if (hasDwarfNArg || DebugInfoKind != llvm::codegenoptions::NoDebugInfo) {
const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
CmdArgs.push_back(
Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
diff --git a/flang/test/Driver/flang-dwarf-version.f90 b/flang/test/Driver/flang-dwarf-version.f90
index d860c970a91f8..e85010161abf9 100644
--- a/flang/test/Driver/flang-dwarf-version.f90
+++ b/flang/test/Driver/flang-dwarf-version.f90
@@ -20,6 +20,43 @@
// RUN: %flang -### -S %s -gdwarf-2 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+// Without an explicit -gdwarf-N, the toolchain default DWARF version is used.
+
+// Linux.
+// RUN: %flang -### -S %s -g --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
+// RUN: %flang -### -S %s -g1 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s
+
+// Android always uses DWARF 4.
+// RUN: %flang -### -S %s -g --target=aarch64-unknown-linux-android21 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s
+
+// Darwin derives the version from the OS version rather than using a constant.
+// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx15 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s
+// RUN: %flang -### -S %s -g --target=x86_64-apple-macosx10.10 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+
+// AIX.
+// RUN: %flang -### -S %s -g --target=powerpc64-ibm-aix 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s
+
+// OpenBSD.
+// RUN: %flang -### -S %s -g --target=x86_64-unknown-openbsd 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s
+
+// No debug info requested means no DWARF version is passed at all.
+// RUN: %flang -### -S %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
+// RUN: %flang -### -S %s -g0 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-NO-DWARF %s
+
+// A version named explicitly is still passed on when debug info is switched
+// off, as clang does.
+// RUN: %flang -### -S %s -gdwarf-5 -g0 --target=x86_64-unknown-linux-gnu 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-DWARF5-G0 %s
+
// CHECK-DWARF5: -debug-info-kind=standalone
// CHECK-DWARF5-SAME: -dwarf-version=5
@@ -31,3 +68,7 @@
// CHECK-DWARF3: -dwarf-version=3
// CHECK-DWARF2: -dwarf-version=2
+
+// CHECK-NO-DWARF-NOT: -dwarf-version=
+
+// CHECK-DWARF5-G0: -dwarf-version=5
``````````
</details>
https://github.com/llvm/llvm-project/pull/217610
More information about the flang-commits
mailing list