@@ -1591,6 +1591,14 @@ static void checkConfigMacro(Preprocessor &PP, StringRef ConfigMacro,
1591
1591
}
1592
1592
}
1593
1593
1594
+ static void checkConfigMacros (Preprocessor &PP, Module *M,
1595
+ SourceLocation ImportLoc) {
1596
+ clang::Module *TopModule = M->getTopLevelModule ();
1597
+ for (const StringRef ConMacro : TopModule->ConfigMacros ) {
1598
+ checkConfigMacro (PP, ConMacro, M, ImportLoc);
1599
+ }
1600
+ }
1601
+
1594
1602
// / Write a new timestamp file with the given path.
1595
1603
static void writeTimestampFile (StringRef TimestampFile) {
1596
1604
std::error_code EC;
@@ -1829,6 +1837,13 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
1829
1837
Module *M =
1830
1838
HS.lookupModule (ModuleName, ImportLoc, true , !IsInclusionDirective);
1831
1839
1840
+ // Check for any configuration macros that have changed. This is done
1841
+ // immediately before potentially building a module in case this module
1842
+ // depends on having one of its configuration macros defined to successfully
1843
+ // build. If this is not done the user will never see the warning.
1844
+ if (M)
1845
+ checkConfigMacros (getPreprocessor (), M, ImportLoc);
1846
+
1832
1847
// Select the source and filename for loading the named module.
1833
1848
std::string ModuleFilename;
1834
1849
ModuleSource Source =
@@ -2006,12 +2021,23 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
2006
2021
if (auto MaybeModule = MM.getCachedModuleLoad (*Path[0 ].first )) {
2007
2022
// Use the cached result, which may be nullptr.
2008
2023
Module = *MaybeModule;
2024
+ // Config macros are already checked before building a module, but they need
2025
+ // to be checked at each import location in case any of the config macros
2026
+ // have a new value at the current `ImportLoc`.
2027
+ if (Module)
2028
+ checkConfigMacros (getPreprocessor (), Module, ImportLoc);
2009
2029
} else if (ModuleName == getLangOpts ().CurrentModule ) {
2010
2030
// This is the module we're building.
2011
2031
Module = PP->getHeaderSearchInfo ().lookupModule (
2012
2032
ModuleName, ImportLoc, /* AllowSearch*/ true ,
2013
2033
/* AllowExtraModuleMapSearch*/ !IsInclusionDirective);
2014
2034
2035
+ // Config macros do not need to be checked here for two reasons.
2036
+ // * This will always be textual inclusion, and thus the config macros
2037
+ // actually do impact the content of the header.
2038
+ // * `Preprocessor::HandleHeaderIncludeOrImport` will never call this
2039
+ // function as the `#include` or `#import` is textual.
2040
+
2015
2041
MM.cacheModuleLoad (*Path[0 ].first , Module);
2016
2042
} else {
2017
2043
ModuleLoadResult Result = findOrCompileModuleAndReadAST (
@@ -2146,18 +2172,11 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
2146
2172
TheASTReader->makeModuleVisible (Module, Visibility, ImportLoc);
2147
2173
}
2148
2174
2149
- // Check for any configuration macros that have changed.
2150
- clang::Module *TopModule = Module->getTopLevelModule ();
2151
- for (unsigned I = 0 , N = TopModule->ConfigMacros .size (); I != N; ++I) {
2152
- checkConfigMacro (getPreprocessor (), TopModule->ConfigMacros [I],
2153
- Module, ImportLoc);
2154
- }
2155
-
2156
2175
// Resolve any remaining module using export_as for this one.
2157
2176
getPreprocessor ()
2158
2177
.getHeaderSearchInfo ()
2159
2178
.getModuleMap ()
2160
- .resolveLinkAsDependencies (TopModule );
2179
+ .resolveLinkAsDependencies (Module-> getTopLevelModule () );
2161
2180
2162
2181
LastModuleImportLoc = ImportLoc;
2163
2182
LastModuleImportResult = ModuleLoadResult (Module);
0 commit comments