diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 49e917a6a0786..a27164b166795 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4677,8 +4677,10 @@ def gdbx : Flag<["-"], "gdbx">, Group; // Equivalent to our default dwarf version. Forces usual dwarf emission when // CodeView is enabled. def gdwarf : Flag<["-"], "gdwarf">, Group, - Visibility<[ClangOption, CLOption, DXCOption]>, + Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>, HelpText<"Generate source-level debug information with the default dwarf version">; + +let Visibility = [ClangOption, FlangOption] in { def gdwarf_2 : Flag<["-"], "gdwarf-2">, Group, HelpText<"Generate source-level debug information with dwarf version 2">; def gdwarf_3 : Flag<["-"], "gdwarf-3">, Group, @@ -4687,6 +4689,7 @@ def gdwarf_4 : Flag<["-"], "gdwarf-4">, Group, HelpText<"Generate source-level debug information with dwarf version 4">; def gdwarf_5 : Flag<["-"], "gdwarf-5">, Group, HelpText<"Generate source-level debug information with dwarf version 5">; +} def gdwarf64 : Flag<["-"], "gdwarf64">, Group, Visibility<[ClangOption, CC1Option, CC1AsOption]>, HelpText<"Enables DWARF64 format for ELF binaries, if debug information emission is enabled.">, @@ -7626,6 +7629,8 @@ def debug_info_kind_EQ : Joined<["-"], "debug-info-kind=">; def record_command_line : Separate<["-"], "record-command-line">, HelpText<"The string to embed in the .LLVM.command.line section.">, MarshallingInfoString>; +def dwarf_version_EQ : Joined<["-"], "dwarf-version=">, + MarshallingInfoInt>; } // let Visibility = [CC1Option, CC1AsOption, FC1Option] @@ -7637,8 +7642,6 @@ def debug_info_macro : Flag<["-"], "debug-info-macro">, def default_function_attr : Separate<["-"], "default-function-attr">, HelpText<"Apply given attribute to all functions">, MarshallingInfoStringVector>; -def dwarf_version_EQ : Joined<["-"], "dwarf-version=">, - MarshallingInfoInt>; def debugger_tuning_EQ : Joined<["-"], "debugger-tuning=">, Values<"gdb,lldb,sce,dbx">, NormalizedValuesScope<"llvm::DebuggerKind">, NormalizedValues<["GDB", "LLDB", "SCE", "DBX"]>, diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 1535f4cebf436..2118c67dedc54 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -134,12 +134,17 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { if (Args.hasArg(options::OPT_gN_Group)) { Arg *gNArg = Args.getLastArg(options::OPT_gN_Group); DebugInfoKind = debugLevelToInfoKind(*gNArg); - } else if (Args.hasArg(options::OPT_g_Flag)) { + } else if (Args.hasArg(options::OPT_g_Group)) { DebugInfoKind = llvm::codegenoptions::FullDebugInfo; } else { DebugInfoKind = llvm::codegenoptions::NoDebugInfo; } addDebugInfoKind(CmdArgs, DebugInfoKind); + if (getDwarfNArg(Args)) { + const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args); + CmdArgs.push_back( + Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion))); + } } void Flang::addCodegenOptions(const ArgList &Args, diff --git a/flang/include/flang/Frontend/CodeGenOptions.def b/flang/include/flang/Frontend/CodeGenOptions.def index cdeea93c9aecb..22f778c5d3d38 100644 --- a/flang/include/flang/Frontend/CodeGenOptions.def +++ b/flang/include/flang/Frontend/CodeGenOptions.def @@ -46,6 +46,7 @@ CODEGENOPT(InterchangeLoops, 1, 0) ///< Enable loop interchange. CODEGENOPT(LoopVersioning, 1, 0) ///< Enable loop versioning. CODEGENOPT(UnrollLoops, 1, 0) ///< Enable loop unrolling CODEGENOPT(AliasAnalysis, 1, 0) ///< Enable alias analysis pass +CODEGENOPT(DwarfVersion, 3, 0) ///< Dwarf version CODEGENOPT(Underscoring, 1, 1) ENUM_CODEGENOPT(RelocationModel, llvm::Reloc::Model, 3, llvm::Reloc::PIC_) ///< Name of the relocation model to use. diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 6295a58b1bdad..c7dbfc46f432c 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -157,6 +157,10 @@ static bool parseDebugArgs(Fortran::frontend::CodeGenOptions &opts, clang::DiagnosticsEngine::Warning, "Unsupported debug option: %0"); diags.Report(debugWarning) << arg->getValue(); } + // The default value of 2 here is to match clang. + opts.DwarfVersion = + getLastArgIntValue(args, clang::driver::options::OPT_dwarf_version_EQ, + /*Default=*/2, diags); } return true; } diff --git a/flang/test/Driver/flang-dwarf-version.f90 b/flang/test/Driver/flang-dwarf-version.f90 new file mode 100644 index 0000000000000..dc69140a7eda1 --- /dev/null +++ b/flang/test/Driver/flang-dwarf-version.f90 @@ -0,0 +1,24 @@ +// RUN: %flang -### -S %s -g -gdwarf-5 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s +// RUN: %flang -### -S %s -gdwarf-5 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-DWARF5 %s +// RUN: %flang -### -S %s -g1 -gdwarf-5 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-WITH-G1-DWARF5 %s +// RUN: %flang -### -S %s -gdwarf-4 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-DWARF4 %s +// RUN: %flang -### -S %s -gdwarf-3 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-DWARF3 %s +// RUN: %flang -### -S %s -gdwarf-2 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-DWARF2 %s + +// CHECK-DWARF5: -debug-info-kind=standalone +// CHECK-DWARF5-SAME: -dwarf-version=5 + +// CHECK-WITH-G1-DWARF5: -debug-info-kind=line-tables-only +// CHECK-WITH-G1-DWARF5-SAME: -dwarf-version=5 + +// CHECK-DWARF4: -dwarf-version=4 + +// CHECK-DWARF3: -dwarf-version=3 + +// CHECK-DWARF2: -dwarf-version=2