diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 8ef3c918f337..57dc49536af2 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -540,7 +540,7 @@ Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) { } // Try to determine the module of the include directive. // FIXME: Look into directly passing the FileEntry from LookupFile instead. - FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getSpellingLoc(FilenameLoc)); + FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc)); if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) { // The include comes from a file. return ModMap.findModuleForHeader(EntryOfIncl).getModule(); diff --git a/clang/test/Modules/Inputs/declare-use/j.h b/clang/test/Modules/Inputs/declare-use/j.h new file mode 100644 index 000000000000..04d46a9fb2b7 --- /dev/null +++ b/clang/test/Modules/Inputs/declare-use/j.h @@ -0,0 +1,14 @@ +#ifndef J_H +#define J_H + +#define STR(x) #x +#define HDR(x) STR(x.h) + +#include ALLOWED_INC +#include HDR(a) + +const int j = a * a + b; + +// expected-no-diagnostics + +#endif diff --git a/clang/test/Modules/Inputs/declare-use/module.map b/clang/test/Modules/Inputs/declare-use/module.map index 3b1b0e743e45..a176fb3a141f 100644 --- a/clang/test/Modules/Inputs/declare-use/module.map +++ b/clang/test/Modules/Inputs/declare-use/module.map @@ -37,6 +37,7 @@ module XG { header "g1.h" use XC use XE + use XJ } module XH { @@ -47,5 +48,9 @@ module XH { use XE } +module XJ { + header "j.h" +} + module XS { } diff --git a/clang/test/Modules/declare-use3.cpp b/clang/test/Modules/declare-use3.cpp new file mode 100644 index 000000000000..8b0bbfa756c3 --- /dev/null +++ b/clang/test/Modules/declare-use3.cpp @@ -0,0 +1,4 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -include "g.h" -include "e.h" -include "f.h" -include "i.h" -fmodule-maps -fmodules-cache-path=%t -fmodules-decluse -fmodule-name=XG -I %S/Inputs/declare-use %s -verify +// expected-error {{module XG does not depend on a module exporting 'f.h'}} +const int g2 = g1 + e + f + aux_i; diff --git a/clang/test/Modules/declare-use4.cpp b/clang/test/Modules/declare-use4.cpp new file mode 100644 index 000000000000..1d346469f1ef --- /dev/null +++ b/clang/test/Modules/declare-use4.cpp @@ -0,0 +1,10 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -fmodule-maps -fmodules-cache-path=%t -fmodules-decluse -fmodule-name=XG -I %S/Inputs/declare-use %s -verify + +#define ALLOWED_INC "b.h" + +#include "j.h" + +const int g2 = j; + +// expected-no-diagnostics