diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -417,7 +417,8 @@ IsInHeaderMap = true; - auto FixupSearchPath = [&]() { + auto FixupSearchPathAndFindUsableModule = + [&](auto file) -> Optional { if (SearchPath) { StringRef SearchPathRef(getName()); SearchPath->clear(); @@ -427,6 +428,12 @@ RelativePath->clear(); RelativePath->append(Filename.begin(), Filename.end()); } + if (!HS.findUsableModuleForHeader( + &file.getFileEntry(), file.getFileEntry().getDir(), + RequestingModule, SuggestedModule, isSystemHeaderDirectory())) { + return None; + } + return file; }; // Check if the headermap maps the filename to a framework include @@ -437,12 +444,10 @@ Filename = StringRef(MappedName.begin(), MappedName.size()); Optional Result = HM->LookupFile(Filename, HS.getFileMgr()); if (Result) { - FixupSearchPath(); - return *Result; + return FixupSearchPathAndFindUsableModule(*Result); } } else if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest)) { - FixupSearchPath(); - return *Res; + return FixupSearchPathAndFindUsableModule(*Res); } return None; diff --git a/clang/test/Modules/Inputs/implicit-module-header-maps/a.h b/clang/test/Modules/Inputs/implicit-module-header-maps/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/implicit-module-header-maps/a.h @@ -0,0 +1,3 @@ +#ifdef FOO +#error foo +#endif diff --git a/clang/test/Modules/Inputs/implicit-module-header-maps/a.hmap.json b/clang/test/Modules/Inputs/implicit-module-header-maps/a.hmap.json new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/implicit-module-header-maps/a.hmap.json @@ -0,0 +1,7 @@ +{ + "mappings" : + { + "Before/Mapping.h" : "After/Mapping.h", + "After/Mapping.h" : "After/Mapping.h" + } +} diff --git a/clang/test/Modules/Inputs/implicit-module-header-maps/a.module.modulemap b/clang/test/Modules/Inputs/implicit-module-header-maps/a.module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/implicit-module-header-maps/a.module.modulemap @@ -0,0 +1,3 @@ +module a { + header "After/Mapping.h" +} diff --git a/clang/test/Modules/Inputs/implicit-module-header-maps/b.hmap.json b/clang/test/Modules/Inputs/implicit-module-header-maps/b.hmap.json new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/implicit-module-header-maps/b.hmap.json @@ -0,0 +1,6 @@ +{ + "mappings" : + { + "Before/Mapping.h" : "OUTPUTS_DIR/After/Mapping.h" + } +} diff --git a/clang/test/Modules/implicit-module-header-maps.cpp b/clang/test/Modules/implicit-module-header-maps.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/implicit-module-header-maps.cpp @@ -0,0 +1,29 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: cd %t +// +// RUN: %hmaptool write %S/Inputs/implicit-module-header-maps/a.hmap.json %t/hmap +// +// RUN: mkdir -p %t/After +// RUN: cp %S/Inputs/implicit-module-header-maps/a.h %t/After/Mapping.h +// RUN: cp %S/Inputs/implicit-module-header-maps/a.module.modulemap %t/module.modulemap +// +// RUN: %clang -Rmodule-build -fmodules -fimplicit-modules -fimplicit-module-maps -fmodule-map-file=%t/module.modulemap -fsyntax-only %s -I %t/hmap +// +// RUN: rm -rf %t +// RUN: mkdir %t +// RUN: cd %t +// +// RUN: sed -e "s|OUTPUTS_DIR|%t|g" %S/Inputs/implicit-module-header-maps/b.hmap.json > %t/hmap.json +// RUN: %hmaptool write %t/hmap.json %t/hmap +// +// RUN: mkdir -p %t/After +// RUN: cp %S/Inputs/implicit-module-header-maps/a.h %t/After/Mapping.h +// RUN: cp %S/Inputs/implicit-module-header-maps/a.module.modulemap %t/module.modulemap +// +// RUN: %clang -Rmodule-build -fmodules -fimplicit-modules -fimplicit-module-maps -fmodule-map-file=%t/module.modulemap -fsyntax-only %s -I %t/hmap + +#define FOO +// This include will fail if modules weren't used. The include name itself +// doesn't exist and relies on the header map to remap it to the real header. +#include "Before/Mapping.h"