comparison clang-tools-extra/clangd/Compiler.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
9 #include "Compiler.h" 9 #include "Compiler.h"
10 #include "support/Logger.h" 10 #include "support/Logger.h"
11 #include "clang/Basic/TargetInfo.h" 11 #include "clang/Basic/TargetInfo.h"
12 #include "clang/Lex/PreprocessorOptions.h" 12 #include "clang/Lex/PreprocessorOptions.h"
13 #include "clang/Serialization/PCHContainerOperations.h" 13 #include "clang/Serialization/PCHContainerOperations.h"
14 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/Format.h"
15 #include "llvm/Support/FormatVariadic.h" 16 #include "llvm/Support/FormatVariadic.h"
16 17
17 namespace clang { 18 namespace clang {
18 namespace clangd { 19 namespace clangd {
45 std::vector<std::string> *CC1Args) { 46 std::vector<std::string> *CC1Args) {
46 std::vector<const char *> ArgStrs; 47 std::vector<const char *> ArgStrs;
47 for (const auto &S : Inputs.CompileCommand.CommandLine) 48 for (const auto &S : Inputs.CompileCommand.CommandLine)
48 ArgStrs.push_back(S.c_str()); 49 ArgStrs.push_back(S.c_str());
49 50
50 if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { 51 auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
51 log("Couldn't set working directory when creating compiler invocation.");
52 // We proceed anyway, our lit-tests rely on results for non-existing working
53 // dirs.
54 }
55
56 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = 52 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
57 CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false); 53 CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
58 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine( 54 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
59 ArgStrs, CommandLineDiagsEngine, Inputs.FS, 55 ArgStrs, CommandLineDiagsEngine, std::move(VFS),
60 /*ShouldRecoverOnErrors=*/true, CC1Args); 56 /*ShouldRecoverOnErrors=*/true, CC1Args);
61 if (!CI) 57 if (!CI)
62 return nullptr; 58 return nullptr;
63 // createInvocationFromCommandLine sets DisableFree. 59 // createInvocationFromCommandLine sets DisableFree.
64 CI->getFrontendOpts().DisableFree = false; 60 CI->getFrontendOpts().DisableFree = false;
65 CI->getLangOpts()->CommentOpts.ParseAllComments = true; 61 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
66 CI->getLangOpts()->RetainCommentsFromSystemHeaders = true; 62 CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
63 // Disable "clang -verify" diagnostics, they are rarely useful in clangd, and
64 // our compiler invocation set-up doesn't seem to work with it (leading
65 // assertions in VerifyDiagnosticConsumer).
66 CI->getDiagnosticOpts().VerifyDiagnostics = false;
67 67
68 // Disable any dependency outputting, we don't want to generate files or write 68 // Disable any dependency outputting, we don't want to generate files or write
69 // to stdout/stderr. 69 // to stdout/stderr.
70 CI->getDependencyOutputOpts().ShowIncludesDest = 70 CI->getDependencyOutputOpts().ShowIncludesDest =
71 ShowIncludesDestination::None; 71 ShowIncludesDestination::None;
80 CI->getPreprocessorOpts().ImplicitPCHInclude.clear(); 80 CI->getPreprocessorOpts().ImplicitPCHInclude.clear();
81 CI->getPreprocessorOpts().PrecompiledPreambleBytes = {0, false}; 81 CI->getPreprocessorOpts().PrecompiledPreambleBytes = {0, false};
82 CI->getPreprocessorOpts().PCHThroughHeader.clear(); 82 CI->getPreprocessorOpts().PCHThroughHeader.clear();
83 CI->getPreprocessorOpts().PCHWithHdrStop = false; 83 CI->getPreprocessorOpts().PCHWithHdrStop = false;
84 CI->getPreprocessorOpts().PCHWithHdrStopCreate = false; 84 CI->getPreprocessorOpts().PCHWithHdrStopCreate = false;
85 // Don't crash on `#pragma clang __debug parser_crash`
86 CI->getPreprocessorOpts().DisablePragmaDebugCrash = true;
85 87
86 // Recovery expression currently only works for C++. 88 // Always default to raw container format as clangd doesn't registry any other
87 if (CI->getLangOpts()->CPlusPlus) { 89 // and clang dies when faced with unknown formats.
88 CI->getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST; 90 CI->getHeaderSearchOpts().ModuleFormat =
89 CI->getLangOpts()->RecoveryASTType = Inputs.Opts.PreserveRecoveryASTType; 91 PCHContainerOperations().getRawReader().getFormat().str();
90 } 92
91 return CI; 93 return CI;
92 } 94 }
93 95
94 std::unique_ptr<CompilerInstance> 96 std::unique_ptr<CompilerInstance>
95 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI, 97 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
119 if (auto VFSWithRemapping = createVFSFromCompilerInvocation( 121 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
120 Clang->getInvocation(), Clang->getDiagnostics(), VFS)) 122 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
121 VFS = VFSWithRemapping; 123 VFS = VFSWithRemapping;
122 Clang->createFileManager(VFS); 124 Clang->createFileManager(VFS);
123 125
124 Clang->setTarget(TargetInfo::CreateTargetInfo( 126 if (!Clang->createTarget())
125 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
126 if (!Clang->hasTarget())
127 return nullptr; 127 return nullptr;
128 128
129 // RemappedFileBuffers will handle the lifetime of the Buffer pointer, 129 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
130 // release it. 130 // release it.
131 Buffer.release(); 131 Buffer.release();