Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- ClangdUnit.cpp -----------------------------------------*- C++-*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangdUnit.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 11 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 12 | #include "clang/Frontend/CompilerInstance.h" |
| 13 | #include "clang/Frontend/CompilerInvocation.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/FrontendActions.h" |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/Utils.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 16 | #include "clang/Index/IndexDataConsumer.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 17 | #include "clang/Index/IndexingAction.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Lexer.h" |
| 19 | #include "clang/Lex/MacroInfo.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 21 | #include "clang/Lex/PreprocessorOptions.h" |
| 22 | #include "clang/Sema/Sema.h" |
| 23 | #include "clang/Serialization/ASTWriter.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 24 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/ArrayRef.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
| 27 | #include "llvm/Support/CrashRecoveryContext.h" |
Krasimir Georgiev | a1de3c9 | 2017-06-15 09:11:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Format.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 31 | #include <chrono> |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 32 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 33 | using namespace clang::clangd; |
| 34 | using namespace clang; |
| 35 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | class DeclTrackingASTConsumer : public ASTConsumer { |
| 39 | public: |
| 40 | DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls) |
| 41 | : TopLevelDecls(TopLevelDecls) {} |
| 42 | |
| 43 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 44 | for (const Decl *D : DG) { |
| 45 | // ObjCMethodDecl are not actually top-level decls. |
| 46 | if (isa<ObjCMethodDecl>(D)) |
| 47 | continue; |
| 48 | |
| 49 | TopLevelDecls.push_back(D); |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | std::vector<const Decl *> &TopLevelDecls; |
| 56 | }; |
| 57 | |
| 58 | class ClangdFrontendAction : public SyntaxOnlyAction { |
| 59 | public: |
| 60 | std::vector<const Decl *> takeTopLevelDecls() { |
| 61 | return std::move(TopLevelDecls); |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 66 | StringRef InFile) override { |
| 67 | return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::vector<const Decl *> TopLevelDecls; |
| 72 | }; |
| 73 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 74 | class CppFilePreambleCallbacks : public PreambleCallbacks { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 75 | public: |
| 76 | std::vector<serialization::DeclID> takeTopLevelDeclIDs() { |
| 77 | return std::move(TopLevelDeclIDs); |
| 78 | } |
| 79 | |
| 80 | void AfterPCHEmitted(ASTWriter &Writer) override { |
| 81 | TopLevelDeclIDs.reserve(TopLevelDecls.size()); |
| 82 | for (Decl *D : TopLevelDecls) { |
| 83 | // Invalid top-level decls may not have been serialized. |
| 84 | if (D->isInvalidDecl()) |
| 85 | continue; |
| 86 | TopLevelDeclIDs.push_back(Writer.getDeclID(D)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void HandleTopLevelDecl(DeclGroupRef DG) override { |
| 91 | for (Decl *D : DG) { |
| 92 | if (isa<ObjCMethodDecl>(D)) |
| 93 | continue; |
| 94 | TopLevelDecls.push_back(D); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | std::vector<Decl *> TopLevelDecls; |
| 100 | std::vector<serialization::DeclID> TopLevelDeclIDs; |
| 101 | }; |
| 102 | |
| 103 | /// Convert from clang diagnostic level to LSP severity. |
| 104 | static int getSeverity(DiagnosticsEngine::Level L) { |
| 105 | switch (L) { |
| 106 | case DiagnosticsEngine::Remark: |
| 107 | return 4; |
| 108 | case DiagnosticsEngine::Note: |
| 109 | return 3; |
| 110 | case DiagnosticsEngine::Warning: |
| 111 | return 2; |
| 112 | case DiagnosticsEngine::Fatal: |
| 113 | case DiagnosticsEngine::Error: |
| 114 | return 1; |
| 115 | case DiagnosticsEngine::Ignored: |
| 116 | return 0; |
| 117 | } |
| 118 | llvm_unreachable("Unknown diagnostic level!"); |
| 119 | } |
| 120 | |
| 121 | llvm::Optional<DiagWithFixIts> toClangdDiag(StoredDiagnostic D) { |
| 122 | auto Location = D.getLocation(); |
| 123 | if (!Location.isValid() || !Location.getManager().isInMainFile(Location)) |
| 124 | return llvm::None; |
| 125 | |
| 126 | Position P; |
| 127 | P.line = Location.getSpellingLineNumber() - 1; |
| 128 | P.character = Location.getSpellingColumnNumber(); |
| 129 | Range R = {P, P}; |
| 130 | clangd::Diagnostic Diag = {R, getSeverity(D.getLevel()), D.getMessage()}; |
| 131 | |
| 132 | llvm::SmallVector<tooling::Replacement, 1> FixItsForDiagnostic; |
| 133 | for (const FixItHint &Fix : D.getFixIts()) { |
| 134 | FixItsForDiagnostic.push_back(clang::tooling::Replacement( |
| 135 | Location.getManager(), Fix.RemoveRange, Fix.CodeToInsert)); |
| 136 | } |
| 137 | return DiagWithFixIts{Diag, std::move(FixItsForDiagnostic)}; |
| 138 | } |
| 139 | |
| 140 | class StoreDiagsConsumer : public DiagnosticConsumer { |
| 141 | public: |
| 142 | StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {} |
| 143 | |
| 144 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 145 | const clang::Diagnostic &Info) override { |
| 146 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 147 | |
| 148 | if (auto convertedDiag = toClangdDiag(StoredDiagnostic(DiagLevel, Info))) |
| 149 | Output.push_back(std::move(*convertedDiag)); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | std::vector<DiagWithFixIts> &Output; |
| 154 | }; |
| 155 | |
| 156 | class EmptyDiagsConsumer : public DiagnosticConsumer { |
| 157 | public: |
| 158 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 159 | const clang::Diagnostic &Info) override {} |
| 160 | }; |
| 161 | |
| 162 | std::unique_ptr<CompilerInvocation> |
| 163 | createCompilerInvocation(ArrayRef<const char *> ArgList, |
| 164 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
| 165 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 166 | auto CI = createInvocationFromCommandLine(ArgList, std::move(Diags), |
| 167 | std::move(VFS)); |
| 168 | // We rely on CompilerInstance to manage the resource (i.e. free them on |
| 169 | // EndSourceFile), but that won't happen if DisableFree is set to true. |
| 170 | // Since createInvocationFromCommandLine sets it to true, we have to override |
| 171 | // it. |
| 172 | CI->getFrontendOpts().DisableFree = false; |
| 173 | return CI; |
| 174 | } |
| 175 | |
| 176 | /// Creates a CompilerInstance from \p CI, with main buffer overriden to \p |
| 177 | /// Buffer and arguments to read the PCH from \p Preamble, if \p Preamble is not |
| 178 | /// null. Note that vfs::FileSystem inside returned instance may differ from \p |
| 179 | /// VFS if additional file remapping were set in command-line arguments. |
| 180 | /// On some errors, returns null. When non-null value is returned, it's expected |
| 181 | /// to be consumed by the FrontendAction as it will have a pointer to the \p |
| 182 | /// Buffer that will only be deleted if BeginSourceFile is called. |
| 183 | std::unique_ptr<CompilerInstance> |
| 184 | prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI, |
| 185 | const PrecompiledPreamble *Preamble, |
| 186 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 187 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 188 | IntrusiveRefCntPtr<vfs::FileSystem> VFS, |
| 189 | DiagnosticConsumer &DiagsClient) { |
| 190 | assert(VFS && "VFS is null"); |
| 191 | assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers && |
| 192 | "Setting RetainRemappedFileBuffers to true will cause a memory leak " |
| 193 | "of ContentsBuffer"); |
| 194 | |
| 195 | // NOTE: we use Buffer.get() when adding remapped files, so we have to make |
| 196 | // sure it will be released if no error is emitted. |
| 197 | if (Preamble) { |
| 198 | Preamble->AddImplicitPreamble(*CI, Buffer.get()); |
| 199 | } else { |
| 200 | CI->getPreprocessorOpts().addRemappedFile( |
| 201 | CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get()); |
| 202 | } |
| 203 | |
| 204 | auto Clang = llvm::make_unique<CompilerInstance>(PCHs); |
| 205 | Clang->setInvocation(std::move(CI)); |
| 206 | Clang->createDiagnostics(&DiagsClient, false); |
| 207 | |
| 208 | if (auto VFSWithRemapping = createVFSFromCompilerInvocation( |
| 209 | Clang->getInvocation(), Clang->getDiagnostics(), VFS)) |
| 210 | VFS = VFSWithRemapping; |
| 211 | Clang->setVirtualFileSystem(VFS); |
| 212 | |
| 213 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
| 214 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
| 215 | if (!Clang->hasTarget()) |
| 216 | return nullptr; |
| 217 | |
| 218 | // RemappedFileBuffers will handle the lifetime of the Buffer pointer, |
| 219 | // release it. |
| 220 | Buffer.release(); |
| 221 | return Clang; |
| 222 | } |
| 223 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 224 | template <class T> bool futureIsReady(std::shared_future<T> const &Future) { |
| 225 | return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; |
| 226 | } |
| 227 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 228 | } // namespace |
| 229 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 230 | namespace { |
| 231 | |
| 232 | CompletionItemKind getKind(CXCursorKind K) { |
| 233 | switch (K) { |
| 234 | case CXCursor_MacroInstantiation: |
| 235 | case CXCursor_MacroDefinition: |
| 236 | return CompletionItemKind::Text; |
| 237 | case CXCursor_CXXMethod: |
| 238 | return CompletionItemKind::Method; |
| 239 | case CXCursor_FunctionDecl: |
| 240 | case CXCursor_FunctionTemplate: |
| 241 | return CompletionItemKind::Function; |
| 242 | case CXCursor_Constructor: |
| 243 | case CXCursor_Destructor: |
| 244 | return CompletionItemKind::Constructor; |
| 245 | case CXCursor_FieldDecl: |
| 246 | return CompletionItemKind::Field; |
| 247 | case CXCursor_VarDecl: |
| 248 | case CXCursor_ParmDecl: |
| 249 | return CompletionItemKind::Variable; |
| 250 | case CXCursor_ClassDecl: |
| 251 | case CXCursor_StructDecl: |
| 252 | case CXCursor_UnionDecl: |
| 253 | case CXCursor_ClassTemplate: |
| 254 | case CXCursor_ClassTemplatePartialSpecialization: |
| 255 | return CompletionItemKind::Class; |
| 256 | case CXCursor_Namespace: |
| 257 | case CXCursor_NamespaceAlias: |
| 258 | case CXCursor_NamespaceRef: |
| 259 | return CompletionItemKind::Module; |
| 260 | case CXCursor_EnumConstantDecl: |
| 261 | return CompletionItemKind::Value; |
| 262 | case CXCursor_EnumDecl: |
| 263 | return CompletionItemKind::Enum; |
| 264 | case CXCursor_TypeAliasDecl: |
| 265 | case CXCursor_TypeAliasTemplateDecl: |
| 266 | case CXCursor_TypedefDecl: |
| 267 | case CXCursor_MemberRef: |
| 268 | case CXCursor_TypeRef: |
| 269 | return CompletionItemKind::Reference; |
| 270 | default: |
| 271 | return CompletionItemKind::Missing; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | class CompletionItemsCollector : public CodeCompleteConsumer { |
| 276 | std::vector<CompletionItem> *Items; |
| 277 | std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; |
| 278 | CodeCompletionTUInfo CCTUInfo; |
| 279 | |
| 280 | public: |
| 281 | CompletionItemsCollector(std::vector<CompletionItem> *Items, |
| 282 | const CodeCompleteOptions &CodeCompleteOpts) |
| 283 | : CodeCompleteConsumer(CodeCompleteOpts, /*OutputIsBinary=*/false), |
| 284 | Items(Items), |
| 285 | Allocator(std::make_shared<clang::GlobalCodeCompletionAllocator>()), |
| 286 | CCTUInfo(Allocator) {} |
| 287 | |
| 288 | void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, |
| 289 | CodeCompletionResult *Results, |
| 290 | unsigned NumResults) override { |
| 291 | for (unsigned I = 0; I != NumResults; ++I) { |
| 292 | CodeCompletionResult &Result = Results[I]; |
| 293 | CodeCompletionString *CCS = Result.CreateCodeCompletionString( |
| 294 | S, Context, *Allocator, CCTUInfo, |
| 295 | CodeCompleteOpts.IncludeBriefComments); |
| 296 | if (CCS) { |
| 297 | CompletionItem Item; |
Krasimir Georgiev | e6035a5 | 2017-06-08 15:11:51 +0000 | [diff] [blame] | 298 | for (CodeCompletionString::Chunk C : *CCS) { |
| 299 | switch (C.Kind) { |
| 300 | case CodeCompletionString::CK_ResultType: |
| 301 | Item.detail = C.Text; |
| 302 | break; |
| 303 | case CodeCompletionString::CK_Optional: |
| 304 | break; |
| 305 | default: |
| 306 | Item.label += C.Text; |
| 307 | break; |
| 308 | } |
| 309 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 310 | assert(CCS->getTypedText()); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 311 | Item.kind = getKind(Result.CursorKind); |
Krasimir Georgiev | a1de3c9 | 2017-06-15 09:11:57 +0000 | [diff] [blame] | 312 | // Priority is a 16-bit integer, hence at most 5 digits. |
Krasimir Georgiev | a1de3c9 | 2017-06-15 09:11:57 +0000 | [diff] [blame] | 313 | assert(CCS->getPriority() < 99999 && "Expecting code completion result " |
| 314 | "priority to have at most " |
| 315 | "5-digits"); |
Ilya Biryukov | de46274 | 2017-07-27 17:43:07 +0000 | [diff] [blame] | 316 | llvm::raw_string_ostream(Item.sortText) |
| 317 | << llvm::format("%05d%s", CCS->getPriority(), CCS->getTypedText()); |
Krasimir Georgiev | a1de3c9 | 2017-06-15 09:11:57 +0000 | [diff] [blame] | 318 | Item.insertText = Item.filterText = CCS->getTypedText(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 319 | if (CCS->getBriefComment()) |
| 320 | Item.documentation = CCS->getBriefComment(); |
| 321 | Items->push_back(std::move(Item)); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | GlobalCodeCompletionAllocator &getAllocator() override { return *Allocator; } |
| 327 | |
| 328 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return CCTUInfo; } |
| 329 | }; |
| 330 | } // namespace |
| 331 | |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 332 | std::vector<CompletionItem> |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 333 | clangd::codeComplete(PathRef FileName, tooling::CompileCommand Command, |
| 334 | PrecompiledPreamble const *Preamble, StringRef Contents, |
| 335 | Position Pos, IntrusiveRefCntPtr<vfs::FileSystem> VFS, |
| 336 | std::shared_ptr<PCHContainerOperations> PCHs) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 337 | std::vector<const char *> ArgStrs; |
| 338 | for (const auto &S : Command.CommandLine) |
| 339 | ArgStrs.push_back(S.c_str()); |
| 340 | |
Krasimir Georgiev | e4130d5 | 2017-07-25 11:37:43 +0000 | [diff] [blame] | 341 | VFS->setCurrentWorkingDirectory(Command.Directory); |
| 342 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 343 | std::unique_ptr<CompilerInvocation> CI; |
| 344 | EmptyDiagsConsumer DummyDiagsConsumer; |
| 345 | { |
| 346 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 347 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 348 | &DummyDiagsConsumer, false); |
| 349 | CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS); |
| 350 | } |
| 351 | assert(CI && "Couldn't create CompilerInvocation"); |
| 352 | |
| 353 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 354 | llvm::MemoryBuffer::getMemBufferCopy(Contents, FileName); |
| 355 | |
| 356 | // Attempt to reuse the PCH from precompiled preamble, if it was built. |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 357 | if (Preamble) { |
| 358 | auto Bounds = |
| 359 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 360 | if (!Preamble->CanReuse(*CI, ContentsBuffer.get(), Bounds, VFS.get())) |
| 361 | Preamble = nullptr; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 364 | auto Clang = prepareCompilerInstance(std::move(CI), Preamble, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 365 | std::move(ContentsBuffer), PCHs, VFS, |
| 366 | DummyDiagsConsumer); |
| 367 | auto &DiagOpts = Clang->getDiagnosticOpts(); |
| 368 | DiagOpts.IgnoreWarnings = true; |
| 369 | |
| 370 | auto &FrontendOpts = Clang->getFrontendOpts(); |
| 371 | FrontendOpts.SkipFunctionBodies = true; |
| 372 | |
| 373 | FrontendOpts.CodeCompleteOpts.IncludeGlobals = true; |
| 374 | // we don't handle code patterns properly yet, disable them. |
| 375 | FrontendOpts.CodeCompleteOpts.IncludeCodePatterns = false; |
| 376 | FrontendOpts.CodeCompleteOpts.IncludeMacros = true; |
| 377 | FrontendOpts.CodeCompleteOpts.IncludeBriefComments = true; |
| 378 | |
| 379 | FrontendOpts.CodeCompletionAt.FileName = FileName; |
| 380 | FrontendOpts.CodeCompletionAt.Line = Pos.line + 1; |
| 381 | FrontendOpts.CodeCompletionAt.Column = Pos.character + 1; |
| 382 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 383 | std::vector<CompletionItem> Items; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 384 | Clang->setCodeCompletionConsumer( |
| 385 | new CompletionItemsCollector(&Items, FrontendOpts.CodeCompleteOpts)); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 386 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 387 | SyntaxOnlyAction Action; |
| 388 | if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { |
| 389 | // FIXME(ibiryukov): log errors |
| 390 | return Items; |
| 391 | } |
| 392 | if (!Action.Execute()) { |
| 393 | // FIXME(ibiryukov): log errors |
| 394 | } |
| 395 | Action.EndSourceFile(); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 396 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 397 | return Items; |
| 398 | } |
| 399 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 400 | void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { |
| 401 | AST.getASTContext().getTranslationUnitDecl()->dump(OS, true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 402 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 403 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 404 | llvm::Optional<ParsedAST> |
| 405 | ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, |
| 406 | const PrecompiledPreamble *Preamble, |
| 407 | ArrayRef<serialization::DeclID> PreambleDeclIDs, |
| 408 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 409 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 410 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 411 | |
| 412 | std::vector<DiagWithFixIts> ASTDiags; |
| 413 | StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags); |
| 414 | |
| 415 | auto Clang = |
| 416 | prepareCompilerInstance(std::move(CI), Preamble, std::move(Buffer), PCHs, |
| 417 | VFS, /*ref*/ UnitDiagsConsumer); |
| 418 | |
| 419 | // Recover resources if we crash before exiting this method. |
| 420 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
| 421 | Clang.get()); |
| 422 | |
| 423 | auto Action = llvm::make_unique<ClangdFrontendAction>(); |
| 424 | if (!Action->BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) { |
| 425 | // FIXME(ibiryukov): log error |
| 426 | return llvm::None; |
| 427 | } |
| 428 | if (!Action->Execute()) { |
| 429 | // FIXME(ibiryukov): log error |
| 430 | } |
| 431 | |
| 432 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 433 | // has a longer lifetime. |
| 434 | Clang->getDiagnostics().setClient(new EmptyDiagsConsumer); |
| 435 | |
| 436 | std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
| 437 | std::vector<serialization::DeclID> PendingDecls; |
| 438 | if (Preamble) { |
| 439 | PendingDecls.reserve(PreambleDeclIDs.size()); |
| 440 | PendingDecls.insert(PendingDecls.begin(), PreambleDeclIDs.begin(), |
| 441 | PreambleDeclIDs.end()); |
| 442 | } |
| 443 | |
| 444 | return ParsedAST(std::move(Clang), std::move(Action), std::move(ParsedDecls), |
| 445 | std::move(PendingDecls), std::move(ASTDiags)); |
| 446 | } |
| 447 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 448 | namespace { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 449 | |
| 450 | SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr, |
| 451 | const FileEntry *FE, |
| 452 | unsigned Offset) { |
| 453 | SourceLocation FileLoc = Mgr.translateFileLineCol(FE, 1, 1); |
| 454 | return Mgr.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); |
| 455 | } |
| 456 | |
| 457 | SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr, |
| 458 | const FileEntry *FE, Position Pos) { |
| 459 | SourceLocation InputLoc = |
| 460 | Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1); |
| 461 | return Mgr.getMacroArgExpandedLocation(InputLoc); |
| 462 | } |
| 463 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 464 | /// Finds declarations locations that a given source location refers to. |
| 465 | class DeclarationLocationsFinder : public index::IndexDataConsumer { |
| 466 | std::vector<Location> DeclarationLocations; |
| 467 | const SourceLocation &SearchedLocation; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 468 | const ASTContext &AST; |
| 469 | Preprocessor &PP; |
| 470 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 471 | public: |
| 472 | DeclarationLocationsFinder(raw_ostream &OS, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 473 | const SourceLocation &SearchedLocation, |
| 474 | ASTContext &AST, Preprocessor &PP) |
| 475 | : SearchedLocation(SearchedLocation), AST(AST), PP(PP) {} |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 476 | |
| 477 | std::vector<Location> takeLocations() { |
| 478 | // Don't keep the same location multiple times. |
| 479 | // This can happen when nodes in the AST are visited twice. |
| 480 | std::sort(DeclarationLocations.begin(), DeclarationLocations.end()); |
Kirill Bobyrev | 4621387 | 2017-06-28 20:57:28 +0000 | [diff] [blame] | 481 | auto last = |
| 482 | std::unique(DeclarationLocations.begin(), DeclarationLocations.end()); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 483 | DeclarationLocations.erase(last, DeclarationLocations.end()); |
| 484 | return std::move(DeclarationLocations); |
| 485 | } |
| 486 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 487 | bool |
| 488 | handleDeclOccurence(const Decl *D, index::SymbolRoleSet Roles, |
| 489 | ArrayRef<index::SymbolRelation> Relations, FileID FID, |
| 490 | unsigned Offset, |
| 491 | index::IndexDataConsumer::ASTNodeInfo ASTNode) override { |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 492 | if (isSearchedLocation(FID, Offset)) { |
| 493 | addDeclarationLocation(D->getSourceRange()); |
| 494 | } |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | private: |
| 499 | bool isSearchedLocation(FileID FID, unsigned Offset) const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 500 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 501 | return SourceMgr.getFileOffset(SearchedLocation) == Offset && |
| 502 | SourceMgr.getFileID(SearchedLocation) == FID; |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 505 | void addDeclarationLocation(const SourceRange &ValSourceRange) { |
| 506 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 507 | const LangOptions &LangOpts = AST.getLangOpts(); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 508 | SourceLocation LocStart = ValSourceRange.getBegin(); |
| 509 | SourceLocation LocEnd = Lexer::getLocForEndOfToken(ValSourceRange.getEnd(), |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 510 | 0, SourceMgr, LangOpts); |
Kirill Bobyrev | 4621387 | 2017-06-28 20:57:28 +0000 | [diff] [blame] | 511 | Position Begin; |
| 512 | Begin.line = SourceMgr.getSpellingLineNumber(LocStart) - 1; |
| 513 | Begin.character = SourceMgr.getSpellingColumnNumber(LocStart) - 1; |
| 514 | Position End; |
| 515 | End.line = SourceMgr.getSpellingLineNumber(LocEnd) - 1; |
| 516 | End.character = SourceMgr.getSpellingColumnNumber(LocEnd) - 1; |
| 517 | Range R = {Begin, End}; |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 518 | Location L; |
| 519 | L.uri = URI::fromFile( |
| 520 | SourceMgr.getFilename(SourceMgr.getSpellingLoc(LocStart))); |
| 521 | L.range = R; |
| 522 | DeclarationLocations.push_back(L); |
| 523 | } |
| 524 | |
Kirill Bobyrev | 4621387 | 2017-06-28 20:57:28 +0000 | [diff] [blame] | 525 | void finish() override { |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 526 | // Also handle possible macro at the searched location. |
| 527 | Token Result; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 528 | if (!Lexer::getRawToken(SearchedLocation, Result, AST.getSourceManager(), |
| 529 | AST.getLangOpts(), false)) { |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 530 | if (Result.is(tok::raw_identifier)) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 531 | PP.LookUpIdentifierInfo(Result); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 532 | } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 533 | IdentifierInfo *IdentifierInfo = Result.getIdentifierInfo(); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 534 | if (IdentifierInfo && IdentifierInfo->hadMacroDefinition()) { |
| 535 | std::pair<FileID, unsigned int> DecLoc = |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 536 | AST.getSourceManager().getDecomposedExpansionLoc(SearchedLocation); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 537 | // Get the definition just before the searched location so that a macro |
| 538 | // referenced in a '#undef MACRO' can still be found. |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 539 | SourceLocation BeforeSearchedLocation = getMacroArgExpandedLocation( |
| 540 | AST.getSourceManager(), |
| 541 | AST.getSourceManager().getFileEntryForID(DecLoc.first), |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 542 | DecLoc.second - 1); |
| 543 | MacroDefinition MacroDef = |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 544 | PP.getMacroDefinitionAtLoc(IdentifierInfo, BeforeSearchedLocation); |
| 545 | MacroInfo *MacroInf = MacroDef.getMacroInfo(); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 546 | if (MacroInf) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 547 | addDeclarationLocation(SourceRange(MacroInf->getDefinitionLoc(), |
| 548 | MacroInf->getDefinitionEndLoc())); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | }; |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 554 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 555 | SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos, |
| 556 | const FileEntry *FE) { |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 557 | // The language server protocol uses zero-based line and column numbers. |
| 558 | // Clang uses one-based numbers. |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 559 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 560 | const ASTContext &AST = Unit.getASTContext(); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 561 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 562 | |
| 563 | SourceLocation InputLocation = |
| 564 | getMacroArgExpandedLocation(SourceMgr, FE, Pos); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 565 | if (Pos.character == 0) { |
| 566 | return InputLocation; |
| 567 | } |
| 568 | |
| 569 | // This handle cases where the position is in the middle of a token or right |
| 570 | // after the end of a token. In theory we could just use GetBeginningOfToken |
| 571 | // to find the start of the token at the input position, but this doesn't |
| 572 | // work when right after the end, i.e. foo|. |
| 573 | // So try to go back by one and see if we're still inside the an identifier |
| 574 | // token. If so, Take the beginning of this token. |
| 575 | // (It should be the same identifier because you can't have two adjacent |
| 576 | // identifiers without another token in between.) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 577 | SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation( |
| 578 | SourceMgr, FE, Position{Pos.line, Pos.character - 1}); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 579 | Token Result; |
Ilya Biryukov | 4203d2a | 2017-06-29 17:11:32 +0000 | [diff] [blame] | 580 | if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 581 | AST.getLangOpts(), false)) { |
Ilya Biryukov | 4203d2a | 2017-06-29 17:11:32 +0000 | [diff] [blame] | 582 | // getRawToken failed, just use InputLocation. |
| 583 | return InputLocation; |
| 584 | } |
| 585 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 586 | if (Result.is(tok::raw_identifier)) { |
| 587 | return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 588 | AST.getLangOpts()); |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | return InputLocation; |
| 592 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 593 | } // namespace |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 594 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 595 | std::vector<Location> clangd::findDefinitions(ParsedAST &AST, Position Pos) { |
| 596 | const SourceManager &SourceMgr = AST.getASTContext().getSourceManager(); |
| 597 | const FileEntry *FE = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()); |
| 598 | if (!FE) |
| 599 | return {}; |
| 600 | |
| 601 | SourceLocation SourceLocationBeg = getBeginningOfIdentifier(AST, Pos, FE); |
| 602 | |
| 603 | auto DeclLocationsFinder = std::make_shared<DeclarationLocationsFinder>( |
| 604 | llvm::errs(), SourceLocationBeg, AST.getASTContext(), |
| 605 | AST.getPreprocessor()); |
| 606 | index::IndexingOptions IndexOpts; |
| 607 | IndexOpts.SystemSymbolFilter = |
| 608 | index::IndexingOptions::SystemSymbolFilterKind::All; |
| 609 | IndexOpts.IndexFunctionLocals = true; |
| 610 | |
| 611 | indexTopLevelDecls(AST.getASTContext(), AST.getTopLevelDecls(), |
| 612 | DeclLocationsFinder, IndexOpts); |
| 613 | |
| 614 | return DeclLocationsFinder->takeLocations(); |
| 615 | } |
| 616 | |
| 617 | void ParsedAST::ensurePreambleDeclsDeserialized() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 618 | if (PendingTopLevelDecls.empty()) |
| 619 | return; |
| 620 | |
| 621 | std::vector<const Decl *> Resolved; |
| 622 | Resolved.reserve(PendingTopLevelDecls.size()); |
| 623 | |
| 624 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
| 625 | for (serialization::DeclID TopLevelDecl : PendingTopLevelDecls) { |
| 626 | // Resolve the declaration ID to an actual declaration, possibly |
| 627 | // deserializing the declaration in the process. |
| 628 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
| 629 | Resolved.push_back(D); |
| 630 | } |
| 631 | |
| 632 | TopLevelDecls.reserve(TopLevelDecls.size() + PendingTopLevelDecls.size()); |
| 633 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 634 | |
| 635 | PendingTopLevelDecls.clear(); |
| 636 | } |
| 637 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 638 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 639 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 640 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 641 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 642 | ParsedAST::~ParsedAST() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 643 | if (Action) { |
| 644 | Action->EndSourceFile(); |
| 645 | } |
| 646 | } |
| 647 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 648 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 649 | |
| 650 | const ASTContext &ParsedAST::getASTContext() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 651 | return Clang->getASTContext(); |
| 652 | } |
| 653 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 654 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 655 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 656 | const Preprocessor &ParsedAST::getPreprocessor() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 657 | return Clang->getPreprocessor(); |
| 658 | } |
| 659 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 660 | ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 661 | ensurePreambleDeclsDeserialized(); |
| 662 | return TopLevelDecls; |
| 663 | } |
| 664 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 665 | const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 666 | return Diags; |
| 667 | } |
| 668 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 669 | ParsedAST::ParsedAST(std::unique_ptr<CompilerInstance> Clang, |
| 670 | std::unique_ptr<FrontendAction> Action, |
| 671 | std::vector<const Decl *> TopLevelDecls, |
| 672 | std::vector<serialization::DeclID> PendingTopLevelDecls, |
| 673 | std::vector<DiagWithFixIts> Diags) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 674 | : Clang(std::move(Clang)), Action(std::move(Action)), |
| 675 | Diags(std::move(Diags)), TopLevelDecls(std::move(TopLevelDecls)), |
| 676 | PendingTopLevelDecls(std::move(PendingTopLevelDecls)) { |
| 677 | assert(this->Clang); |
| 678 | assert(this->Action); |
| 679 | } |
| 680 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 681 | ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper) |
| 682 | : AST(std::move(Wrapper.AST)) {} |
| 683 | |
| 684 | ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST) |
| 685 | : AST(std::move(AST)) {} |
| 686 | |
| 687 | PreambleData::PreambleData(PrecompiledPreamble Preamble, |
| 688 | std::vector<serialization::DeclID> TopLevelDeclIDs, |
| 689 | std::vector<DiagWithFixIts> Diags) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 690 | : Preamble(std::move(Preamble)), |
| 691 | TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {} |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 692 | |
| 693 | std::shared_ptr<CppFile> |
| 694 | CppFile::Create(PathRef FileName, tooling::CompileCommand Command, |
| 695 | std::shared_ptr<PCHContainerOperations> PCHs) { |
| 696 | return std::shared_ptr<CppFile>( |
| 697 | new CppFile(FileName, std::move(Command), std::move(PCHs))); |
| 698 | } |
| 699 | |
| 700 | CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command, |
| 701 | std::shared_ptr<PCHContainerOperations> PCHs) |
| 702 | : FileName(FileName), Command(std::move(Command)), RebuildCounter(0), |
| 703 | RebuildInProgress(false), PCHs(std::move(PCHs)) { |
| 704 | |
| 705 | std::lock_guard<std::mutex> Lock(Mutex); |
| 706 | LatestAvailablePreamble = nullptr; |
| 707 | PreamblePromise.set_value(nullptr); |
| 708 | PreambleFuture = PreamblePromise.get_future(); |
| 709 | |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 710 | ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 711 | ASTFuture = ASTPromise.get_future(); |
| 712 | } |
| 713 | |
| 714 | void CppFile::cancelRebuilds() { |
| 715 | std::unique_lock<std::mutex> Lock(Mutex); |
| 716 | // Cancel an ongoing rebuild, if any, and wait for it to finish. |
| 717 | ++this->RebuildCounter; |
| 718 | // Rebuild asserts that futures aren't ready if rebuild is cancelled. |
| 719 | // We want to keep this invariant. |
| 720 | if (futureIsReady(PreambleFuture)) { |
| 721 | PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>(); |
| 722 | PreambleFuture = PreamblePromise.get_future(); |
| 723 | } |
| 724 | if (futureIsReady(ASTFuture)) { |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 725 | ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 726 | ASTFuture = ASTPromise.get_future(); |
| 727 | } |
| 728 | // Now wait for rebuild to finish. |
| 729 | RebuildCond.wait(Lock, [this]() { return !this->RebuildInProgress; }); |
| 730 | |
| 731 | // Return empty results for futures. |
| 732 | PreamblePromise.set_value(nullptr); |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 733 | ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | llvm::Optional<std::vector<DiagWithFixIts>> |
| 737 | CppFile::rebuild(StringRef NewContents, |
| 738 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 739 | return deferRebuild(NewContents, std::move(VFS)).get(); |
| 740 | } |
| 741 | |
| 742 | std::future<llvm::Optional<std::vector<DiagWithFixIts>>> |
| 743 | CppFile::deferRebuild(StringRef NewContents, |
| 744 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
| 745 | std::shared_ptr<const PreambleData> OldPreamble; |
| 746 | std::shared_ptr<PCHContainerOperations> PCHs; |
| 747 | unsigned RequestRebuildCounter; |
| 748 | { |
| 749 | std::unique_lock<std::mutex> Lock(Mutex); |
| 750 | // Increase RebuildCounter to cancel all ongoing FinishRebuild operations. |
| 751 | // They will try to exit as early as possible and won't call set_value on |
| 752 | // our promises. |
| 753 | RequestRebuildCounter = ++this->RebuildCounter; |
| 754 | PCHs = this->PCHs; |
| 755 | |
| 756 | // Remember the preamble to be used during rebuild. |
| 757 | OldPreamble = this->LatestAvailablePreamble; |
| 758 | // Setup std::promises and std::futures for Preamble and AST. Corresponding |
| 759 | // futures will wait until the rebuild process is finished. |
| 760 | if (futureIsReady(this->PreambleFuture)) { |
| 761 | this->PreamblePromise = |
| 762 | std::promise<std::shared_ptr<const PreambleData>>(); |
| 763 | this->PreambleFuture = this->PreamblePromise.get_future(); |
| 764 | } |
| 765 | if (futureIsReady(this->ASTFuture)) { |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 766 | this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 767 | this->ASTFuture = this->ASTPromise.get_future(); |
| 768 | } |
| 769 | } // unlock Mutex. |
| 770 | |
| 771 | // A helper to function to finish the rebuild. May be run on a different |
| 772 | // thread. |
| 773 | |
| 774 | // Don't let this CppFile die before rebuild is finished. |
| 775 | std::shared_ptr<CppFile> That = shared_from_this(); |
| 776 | auto FinishRebuild = [OldPreamble, VFS, RequestRebuildCounter, PCHs, |
| 777 | That](std::string NewContents) |
| 778 | -> llvm::Optional<std::vector<DiagWithFixIts>> { |
| 779 | // Only one execution of this method is possible at a time. |
| 780 | // RebuildGuard will wait for any ongoing rebuilds to finish and will put us |
| 781 | // into a state for doing a rebuild. |
| 782 | RebuildGuard Rebuild(*That, RequestRebuildCounter); |
| 783 | if (Rebuild.wasCancelledBeforeConstruction()) |
| 784 | return llvm::None; |
| 785 | |
| 786 | std::vector<const char *> ArgStrs; |
| 787 | for (const auto &S : That->Command.CommandLine) |
| 788 | ArgStrs.push_back(S.c_str()); |
| 789 | |
| 790 | VFS->setCurrentWorkingDirectory(That->Command.Directory); |
| 791 | |
| 792 | std::unique_ptr<CompilerInvocation> CI; |
| 793 | { |
| 794 | // FIXME(ibiryukov): store diagnostics from CommandLine when we start |
| 795 | // reporting them. |
| 796 | EmptyDiagsConsumer CommandLineDiagsConsumer; |
| 797 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 798 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 799 | &CommandLineDiagsConsumer, false); |
| 800 | CI = createCompilerInvocation(ArgStrs, CommandLineDiagsEngine, VFS); |
| 801 | } |
| 802 | assert(CI && "Couldn't create CompilerInvocation"); |
| 803 | |
| 804 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 805 | llvm::MemoryBuffer::getMemBufferCopy(NewContents, That->FileName); |
| 806 | |
| 807 | // A helper function to rebuild the preamble or reuse the existing one. Does |
| 808 | // not mutate any fields, only does the actual computation. |
| 809 | auto DoRebuildPreamble = [&]() -> std::shared_ptr<const PreambleData> { |
| 810 | auto Bounds = |
| 811 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0); |
| 812 | if (OldPreamble && OldPreamble->Preamble.CanReuse( |
| 813 | *CI, ContentsBuffer.get(), Bounds, VFS.get())) { |
| 814 | return OldPreamble; |
| 815 | } |
| 816 | |
| 817 | std::vector<DiagWithFixIts> PreambleDiags; |
| 818 | StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags); |
| 819 | IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 820 | CompilerInstance::createDiagnostics( |
| 821 | &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false); |
| 822 | CppFilePreambleCallbacks SerializedDeclsCollector; |
| 823 | auto BuiltPreamble = PrecompiledPreamble::Build( |
| 824 | *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, VFS, PCHs, |
| 825 | SerializedDeclsCollector); |
| 826 | |
| 827 | if (BuiltPreamble) { |
| 828 | return std::make_shared<PreambleData>( |
| 829 | std::move(*BuiltPreamble), |
| 830 | SerializedDeclsCollector.takeTopLevelDeclIDs(), |
| 831 | std::move(PreambleDiags)); |
| 832 | } else { |
| 833 | return nullptr; |
| 834 | } |
| 835 | }; |
| 836 | |
| 837 | // Compute updated Preamble. |
| 838 | std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble(); |
| 839 | // Publish the new Preamble. |
| 840 | { |
| 841 | std::lock_guard<std::mutex> Lock(That->Mutex); |
| 842 | // We always set LatestAvailablePreamble to the new value, hoping that it |
| 843 | // will still be usable in the further requests. |
| 844 | That->LatestAvailablePreamble = NewPreamble; |
| 845 | if (RequestRebuildCounter != That->RebuildCounter) |
| 846 | return llvm::None; // Our rebuild request was cancelled, do nothing. |
| 847 | That->PreamblePromise.set_value(NewPreamble); |
| 848 | } // unlock Mutex |
| 849 | |
| 850 | // Prepare the Preamble and supplementary data for rebuilding AST. |
| 851 | const PrecompiledPreamble *PreambleForAST = nullptr; |
| 852 | ArrayRef<serialization::DeclID> SerializedPreambleDecls = llvm::None; |
| 853 | std::vector<DiagWithFixIts> Diagnostics; |
| 854 | if (NewPreamble) { |
| 855 | PreambleForAST = &NewPreamble->Preamble; |
| 856 | SerializedPreambleDecls = NewPreamble->TopLevelDeclIDs; |
| 857 | Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(), |
| 858 | NewPreamble->Diags.end()); |
| 859 | } |
| 860 | |
| 861 | // Compute updated AST. |
| 862 | llvm::Optional<ParsedAST> NewAST = |
| 863 | ParsedAST::Build(std::move(CI), PreambleForAST, SerializedPreambleDecls, |
| 864 | std::move(ContentsBuffer), PCHs, VFS); |
| 865 | |
| 866 | if (NewAST) { |
| 867 | Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), |
| 868 | NewAST->getDiagnostics().end()); |
| 869 | } else { |
| 870 | // Don't report even Preamble diagnostics if we coulnd't build AST. |
| 871 | Diagnostics.clear(); |
| 872 | } |
| 873 | |
| 874 | // Publish the new AST. |
| 875 | { |
| 876 | std::lock_guard<std::mutex> Lock(That->Mutex); |
| 877 | if (RequestRebuildCounter != That->RebuildCounter) |
| 878 | return Diagnostics; // Our rebuild request was cancelled, don't set |
| 879 | // ASTPromise. |
| 880 | |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 881 | That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(std::move(NewAST))); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 882 | } // unlock Mutex |
| 883 | |
| 884 | return Diagnostics; |
| 885 | }; |
| 886 | |
| 887 | return std::async(std::launch::deferred, FinishRebuild, NewContents.str()); |
| 888 | } |
| 889 | |
| 890 | std::shared_future<std::shared_ptr<const PreambleData>> |
| 891 | CppFile::getPreamble() const { |
| 892 | std::lock_guard<std::mutex> Lock(Mutex); |
| 893 | return PreambleFuture; |
| 894 | } |
| 895 | |
| 896 | std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const { |
| 897 | std::lock_guard<std::mutex> Lock(Mutex); |
| 898 | return LatestAvailablePreamble; |
| 899 | } |
| 900 | |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame^] | 901 | std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 902 | std::lock_guard<std::mutex> Lock(Mutex); |
| 903 | return ASTFuture; |
| 904 | } |
| 905 | |
| 906 | tooling::CompileCommand const &CppFile::getCompileCommand() const { |
| 907 | return Command; |
| 908 | } |
| 909 | |
| 910 | CppFile::RebuildGuard::RebuildGuard(CppFile &File, |
| 911 | unsigned RequestRebuildCounter) |
| 912 | : File(File), RequestRebuildCounter(RequestRebuildCounter) { |
| 913 | std::unique_lock<std::mutex> Lock(File.Mutex); |
| 914 | WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter; |
| 915 | if (WasCancelledBeforeConstruction) |
| 916 | return; |
| 917 | |
| 918 | File.RebuildCond.wait(Lock, [&File]() { return !File.RebuildInProgress; }); |
| 919 | |
| 920 | WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter; |
| 921 | if (WasCancelledBeforeConstruction) |
| 922 | return; |
| 923 | |
| 924 | File.RebuildInProgress = true; |
| 925 | } |
| 926 | |
| 927 | bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const { |
| 928 | return WasCancelledBeforeConstruction; |
| 929 | } |
| 930 | |
| 931 | CppFile::RebuildGuard::~RebuildGuard() { |
| 932 | if (WasCancelledBeforeConstruction) |
| 933 | return; |
| 934 | |
| 935 | std::unique_lock<std::mutex> Lock(File.Mutex); |
| 936 | assert(File.RebuildInProgress); |
| 937 | File.RebuildInProgress = false; |
| 938 | |
| 939 | if (File.RebuildCounter == RequestRebuildCounter) { |
| 940 | // Our rebuild request was successful. |
| 941 | assert(futureIsReady(File.ASTFuture)); |
| 942 | assert(futureIsReady(File.PreambleFuture)); |
| 943 | } else { |
| 944 | // Our rebuild request was cancelled, because further reparse was requested. |
| 945 | assert(!futureIsReady(File.ASTFuture)); |
| 946 | assert(!futureIsReady(File.PreambleFuture)); |
| 947 | } |
| 948 | |
| 949 | Lock.unlock(); |
| 950 | File.RebuildCond.notify_all(); |
| 951 | } |