@@ -1686,6 +1686,14 @@ static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1686
1686
}
1687
1687
}
1688
1688
1689
+ static void checkConfigMacros (Preprocessor &PP, Module *M,
1690
+ SourceLocation ImportLoc) {
1691
+ clang::Module *TopModule = M->getTopLevelModule ();
1692
+ for (const StringRef ConMacro : TopModule->ConfigMacros ) {
1693
+ checkConfigMacro (PP, ConMacro, M, ImportLoc);
1694
+ }
1695
+ }
1696
+
1689
1697
// / Write a new timestamp file with the given path.
1690
1698
static void writeTimestampFile (StringRef TimestampFile) {
1691
1699
std::error_code EC;
@@ -1926,6 +1934,13 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1926
1934
Module *M =
1927
1935
HS.lookupModule (ModuleName, ImportLoc, true , !IsInclusionDirective);
1928
1936
1937
+ // Check for any configuration macros that have changed. This is done
1938
+ // immediately before potentially building a module in case this module
1939
+ // depends on having one of its configuration macros defined to successfully
1940
+ // build. If this is not done the user will never see the warning.
1941
+ if (M)
1942
+ checkConfigMacros (getPreprocessor (), M, ImportLoc);
1943
+
1929
1944
// Select the source and filename for loading the named module.
1930
1945
std::string ModuleFilename;
1931
1946
ModuleSource Source =
@@ -2110,12 +2125,23 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
2110
2125
if (auto MaybeModule = MM.getCachedModuleLoad (*Path[0 ].first )) {
2111
2126
// Use the cached result, which may be nullptr.
2112
2127
Module = *MaybeModule;
2128
+ // Config macros are already checked before building a module, but they need
2129
+ // to be checked at each import location in case any of the config macros
2130
+ // have a new value at the current `ImportLoc`.
2131
+ if (Module)
2132
+ checkConfigMacros (getPreprocessor (), Module, ImportLoc);
2113
2133
} else if (ModuleName == getLangOpts ().CurrentModule ) {
2114
2134
// This is the module we're building.
2115
2135
Module = PP->getHeaderSearchInfo ().lookupModule (
2116
2136
ModuleName, ImportLoc, /* AllowSearch*/ true ,
2117
2137
/* AllowExtraModuleMapSearch*/ !IsInclusionDirective);
2118
2138
2139
+ // Config macros do not need to be checked here for two reasons.
2140
+ // * This will always be textual inclusion, and thus the config macros
2141
+ // actually do impact the content of the header.
2142
+ // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
2143
+ // function as the `#include` or `#import` is textual.
2144
+
2119
2145
MM.cacheModuleLoad (*Path[0 ].first , Module);
2120
2146
} else {
2121
2147
ModuleLoadResult Result = findOrCompileModuleAndReadAST (
@@ -2252,18 +2278,11 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
2252
2278
TheASTReader->makeModuleVisible (Module, Visibility, ImportLoc);
2253
2279
}
2254
2280
2255
- // Check for any configuration macros that have changed.
2256
- clang::Module *TopModule = Module->getTopLevelModule ();
2257
- for (unsigned I = 0 , N = TopModule->ConfigMacros .size (); I != N; ++I) {
2258
- checkConfigMacro (getPreprocessor (), TopModule->ConfigMacros [I],
2259
- Module, ImportLoc);
2260
- }
2261
-
2262
2281
// Resolve any remaining module using export_as for this one.
2263
2282
getPreprocessor ()
2264
2283
.getHeaderSearchInfo ()
2265
2284
.getModuleMap ()
2266
- .resolveLinkAsDependencies (TopModule );
2285
+ .resolveLinkAsDependencies (Module-> getTopLevelModule () );
2267
2286
2268
2287
LastModuleImportLoc = ImportLoc;
2269
2288
LastModuleImportResult = ModuleLoadResult (Module);
0 commit comments