This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Don't include CommandFlags.h in CommonLinkerContext.h

Authored by int3 on Feb 15 2022, 8:45 PM.



Main motivation: including llvm/CodeGen/CommandFlags.h in
CommonLinkerContext.h means that the declaration of llvm::Reloc is
visible in any file that includes CommonLinkerContext.h. Since our
cpp files have both using namespace llvm and `using namespace
lld::macho`, this results in conflicts with lld::macho::Reloc.

I suppose we could put llvm::Reloc into a nested namespace, but in general,
I think we should avoid transitively including too many header files in
a very widely used header like CommonLinkerContext.h.

RegisterCodeGenFlags' ctor initializes a bunch of function-static
structures and does nothing else, so it should be fine to "initialize"
it as a temporary stack variable rather than as a file static.

Diff Detail

Event Timeline

int3 requested review of this revision.Feb 15 2022, 8:45 PM
int3 created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 8:45 PM

Ideally RegisterCodeGenFlags should be a function call not a class, since it only registers static cl::opts. I think you can simply remove cgf from CommonLinkerContext.h and create a short-lived llvm::codegen::RegisterCodeGenFlags object on the stack inside the CommonLinkerContext constructor definition.

Truly, D108850 does not solve yet the multi-threading issues with cl::opts. This is something I wanted to address later (I have a few patches ready). However in the meanwhile, the objective with D108850 was also to enable -Werror=global-constructors, at least in lldCommon and lldCOFF.

int3 updated this revision to Diff 409239.Feb 16 2022, 7:19 AM
int3 retitled this revision from [lld-macho] Make RegisterCodeFlags a file-scope static again to [lld-macho] Don't include CommandFlags.h in CommonLinkerContext.h.
int3 edited the summary of this revision. (Show Details)

init CGF on the stack

Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2022, 7:19 AM
aganea accepted this revision.Feb 16 2022, 7:57 AM

LGTM, thanks!

This revision is now accepted and ready to land.Feb 16 2022, 7:57 AM
This revision was landed with ongoing or failed builds.Feb 16 2022, 5:05 PM
This revision was automatically updated to reflect the committed changes.