Skip to content

Commit 82f75ed

Browse files
authored
[Driver] Ignore non-clang pch files when -include a.h probes a.h.gch (#69204)
Instead of deprecating the "gch probe" as in f726da1, this makes clang ignore files which are not clang pch files (See discussion on PR #67084). This fixes the issues mentioned in the former patch, with GCC-generated .gch files getting in the way when using clang, while maintaining the probing behavior for builds which rely on that.
1 parent 5d7f346 commit 82f75ed

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ C/C++ Language Potentially Breaking Changes
4343

4444
- The default extension name for PCH generation (``-c -xc-header`` and ``-c
4545
-xc++-header``) is now ``.pch`` instead of ``.gch``.
46-
- ``-include a.h`` probing ``a.h.gch`` is deprecated. Change the extension name
47-
to ``.pch`` or use ``-include-pch a.h.gch``.
46+
- ``-include a.h`` probing ``a.h.gch`` will now ignore ``a.h.gch`` if it is not
47+
a clang pch file or a directory containing any clang pch file.
4848
- Fixed a bug that caused ``__has_cpp_attribute`` and ``__has_c_attribute``
4949
return incorrect values for some C++-11-style attributes. Below is a complete
5050
list of behavior changes.

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,14 @@ def warn_drv_overriding_option : Warning<
441441
def warn_drv_treating_input_as_cxx : Warning<
442442
"treating '%0' input as '%1' when in C++ mode, this behavior is deprecated">,
443443
InGroup<Deprecated>;
444-
def warn_drv_include_probe_gch : Warning<
445-
"'%0' probing .gch is deprecated. Use '-include-pch %1' or switch to .pch instead">,
446-
InGroup<DeprecatedIncludeGch>;
447444
def warn_drv_pch_not_first_include : Warning<
448445
"precompiled header '%0' was ignored because '%1' is not first '-include'">;
446+
def warn_drv_pch_ignoring_gch_file : Warning<
447+
"precompiled header '%0' was ignored because it is not a clang PCH file">,
448+
InGroup<IgnoredGCH>;
449+
def warn_drv_pch_ignoring_gch_dir : Warning<
450+
"precompiled header directory '%0' was ignored because it contains no clang PCH files">,
451+
InGroup<IgnoredGCH>;
449452
def warn_missing_sysroot : Warning<"no such sysroot directory: '%0'">,
450453
InGroup<DiagGroup<"missing-sysroot">>;
451454
def warn_incompatible_sysroot : Warning<"using sysroot for '%0' but targeting '%1'">,

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ def DeprecatedWritableStr : DiagGroup<"deprecated-writable-strings",
210210
[CXX11CompatDeprecatedWritableStr]>;
211211
def DeprecatedPragma : DiagGroup<"deprecated-pragma">;
212212
def DeprecatedType : DiagGroup<"deprecated-type">;
213-
def DeprecatedIncludeGch : DiagGroup<"deprecated-include-gch">;
214213
// FIXME: Why is DeprecatedImplementations not in this group?
215214
def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
216215
DeprecatedArrayCompare,
@@ -233,8 +232,7 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
233232
DeprecatedType,
234233
DeprecatedVolatile,
235234
DeprecatedWritableStr,
236-
DeprecatedRedundantConstexprStaticDef,
237-
DeprecatedIncludeGch
235+
DeprecatedRedundantConstexprStaticDef
238236
]>,
239237
DiagCategory<"Deprecations">;
240238

@@ -444,6 +442,7 @@ def IncrementBool : DiagGroup<"increment-bool", [DeprecatedIncrementBool]>;
444442
def InfiniteRecursion : DiagGroup<"infinite-recursion">;
445443
def PureVirtualCallFromCtorDtor: DiagGroup<"call-to-pure-virtual-from-ctor-dtor">;
446444
def GNUImaginaryConstant : DiagGroup<"gnu-imaginary-constant">;
445+
def IgnoredGCH : DiagGroup<"ignored-gch">;
447446
def IgnoredReferenceQualifiers : DiagGroup<"ignored-reference-qualifiers">;
448447
def IgnoredQualifiers : DiagGroup<"ignored-qualifiers", [IgnoredReferenceQualifiers]>;
449448
def : DiagGroup<"import">;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,35 @@ static void handleAMDGPUCodeObjectVersionOptions(const Driver &D,
10671067
}
10681068
}
10691069

1070+
static bool hasClangPchSignature(const Driver &D, StringRef Path) {
1071+
if (llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MemBuf =
1072+
D.getVFS().getBufferForFile(Path))
1073+
return (*MemBuf)->getBuffer().startswith("CPCH");
1074+
return false;
1075+
}
1076+
1077+
static bool gchProbe(const Driver &D, StringRef Path) {
1078+
llvm::ErrorOr<llvm::vfs::Status> Status = D.getVFS().status(Path);
1079+
if (!Status)
1080+
return false;
1081+
1082+
if (Status->isDirectory()) {
1083+
std::error_code EC;
1084+
for (llvm::vfs::directory_iterator DI = D.getVFS().dir_begin(Path, EC), DE;
1085+
!EC && DI != DE; DI = DI.increment(EC)) {
1086+
if (hasClangPchSignature(D, DI->path()))
1087+
return true;
1088+
}
1089+
D.Diag(diag::warn_drv_pch_ignoring_gch_dir) << Path;
1090+
return false;
1091+
}
1092+
1093+
if (hasClangPchSignature(D, Path))
1094+
return true;
1095+
D.Diag(diag::warn_drv_pch_ignoring_gch_file) << Path;
1096+
return false;
1097+
}
1098+
10701099
void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
10711100
const Driver &D, const ArgList &Args,
10721101
ArgStringList &CmdArgs,
@@ -1283,11 +1312,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
12831312
FoundPCH = true;
12841313

12851314
if (!FoundPCH) {
1315+
// For GCC compat, probe for a file or directory ending in .gch instead.
12861316
llvm::sys::path::replace_extension(P, "gch");
1287-
if (D.getVFS().exists(P)) {
1288-
FoundPCH = true;
1289-
D.Diag(diag::warn_drv_include_probe_gch) << A->getAsString(Args) << P;
1290-
}
1317+
FoundPCH = gchProbe(D, P.str());
12911318
}
12921319

12931320
if (FoundPCH) {

clang/test/PCH/Inputs/gch-probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void g(void);

clang/test/PCH/Inputs/gch-probe.h.gch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is not a pch file.

clang/test/PCH/gch-probe.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// For GCC compatibility, clang should probe also with the .gch extension.
2+
// RUN: %clang -x c-header -c %s -o %t.h.gch
3+
// RUN: %clang -fsyntax-only -include %t.h %s
4+
5+
// gch probing should ignore files which are not clang pch files.
6+
// RUN: %clang -fsyntax-only -include %S/Inputs/gch-probe.h %s 2>&1 | FileCheck %s
7+
// CHECK: warning: precompiled header '{{.*}}gch-probe.h.gch' was ignored because it is not a clang PCH file
8+
9+
void f(void);

clang/test/PCH/pch-dir.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@
2323
// RUN: not %clang_cc1 -include-pch %t.h.gch -DFOO=baz -fsyntax-only %s -print-stats 2> %t.missinglog
2424
// RUN: FileCheck -check-prefix=CHECK-NO-SUITABLE %s < %t.missinglog
2525

26+
// Don't gch probe directories which contain no pch files.
27+
// RUN: rm -rf %t.x.h.gch
28+
// RUN: mkdir -p %t.x.h.gch
29+
// RUN: not %clang -include %t.x.h -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-IGNORED-DIR %s
30+
2631
// CHECK-CBAR: int bar
2732
int FOO;
2833

2934
int get(void) {
3035
#ifdef __cplusplus
31-
// CHECK-CPP: warning: '-include {{.*}}.h' probing .gch is deprecated. Use '-include-pch {{.*}}.h.gch' or switch to .pch instead
3236
// CHECK-CPP: .h.gch{{[/\\]}}cpp.gch
3337
return i;
3438
#else
35-
// CHECK-C: warning: '-include {{.*}}.h' probing .gch is deprecated. Use '-include-pch {{.*}}.h.gch' or switch to .pch instead
3639
// CHECK-C: .h.gch{{[/\\]}}c.gch
3740
return j;
3841
#endif
3942
}
4043

4144
// CHECK-NO-SUITABLE: no suitable precompiled header file found in directory
45+
46+
// CHECK-IGNORED-DIR: precompiled header directory '{{.*}}pch-dir.c.tmp.x.h.gch' was ignored because it contains no clang PCH files
47+
// CHECK-IGNORED-DIR: '{{.*}}pch-dir.c.tmp.x.h' file not found

0 commit comments

Comments
 (0)