Author: Hans Date: 2023-10-24T14:03:13+02:00 New Revision: 82f75ed5997c317350b539dc185f5dbe2ec197a3
URL: https://github.com/llvm/llvm-project/commit/82f75ed5997c317350b539dc185f5dbe2ec197a3 DIFF: https://github.com/llvm/llvm-project/commit/82f75ed5997c317350b539dc185f5dbe2ec197a3.diff LOG: [Driver] Ignore non-clang pch files when -include a.h probes a.h.gch (#69204) Instead of deprecating the "gch probe" as in f726da1193baf51e0a66453cc32dcffb8a9121d4, 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. Added: clang/test/PCH/Inputs/gch-probe.h clang/test/PCH/Inputs/gch-probe.h.gch clang/test/PCH/gch-probe.c Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticDriverKinds.td clang/include/clang/Basic/DiagnosticGroups.td clang/lib/Driver/ToolChains/Clang.cpp clang/test/PCH/pch-dir.c Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 65c1c07e3ded9f6..4c24216888525cd 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -43,8 +43,8 @@ C/C++ Language Potentially Breaking Changes - The default extension name for PCH generation (``-c -xc-header`` and ``-c -xc++-header``) is now ``.pch`` instead of ``.gch``. -- ``-include a.h`` probing ``a.h.gch`` is deprecated. Change the extension name - to ``.pch`` or use ``-include-pch a.h.gch``. +- ``-include a.h`` probing ``a.h.gch`` will now ignore ``a.h.gch`` if it is not + a clang pch file or a directory containing any clang pch file. - Fixed a bug that caused ``__has_cpp_attribute`` and ``__has_c_attribute`` return incorrect values for some C++-11-style attributes. Below is a complete list of behavior changes. diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 6110c6d6ea195b8..5dbc5b5edfb4aeb 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -441,11 +441,14 @@ def warn_drv_overriding_option : Warning< def warn_drv_treating_input_as_cxx : Warning< "treating '%0' input as '%1' when in C++ mode, this behavior is deprecated">, InGroup<Deprecated>; -def warn_drv_include_probe_gch : Warning< - "'%0' probing .gch is deprecated. Use '-include-pch %1' or switch to .pch instead">, - InGroup<DeprecatedIncludeGch>; def warn_drv_pch_not_first_include : Warning< "precompiled header '%0' was ignored because '%1' is not first '-include'">; +def warn_drv_pch_ignoring_gch_file : Warning< + "precompiled header '%0' was ignored because it is not a clang PCH file">, + InGroup<IgnoredGCH>; +def warn_drv_pch_ignoring_gch_dir : Warning< + "precompiled header directory '%0' was ignored because it contains no clang PCH files">, + InGroup<IgnoredGCH>; def warn_missing_sysroot : Warning<"no such sysroot directory: '%0'">, InGroup<DiagGroup<"missing-sysroot">>; def warn_incompatible_sysroot : Warning<"using sysroot for '%0' but targeting '%1'">, diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index ee4383216bdccf7..318eb0d6f889065 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -210,7 +210,6 @@ def DeprecatedWritableStr : DiagGroup<"deprecated-writable-strings", [CXX11CompatDeprecatedWritableStr]>; def DeprecatedPragma : DiagGroup<"deprecated-pragma">; def DeprecatedType : DiagGroup<"deprecated-type">; -def DeprecatedIncludeGch : DiagGroup<"deprecated-include-gch">; // FIXME: Why is DeprecatedImplementations not in this group? def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion, DeprecatedArrayCompare, @@ -233,8 +232,7 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion, DeprecatedType, DeprecatedVolatile, DeprecatedWritableStr, - DeprecatedRedundantConstexprStaticDef, - DeprecatedIncludeGch + DeprecatedRedundantConstexprStaticDef ]>, DiagCategory<"Deprecations">; @@ -444,6 +442,7 @@ def IncrementBool : DiagGroup<"increment-bool", [DeprecatedIncrementBool]>; def InfiniteRecursion : DiagGroup<"infinite-recursion">; def PureVirtualCallFromCtorDtor: DiagGroup<"call-to-pure-virtual-from-ctor-dtor">; def GNUImaginaryConstant : DiagGroup<"gnu-imaginary-constant">; +def IgnoredGCH : DiagGroup<"ignored-gch">; def IgnoredReferenceQualifiers : DiagGroup<"ignored-reference-qualifiers">; def IgnoredQualifiers : DiagGroup<"ignored-qualifiers", [IgnoredReferenceQualifiers]>; def : DiagGroup<"import">; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index b9c54a324b0fd4d..601bbfb927746fc 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -1067,6 +1067,35 @@ static void handleAMDGPUCodeObjectVersionOptions(const Driver &D, } } +static bool hasClangPchSignature(const Driver &D, StringRef Path) { + if (llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MemBuf = + D.getVFS().getBufferForFile(Path)) + return (*MemBuf)->getBuffer().startswith("CPCH"); + return false; +} + +static bool gchProbe(const Driver &D, StringRef Path) { + llvm::ErrorOr<llvm::vfs::Status> Status = D.getVFS().status(Path); + if (!Status) + return false; + + if (Status->isDirectory()) { + std::error_code EC; + for (llvm::vfs::directory_iterator DI = D.getVFS().dir_begin(Path, EC), DE; + !EC && DI != DE; DI = DI.increment(EC)) { + if (hasClangPchSignature(D, DI->path())) + return true; + } + D.Diag(diag::warn_drv_pch_ignoring_gch_dir) << Path; + return false; + } + + if (hasClangPchSignature(D, Path)) + return true; + D.Diag(diag::warn_drv_pch_ignoring_gch_file) << Path; + return false; +} + void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, const Driver &D, const ArgList &Args, ArgStringList &CmdArgs, @@ -1283,11 +1312,9 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, FoundPCH = true; if (!FoundPCH) { + // For GCC compat, probe for a file or directory ending in .gch instead. llvm::sys::path::replace_extension(P, "gch"); - if (D.getVFS().exists(P)) { - FoundPCH = true; - D.Diag(diag::warn_drv_include_probe_gch) << A->getAsString(Args) << P; - } + FoundPCH = gchProbe(D, P.str()); } if (FoundPCH) { diff --git a/clang/test/PCH/Inputs/gch-probe.h b/clang/test/PCH/Inputs/gch-probe.h new file mode 100644 index 000000000000000..940adf12a97ae06 --- /dev/null +++ b/clang/test/PCH/Inputs/gch-probe.h @@ -0,0 +1 @@ +void g(void); diff --git a/clang/test/PCH/Inputs/gch-probe.h.gch b/clang/test/PCH/Inputs/gch-probe.h.gch new file mode 100644 index 000000000000000..fc551fc058b7c27 --- /dev/null +++ b/clang/test/PCH/Inputs/gch-probe.h.gch @@ -0,0 +1 @@ +This is not a pch file. diff --git a/clang/test/PCH/gch-probe.c b/clang/test/PCH/gch-probe.c new file mode 100644 index 000000000000000..8b1e1fab5ad97b6 --- /dev/null +++ b/clang/test/PCH/gch-probe.c @@ -0,0 +1,9 @@ +// For GCC compatibility, clang should probe also with the .gch extension. +// RUN: %clang -x c-header -c %s -o %t.h.gch +// RUN: %clang -fsyntax-only -include %t.h %s + +// gch probing should ignore files which are not clang pch files. +// RUN: %clang -fsyntax-only -include %S/Inputs/gch-probe.h %s 2>&1 | FileCheck %s +// CHECK: warning: precompiled header '{{.*}}gch-probe.h.gch' was ignored because it is not a clang PCH file + +void f(void); diff --git a/clang/test/PCH/pch-dir.c b/clang/test/PCH/pch-dir.c index d8ec687b4d72478..fd7d24f9f83ff41 100644 --- a/clang/test/PCH/pch-dir.c +++ b/clang/test/PCH/pch-dir.c @@ -23,19 +23,25 @@ // RUN: not %clang_cc1 -include-pch %t.h.gch -DFOO=baz -fsyntax-only %s -print-stats 2> %t.missinglog // RUN: FileCheck -check-prefix=CHECK-NO-SUITABLE %s < %t.missinglog +// Don't gch probe directories which contain no pch files. +// RUN: rm -rf %t.x.h.gch +// RUN: mkdir -p %t.x.h.gch +// RUN: not %clang -include %t.x.h -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-IGNORED-DIR %s + // CHECK-CBAR: int bar int FOO; int get(void) { #ifdef __cplusplus - // CHECK-CPP: warning: '-include {{.*}}.h' probing .gch is deprecated. Use '-include-pch {{.*}}.h.gch' or switch to .pch instead // CHECK-CPP: .h.gch{{[/\\]}}cpp.gch return i; #else - // CHECK-C: warning: '-include {{.*}}.h' probing .gch is deprecated. Use '-include-pch {{.*}}.h.gch' or switch to .pch instead // CHECK-C: .h.gch{{[/\\]}}c.gch return j; #endif } // CHECK-NO-SUITABLE: no suitable precompiled header file found in directory + +// CHECK-IGNORED-DIR: precompiled header directory '{{.*}}pch-dir.c.tmp.x.h.gch' was ignored because it contains no clang PCH files +// CHECK-IGNORED-DIR: '{{.*}}pch-dir.c.tmp.x.h' file not found _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits