llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang-cl /showIncludes reports wrong filename for symlinked files

Open zmodem opened this issue 4 years ago • 11 comments

This can come up in distributed build systems with content-addressed filesystems, where identical files (often empty files) end up hard-linked to the same file.

Consider:

$ touch /tmp/foo.h
$ ln /tmp/foo.h /tmp/bar.h
$ cat > /tmp/a.cc
#include "/tmp/foo.h"
#include "/tmp/bar.h"
void f();
$ clang-cl -c /tmp/a.cc -showIncludes
Note: including file: /tmp/foo.h
Note: including file: /tmp/foo.h

The second include line should refer to bar.h.

This is a problem for systems which rely on the output of /showIncludes to discover all compilation inputs.

Interestingly, the -MD option does not seem to have this problem:

$ clang -MD -c /tmp/a.cc && cat a.d     
a.o: /tmp/a.cc /tmp/foo.h /tmp/bar.h

This all seems related to the discussion in https://reviews.llvm.org/D135220#3862893 about FileManager merging files based on inode.

zmodem avatar Nov 01 '22 13:11 zmodem

@llvm/issue-subscribers-flang-frontend

llvmbot avatar Nov 01 '22 13:11 llvmbot

I debugged the difference between these:

  • We are tracking a separate FileID and FileInfo per include, but only one ContentCache for the file.
  • FileInfo::get mutates ContentCache::Filename. This is very similar to the issue with OrigEntry here.
  • -MD uses SourceManager::getNonBuiltinFilenameForID which uses ContentCache::Filename
  • /showIncludes uses SourceManager::getPresumedLoc which ultimately uses ContentCache->OrigEntry->getName(), which is not mutated since D135220.

It seems like either the filename/OrigEntry stuff should move up into FileInfo or we should be adding a separate ContentCache for the differently named entry instead of sharing one.

benlangmuir avatar Nov 01 '22 18:11 benlangmuir

IMO, the filename should move up into FileInfo.

dexonsmith avatar Nov 01 '22 19:11 dexonsmith

Thanks for investigating!

Is someone looking into moving the filename, or is it a "patches welcome" situation?

zmodem avatar Nov 02 '22 23:11 zmodem

Sketch of a fix: https://reviews.llvm.org/D137304. It may take me a little while to get back to this and finish the TODO items.

benlangmuir avatar Nov 03 '22 00:11 benlangmuir

@zmodem this issue is for hardlinked file instaed of symlinked file?

atetubou avatar Oct 26 '23 06:10 atetubou

@zmodem this issue is for hardlinked file instaed of symlinked file?

I don't remember the details. In the original bug where we hit this (internal b/256760757) it was hardlinks, but I think symlinks may have the same problem.

zmodem avatar Oct 26 '23 10:10 zmodem

Is this what https://reviews.llvm.org/D149187 was for (reverted in #71697 recently), or is that something else?

nico avatar Nov 28 '23 05:11 nico

No, this is something else.

Arthur's patch was for https://crbug.com/1182249 / https://github.com/ninja-build/ninja/issues/1330

zmodem avatar Dec 06 '23 13:12 zmodem

Same root issue as https://github.com/llvm/llvm-project/issues/26953? We hit this on AerynOS where the /usr tree is backed by a content-addressable store.

ReillyBrogan avatar Aug 12 '25 14:08 ReillyBrogan

https://github.com/llvm/llvm-project/pull/189475

keith avatar Mar 30 '26 20:03 keith