Repository: llvm/llvm-project
File: clang/test/ClangScanDeps/modules-symlink-dir-from-module.c (C)

Code before:
// Check that the path of an imported modulemap file is not influenced by
// modules outside that module's dependency graph. Specifically, the "Foo"
// module below does not transitively import Mod via a symlink, so it should not
// see the symlinked path.

// REQUIRES: symlinks

// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
// RUN: ln -s module %t/include/symlink-to-module

// RUN: clang-scan-deps -compilation-database %t/cdb.json -j 1 \
// RUN:   -format experimental-full  -mode=preprocess-dependency-directives \
// RUN:   -optimize-args=all -module-files-dir %t/build > %t/deps.json

// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t

// CHECK: "modules": [
// CHECK:   {
// CHECK:     "command-line": [
// CHECK-NOT: ]
// CHECK:       "-fmodule-map-file=[[PREFIX]]/include/module/module.modulemap"
// CHECK:     ]
// CHECK:     "name": "Foo"
// CHECK:   }

//--- cdb.json.in
[{
  "directory": "DIR",
  "command": "clang -fsyntax-only DIR/test.c -F DIR/Frameworks -I DIR/include -fmodules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache",
  "file": "DIR/test.c"
}]

//--- include/module/module.modulemap
module Mod { header "mod.h" export * }

//--- include/module/mod.h

Review comment (bug):
You may be wondering why this test was changed. This was my not-so-great way of handling a problem where the module has different modulemaps through a symlink, which triggers the relocation check to fail within a single compilation. This basically skips relocation checks in a single compilation. This also passes fine in a normal implicit module build because the modulemap files get resolved through inode resolution.

I initially thought this was an invalid use, but I was convinced otherwise, given that it seems pretty easy to end up in this state as a client of external interconnected libraries like an SDK. 

When `clang-scan-deps` scans a TU that reaches the same module map file via two different include paths: 
1. canonical (include/module/module.modulemap) and 
2. through a symlinked directory (include/symlink-to-module/module.modulemap)
 
It builds the affected module `Mod` twice with different context hashes, because `ClangModuleMapFile` in `ModuleDepCollector` is set from `FileEntryRef::getNameAsRequested()`, which returns the path as it was first requested rather than a stable canonical path.
The two PCMs then conflict during the scan. When `Foo_Private` is loaded and its imported `Foo` pcm references the canonical path for  `Mod`'s pcm, `ReadModuleMapFileBlock` in ASTReader calls `lookupModule("Mod")` to validate it. But `Mod`'s `Module*` object was populated from the
  symlink-path PCM, and its DefinitionLoc has no `FileID` in the current `SourceManager` because the canonical module map was never parsed into it. `getModuleMapFileForUniquing` returns null, and the relocation check fires.

This touches on the abstraction layers of the scanner's vfs that I don't have good suggestions for at the moment.

Code after:
// Check that the path of an imported modulemap file is not influenced by
// modules outside that module's dependency graph. Specifically, the "Foo"
// module below does not transitively import Mod via a symlink, so it should not
// see the symlinked path.

// REQUIRES: symlinks

// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.in > %t/cdb.json
// RUN: ln -s module %t/include/symlink-to-module

// RUN: touch %t/session.timestamp

// RUN: clang-scan-deps -compilation-database %t/cdb.json -j 1 \
// RUN:   -format experimental-full  -mode=preprocess-dependency-directives \
// RUN:   -optimize-args=all -module-files-dir %t/build > %t/deps.json

// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t

// CHECK: "modules": [
// CHECK:   {
// CHECK:     "command-line": [
// CHECK-NOT: ]
// CHECK:       "-fmodule-map-file=[[PREFIX]]/include/module/module.modulemap"
// CHECK:     ]
// CHECK:     "name": "Foo"
// CHECK:   }

//--- cdb.json.in
[{
  "directory": "DIR",
  "command": "clang -fsyntax-only DIR/test.c -F DIR/Frameworks -I DIR/include -fmodules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache -fbuild-session-file=DIR/session.timestamp -fmodules-validate-once-per-build-session",
  "file": "DIR/test.c"
}]

//--- include/module/module.modulemap
module Mod { header "mod.h" export * }