[PATCH] D36487: Emit section information for extern variables.

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 31 16:45:55 PDT 2017

efriedma added inline comments.

Comment at: lib/CodeGen/CodeGenModule.cpp:2434
+    // Emit section information for extern variables.
+    if (D->hasExternalStorage() && !D->isThisDeclarationADefinition()) {
+      if (const SectionAttr *SA = D->getAttr<SectionAttr>())
eandrews wrote:
> efriedma wrote:
> > Why do you specifically check "D->hasExternalStorage() && !D->isThisDeclarationADefinition()", instead of just setting the section unconditionally?
> I noticed that you enter GetOrCreateLLVMGlobal( ) whenever the extern variable is declared as well as when it is defined. The flow of the program is different in each case. When the variable is defined, it also enters EmitGlobalVarDefinition( ). There is existing code handling section information here. I added the check in GetOrCreateLLVMGlobal( ) so the block gets skipped for variable definition, since its already handled elsewhere.
I would rather just call setSection unconditionally here, as opposed to trying to guess whether the global will eventually be defined.

I'm also sort of concerned the behavior here could be weird if a section attribute is added on a redeclaration (e.g. what happens if you write `extern int x; int y = &x; extern int x __attribute((section("foo")));`)... maybe we should emit a warning?


More information about the cfe-commits mailing list