Page MenuHomePhabricator

[Clang] Use -main-file-name for source filename if not set
ClosedPublic

Authored by the_jk on Sep 15 2019, 11:08 AM.

Details

Summary

-main-file-name is currently used to set the source name used in debug information.

If the source filename is "-" and -main-file-name is set, then use the filename also for source_filename and ModuleID of the output.

The argument is generally used outside the internal clang calls when running clang in a wrapper like icecc which gives the source via stdin but still wants to get a object file with the original source filename both in debug info and IR code.

Diff Detail

Event Timeline

the_jk created this revision.Sep 15 2019, 11:08 AM

Please upload patches with context (https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface)

I'm not terribly familiar with this part of clang, but my concern would be that overriding the PresumedInputFile here could have unintended side effects. How about simply overriding the source file name on the Module object that eventually gets created, if it is "-"? I believe that is in the CodeGeneratorImpl constructor, and the CodeGenOptions are available there.

the_jk updated this revision to Diff 222251.Sep 27 2019, 2:55 PM

Moved ModuleName change to CodeGeneratorImpl as suggested, agree that it seems to fit well there.

This revision is now accepted and ready to land.Sep 27 2019, 3:33 PM

I don't have commit access so I'd need someone to push this, thanks.

I don't have commit access so I'd need someone to push this, thanks.

I can commit for you today.

Great, thanks again.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptSep 30 2019, 8:04 AM

This is failing on my mac like so:

FAIL: Clang :: Frontend/stdin-input.c (1 of 1)
******************** TEST 'Clang :: Frontend/stdin-input.c' FAILED ********************
Script:
--
: 'RUN: at line 1';   cat /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c | /Users/thakis/src/llvm-project/out/gn/bin/clang -emit-llvm -g -S  -Xclang -main-file-name -Xclang test/foo.c -x c - -o - | /Users/thakis/src/llvm-project/out/gn/bin/FileCheck /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 1'
+ cat /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
+ /Users/thakis/src/llvm-project/out/gn/bin/clang -emit-llvm -g -S -Xclang -main-file-name -Xclang test/foo.c -x c - -o -
+ /Users/thakis/src/llvm-project/out/gn/bin/FileCheck /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
/Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c:5:11: error: CHECK: expected string not found in input
// CHECK: !1 = !DIFile(filename: "test/foo.c"
          ^
<stdin>:3:1: note: scanning from here
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
^
<stdin>:24:1: note: possible intended match here
!6 = !DIFile(filename: "test/foo.c", directory: "/Users/thakis/src/llvm-project/out/gn/obj/clang/test/Frontend")
^

--

********************
Testing Time: 0.06s
********************
Failing Tests (1):
    Clang :: Frontend/stdin-input.c

If I manually run bin/llvm-lit clang/test/Frontend/stdin-input.c it fails, but if I run the command it claims to be running it passes.

If I replace the FileCheck on the run line with > tmp.txt I see what the failing FileCheck is seeing:

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 10, i32 14]}
!1 = !{i32 2, !"Dwarf Version", i32 4}
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = !{i32 1, !"wchar_size", i32 4}
!4 = !{i32 7, !"PIC Level", i32 2}
!5 = distinct !DICompileUnit(language: DW_LANG_C99, file: !6, producer: "clang version 10.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !7, nameTableKind: None)
!6 = !DIFile(filename: "test/foo.c", directory: "/Users/thakis/src/llvm-project/out/gn/obj/clang/test/Frontend")

I'm guessing lit sets some env var that makes clang emit these additional values (?)

This is failing on my mac like so:

This should be fixed by r373237. I didn't get any email with buildbot failures, but someone mailed one to me manually. Not sure why it only fails sometimes.

FAIL: Clang :: Frontend/stdin-input.c (1 of 1)
******************** TEST 'Clang :: Frontend/stdin-input.c' FAILED ********************
Script:
--
: 'RUN: at line 1';   cat /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c | /Users/thakis/src/llvm-project/out/gn/bin/clang -emit-llvm -g -S  -Xclang -main-file-name -Xclang test/foo.c -x c - -o - | /Users/thakis/src/llvm-project/out/gn/bin/FileCheck /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 1'
+ cat /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
+ /Users/thakis/src/llvm-project/out/gn/bin/clang -emit-llvm -g -S -Xclang -main-file-name -Xclang test/foo.c -x c - -o -
+ /Users/thakis/src/llvm-project/out/gn/bin/FileCheck /Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c
/Users/thakis/src/llvm-project/clang/test/Frontend/stdin-input.c:5:11: error: CHECK: expected string not found in input
// CHECK: !1 = !DIFile(filename: "test/foo.c"
          ^
<stdin>:3:1: note: scanning from here
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
^
<stdin>:24:1: note: possible intended match here
!6 = !DIFile(filename: "test/foo.c", directory: "/Users/thakis/src/llvm-project/out/gn/obj/clang/test/Frontend")
^

--

********************
Testing Time: 0.06s
********************
Failing Tests (1):
    Clang :: Frontend/stdin-input.c

If I manually run bin/llvm-lit clang/test/Frontend/stdin-input.c it fails, but if I run the command it claims to be running it passes.

If I replace the FileCheck on the run line with > tmp.txt I see what the failing FileCheck is seeing:

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 10, i32 14]}
!1 = !{i32 2, !"Dwarf Version", i32 4}
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = !{i32 1, !"wchar_size", i32 4}
!4 = !{i32 7, !"PIC Level", i32 2}
!5 = distinct !DICompileUnit(language: DW_LANG_C99, file: !6, producer: "clang version 10.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !7, nameTableKind: None)
!6 = !DIFile(filename: "test/foo.c", directory: "/Users/thakis/src/llvm-project/out/gn/obj/clang/test/Frontend")

I'm guessing lit sets some env var that makes clang emit these additional values (?)

Yup, test is happy now, thanks!