Skip to content

Commit fa3e90f

Browse files
authored
Merge pull request #5213 from akyrtzi/pr/stable/cmd-macro-and-pch
[stable][clang][cas] ScanAndUpdateArgs: Keep the preprocessor option macros for the include-tree
2 parents 2147fab + bb2fb50 commit fa3e90f

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ static void updateCompilerInvocation(CompilerInvocation &Invocation,
8787
Invocation.getFrontendOpts().Inputs.clear();
8888
Invocation.getHeaderSearchOpts() = HeaderSearchOptions();
8989
auto &PPOpts = Invocation.getPreprocessorOpts();
90-
// We don't need these because we save the contents of the predefines buffer
91-
// and the PCH file in the include tree root.
92-
PPOpts.Macros.clear();
93-
PPOpts.MacroIncludes.clear();
90+
// We don't need this because we save the contents of the PCH file in the
91+
// include tree root.
9492
PPOpts.ImplicitPCHInclude.clear();
95-
PPOpts.Includes.clear();
93+
if (Invocation.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
94+
// We don't need these because we save the contents of the predefines
95+
// buffer in the include tree. But if we generate a PCH file we still need
96+
// to keep them as preprocessor options so that they are preserved in a
97+
// PCH file and compared with the preprocessor options of the dep-scan
98+
// invocation that uses the PCH.
99+
PPOpts.Macros.clear();
100+
PPOpts.MacroIncludes.clear();
101+
PPOpts.Includes.clear();
102+
}
96103
} else {
97104
FileSystemOpts.CASFileSystemRootID = RootID;
98105
FileSystemOpts.CASFileSystemWorkingDirectory = CASWorkingDirectory.str();
@@ -152,6 +159,12 @@ static void updateCompilerInvocation(CompilerInvocation &Invocation,
152159
remapInPlaceOrFilterOut(HeaderSearchOpts.PrebuiltModulePaths);
153160
remapInPlaceOrFilterOut(HeaderSearchOpts.VFSOverlayFiles);
154161

162+
// Preprocessor options.
163+
auto &PPOpts = Invocation.getPreprocessorOpts();
164+
remapInPlaceOrFilterOut(PPOpts.MacroIncludes);
165+
remapInPlaceOrFilterOut(PPOpts.Includes);
166+
Mapper.mapInPlaceOrClear(PPOpts.ImplicitPCHInclude);
167+
155168
// Frontend options.
156169
remapInPlaceOrFilterOutWith(
157170
FrontendOpts.Inputs, [&](FrontendInputFile &Input) {

clang/test/CAS/cmd-macro-and-pch.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// Normal compilation for baseline.
5+
// RUN: %clang_cc1 -x c-header %t/prefix.h -DSOME_MACRO=1 -emit-pch -o %t/prefix1.pch -Werror
6+
// RUN: %clang_cc1 %t/t1.c -include-pch %t/prefix1.pch -DSOME_MACRO=1 -fsyntax-only -Werror
7+
8+
// RUN: %clang -cc1depscan -o %t/pch.rsp -fdepscan=inline -fdepscan-include-tree -cc1-args \
9+
// RUN: -cc1 -x c-header %t/prefix.h -emit-pch -DSOME_MACRO=1 -fcas-path %t/cas -Werror
10+
// RUN: %clang @%t/pch.rsp -emit-pch -o %t/prefix2.pch
11+
12+
// RUN: %clang -cc1depscan -o %t/tu.rsp -fdepscan=inline -fdepscan-include-tree -cc1-args \
13+
// RUN: -cc1 %t/t1.c -include-pch %t/prefix2.pch -DSOME_MACRO=1 -fcas-path %t/cas -Werror
14+
// RUN: %clang @%t/tu.rsp -fsyntax-only
15+
16+
//--- t1.c
17+
18+
//--- prefix.h
19+
#undef SOME_MACRO
20+
#define SOME_MACRO 0

0 commit comments

Comments
 (0)