Skip to content

Commit cb89646

Browse files
kadircetsam-mccall
authored andcommitted
[clangd] Filter pch related flags coming from the user
Summary: PCH format is unstable, hence using a preamble built with a different version of clang (or even worse, a different compiler) might result in unexpected behaviour. PCH creation on the other hand is something clangd wouldn't want to perform, as it doesn't generate any output files. This patch makes sure clangd drops any PCH related compile commands after parsing the command line args. Fixes clangd/clangd#248 Reviewers: sammccall Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79669 (cherry picked from commit 35d867a) Dropped the test as it depends on nontrivial changes from master. The code is very simple and identical to that tested on master. Fixes clangd/clangd#419
1 parent 357e79c commit cb89646

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

clang-tools-extra/clangd/Compiler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
4141
}
4242

4343
std::unique_ptr<CompilerInvocation>
44-
buildCompilerInvocation(const ParseInputs &Inputs,
45-
clang::DiagnosticConsumer &D,
44+
buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
4645
std::vector<std::string> *CC1Args) {
4746
std::vector<const char *> ArgStrs;
4847
for (const auto &S : Inputs.CompileCommand.CommandLine)
@@ -74,6 +73,15 @@ buildCompilerInvocation(const ParseInputs &Inputs,
7473
CI->getDependencyOutputOpts().HeaderIncludeOutputFile.clear();
7574
CI->getDependencyOutputOpts().DOTOutputFile.clear();
7675
CI->getDependencyOutputOpts().ModuleDependencyOutputDir.clear();
76+
77+
// Disable any pch generation/usage operations. Since serialized preamble
78+
// format is unstable, using an incompatible one might result in unexpected
79+
// behaviours, including crashes.
80+
CI->getPreprocessorOpts().ImplicitPCHInclude.clear();
81+
CI->getPreprocessorOpts().PrecompiledPreambleBytes = {0, false};
82+
CI->getPreprocessorOpts().PCHThroughHeader.clear();
83+
CI->getPreprocessorOpts().PCHWithHdrStop = false;
84+
CI->getPreprocessorOpts().PCHWithHdrStopCreate = false;
7785
return CI;
7886
}
7987

0 commit comments

Comments
 (0)