[PATCH] Don't segfault in EmitCXXGlobalInitFunc when main file is a membuf

Keno Fischer kfischer at college.harvard.edu
Sun Aug 24 20:33:40 PDT 2014


Hi majnemer, thakis,

When the main file is created from a membuffer, there is no file entry that can be retrieved. This uses "__GLOBAL_I_a" in that case which is what was always used before r208128.

http://reviews.llvm.org/D5043

Files:
  lib/CodeGen/CGDeclCXX.cpp

Index: lib/CodeGen/CGDeclCXX.cpp
===================================================================
--- lib/CodeGen/CGDeclCXX.cpp
+++ lib/CodeGen/CGDeclCXX.cpp
@@ -409,21 +409,26 @@
       AddGlobalCtor(Fn, Priority);
     }
   }
-  
-  // Include the filename in the symbol name. Including "sub_" matches gcc and
-  // makes sure these symbols appear lexicographically behind the symbols with
-  // priority emitted above.
-  SourceManager &SM = Context.getSourceManager();
-  SmallString<128> FileName(llvm::sys::path::filename(
-      SM.getFileEntryForID(SM.getMainFileID())->getName()));
-  for (size_t i = 0; i < FileName.size(); ++i) {
-    // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
-    // to be the set of C preprocessing numbers.
-    if (!isPreprocessingNumberBody(FileName[i]))
-      FileName[i] = '_';
+
+  llvm::Function *Fn = nullptr;
+  SourceManager &SM = Context.getSourceManager();
+  if (SM.getFileEntryForID(SM.getMainFileID())) {
+    // Include the filename in the symbol name. Including "sub_" matches gcc and
+    // makes sure these symbols appear lexicographically behind the symbols with
+    // priority emitted above.
+    SmallString<128> FileName(llvm::sys::path::filename(
+        SM.getFileEntryForID(SM.getMainFileID())->getName()));
+    for (size_t i = 0; i < FileName.size(); ++i) {
+      // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
+      // to be the set of C preprocessing numbers.
+      if (!isPreprocessingNumberBody(FileName[i]))
+        FileName[i] = '_';
+    }
+    Fn = CreateGlobalInitOrDestructFunction(
+        *this, FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
+  } else {
+    Fn = CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
   }
-  llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
-      *this, FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
 
   CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
   AddGlobalCtor(Fn);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5043.12888.patch
Type: text/x-patch
Size: 1996 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140825/8a7b224c/attachment.bin>


More information about the cfe-commits mailing list