Skip to content

Commit cd01bae

Browse files
authored
Merge pull request #7760 from apple/jan_svoboda/20230725-has-include-fix-2
[clang][modules] Avoid modules diagnostics for `__has_include()`
2 parents 19a3035 + b62ebbb commit cd01bae

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

clang/lib/Lex/PPDirectives.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
953953

954954
Module *RequestingModule = getModuleForLocation(
955955
FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
956-
bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
957956

958957
// If the header lookup mechanism may be relative to the current inclusion
959958
// stack, record the parent #includes.
@@ -1029,13 +1028,8 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10291028
Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
10301029
RelativePath, RequestingModule, SuggestedModule, IsMapped,
10311030
IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
1032-
if (FE) {
1033-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1034-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1035-
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1036-
Filename, *FE);
1031+
if (FE)
10371032
return FE;
1038-
}
10391033

10401034
const FileEntry *CurFileEnt;
10411035
// Otherwise, see if this is a subframework header. If so, this is relative
@@ -1046,10 +1040,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10461040
if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
10471041
Filename, CurFileEnt, SearchPath, RelativePath, RequestingModule,
10481042
SuggestedModule)) {
1049-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1050-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1051-
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1052-
Filename, *FE);
10531043
return FE;
10541044
}
10551045
}
@@ -1061,10 +1051,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10611051
if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
10621052
Filename, CurFileEnt, SearchPath, RelativePath,
10631053
RequestingModule, SuggestedModule)) {
1064-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1065-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1066-
RequestingModule, RequestingModuleIsModuleInterface,
1067-
FilenameLoc, Filename, *FE);
10681054
return FE;
10691055
}
10701056
}
@@ -2093,12 +2079,28 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
20932079
const FileEntry *LookupFromFile, StringRef &LookupFilename,
20942080
SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
20952081
ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2082+
auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) {
2083+
if (LangOpts.AsmPreprocessor)
2084+
return;
2085+
2086+
Module *RequestingModule = getModuleForLocation(
2087+
FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
2088+
bool RequestingModuleIsModuleInterface =
2089+
!SourceMgr.isInMainFile(FilenameLoc);
2090+
2091+
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
2092+
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
2093+
Filename, FE);
2094+
};
2095+
20962096
OptionalFileEntryRef File = LookupFile(
20972097
FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
20982098
Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
20992099
&SuggestedModule, &IsMapped, &IsFrameworkFound);
2100-
if (File)
2100+
if (File) {
2101+
DiagnoseHeaderInclusion(*File);
21012102
return File;
2103+
}
21022104

21032105
// Give the clients a chance to silently skip this include.
21042106
if (Callbacks && Callbacks->FileNotFound(Filename))
@@ -2117,6 +2119,7 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
21172119
&SuggestedModule, &IsMapped,
21182120
/*IsFrameworkFound=*/nullptr);
21192121
if (File) {
2122+
DiagnoseHeaderInclusion(*File);
21202123
Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
21212124
<< Filename << IsImportDecl
21222125
<< FixItHint::CreateReplacement(FilenameRange,
@@ -2147,6 +2150,7 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
21472150
Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
21482151
/*IsFrameworkFound=*/nullptr);
21492152
if (File) {
2153+
DiagnoseHeaderInclusion(*File);
21502154
auto Hint =
21512155
isAngled ? FixItHint::CreateReplacement(
21522156
FilenameRange, "<" + TypoCorrectionName.str() + ">")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
//--- module.modulemap
5+
module Mod { header "mod.h" }
6+
//--- mod.h
7+
#if __has_include("textual.h")
8+
#endif
9+
//--- textual.h
10+
11+
//--- tu.c
12+
#include "mod.h"
13+
14+
// RUN: %clang -fsyntax-only %t/tu.c -fmodules -fmodules-cache-path=%t/cache -Werror=non-modular-include-in-module

0 commit comments

Comments
 (0)