Skip to content

Commit c0cc666

Browse files
committed
[Frontend][PCH]-Add support for ignoring PCH options (-ignore-pch).
Visual Studio has an argument to ignore all PCH related switches. clang-cl has also support option /Y-. Having the same option in clang would be helpful. This commit is to add support for ignoring PCH options (-ignore-pch).
1 parent 3290d62 commit c0cc666

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

clang/docs/PCHInternals.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ option:
3131
3232
$ clang -cc1 -include-pch test.h.pch test.c -o test.s
3333
34+
To ignore PCH options using ``clang -cc1``, use the option `-ignore-pch`:
35+
36+
.. code-block:: bash
37+
38+
$ clang -cc1 test.h -emit-pch -ignore-pch -o test.h.pch
39+
$ clang -cc1 -include-pch test.h.pch -ignore-pch test.c -o test.s
40+
41+
This option disables precompiled headers, overrides -emit-pch and -include-pch.
42+
test.h.pch is not generated and not used as a prefix header.
43+
3444
Design Philosophy
3545
-----------------
3646

clang/docs/UsersManual.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,20 @@ will be processed from the PCH file. Otherwise, Clang will report an error.
14581458
``test.h`` since ``test.h`` was included directly in the source file and not
14591459
specified on the command line using ``-include-pch``.
14601460

1461+
Ignoring a PCH File
1462+
^^^^^^^^^^^^^^^^^^^
1463+
1464+
To ignore a PCH file using Clang, the `-Xclang -ignore-pch` option is passed to
1465+
``clang``:
1466+
1467+
.. code-block:: console
1468+
1469+
$ clang -x c-header test.h -Xclang -ignore-pch -o test.h.pch
1470+
$ clang -include-pch test.h.pch -Xclang -ignore-pch test.c -o test
1471+
1472+
This option disables precompiled headers, overrides -emit-pch and -include-pch.
1473+
test.h.pch is not generated and not used as a prefix header.
1474+
14611475
Relocatable PCH Files
14621476
^^^^^^^^^^^^^^^^^^^^^
14631477

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8127,6 +8127,8 @@ def emit_header_unit : Flag<["-"], "emit-header-unit">,
81278127
HelpText<"Generate C++20 header units from header files">;
81288128
def emit_pch : Flag<["-"], "emit-pch">,
81298129
HelpText<"Generate pre-compiled header file">;
8130+
def ignore_pch : Flag<["-"], "ignore-pch">,
8131+
HelpText<"Ignore pre-compiled header options">;
81308132
def emit_llvm_only : Flag<["-"], "emit-llvm-only">,
81318133
HelpText<"Build ASTs and convert to LLVM, discarding output">;
81328134
def emit_codegen_only : Flag<["-"], "emit-codegen-only">,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,6 +2982,15 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
29822982
#undef FRONTEND_OPTION_WITH_MARSHALLING
29832983

29842984
Opts.ProgramAction = frontend::ParseSyntaxOnly;
2985+
2986+
// If -ignore-pch is used, all pch handling is disabled. clang pch-related
2987+
// flags are removed.
2988+
if (Args.hasArg(options::OPT_ignore_pch)) {
2989+
Args.eraseArg(options::OPT_emit_pch);
2990+
Args.eraseArg(options::OPT_include_pch);
2991+
Args.eraseArg(options::OPT_ignore_pch);
2992+
}
2993+
29852994
if (const Arg *A = Args.getLastArg(OPT_Action_Group)) {
29862995
OptSpecifier Opt = OptSpecifier(A->getOption().getID());
29872996
std::optional<frontend::ActionKind> ProgramAction = getFrontendAction(Opt);

clang/test/PCH/Inputs/ignored-pch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef IGNORED_PCH_H
2+
#define IGNORED_PCH_H
3+
inline int f() {
4+
return 42;
5+
}
6+
#endif // IGNORED_PCH_H

clang/test/PCH/ignored-pch.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// RUN: rm -rf %t.pch %t.ll
2+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -o %t.pch
3+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -o %t.ll
4+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
5+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
6+
7+
// Check that -ignore-pch causes -emit-pch and -include-pch options to be ignored.
8+
// RUN: rm -rf %t.pch %t.ll
9+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -ignore-pch -emit-pch -o %t.pch
10+
// RUN: %clang_cc1 %s -include-pch %t.pch -ignore-pch -emit-llvm -o %t.ll
11+
// RUN: not ls %t.pch 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
12+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
13+
14+
// Check that -ignore-pch is passed through Driver.
15+
// RUN: rm -rf %t.pch %t.ll
16+
// RUN: %clang -x c-header %S/Inputs/ignored-pch.h -Xclang -emit-pch -o %t.pch
17+
// RUN: %clang -S %s -include-pch %t.pch -Xclang -emit-llvm -o %t.ll
18+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
19+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
20+
21+
// RUN: rm -rf %t.pch %t.ll
22+
// RUN: %clang -x c-header %S/Inputs/ignored-pch.h -Xclang -ignore-pch -Xclang -emit-pch -o %t.pch
23+
// RUN: %clang -S %s -include-pch %t.pch -Xclang -ignore-pch -Xclang -emit-llvm -o %t.ll
24+
// RUN: not ls %t.pch 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
25+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
26+
27+
// Check that -ignore-pch works for multiple PCH related options.
28+
// Test with -building-pch-with-obj.
29+
// RUN: rm -rf %t.pch %t.ll
30+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -ignore-pch -emit-pch -building-pch-with-obj -o %t.pch
31+
// RUN: %clang_cc1 %s -include-pch %t.pch -ignore-pch -emit-llvm -building-pch-with-obj -o %t.ll
32+
// RUN: not ls %t.pch 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
33+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
34+
35+
// Test with -fallow-pch-with-compiler-errors.
36+
// RUN: rm -rf %t.pch %t.ll
37+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -ignore-pch -emit-pch -fallow-pch-with-compiler-errors -o %t.pch
38+
// RUN: %clang_cc1 %s -include-pch %t.pch -ignore-pch -emit-llvm -fallow-pch-with-compiler-errors -o %t.ll
39+
// RUN: not ls %t.pch 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
40+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
41+
42+
// Test with -fallow-pch-with-different-modules-cache-path.
43+
// RUN: rm -rf %t.pch %t.ll
44+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fallow-pch-with-different-modules-cache-path -o %t.pch
45+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fallow-pch-with-different-modules-cache-path -o %t.ll
46+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
47+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
48+
49+
// Test with -fpch-codegen.
50+
// RUN: rm -rf %t.pch %t.ll
51+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fpch-codegen -o %t.pch
52+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fpch-codegen -o %t.ll
53+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
54+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
55+
56+
// Test with -fpch-debuginfo.
57+
// RUN: rm -rf %t.pch %t.ll
58+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fpch-debuginfo -o %t.pch
59+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fpch-debuginfo -o %t.ll
60+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
61+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
62+
63+
// Test with -fpch-instantiate-templates.
64+
// RUN: rm -rf %t.pch %t.ll
65+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fpch-instantiate-templates -o %t.pch
66+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fpch-instantiate-templates -o %t.ll
67+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
68+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
69+
70+
// Test with -fno-pch-timestamp.
71+
// RUN: rm -rf %t.pch %t.ll
72+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fno-pch-timestamp -o %t.pch
73+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fno-pch-timestamp -o %t.ll
74+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
75+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
76+
77+
// Test with -fno-validate-pch.
78+
// RUN: rm -rf %t.pch %t.ll
79+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -fno-validate-pch -o %t.pch
80+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -fno-validate-pch -o %t.ll
81+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
82+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
83+
84+
// Test with -relocatable-pch.
85+
// RUN: rm -rf %t.pch %t.ll
86+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -relocatable-pch -o %t.pch
87+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -relocatable-pch -o %t.ll
88+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
89+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
90+
91+
// Test with -pch-through-hdrstop-create/-pch-through-hdrstop-use
92+
// RUN: rm -rf %t.pch %t.ll
93+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -pch-through-hdrstop-create -o %t.pch
94+
// RUN: %clang_cc1 %s -include-pch %t.pch -emit-llvm -pch-through-hdrstop-use -o %t.ll
95+
// RUN: ls %t.pch | FileCheck --check-prefix=CHECK-PCH %s
96+
// RUN: ls %t.ll | FileCheck --check-prefix=CHECK-OBJ %s
97+
98+
// Test with AST dump output:
99+
// RUN: rm -rf %t.pch %t.ll
100+
// RUN: %clang_cc1 -x c-header %S/Inputs/ignored-pch.h -emit-pch -o %t.pch
101+
// RUN: %clang_cc1 %s -include-pch %t.pch -ast-dump-all | FileCheck --check-prefix=CHECK-AST-PCH %s
102+
// RUN: %clang_cc1 %s -include-pch %t.pch -ignore-pch -ast-dump-all | FileCheck --check-prefix=CHECK-AST %s
103+
104+
// CHECK-PCH: ignored-pch.c.{{.*}}.pch
105+
// CHECK-OBJ: ignored-pch.c.{{.*}}.ll
106+
// CHECK-ERROR: ignored-pch.c.{{.*}}.pch{{'?}}: No such file or directory
107+
// CHECK-AST-PCH: <undeserialized declarations>
108+
// CHECK-AST-NOT: <undeserialized declarations>
109+
110+
#include "Inputs/ignored-pch.h"
111+
#pragma hdrstop
112+
int main() {
113+
return f();
114+
}

0 commit comments

Comments
 (0)