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

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 16 17:05:16 PST 2022


Author: Jez Ng
Date: 2022-02-16T20:05:07-05:00
New Revision: 69297cf639044acf48dd5d9b39b95c54dd50561d

URL: https://github.com/llvm/llvm-project/commit/69297cf639044acf48dd5d9b39b95c54dd50561d
DIFF: https://github.com/llvm/llvm-project/commit/69297cf639044acf48dd5d9b39b95c54dd50561d.diff

LOG: [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

Added: 
    

Modified: 
    lld/Common/CommonLinkerContext.cpp
    lld/ELF/Writer.cpp
    lld/MachO/ICF.cpp
    lld/include/lld/Common/CommonLinkerContext.h

Removed: 
    


################################################################################
diff  --git a/lld/Common/CommonLinkerContext.cpp b/lld/Common/CommonLinkerContext.cpp
index 50ccbb37c7966..12f56bc10ec96 100644
--- a/lld/Common/CommonLinkerContext.cpp
+++ b/lld/Common/CommonLinkerContext.cpp
@@ -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);

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 35d8b01308f74..0282d7d6b5a78 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -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"

diff  --git a/lld/MachO/ICF.cpp b/lld/MachO/ICF.cpp
index f9dea4b861ac3..fa018f4d3ce13 100644
--- a/lld/MachO/ICF.cpp
+++ b/lld/MachO/ICF.cpp
@@ -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"
 

diff  --git a/lld/include/lld/Common/CommonLinkerContext.h b/lld/include/lld/Common/CommonLinkerContext.h
index 3954d38ded636..0627bbdc8bd87 100644
--- a/lld/include/lld/Common/CommonLinkerContext.h
+++ b/lld/include/lld/Common/CommonLinkerContext.h
@@ -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 @@ class CommonLinkerContext {
   llvm::DenseMap<void *, SpecificAllocBase *> instances;
 
   ErrorHandler e;
-
-private:
-  llvm::codegen::RegisterCodeGenFlags cgf;
 };
 
 // Retrieve the global state. Currently only one state can exist per process,


        


More information about the llvm-commits mailing list