Skip to content

Commit bdb309c

Browse files
authored
[clang][modules] Avoid modules diagnostics for __has_include() (#71450)
After #70144 Clang started resolving module maps even for `__has_include()` expressions. This had the unintended consequence of emitting diagnostics around header misuse. These don't make sense if you actually don't bring contents of the header into the importer, so should be skipped for `__has_include()`. This patch moves emission of these diagnostics out of `Preprocessor::LookupFile()` up into `Preprocessor::LookupHeaderIncludeOrImport()`.
1 parent 160d483 commit bdb309c

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
@@ -960,7 +960,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
960960

961961
Module *RequestingModule = getModuleForLocation(
962962
FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
963-
bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
964963

965964
// If the header lookup mechanism may be relative to the current inclusion
966965
// stack, record the parent #includes.
@@ -1041,13 +1040,8 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10411040
Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
10421041
RelativePath, RequestingModule, SuggestedModule, IsMapped,
10431042
IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
1044-
if (FE) {
1045-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1046-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1047-
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1048-
Filename, *FE);
1043+
if (FE)
10491044
return FE;
1050-
}
10511045

10521046
OptionalFileEntryRef CurFileEnt;
10531047
// Otherwise, see if this is a subframework header. If so, this is relative
@@ -1058,10 +1052,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10581052
if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
10591053
Filename, *CurFileEnt, SearchPath, RelativePath, RequestingModule,
10601054
SuggestedModule)) {
1061-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1062-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1063-
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1064-
Filename, *FE);
10651055
return FE;
10661056
}
10671057
}
@@ -1073,10 +1063,6 @@ OptionalFileEntryRef Preprocessor::LookupFile(
10731063
if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
10741064
Filename, *CurFileEnt, SearchPath, RelativePath,
10751065
RequestingModule, SuggestedModule)) {
1076-
if (SuggestedModule && !LangOpts.AsmPreprocessor)
1077-
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1078-
RequestingModule, RequestingModuleIsModuleInterface,
1079-
FilenameLoc, Filename, *FE);
10801066
return FE;
10811067
}
10821068
}
@@ -2027,12 +2013,28 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
20272013
const FileEntry *LookupFromFile, StringRef &LookupFilename,
20282014
SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
20292015
ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2016+
auto DiagnoseHeaderInclusion = [&](FileEntryRef FE) {
2017+
if (LangOpts.AsmPreprocessor)
2018+
return;
2019+
2020+
Module *RequestingModule = getModuleForLocation(
2021+
FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
2022+
bool RequestingModuleIsModuleInterface =
2023+
!SourceMgr.isInMainFile(FilenameLoc);
2024+
2025+
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
2026+
RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
2027+
Filename, FE);
2028+
};
2029+
20302030
OptionalFileEntryRef File = LookupFile(
20312031
FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
20322032
Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
20332033
&SuggestedModule, &IsMapped, &IsFrameworkFound);
2034-
if (File)
2034+
if (File) {
2035+
DiagnoseHeaderInclusion(*File);
20352036
return File;
2037+
}
20362038

20372039
// Give the clients a chance to silently skip this include.
20382040
if (Callbacks && Callbacks->FileNotFound(Filename))
@@ -2051,6 +2053,7 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
20512053
&SuggestedModule, &IsMapped,
20522054
/*IsFrameworkFound=*/nullptr);
20532055
if (File) {
2056+
DiagnoseHeaderInclusion(*File);
20542057
Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
20552058
<< Filename << IsImportDecl
20562059
<< FixItHint::CreateReplacement(FilenameRange,
@@ -2081,6 +2084,7 @@ OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
20812084
Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
20822085
/*IsFrameworkFound=*/nullptr);
20832086
if (File) {
2087+
DiagnoseHeaderInclusion(*File);
20842088
auto Hint =
20852089
isAngled ? FixItHint::CreateReplacement(
20862090
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)