1 //===--- Compiler.cpp --------------------------------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Compiler.h" 10 #include "support/Logger.h" 11 #include "clang/Basic/TargetInfo.h" 12 #include "clang/Lex/PreprocessorOptions.h" 13 #include "clang/Serialization/PCHContainerOperations.h" 14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/FormatVariadic.h" 16 17 namespace clang { 18 namespace clangd { 19 20 void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel, 21 const clang::Diagnostic &Info) { 22 // FIXME: format lazily, in case vlog is off. 23 llvm::SmallString<64> Message; 24 Info.FormatDiagnostic(Message); 25 26 llvm::SmallString<64> Location; 27 if (Info.hasSourceManager() && Info.getLocation().isValid()) { 28 auto &SourceMgr = Info.getSourceManager(); 29 auto Loc = SourceMgr.getFileLoc(Info.getLocation()); 30 llvm::raw_svector_ostream OS(Location); 31 Loc.print(OS, SourceMgr); 32 OS << ":"; 33 } 34 35 clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message); 36 } 37 38 void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 39 const clang::Diagnostic &Info) { 40 IgnoreDiagnostics::log(DiagLevel, Info); 41 } 42 43 std::unique_ptr<CompilerInvocation> 44 buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, 45 std::vector<std::string> *CC1Args) { 46 std::vector<const char *> ArgStrs; 47 for (const auto &S : Inputs.CompileCommand.CommandLine) 48 ArgStrs.push_back(S.c_str()); 49 50 auto VFS = Inputs.FSProvider->getFileSystem(); 51 if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { 52 log("Couldn't set working directory when creating compiler invocation."); 53 // We proceed anyway, our lit-tests rely on results for non-existing working 54 // dirs. 55 } 56 57 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = 58 CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false); 59 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine( 60 ArgStrs, CommandLineDiagsEngine, std::move(VFS), 61 /*ShouldRecoverOnErrors=*/true, CC1Args); 62 if (!CI) 63 return nullptr; 64 // createInvocationFromCommandLine sets DisableFree. 65 CI->getFrontendOpts().DisableFree = false; 66 CI->getLangOpts()->CommentOpts.ParseAllComments = true; 67 CI->getLangOpts()->RetainCommentsFromSystemHeaders = true; 68 69 // Disable any dependency outputting, we don't want to generate files or write 70 // to stdout/stderr. 71 CI->getDependencyOutputOpts().ShowIncludesDest = 72 ShowIncludesDestination::None; 73 CI->getDependencyOutputOpts().OutputFile.clear(); 74 CI->getDependencyOutputOpts().HeaderIncludeOutputFile.clear(); 75 CI->getDependencyOutputOpts().DOTOutputFile.clear(); 76 CI->getDependencyOutputOpts().ModuleDependencyOutputDir.clear(); 77 78 // Disable any pch generation/usage operations. Since serialized preamble 79 // format is unstable, using an incompatible one might result in unexpected 80 // behaviours, including crashes. 81 CI->getPreprocessorOpts().ImplicitPCHInclude.clear(); 82 CI->getPreprocessorOpts().PrecompiledPreambleBytes = {0, false}; 83 CI->getPreprocessorOpts().PCHThroughHeader.clear(); 84 CI->getPreprocessorOpts().PCHWithHdrStop = false; 85 CI->getPreprocessorOpts().PCHWithHdrStopCreate = false; 86 87 // Recovery expression currently only works for C++. 88 if (CI->getLangOpts()->CPlusPlus) { 89 CI->getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST; 90 CI->getLangOpts()->RecoveryASTType = Inputs.Opts.PreserveRecoveryASTType; 91 } 92 return CI; 93 } 94 95 std::unique_ptr<CompilerInstance> 96 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI, 97 const PrecompiledPreamble *Preamble, 98 std::unique_ptr<llvm::MemoryBuffer> Buffer, 99 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, 100 DiagnosticConsumer &DiagsClient) { 101 assert(VFS && "VFS is null"); 102 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers && 103 "Setting RetainRemappedFileBuffers to true will cause a memory leak " 104 "of ContentsBuffer"); 105 106 // NOTE: we use Buffer.get() when adding remapped files, so we have to make 107 // sure it will be released if no error is emitted. 108 if (Preamble) { 109 Preamble->OverridePreamble(*CI, VFS, Buffer.get()); 110 } else { 111 CI->getPreprocessorOpts().addRemappedFile( 112 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get()); 113 } 114 115 auto Clang = std::make_unique<CompilerInstance>( 116 std::make_shared<PCHContainerOperations>()); 117 Clang->setInvocation(std::move(CI)); 118 Clang->createDiagnostics(&DiagsClient, false); 119 120 if (auto VFSWithRemapping = createVFSFromCompilerInvocation( 121 Clang->getInvocation(), Clang->getDiagnostics(), VFS)) 122 VFS = VFSWithRemapping; 123 Clang->createFileManager(VFS); 124 125 Clang->setTarget(TargetInfo::CreateTargetInfo( 126 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); 127 if (!Clang->hasTarget()) 128 return nullptr; 129 130 // RemappedFileBuffers will handle the lifetime of the Buffer pointer, 131 // release it. 132 Buffer.release(); 133 return Clang; 134 } 135 136 } // namespace clangd 137 } // namespace clang 138