Skip to content

Commit afaa1cc

Browse files
authored
Merge pull request #7687 from apple/jan_svoboda/20230725-had-include-cherry-pick
[clang][deps] Fix `__has_include` behavior with umbrella headers
2 parents 8c51c4f + 8c81edb commit afaa1cc

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,10 +1254,15 @@ static bool EvaluateHasIncludeCommon(Token &Tok, IdentifierInfo *II,
12541254
if (Filename.empty())
12551255
return false;
12561256

1257+
// Passing this to LookupFile forces header search to check whether the found
1258+
// file belongs to a module. Skipping that check could incorrectly mark
1259+
// modular header as textual, causing issues down the line.
1260+
ModuleMap::KnownHeader KH;
1261+
12571262
// Search include directories.
12581263
OptionalFileEntryRef File =
12591264
PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile,
1260-
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
1265+
nullptr, nullptr, nullptr, &KH, nullptr, nullptr);
12611266

12621267
if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
12631268
SrcMgr::CharacteristicKind FileType = SrcMgr::C_User;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// This test checks that __has_include(<FW/PrivateHeader.h>) in a module does
2+
// not clobber #include <FW/PrivateHeader.h> in importers of said module.
3+
4+
// RUN: rm -rf %t
5+
// RUN: split-file %s %t
6+
7+
//--- cdb.json.template
8+
[{
9+
"file": "DIR/tu.c",
10+
"directory": "DIR",
11+
"command": "clang DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -I DIR/modules -F DIR/frameworks -o DIR/tu.o"
12+
}]
13+
14+
//--- frameworks/FW.framework/Modules/module.private.modulemap
15+
framework module FW_Private {
16+
umbrella header "A.h"
17+
module * { export * }
18+
}
19+
//--- frameworks/FW.framework/PrivateHeaders/A.h
20+
#include <FW/B.h>
21+
//--- frameworks/FW.framework/PrivateHeaders/B.h
22+
#include "dependency.h"
23+
24+
//--- modules/module.modulemap
25+
module Poison { header "poison.h" }
26+
module Import { header "import.h" }
27+
module Dependency { header "dependency.h" }
28+
//--- modules/poison.h
29+
#if __has_include(<FW/B.h>)
30+
#define HAS_B 1
31+
#else
32+
#define HAS_B 0
33+
#endif
34+
//--- modules/import.h
35+
#include <FW/B.h>
36+
//--- modules/dependency.h
37+
38+
//--- tu.c
39+
#include "poison.h"
40+
41+
#if __has_include(<FW/B.h>)
42+
#endif
43+
44+
#include "import.h"
45+
46+
#include <FW/B.h>
47+
48+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
49+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/deps.json
50+
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
51+
52+
// Let's check that the TU actually depends on `FW_Private` (and does not treat FW/B.h as textual).
53+
// CHECK: {
54+
// CHECK: "translation-units": [
55+
// CHECK-NEXT: {
56+
// CHECK-NEXT: "commands": [
57+
// CHECK-NEXT: {
58+
// CHECK-NEXT: "clang-context-hash": "{{.*}}",
59+
// CHECK-NEXT: "clang-module-deps": [
60+
// CHECK-NEXT: {
61+
// CHECK-NEXT: "context-hash": "{{.*}}",
62+
// CHECK-NEXT: "module-name": "FW_Private"
63+
// CHECK-NEXT: }
64+
// CHECK: ],
65+
// CHECK-NEXT: "command-line": [
66+
// CHECK: ],
67+
// CHECK-NEXT: "executable": "clang",
68+
// CHECK-NEXT: "file-deps": [
69+
// CHECK-NEXT: "[[PREFIX]]/tu.c"
70+
// CHECK-NEXT: ],
71+
// CHECK-NEXT: "input-file": "[[PREFIX]]/tu.c"
72+
// CHECK-NEXT: }
73+
// CHECK: ]
74+
// CHECK: }
75+
// CHECK: ]
76+
// CHECK: }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// This test checks that __has_include(<FW/PrivateHeader.h>) in a module does
2+
// not clobber #include <FW/PrivateHeader.h> in importers of said module.
3+
4+
// RUN: rm -rf %t
5+
// RUN: split-file %s %t
6+
7+
//--- cdb.json.template
8+
[{
9+
"file": "DIR/tu.c",
10+
"directory": "DIR",
11+
"command": "clang DIR/tu.c -fmodules -fmodules-cache-path=DIR/cache -I DIR/modules -F DIR/frameworks -o DIR/tu.o"
12+
}]
13+
14+
//--- frameworks/FW.framework/Modules/module.private.modulemap
15+
framework module FW_Private {
16+
umbrella header "A.h"
17+
module * { export * }
18+
}
19+
//--- frameworks/FW.framework/PrivateHeaders/A.h
20+
#include <FW/B.h>
21+
//--- frameworks/FW.framework/PrivateHeaders/B.h
22+
#include "dependency.h"
23+
24+
//--- modules/module.modulemap
25+
module Poison { header "poison.h" }
26+
module Import { header "import.h" }
27+
module Dependency { header "dependency.h" }
28+
//--- modules/poison.h
29+
#if __has_include(<FW/B.h>)
30+
#define HAS_B 1
31+
#else
32+
#define HAS_B 0
33+
#endif
34+
//--- modules/import.h
35+
#include <FW/B.h>
36+
//--- modules/dependency.h
37+
38+
//--- tu.c
39+
#include "poison.h"
40+
41+
#if __has_include(<FW/B.h>)
42+
#endif
43+
44+
#include "import.h"
45+
46+
#include <FW/B.h>
47+
48+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
49+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree-full -cas-path %t/cas > %t/deps.json
50+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
51+
// RUN: cat %t/tu.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/tu.casid
52+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/tu.casid | FileCheck %s -DPREFIX=%/t
53+
54+
// Let's check that the TU actually imports FW_Private.B instead of treating FW/B.h as textual.
55+
// CHECK: [[PREFIX]]/tu.c llvmcas://
56+
// CHECK-NEXT: 1:1 <built-in> llvmcas://
57+
// CHECK-NEXT: 2:1 (Module) Poison
58+
// CHECK-NEXT: 7:1 (Module) Import
59+
// CHECK-NEXT: 9:1 (Module) FW_Private.B

0 commit comments

Comments
 (0)