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

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.

Reviewed By: aganea

Differential Revision: https://reviews.llvm.org/D119913
This commit is contained in:
Jez Ng 2022-02-16 20:03:57 -05:00
parent a99989529e
commit 69297cf639
4 changed files with 9 additions and 5 deletions

View file

@ -10,6 +10,8 @@
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
#include "llvm/CodeGen/CommandFlags.h"
using namespace llvm;
using namespace lld;
@ -20,7 +22,11 @@ using namespace lld;
// state.
static CommonLinkerContext *lctx;
CommonLinkerContext::CommonLinkerContext() { lctx = this; }
CommonLinkerContext::CommonLinkerContext() {
lctx = this;
// Fire off the static initializations in CGF's constructor.
codegen::RegisterCodeGenFlags CGF;
}
CommonLinkerContext::~CommonLinkerContext() {
assert(lctx);

View file

@ -24,6 +24,7 @@
#include "lld/Common/Filesystem.h"
#include "lld/Common/Strings.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Support/SHA1.h"

View file

@ -12,6 +12,7 @@
#include "Symbols.h"
#include "UnwindInfoSection.h"
#include "lld/Common/CommonLinkerContext.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/TimeProfiler.h"

View file

@ -21,7 +21,6 @@
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/Support/StringSaver.h"
namespace llvm {
@ -42,9 +41,6 @@ public:
llvm::DenseMap<void *, SpecificAllocBase *> instances;
ErrorHandler e;
private:
llvm::codegen::RegisterCodeGenFlags cgf;
};
// Retrieve the global state. Currently only one state can exist per process,