This is an archive of the discontinued LLVM Phabricator instance.

Don't segfault in EmitCXXGlobalInitFunc when main file is a membuf
ClosedPublic

Authored by loladiro on Aug 24 2014, 8:33 PM.

Details

Summary

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.

Diff Detail

Event Timeline

loladiro updated this revision to Diff 12888.Aug 24 2014, 8:33 PM
loladiro retitled this revision from to Don't segfault in EmitCXXGlobalInitFunc when main file is a membuf.
loladiro updated this object.
loladiro edited the test plan for this revision. (Show Details)
loladiro added reviewers: majnemer, thakis.
loladiro set the repository for this revision to rL LLVM.
loladiro added a subscriber: Unknown Object (MLST).
thakis edited edge metadata.Aug 24 2014, 8:49 PM

Is it possible to test this?

I think if the input comes from stdin, sys::path::filename() returns <stdin>, which then gets turned into _stdin_ by the isPreprocessingNumberBody() loop – maybe this should be <null> / _null_ instead of the old _a spelling? (I don't care too much about this part.)

rsmith added a subscriber: rsmith.Aug 24 2014, 10:02 PM
rsmith added inline comments.
lib/CodeGen/CGDeclCXX.cpp
415
if (FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
420
MainFile->getName()));
loladiro updated this revision to Diff 12909.Aug 25 2014, 12:32 PM
loladiro edited edge metadata.

Fixed minor nits, changed GLOBAL_I_a to _GLOBALsub_I__null_ and added a test. I'd appreciate if somebody who uses CMake to run the unittests could test this. I tried to modify the CMakeLists by pattern matching, but haven't actually tested this.

I patched this in and tried it with CMake, it mostly works (with the capital G mentioned below). I also checked that it runs as part of ninja check-clang. lgtm with comments addressed.

lib/CodeGen/CGDeclCXX.cpp
430

Nit: I'd just do

- SmallString<128> FileName(llvm::sys::path::filename(
-      SM.getFileEntryForID(SM.getMainFileID())->getName()));
+  SmallString<128> FileName;
+  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
+    FileName = llvm::sys::path::filename(MainFile->getName());
+  else
+    FileName = "<null>";

and nothing else, then you don't duplicate the CreateGlobalInitOrDestructFunction call.

unittests/CodeGen/BufferSourceTest.cpp
33 ↗(On Diff #12909)

const char TestProgram[] =

42 ↗(On Diff #12909)

Does anything own this? I think this should be stack-allocated. (?)

unittests/CodeGen/CMakeLists.txt
16 ↗(On Diff #12909)

This has to be clangCodeGen (note capital G). Maybe it should be CodeGenTests for ConsistenCy.

(I left a comment on this on phab a while ago, but I haven't received an
email for that yet. Not sure if phab's mailer is slow. The comment does
show up for me on the web interface.)

loladiro updated this revision to Diff 12970.Aug 26 2014, 2:44 PM

Addressed review comments.

thakis accepted this revision.Aug 26 2014, 3:04 PM
thakis edited edge metadata.

lgtm

This revision is now accepted and ready to land.Aug 26 2014, 3:04 PM
Diffusion closed this revision.Aug 26 2014, 3:19 PM
Diffusion updated this revision to Diff 12973.

Closed by commit rL216495 (authored by kfischer).