Skip to content

Commit f58330c

Browse files
committed
[Driver] Make /Zi and /Z7 aliases of -g rather than handling them specially
The -g flag has been selecting whether to emit dwarf or codeview based on the target ABI since 2018, so simply aliasing these flags does the right thing for clang-cl. This moves some code from Clang::ConstructJob to renderDebugOptions to make things a little clearer now that we don't need to keep track of whether we're doing codeview or not in multiple places, and also combines the duplicate handling of the cl vs clang handling of jmc flags as a result. This is mostly NFC, but some -cc1 flags may be rendered in a slightly different order because of the code that was moved around. Differential Revision: https://reviews.llvm.org/D157794
1 parent c4ada13 commit f58330c

File tree

5 files changed

+45
-78
lines changed

5 files changed

+45
-78
lines changed

clang/include/clang/Driver/Options.td

+4-4
Original file line numberDiff line numberDiff line change
@@ -7182,7 +7182,7 @@ def _SLASH_Zc_wchar_t : CLFlag<"Zc:wchar_t">,
71827182
HelpText<"Enable C++ builtin type wchar_t (default)">;
71837183
def _SLASH_Zc_wchar_t_ : CLFlag<"Zc:wchar_t-">,
71847184
HelpText<"Disable C++ builtin type wchar_t">;
7185-
def _SLASH_Z7 : CLFlag<"Z7">,
7185+
def _SLASH_Z7 : CLFlag<"Z7">, Alias<g_Flag>,
71867186
HelpText<"Enable CodeView debug information in object files">;
71877187
def _SLASH_ZH_MD5 : CLFlag<"ZH:MD5">,
71887188
HelpText<"Use MD5 for file checksums in debug info (default)">,
@@ -7193,7 +7193,7 @@ def _SLASH_ZH_SHA1 : CLFlag<"ZH:SHA1">,
71937193
def _SLASH_ZH_SHA_256 : CLFlag<"ZH:SHA_256">,
71947194
HelpText<"Use SHA256 for file checksums in debug info">,
71957195
Alias<gsrc_hash_EQ>, AliasArgs<["sha256"]>;
7196-
def _SLASH_Zi : CLFlag<"Zi">, Alias<_SLASH_Z7>,
7196+
def _SLASH_Zi : CLFlag<"Zi">, Alias<g_Flag>,
71977197
HelpText<"Like /Z7">;
71987198
def _SLASH_Zp : CLJoined<"Zp">,
71997199
HelpText<"Set default maximum struct packing alignment">,
@@ -7261,9 +7261,9 @@ def _SLASH_GX_ : CLFlag<"GX-">,
72617261
def _SLASH_imsvc : CLJoinedOrSeparate<"imsvc">,
72627262
HelpText<"Add <dir> to system include search path, as if in %INCLUDE%">,
72637263
MetaVarName<"<dir>">;
7264-
def _SLASH_JMC : CLFlag<"JMC">,
7264+
def _SLASH_JMC : CLFlag<"JMC">, Alias<fjmc>,
72657265
HelpText<"Enable just-my-code debugging">;
7266-
def _SLASH_JMC_ : CLFlag<"JMC-">,
7266+
def _SLASH_JMC_ : CLFlag<"JMC-">, Alias<fno_jmc>,
72677267
HelpText<"Disable just-my-code debugging (default)">;
72687268
def _SLASH_LD : CLFlag<"LD">, HelpText<"Create DLL">;
72697269
def _SLASH_LDd : CLFlag<"LDd">, HelpText<"Create debug DLL">;

clang/lib/Driver/ToolChains/Clang.cpp

+36-61
Original file line numberDiff line numberDiff line change
@@ -4204,8 +4204,8 @@ static void renderDwarfFormat(const Driver &D, const llvm::Triple &T,
42044204

42054205
static void
42064206
renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
4207-
const ArgList &Args, bool EmitCodeView, bool IRInput,
4208-
ArgStringList &CmdArgs,
4207+
const ArgList &Args, bool IRInput, ArgStringList &CmdArgs,
4208+
const InputInfo &Output,
42094209
llvm::codegenoptions::DebugInfoKind &DebugInfoKind,
42104210
DwarfFissionKind &DwarfFission) {
42114211
if (Args.hasFlag(options::OPT_fdebug_info_for_profiling,
@@ -4282,6 +4282,7 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
42824282
if (const Arg *A = getDwarfNArg(Args))
42834283
EmitDwarf = checkDebugInfoOption(A, Args, D, TC);
42844284

4285+
bool EmitCodeView = false;
42854286
if (const Arg *A = Args.getLastArg(options::OPT_gcodeview))
42864287
EmitCodeView = checkDebugInfoOption(A, Args, D, TC);
42874288

@@ -4518,6 +4519,33 @@ renderDebugOptions(const ToolChain &TC, const Driver &D, const llvm::Triple &T,
45184519

45194520
renderDwarfFormat(D, T, Args, CmdArgs, EffectiveDWARFVersion);
45204521
RenderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
4522+
4523+
// This controls whether or not we perform JustMyCode instrumentation.
4524+
if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) {
4525+
if (TC.getTriple().isOSBinFormatELF() || D.IsCLMode()) {
4526+
if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor)
4527+
CmdArgs.push_back("-fjmc");
4528+
else if (D.IsCLMode())
4529+
D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC"
4530+
<< "'/Zi', '/Z7'";
4531+
else
4532+
D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc"
4533+
<< "-g";
4534+
} else {
4535+
D.Diag(clang::diag::warn_drv_fjmc_for_elf_only);
4536+
}
4537+
}
4538+
4539+
// Add in -fdebug-compilation-dir if necessary.
4540+
const char *DebugCompilationDir =
4541+
addDebugCompDirArg(Args, CmdArgs, D.getVFS());
4542+
4543+
addDebugPrefixMapArg(D, TC, Args, CmdArgs);
4544+
4545+
// Add the output path to the object file for CodeView debug infos.
4546+
if (EmitCodeView && Output.isFilename())
4547+
addDebugObjectName(Args, CmdArgs, DebugCompilationDir,
4548+
Output.getFilename());
45214549
}
45224550

45234551
static void ProcessVSRuntimeLibrary(const ArgList &Args,
@@ -5697,33 +5725,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56975725

56985726
RenderTargetOptions(Triple, Args, KernelOrKext, CmdArgs);
56995727

5700-
// These two are potentially updated by AddClangCLArgs.
5701-
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
5702-
llvm::codegenoptions::NoDebugInfo;
5703-
bool EmitCodeView = false;
5704-
57055728
// Add clang-cl arguments.
57065729
types::ID InputType = Input.getType();
57075730
if (D.IsCLMode())
5708-
AddClangCLArgs(Args, InputType, CmdArgs, &DebugInfoKind, &EmitCodeView);
5731+
AddClangCLArgs(Args, InputType, CmdArgs);
57095732

5733+
llvm::codegenoptions::DebugInfoKind DebugInfoKind =
5734+
llvm::codegenoptions::NoDebugInfo;
57105735
DwarfFissionKind DwarfFission = DwarfFissionKind::None;
5711-
renderDebugOptions(TC, D, RawTriple, Args, EmitCodeView,
5712-
types::isLLVMIR(InputType), CmdArgs, DebugInfoKind,
5713-
DwarfFission);
5714-
5715-
// This controls whether or not we perform JustMyCode instrumentation.
5716-
if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) {
5717-
if (TC.getTriple().isOSBinFormatELF()) {
5718-
if (DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor)
5719-
CmdArgs.push_back("-fjmc");
5720-
else
5721-
D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "-fjmc"
5722-
<< "-g";
5723-
} else {
5724-
D.Diag(clang::diag::warn_drv_fjmc_for_elf_only);
5725-
}
5726-
}
5736+
renderDebugOptions(TC, D, RawTriple, Args, types::isLLVMIR(InputType),
5737+
CmdArgs, Output, DebugInfoKind, DwarfFission);
57275738

57285739
// Add the split debug info name to the command lines here so we
57295740
// can propagate it to the backend.
@@ -6076,12 +6087,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60766087
if (!ShouldEnableAutolink(Args, TC, JA))
60776088
CmdArgs.push_back("-fno-autolink");
60786089

6079-
// Add in -fdebug-compilation-dir if necessary.
6080-
const char *DebugCompilationDir =
6081-
addDebugCompDirArg(Args, CmdArgs, D.getVFS());
6082-
6083-
addDebugPrefixMapArg(D, TC, Args, CmdArgs);
6084-
60856090
Args.AddLastArg(CmdArgs, options::OPT_ftemplate_depth_EQ);
60866091
Args.AddLastArg(CmdArgs, options::OPT_foperator_arrow_depth_EQ);
60876092
Args.AddLastArg(CmdArgs, options::OPT_fconstexpr_depth_EQ);
@@ -7518,11 +7523,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
75187523
CmdArgs.push_back(Args.MakeArgString(Str));
75197524
}
75207525

7521-
// Add the output path to the object file for CodeView debug infos.
7522-
if (EmitCodeView && Output.isFilename())
7523-
addDebugObjectName(Args, CmdArgs, DebugCompilationDir,
7524-
Output.getFilename());
7525-
75267526
// Add the "-o out -x type src.c" flags last. This is done primarily to make
75277527
// the -cc1 command easier to edit when reproducing compiler crashes.
75287528
if (Output.getType() == types::TY_Dependencies) {
@@ -7808,9 +7808,7 @@ static EHFlags parseClangCLEHFlags(const Driver &D, const ArgList &Args) {
78087808
}
78097809

78107810
void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
7811-
ArgStringList &CmdArgs,
7812-
llvm::codegenoptions::DebugInfoKind *DebugInfoKind,
7813-
bool *EmitCodeView) const {
7811+
ArgStringList &CmdArgs) const {
78147812
bool isNVPTX = getToolChain().getTriple().isNVPTX();
78157813

78167814
ProcessVSRuntimeLibrary(Args, CmdArgs);
@@ -7836,31 +7834,8 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
78367834
CmdArgs.push_back(Args.MakeArgString(Twine(LangOptions::SSPStrong)));
78377835
}
78387836

7839-
// Emit CodeView if -Z7 or -gline-tables-only are present.
7840-
if (Arg *DebugInfoArg = Args.getLastArg(options::OPT__SLASH_Z7,
7841-
options::OPT_gline_tables_only)) {
7842-
*EmitCodeView = true;
7843-
if (DebugInfoArg->getOption().matches(options::OPT__SLASH_Z7))
7844-
*DebugInfoKind = llvm::codegenoptions::DebugInfoConstructor;
7845-
else
7846-
*DebugInfoKind = llvm::codegenoptions::DebugLineTablesOnly;
7847-
} else {
7848-
*EmitCodeView = false;
7849-
}
7850-
78517837
const Driver &D = getToolChain().getDriver();
78527838

7853-
// This controls whether or not we perform JustMyCode instrumentation.
7854-
if (Args.hasFlag(options::OPT__SLASH_JMC, options::OPT__SLASH_JMC_,
7855-
/*Default=*/false)) {
7856-
if (*EmitCodeView &&
7857-
*DebugInfoKind >= llvm::codegenoptions::DebugInfoConstructor)
7858-
CmdArgs.push_back("-fjmc");
7859-
else
7860-
D.Diag(clang::diag::warn_drv_jmc_requires_debuginfo) << "/JMC"
7861-
<< "'/Zi', '/Z7'";
7862-
}
7863-
78647839
EHFlags EH = parseClangCLEHFlags(D, Args);
78657840
if (!isNVPTX && (EH.Synch || EH.Asynch)) {
78667841
if (types::isCXX(InputType))

clang/lib/Driver/ToolChains/Clang.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ class LLVM_LIBRARY_VISIBILITY Clang : public Tool {
9090
RewriteKind rewrite) const;
9191

9292
void AddClangCLArgs(const llvm::opt::ArgList &Args, types::ID InputType,
93-
llvm::opt::ArgStringList &CmdArgs,
94-
llvm::codegenoptions::DebugInfoKind *DebugInfoKind,
95-
bool *EmitCodeView) const;
93+
llvm::opt::ArgStringList &CmdArgs) const;
9694

9795
mutable std::unique_ptr<llvm::raw_fd_ostream> CompilationDatabase = nullptr;
9896
void DumpCompilationDatabase(Compilation &C, StringRef Filename,

clang/test/Driver/cl-options.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -564,16 +564,10 @@
564564
// RUN: %clang_cl /Brepro /Brepro- /c '-###' -- %s 2>&1 | FileCheck -check-prefix=Brepro_ %s
565565
// Brepro_: "-mincremental-linker-compatible"
566566

567-
// This test was super sneaky: "/Z7" means "line-tables", but "-gdwarf" occurs
568-
// later on the command line, so it should win. Interestingly the cc1 arguments
569-
// came out right, but had wrong semantics, because an invariant assumed by
570-
// CompilerInvocation was violated: it expects that at most one of {gdwarfN,
571-
// line-tables-only} appear. If you assume that, then you can safely use
572-
// Args.hasArg to test whether a boolean flag is present without caring
573-
// where it appeared. And for this test, it appeared to the left of -gdwarf
574-
// which made it "win". This test could not detect that bug.
567+
// If We specify both /Z7 and -gdwarf we should get dwarf, not codeview.
575568
// RUN: %clang_cl /Z7 -gdwarf /c -### -- %s 2>&1 | FileCheck -check-prefix=Z7_gdwarf %s
576-
// Z7_gdwarf: "-gcodeview"
569+
// RUN: %clang_cl -gdwarf /Z7 /c -### -- %s 2>&1 | FileCheck -check-prefix=Z7_gdwarf %s
570+
// Z7_gdwarf-NOT: "-gcodeview"
577571
// Z7_gdwarf: "-debug-info-kind=constructor"
578572
// Z7_gdwarf: "-dwarf-version=
579573

clang/test/Driver/working-directory.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
// CHECK_NO_FILE: no such file or directory: 'no_such_file.cpp'
88

9+
// CHECK_WORKS: "-fdebug-compilation-dir={{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
910
// CHECK_WORKS: "-coverage-notes-file" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs{{/|\\\\}}pchfile.gcno"
1011
// CHECK_WORKS: "-working-directory" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
11-
// CHECK_WORKS: "-fdebug-compilation-dir={{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"

0 commit comments

Comments
 (0)