Page MenuHomePhabricator

Initial implementation of -fmacro-prefix-map and -ffile-prefix-map
ClosedPublic

Authored by dankm on Jul 17 2018, 8:51 PM.

Details

Summary

GCC 8 implements -fmacro-prefix-map. Like -fdebug-prefix-map, it replaces a string prefix for the FILE macro.
-ffile-prefix-map is the union of -fdebug-prefix-map and -fmacro-prefix-map

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
Lekensteyn added inline comments.Jan 12 2019, 4:52 PM
lib/Driver/ToolChains/Clang.cpp
612 ↗(On Diff #156003)

Wouldn't using if (...) { D.diag(...); continue; } also skip the A->claim() call? Presumably that could result in spurious errors as well about unused arguments?

This revision is now accepted and ready to land.Jan 12 2019, 4:52 PM
dankm updated this revision to Diff 181562.Jan 14 2019, 8:07 AM

Restored original test case file names.

dankm marked 3 inline comments as done.Jan 14 2019, 8:09 AM
dankm added inline comments.
lib/Driver/ToolChains/Clang.cpp
612 ↗(On Diff #156003)

It would, and did. I had @joerg's suggestion in an earlier patch on this review.

Lekensteyn accepted this revision.Jan 14 2019, 8:25 AM

Still fine by me, thanks!

As for the commit message, perhaps reference:
https://bugs.llvm.org/show_bug.cgi?id=38135

joerg added a comment.Jan 15 2019, 4:41 AM

As discussed with dankm on IRC, I still would like to see the correct behavior going into 8.0, i.e. not change it later. Since this also matters for potential faster implementations later, it seems like a good idea to do it now. The changes are well-localized.

(1) Do path prefix matching and not string prefix matching. The difference is that the file name must be longer than the prefix and the prefix must be followed by a path separator.
(2) The longest prefix match wins. Substituation is applied only once per file name, independent of the rules. This gives more predictable output and allows switching to a tree-lookup later.

dankm updated this revision to Diff 181964.Jan 15 2019, 7:23 PM
dankm marked an inline comment as done.

Enforce path mapping. This requires LLVM review D56769.

Changes still look reasonable, but the preceding path (https://reviews.llvm.org/D56769) needs some work.

lib/CodeGen/CGDebugInfo.cpp
607 ↗(On Diff #181964)

Any reason for dropping remapDIPath here? Wouldn't this result in the full path being included even when using:

clang -fdebug-prefix-map=/full/path/= /full/path/source.c
lib/Lex/PPMacroExpansion.cpp
1466 ↗(On Diff #181964)

Style: space between if and (

dankm marked an inline comment as done.Jan 16 2019, 6:49 AM
I'll update the style nit, and spend some non-tired time on the string remapping. Thanks
lib/CodeGen/CGDebugInfo.cpp
607 ↗(On Diff #181964)

Whoops. That probably shouldn't have been included this round. This is a bugfix. MainFileName is already remapped from earlier in this function, this keeps it from remapping twice if you have an empty old prefix.

dankm updated this revision to Diff 182037.Jan 16 2019, 6:51 AM

Update style.

Lekensteyn added inline comments.Jan 16 2019, 7:30 AM
lib/CodeGen/CGDebugInfo.cpp
607 ↗(On Diff #181964)

The remapping was done here:

c
  std::string MainFileDir;
  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
    MainFileDir = remapDIPath(MainFile->getDir()->getName());

(Observation: the declaration could probably be moved inside the if block since it is not used outside.)

What about the second case though? For example, assume /tmp/testdir/mytest.ii:

# 1 "/tmp/mytest.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "/tmp/mytest.c"
int main(int argc, const char *argv[])
{
    return 0;
}

What happens if you now compile with clang -fdebug-prefix-map=/tmp/=/bla/ /tmp/testdir/mytest.ii from /tmp/testdir?

Unless this affects the current patch, consider moving it to a separate change.

dankm updated this revision to Diff 182047.Jan 16 2019, 7:44 AM

Undo accidental change.

dankm marked an inline comment as done.Jan 16 2019, 7:45 AM

Sure, I'll (eventually) make a separate review.

dankm updated this revision to Diff 182121.Jan 16 2019, 12:32 PM

Move trailing path separator stripping back to Clang.

Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2019, 10:00 AM
Herald added a subscriber: jdoerfert. · View Herald Transcript
raj.khem added inline comments.Feb 15 2019, 11:27 AM
lib/CodeGen/CGDebugInfo.cpp
476 ↗(On Diff #182121)

looking at llvm/lib/Support/Path.cpp replace_path_prefix() returns void but here inside if() it will expect a bool return value

raj.khem added inline comments.Feb 15 2019, 11:34 AM
lib/CodeGen/CGDebugInfo.cpp
476 ↗(On Diff #182121)

nm I guess I needed to look into https://reviews.llvm.org/D56769 as well.

Hi @dankm, any progress on this feature? The proposed branch off date for Clang 9.0.0 is 18 July 2019: https://lists.llvm.org/pipermail/cfe-dev/2019-June/062628.html

E5ten added a subscriber: E5ten.Jul 3 2019, 9:57 PM

@dankm are you still working on this patch?

dankm updated this revision to Diff 212723.Jul 31 2019, 9:36 PM

Latest changes. I've been sitting on these for months, so I don't remember all that changed. The path remapping contract changed somewhat, and it's now based on the git monorepo.

Herald added a project: Restricted Project. · View Herald TranscriptJul 31 2019, 9:36 PM
dankm added a comment.Jul 31 2019, 9:37 PM

@dankm are you still working on this patch?

Yes, I've been afk for a bit due to family circumstances, but I just uploaded more.

Thanks for picking this up again. I've left some nitpicks below in a quick review.

The "strict" parameter is not precisely defined, if that is fixed I think this would be ready for merge.

clang/test/Driver/debug-prefix-map.c
8

What about combining these two tests? The command is the same, maybe you could have a new -check-prefix to reduce the number of invocations? Likewise for the cases below.

llvm/include/llvm/Support/Path.h
174

"strict checking" is ambiguous on its own. What about something like:

If strict is true, a directory separator following \a OldPrefix will also be stripped. Otherwise, directory separators will only be matched and stripped when present in \a OldPrefix.

Or whatever semantics you would like to assign to "strict mode".

186

Why have a variant with the parameters swapped, is it common in LLVM to have such convenience wrappers?

Why not require callers to pass Style::native whenever they want to modify "strict"?

llvm/lib/Support/Path.cpp
512

this condition is duplicated above

phosek added a subscriber: phosek.Sep 11 2019, 4:18 PM

@dankm do you still plan to work on this? We would really like to see this landed and we could help if needed.

It'd be nice to see this land.

@dankm is it OK if we take over this change to push it forward?

aganea added a subscriber: aganea.Nov 12 2019, 9:10 AM
dankm marked 2 inline comments as done.Nov 17 2019, 3:15 PM
dankm added inline comments.
llvm/include/llvm/Support/Path.h
174

Thanks. I like your wording, and I've used it in the incoming patch.

186

Works for me. I did that to make my call sites somewhat more readable. It'll be changed too.

dankm marked an inline comment as done.Nov 18 2019, 6:52 AM
dankm added inline comments.
clang/test/Driver/debug-prefix-map.c
8

The tests will need more thinking, you're probably right, but I don't have much time to work on this at the moment. How do you feel about addressing this in the future?

dankm updated this revision to Diff 229835.Nov 18 2019, 6:54 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 18 2019, 6:54 AM
dankm added a comment.Nov 18 2019, 6:56 AM

Whoops. There are extra changes here I didn't mean to submit :/

dankm updated this revision to Diff 229837.Nov 18 2019, 7:00 AM
dankm added a comment.Nov 18 2019, 7:03 AM

@dankm is it OK if we take over this change to push it forward?

At this point sure. Unless it's accepted as-is now, then I don't have a commit bit to finish it off.

Ping?

The tests need fixing... I can commit it. Now that we've migrated to the llvm monorepo, the git commit message can retain the author info properly...

MaskRay updated this revision to Diff 231140.Nov 26 2019, 3:06 PM

Add back remapDIPath that was unintentionally deleted by D69213, caught by a test.

Small adjustment of the code

MaskRay updated this revision to Diff 231142.Nov 26 2019, 3:16 PM

Minimize diff in Options.td
Properly rebase remapDIPath on top of D69213

MaskRay accepted this revision.Nov 26 2019, 3:18 PM
This revision was automatically updated to reflect the committed changes.
dankm added a comment.Nov 26 2019, 3:41 PM

Add back remapDIPath that was unintentionally deleted by D69213, caught by a test.

Small adjustment of the code

Right. Can't believe I missed that. Thanks.

There's still one failing test on Windows after the fix attempt: http://45.33.8.238/win/3052/step_6.txt

Please take a look and revert if it's not an easy fix. (And please watch bots after committing stuff.)

There's still one failing test on Windows after the fix attempt: http://45.33.8.238/win/3052/step_6.txt

Please take a look and revert if it's not an easy fix. (And please watch bots after committing stuff.)

XFAIL'ed clang/test/Preprocessor/file_test.c. I didn't receive an email from http://45.33.8.238/win/3052/step_6.txt not sure if that was because the author email was @dankm's, not mine. If there is a console. please tell me :)

MaskRay added a comment.EditedNov 26 2019, 8:13 PM

Does this work on Windows?

--- i/clang/test/Preprocessor/file_test.c
+++ w/clang/test/Preprocessor/file_test.c
@@ -1,8 +1,7 @@
-// XFAIL: system-windows
 // RUN: %clang -E -ffile-prefix-map=%p=/UNLIKELY_PATH/empty -c -o - %s | FileCheck %s
 // RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH/empty -c -o - %s | FileCheck %s
 // RUN: %clang -E -fmacro-prefix-map=%p=/UNLIKELY_PATH=empty -c -o - %s | FileCheck %s -check-prefix CHECK-EVIL
-// RUN: %clang -E -fmacro-prefix-map=%p/= -c -o - %s | FileCheck %s --check-prefix CHECK-REMOVE
+// RUN: %clang -E -fmacro-prefix-map=%/p/= -c -o - %/s | FileCheck %s --check-prefix CHECK-REMOVE

startswith is not ideal because /t will match /tmp. However, gcc appears to use something similar to startswith, see:

% gcc -c -g -fdebug-prefix-map=/t=x /tmp/c/a.c -o /tmp/c/a.o
% llvm-dwarfdump /tmp/c/a.o | grep -m 1 DW_AT_name
              DW_AT_name        ("xmp/c/a.c")

The ugly path separator pattern {{(/|\\\\)}} appears in 60+ tests. Can we teach clang and other tools to

  1. accept both / and \ input
  2. but only output /

on Windows? We can probably remove llvm::sys::path::Style::{windows,posix,native} from include/llvm/Support/Path.h and only keep the posix form.

The ugly path separator pattern {{(/|\\\\)}} appears in 60+ tests. Can we teach clang and other tools to

  1. accept both / and \ input

In general they do, AFAIK, although it's not feasible in cases where / is the character that introduces an option, which is common on standard Windows utilities.

  1. but only output /

    on Windows?

This is often actually incorrect for Windows.

We can probably remove llvm::sys::path::Style::{windows,posix,native} from include/llvm/Support/Path.h and only keep the posix form.

It's highly unlikely that will be correct for all cases, and certainly will not match users' expectations. Debug info, for example, will want normal Windows syntax file paths with \.