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" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 11 | #include "Compiler.h" |
Ilya Biryukov | 83ca8a2 | 2017-09-20 10:46:58 +0000 | [diff] [blame] | 12 | #include "Logger.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 13 | #include "Trace.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/CompilerInvocation.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/FrontendActions.h" |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 18 | #include "clang/Index/IndexDataConsumer.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 19 | #include "clang/Index/IndexingAction.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Lexer.h" |
| 21 | #include "clang/Lex/MacroInfo.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Serialization/ASTWriter.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 25 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ArrayRef.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/Support/CrashRecoveryContext.h" |
Krasimir Georgiev | a1de3c9 | 2017-06-15 09:11:57 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
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 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 38 | template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) { |
| 39 | return Vec.capacity() * sizeof(T); |
| 40 | } |
| 41 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 42 | class DeclTrackingASTConsumer : public ASTConsumer { |
| 43 | public: |
| 44 | DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls) |
| 45 | : TopLevelDecls(TopLevelDecls) {} |
| 46 | |
| 47 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 48 | for (const Decl *D : DG) { |
| 49 | // ObjCMethodDecl are not actually top-level decls. |
| 50 | if (isa<ObjCMethodDecl>(D)) |
| 51 | continue; |
| 52 | |
| 53 | TopLevelDecls.push_back(D); |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | std::vector<const Decl *> &TopLevelDecls; |
| 60 | }; |
| 61 | |
| 62 | class ClangdFrontendAction : public SyntaxOnlyAction { |
| 63 | public: |
| 64 | std::vector<const Decl *> takeTopLevelDecls() { |
| 65 | return std::move(TopLevelDecls); |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 70 | StringRef InFile) override { |
| 71 | return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | std::vector<const Decl *> TopLevelDecls; |
| 76 | }; |
| 77 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 78 | class CppFilePreambleCallbacks : public PreambleCallbacks { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 79 | public: |
| 80 | std::vector<serialization::DeclID> takeTopLevelDeclIDs() { |
| 81 | return std::move(TopLevelDeclIDs); |
| 82 | } |
| 83 | |
| 84 | void AfterPCHEmitted(ASTWriter &Writer) override { |
| 85 | TopLevelDeclIDs.reserve(TopLevelDecls.size()); |
| 86 | for (Decl *D : TopLevelDecls) { |
| 87 | // Invalid top-level decls may not have been serialized. |
| 88 | if (D->isInvalidDecl()) |
| 89 | continue; |
| 90 | TopLevelDeclIDs.push_back(Writer.getDeclID(D)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void HandleTopLevelDecl(DeclGroupRef DG) override { |
| 95 | for (Decl *D : DG) { |
| 96 | if (isa<ObjCMethodDecl>(D)) |
| 97 | continue; |
| 98 | TopLevelDecls.push_back(D); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | std::vector<Decl *> TopLevelDecls; |
| 104 | std::vector<serialization::DeclID> TopLevelDeclIDs; |
| 105 | }; |
| 106 | |
| 107 | /// Convert from clang diagnostic level to LSP severity. |
| 108 | static int getSeverity(DiagnosticsEngine::Level L) { |
| 109 | switch (L) { |
| 110 | case DiagnosticsEngine::Remark: |
| 111 | return 4; |
| 112 | case DiagnosticsEngine::Note: |
| 113 | return 3; |
| 114 | case DiagnosticsEngine::Warning: |
| 115 | return 2; |
| 116 | case DiagnosticsEngine::Fatal: |
| 117 | case DiagnosticsEngine::Error: |
| 118 | return 1; |
| 119 | case DiagnosticsEngine::Ignored: |
| 120 | return 0; |
| 121 | } |
| 122 | llvm_unreachable("Unknown diagnostic level!"); |
| 123 | } |
| 124 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 125 | // Checks whether a location is within a half-open range. |
| 126 | // Note that clang also uses closed source ranges, which this can't handle! |
| 127 | bool locationInRange(SourceLocation L, CharSourceRange R, |
| 128 | const SourceManager &M) { |
| 129 | assert(R.isCharRange()); |
| 130 | if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) || |
| 131 | M.getFileID(R.getBegin()) != M.getFileID(L)) |
| 132 | return false; |
| 133 | return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd()); |
| 134 | } |
| 135 | |
| 136 | // Converts a half-open clang source range to an LSP range. |
| 137 | // Note that clang also uses closed source ranges, which this can't handle! |
| 138 | Range toRange(CharSourceRange R, const SourceManager &M) { |
| 139 | // Clang is 1-based, LSP uses 0-based indexes. |
| 140 | return {{static_cast<int>(M.getSpellingLineNumber(R.getBegin())) - 1, |
| 141 | static_cast<int>(M.getSpellingColumnNumber(R.getBegin())) - 1}, |
| 142 | {static_cast<int>(M.getSpellingLineNumber(R.getEnd())) - 1, |
| 143 | static_cast<int>(M.getSpellingColumnNumber(R.getEnd())) - 1}}; |
| 144 | } |
| 145 | |
| 146 | // Clang diags have a location (shown as ^) and 0 or more ranges (~~~~). |
| 147 | // LSP needs a single range. |
| 148 | Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) { |
| 149 | auto &M = D.getSourceManager(); |
| 150 | auto Loc = M.getFileLoc(D.getLocation()); |
| 151 | // Accept the first range that contains the location. |
| 152 | for (const auto &CR : D.getRanges()) { |
| 153 | auto R = Lexer::makeFileCharRange(CR, M, L); |
| 154 | if (locationInRange(Loc, R, M)) |
| 155 | return toRange(R, M); |
| 156 | } |
| 157 | // The range may be given as a fixit hint instead. |
| 158 | for (const auto &F : D.getFixItHints()) { |
| 159 | auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L); |
| 160 | if (locationInRange(Loc, R, M)) |
| 161 | return toRange(R, M); |
| 162 | } |
| 163 | // If no suitable range is found, just use the token at the location. |
| 164 | auto R = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(Loc), M, L); |
| 165 | if (!R.isValid()) // Fall back to location only, let the editor deal with it. |
| 166 | R = CharSourceRange::getCharRange(Loc); |
| 167 | return toRange(R, M); |
| 168 | } |
| 169 | |
| 170 | TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, |
| 171 | const LangOptions &L) { |
| 172 | TextEdit Result; |
| 173 | Result.range = toRange(Lexer::makeFileCharRange(FixIt.RemoveRange, M, L), M); |
| 174 | Result.newText = FixIt.CodeToInsert; |
| 175 | return Result; |
| 176 | } |
| 177 | |
| 178 | llvm::Optional<DiagWithFixIts> toClangdDiag(const clang::Diagnostic &D, |
| 179 | DiagnosticsEngine::Level Level, |
| 180 | const LangOptions &LangOpts) { |
| 181 | if (!D.hasSourceManager() || !D.getLocation().isValid() || |
| 182 | !D.getSourceManager().isInMainFile(D.getLocation())) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 183 | return llvm::None; |
| 184 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 185 | DiagWithFixIts Result; |
| 186 | Result.Diag.range = diagnosticRange(D, LangOpts); |
| 187 | Result.Diag.severity = getSeverity(Level); |
| 188 | SmallString<64> Message; |
| 189 | D.FormatDiagnostic(Message); |
| 190 | Result.Diag.message = Message.str(); |
| 191 | for (const FixItHint &Fix : D.getFixItHints()) |
| 192 | Result.FixIts.push_back(toTextEdit(Fix, D.getSourceManager(), LangOpts)); |
| 193 | return std::move(Result); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | class StoreDiagsConsumer : public DiagnosticConsumer { |
| 197 | public: |
| 198 | StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {} |
| 199 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 200 | // Track language options in case we need to expand token ranges. |
| 201 | void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override { |
| 202 | LangOpts = Opts; |
| 203 | } |
| 204 | |
| 205 | void EndSourceFile() override { LangOpts = llvm::None; } |
| 206 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 207 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 208 | const clang::Diagnostic &Info) override { |
| 209 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 210 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 211 | if (LangOpts) |
| 212 | if (auto D = toClangdDiag(Info, DiagLevel, *LangOpts)) |
| 213 | Output.push_back(std::move(*D)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private: |
| 217 | std::vector<DiagWithFixIts> &Output; |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 218 | llvm::Optional<LangOptions> LangOpts; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 221 | template <class T> bool futureIsReady(std::shared_future<T> const &Future) { |
| 222 | return Future.wait_for(std::chrono::seconds(0)) == std::future_status::ready; |
| 223 | } |
| 224 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 225 | } // namespace |
| 226 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 227 | void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { |
| 228 | AST.getASTContext().getTranslationUnitDecl()->dump(OS, true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 229 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 230 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 231 | llvm::Optional<ParsedAST> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 232 | ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 233 | std::shared_ptr<const PreambleData> Preamble, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 234 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 235 | std::shared_ptr<PCHContainerOperations> PCHs, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 236 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 237 | |
| 238 | std::vector<DiagWithFixIts> ASTDiags; |
| 239 | StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags); |
| 240 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 241 | const PrecompiledPreamble *PreamblePCH = |
| 242 | Preamble ? &Preamble->Preamble : nullptr; |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 243 | auto Clang = prepareCompilerInstance( |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 244 | std::move(CI), PreamblePCH, std::move(Buffer), std::move(PCHs), |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 245 | std::move(VFS), /*ref*/ UnitDiagsConsumer); |
Ilya Biryukov | cec6335 | 2018-01-29 14:30:28 +0000 | [diff] [blame] | 246 | if (!Clang) |
| 247 | return llvm::None; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 248 | |
| 249 | // Recover resources if we crash before exiting this method. |
| 250 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
| 251 | Clang.get()); |
| 252 | |
| 253 | auto Action = llvm::make_unique<ClangdFrontendAction>(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 254 | const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0]; |
| 255 | if (!Action->BeginSourceFile(*Clang, MainInput)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 256 | log("BeginSourceFile() failed when building AST for " + |
| 257 | MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 258 | return llvm::None; |
| 259 | } |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 260 | if (!Action->Execute()) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 261 | log("Execute() failed when building AST for " + MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 262 | |
| 263 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 264 | // has a longer lifetime. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 265 | Clang->getDiagnostics().setClient(new IgnoreDiagnostics); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 266 | |
| 267 | std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 268 | return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action), |
| 269 | std::move(ParsedDecls), std::move(ASTDiags)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 272 | namespace { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 273 | |
| 274 | SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 275 | const FileEntry *FE, Position Pos) { |
| 276 | SourceLocation InputLoc = |
| 277 | Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1); |
| 278 | return Mgr.getMacroArgExpandedLocation(InputLoc); |
| 279 | } |
| 280 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 281 | } // namespace |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 282 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 283 | void ParsedAST::ensurePreambleDeclsDeserialized() { |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 284 | if (PreambleDeclsDeserialized || !Preamble) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 285 | return; |
| 286 | |
| 287 | std::vector<const Decl *> Resolved; |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 288 | Resolved.reserve(Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 289 | |
| 290 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 291 | for (serialization::DeclID TopLevelDecl : Preamble->TopLevelDeclIDs) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 292 | // Resolve the declaration ID to an actual declaration, possibly |
| 293 | // deserializing the declaration in the process. |
| 294 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
| 295 | Resolved.push_back(D); |
| 296 | } |
| 297 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 298 | TopLevelDecls.reserve(TopLevelDecls.size() + |
| 299 | Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 300 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 301 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 302 | PreambleDeclsDeserialized = true; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 305 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 306 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 307 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 308 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 309 | ParsedAST::~ParsedAST() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 310 | if (Action) { |
| 311 | Action->EndSourceFile(); |
| 312 | } |
| 313 | } |
| 314 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 315 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 316 | |
| 317 | const ASTContext &ParsedAST::getASTContext() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 318 | return Clang->getASTContext(); |
| 319 | } |
| 320 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 321 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 322 | |
Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 323 | std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() { |
| 324 | return Clang->getPreprocessorPtr(); |
| 325 | } |
| 326 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 327 | const Preprocessor &ParsedAST::getPreprocessor() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 328 | return Clang->getPreprocessor(); |
| 329 | } |
| 330 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 331 | ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 332 | ensurePreambleDeclsDeserialized(); |
| 333 | return TopLevelDecls; |
| 334 | } |
| 335 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 336 | const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 337 | return Diags; |
| 338 | } |
| 339 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 340 | std::size_t ParsedAST::getUsedBytes() const { |
| 341 | auto &AST = getASTContext(); |
| 342 | // FIXME(ibiryukov): we do not account for the dynamically allocated part of |
| 343 | // SmallVector<FixIt> inside each Diag. |
| 344 | return AST.getASTAllocatedMemory() + AST.getSideTableAllocatedMemory() + |
| 345 | ::getUsedBytes(TopLevelDecls) + ::getUsedBytes(Diags); |
| 346 | } |
| 347 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 348 | PreambleData::PreambleData(PrecompiledPreamble Preamble, |
| 349 | std::vector<serialization::DeclID> TopLevelDeclIDs, |
| 350 | std::vector<DiagWithFixIts> Diags) |
| 351 | : Preamble(std::move(Preamble)), |
| 352 | TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {} |
| 353 | |
| 354 | ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble, |
| 355 | std::unique_ptr<CompilerInstance> Clang, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 356 | std::unique_ptr<FrontendAction> Action, |
| 357 | std::vector<const Decl *> TopLevelDecls, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 358 | std::vector<DiagWithFixIts> Diags) |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 359 | : Preamble(std::move(Preamble)), Clang(std::move(Clang)), |
| 360 | Action(std::move(Action)), Diags(std::move(Diags)), |
| 361 | TopLevelDecls(std::move(TopLevelDecls)), |
| 362 | PreambleDeclsDeserialized(false) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 363 | assert(this->Clang); |
| 364 | assert(this->Action); |
| 365 | } |
| 366 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 367 | ParsedASTWrapper::ParsedASTWrapper(ParsedASTWrapper &&Wrapper) |
| 368 | : AST(std::move(Wrapper.AST)) {} |
| 369 | |
| 370 | ParsedASTWrapper::ParsedASTWrapper(llvm::Optional<ParsedAST> AST) |
| 371 | : AST(std::move(AST)) {} |
| 372 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 373 | std::shared_ptr<CppFile> |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 374 | CppFile::Create(PathRef FileName, bool StorePreamblesInMemory, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 375 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 376 | ASTParsedCallback ASTCallback) { |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 377 | return std::shared_ptr<CppFile>(new CppFile(FileName, StorePreamblesInMemory, |
| 378 | std::move(PCHs), |
| 379 | std::move(ASTCallback))); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 382 | CppFile::CppFile(PathRef FileName, bool StorePreamblesInMemory, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 383 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 384 | ASTParsedCallback ASTCallback) |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 385 | : FileName(FileName), StorePreamblesInMemory(StorePreamblesInMemory), |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 386 | RebuildCounter(0), RebuildInProgress(false), ASTMemUsage(0), |
| 387 | PreambleMemUsage(0), PCHs(std::move(PCHs)), |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 388 | ASTCallback(std::move(ASTCallback)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 389 | log("Created CppFile for " + FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 390 | |
| 391 | std::lock_guard<std::mutex> Lock(Mutex); |
| 392 | LatestAvailablePreamble = nullptr; |
| 393 | PreamblePromise.set_value(nullptr); |
| 394 | PreambleFuture = PreamblePromise.get_future(); |
| 395 | |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 396 | ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 397 | ASTFuture = ASTPromise.get_future(); |
| 398 | } |
| 399 | |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 400 | void CppFile::cancelRebuild() { deferCancelRebuild()(); } |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 401 | |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 402 | UniqueFunction<void()> CppFile::deferCancelRebuild() { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 403 | std::unique_lock<std::mutex> Lock(Mutex); |
| 404 | // Cancel an ongoing rebuild, if any, and wait for it to finish. |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 405 | unsigned RequestRebuildCounter = ++this->RebuildCounter; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 406 | // Rebuild asserts that futures aren't ready if rebuild is cancelled. |
| 407 | // We want to keep this invariant. |
| 408 | if (futureIsReady(PreambleFuture)) { |
| 409 | PreamblePromise = std::promise<std::shared_ptr<const PreambleData>>(); |
| 410 | PreambleFuture = PreamblePromise.get_future(); |
| 411 | } |
| 412 | if (futureIsReady(ASTFuture)) { |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 413 | ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 414 | ASTFuture = ASTPromise.get_future(); |
| 415 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 416 | |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 417 | Lock.unlock(); |
| 418 | // Notify about changes to RebuildCounter. |
| 419 | RebuildCond.notify_all(); |
| 420 | |
| 421 | std::shared_ptr<CppFile> That = shared_from_this(); |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 422 | return [That, RequestRebuildCounter]() { |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 423 | std::unique_lock<std::mutex> Lock(That->Mutex); |
| 424 | CppFile *This = &*That; |
| 425 | This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() { |
| 426 | return !This->RebuildInProgress || |
| 427 | This->RebuildCounter != RequestRebuildCounter; |
| 428 | }); |
| 429 | |
| 430 | // This computation got cancelled itself, do nothing. |
| 431 | if (This->RebuildCounter != RequestRebuildCounter) |
| 432 | return; |
| 433 | |
| 434 | // Set empty results for Promises. |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 435 | That->PreambleMemUsage = 0; |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 436 | That->PreamblePromise.set_value(nullptr); |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 437 | That->ASTMemUsage = 0; |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 438 | That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None)); |
Ilya Biryukov | 98a1fd7 | 2017-10-10 16:12:54 +0000 | [diff] [blame] | 439 | }; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | llvm::Optional<std::vector<DiagWithFixIts>> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 443 | CppFile::rebuild(ParseInputs &&Inputs) { |
| 444 | return deferRebuild(std::move(Inputs))(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 447 | UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()> |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 448 | CppFile::deferRebuild(ParseInputs &&Inputs) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 449 | std::shared_ptr<const PreambleData> OldPreamble; |
| 450 | std::shared_ptr<PCHContainerOperations> PCHs; |
| 451 | unsigned RequestRebuildCounter; |
| 452 | { |
| 453 | std::unique_lock<std::mutex> Lock(Mutex); |
| 454 | // Increase RebuildCounter to cancel all ongoing FinishRebuild operations. |
| 455 | // They will try to exit as early as possible and won't call set_value on |
| 456 | // our promises. |
| 457 | RequestRebuildCounter = ++this->RebuildCounter; |
| 458 | PCHs = this->PCHs; |
| 459 | |
| 460 | // Remember the preamble to be used during rebuild. |
| 461 | OldPreamble = this->LatestAvailablePreamble; |
| 462 | // Setup std::promises and std::futures for Preamble and AST. Corresponding |
| 463 | // futures will wait until the rebuild process is finished. |
| 464 | if (futureIsReady(this->PreambleFuture)) { |
| 465 | this->PreamblePromise = |
| 466 | std::promise<std::shared_ptr<const PreambleData>>(); |
| 467 | this->PreambleFuture = this->PreamblePromise.get_future(); |
| 468 | } |
| 469 | if (futureIsReady(this->ASTFuture)) { |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 470 | this->ASTPromise = std::promise<std::shared_ptr<ParsedASTWrapper>>(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 471 | this->ASTFuture = this->ASTPromise.get_future(); |
| 472 | } |
| 473 | } // unlock Mutex. |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 474 | // Notify about changes to RebuildCounter. |
| 475 | RebuildCond.notify_all(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 476 | |
| 477 | // A helper to function to finish the rebuild. May be run on a different |
| 478 | // thread. |
| 479 | |
| 480 | // Don't let this CppFile die before rebuild is finished. |
| 481 | std::shared_ptr<CppFile> That = shared_from_this(); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 482 | auto FinishRebuild = |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 483 | [OldPreamble, RequestRebuildCounter, PCHs, |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 484 | That](ParseInputs Inputs) mutable /* to allow changing OldPreamble. */ |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 485 | -> llvm::Optional<std::vector<DiagWithFixIts>> { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 486 | log("Rebuilding file " + That->FileName + " with command [" + |
| 487 | Inputs.CompileCommand.Directory + "] " + |
| 488 | llvm::join(Inputs.CompileCommand.CommandLine, " ")); |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 489 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 490 | // Only one execution of this method is possible at a time. |
| 491 | // RebuildGuard will wait for any ongoing rebuilds to finish and will put us |
| 492 | // into a state for doing a rebuild. |
| 493 | RebuildGuard Rebuild(*That, RequestRebuildCounter); |
| 494 | if (Rebuild.wasCancelledBeforeConstruction()) |
| 495 | return llvm::None; |
| 496 | |
| 497 | std::vector<const char *> ArgStrs; |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 498 | for (const auto &S : Inputs.CompileCommand.CommandLine) |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 499 | ArgStrs.push_back(S.c_str()); |
| 500 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 501 | Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 502 | |
| 503 | std::unique_ptr<CompilerInvocation> CI; |
| 504 | { |
| 505 | // FIXME(ibiryukov): store diagnostics from CommandLine when we start |
| 506 | // reporting them. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 507 | IgnoreDiagnostics IgnoreDiagnostics; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 508 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 509 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 510 | &IgnoreDiagnostics, false); |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 511 | CI = createInvocationFromCommandLine(ArgStrs, CommandLineDiagsEngine, |
| 512 | Inputs.FS); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 513 | // createInvocationFromCommandLine sets DisableFree. |
| 514 | CI->getFrontendOpts().DisableFree = false; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 515 | } |
| 516 | assert(CI && "Couldn't create CompilerInvocation"); |
| 517 | |
| 518 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 519 | llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, That->FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 520 | |
| 521 | // A helper function to rebuild the preamble or reuse the existing one. Does |
Ilya Biryukov | 11a0252 | 2017-11-17 19:05:56 +0000 | [diff] [blame] | 522 | // not mutate any fields of CppFile, only does the actual computation. |
| 523 | // Lamdba is marked mutable to call reset() on OldPreamble. |
| 524 | auto DoRebuildPreamble = |
| 525 | [&]() mutable -> std::shared_ptr<const PreambleData> { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 526 | auto Bounds = |
| 527 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0); |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 528 | if (OldPreamble && |
| 529 | OldPreamble->Preamble.CanReuse(*CI, ContentsBuffer.get(), Bounds, |
| 530 | Inputs.FS.get())) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 531 | log("Reusing preamble for file " + Twine(That->FileName)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 532 | return OldPreamble; |
| 533 | } |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 534 | log("Premble for file " + Twine(That->FileName) + |
| 535 | " cannot be reused. Attempting to rebuild it."); |
Ilya Biryukov | eaeea04 | 2017-12-21 14:05:28 +0000 | [diff] [blame] | 536 | // We won't need the OldPreamble anymore, release it so it can be |
| 537 | // deleted (if there are no other references to it). |
Ilya Biryukov | 11a0252 | 2017-11-17 19:05:56 +0000 | [diff] [blame] | 538 | OldPreamble.reset(); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 539 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 540 | trace::Span Tracer("Preamble"); |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 541 | SPAN_ATTACH(Tracer, "File", That->FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 542 | std::vector<DiagWithFixIts> PreambleDiags; |
| 543 | StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags); |
| 544 | IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 545 | CompilerInstance::createDiagnostics( |
| 546 | &CI->getDiagnosticOpts(), &PreambleDiagnosticsConsumer, false); |
Ilya Biryukov | da8daa3 | 2017-12-28 13:10:15 +0000 | [diff] [blame] | 547 | |
| 548 | // Skip function bodies when building the preamble to speed up building |
| 549 | // the preamble and make it smaller. |
| 550 | assert(!CI->getFrontendOpts().SkipFunctionBodies); |
| 551 | CI->getFrontendOpts().SkipFunctionBodies = true; |
| 552 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 553 | CppFilePreambleCallbacks SerializedDeclsCollector; |
| 554 | auto BuiltPreamble = PrecompiledPreamble::Build( |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 555 | *CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, Inputs.FS, |
| 556 | PCHs, |
Ilya Biryukov | e9eb7f0 | 2017-11-16 16:25:18 +0000 | [diff] [blame] | 557 | /*StoreInMemory=*/That->StorePreamblesInMemory, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 558 | SerializedDeclsCollector); |
| 559 | |
Ilya Biryukov | da8daa3 | 2017-12-28 13:10:15 +0000 | [diff] [blame] | 560 | // When building the AST for the main file, we do want the function |
| 561 | // bodies. |
| 562 | CI->getFrontendOpts().SkipFunctionBodies = false; |
| 563 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 564 | if (BuiltPreamble) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 565 | log("Built preamble of size " + Twine(BuiltPreamble->getSize()) + |
| 566 | " for file " + Twine(That->FileName)); |
Ilya Biryukov | eaeea04 | 2017-12-21 14:05:28 +0000 | [diff] [blame] | 567 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 568 | return std::make_shared<PreambleData>( |
| 569 | std::move(*BuiltPreamble), |
| 570 | SerializedDeclsCollector.takeTopLevelDeclIDs(), |
| 571 | std::move(PreambleDiags)); |
| 572 | } else { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 573 | log("Could not build a preamble for file " + Twine(That->FileName)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 574 | return nullptr; |
| 575 | } |
| 576 | }; |
| 577 | |
| 578 | // Compute updated Preamble. |
| 579 | std::shared_ptr<const PreambleData> NewPreamble = DoRebuildPreamble(); |
| 580 | // Publish the new Preamble. |
| 581 | { |
| 582 | std::lock_guard<std::mutex> Lock(That->Mutex); |
| 583 | // We always set LatestAvailablePreamble to the new value, hoping that it |
| 584 | // will still be usable in the further requests. |
| 585 | That->LatestAvailablePreamble = NewPreamble; |
| 586 | if (RequestRebuildCounter != That->RebuildCounter) |
| 587 | return llvm::None; // Our rebuild request was cancelled, do nothing. |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 588 | That->PreambleMemUsage = |
| 589 | NewPreamble ? NewPreamble->Preamble.getSize() : 0; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 590 | That->PreamblePromise.set_value(NewPreamble); |
| 591 | } // unlock Mutex |
| 592 | |
| 593 | // Prepare the Preamble and supplementary data for rebuilding AST. |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 594 | std::vector<DiagWithFixIts> Diagnostics; |
| 595 | if (NewPreamble) { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 596 | Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(), |
| 597 | NewPreamble->Diags.end()); |
| 598 | } |
| 599 | |
| 600 | // Compute updated AST. |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 601 | llvm::Optional<ParsedAST> NewAST; |
| 602 | { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 603 | trace::Span Tracer("Build"); |
Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 604 | SPAN_ATTACH(Tracer, "File", That->FileName); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 605 | NewAST = ParsedAST::Build(std::move(CI), std::move(NewPreamble), |
| 606 | std::move(ContentsBuffer), PCHs, Inputs.FS); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 607 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 608 | |
| 609 | if (NewAST) { |
| 610 | Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), |
| 611 | NewAST->getDiagnostics().end()); |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 612 | if (That->ASTCallback) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 613 | That->ASTCallback(That->FileName, NewAST.getPointer()); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 614 | } else { |
| 615 | // Don't report even Preamble diagnostics if we coulnd't build AST. |
| 616 | Diagnostics.clear(); |
| 617 | } |
| 618 | |
| 619 | // Publish the new AST. |
| 620 | { |
| 621 | std::lock_guard<std::mutex> Lock(That->Mutex); |
| 622 | if (RequestRebuildCounter != That->RebuildCounter) |
| 623 | return Diagnostics; // Our rebuild request was cancelled, don't set |
| 624 | // ASTPromise. |
| 625 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 626 | That->ASTMemUsage = NewAST ? NewAST->getUsedBytes() : 0; |
Ilya Biryukov | 574b753 | 2017-08-02 09:08:39 +0000 | [diff] [blame] | 627 | That->ASTPromise.set_value( |
| 628 | std::make_shared<ParsedASTWrapper>(std::move(NewAST))); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 629 | } // unlock Mutex |
| 630 | |
| 631 | return Diagnostics; |
| 632 | }; |
| 633 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 634 | return BindWithForward(FinishRebuild, std::move(Inputs)); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | std::shared_future<std::shared_ptr<const PreambleData>> |
| 638 | CppFile::getPreamble() const { |
| 639 | std::lock_guard<std::mutex> Lock(Mutex); |
| 640 | return PreambleFuture; |
| 641 | } |
| 642 | |
| 643 | std::shared_ptr<const PreambleData> CppFile::getPossiblyStalePreamble() const { |
| 644 | std::lock_guard<std::mutex> Lock(Mutex); |
| 645 | return LatestAvailablePreamble; |
| 646 | } |
| 647 | |
Ilya Biryukov | 6e1f3b1 | 2017-08-01 18:27:58 +0000 | [diff] [blame] | 648 | std::shared_future<std::shared_ptr<ParsedASTWrapper>> CppFile::getAST() const { |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 649 | std::lock_guard<std::mutex> Lock(Mutex); |
| 650 | return ASTFuture; |
| 651 | } |
| 652 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 653 | std::size_t CppFile::getUsedBytes() const { |
| 654 | std::lock_guard<std::mutex> Lock(Mutex); |
| 655 | // FIXME: We should not store extra size fields. When we store AST and |
| 656 | // Preamble directly, not inside futures, we could compute the sizes from the |
| 657 | // stored AST and the preamble in this function directly. |
| 658 | return ASTMemUsage + PreambleMemUsage; |
| 659 | } |
| 660 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 661 | CppFile::RebuildGuard::RebuildGuard(CppFile &File, |
| 662 | unsigned RequestRebuildCounter) |
| 663 | : File(File), RequestRebuildCounter(RequestRebuildCounter) { |
| 664 | std::unique_lock<std::mutex> Lock(File.Mutex); |
| 665 | WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter; |
| 666 | if (WasCancelledBeforeConstruction) |
| 667 | return; |
| 668 | |
Ilya Biryukov | c5ad35f | 2017-08-14 08:17:24 +0000 | [diff] [blame] | 669 | File.RebuildCond.wait(Lock, [&File, RequestRebuildCounter]() { |
| 670 | return !File.RebuildInProgress || |
| 671 | File.RebuildCounter != RequestRebuildCounter; |
| 672 | }); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 673 | |
| 674 | WasCancelledBeforeConstruction = File.RebuildCounter != RequestRebuildCounter; |
| 675 | if (WasCancelledBeforeConstruction) |
| 676 | return; |
| 677 | |
| 678 | File.RebuildInProgress = true; |
| 679 | } |
| 680 | |
| 681 | bool CppFile::RebuildGuard::wasCancelledBeforeConstruction() const { |
| 682 | return WasCancelledBeforeConstruction; |
| 683 | } |
| 684 | |
| 685 | CppFile::RebuildGuard::~RebuildGuard() { |
| 686 | if (WasCancelledBeforeConstruction) |
| 687 | return; |
| 688 | |
| 689 | std::unique_lock<std::mutex> Lock(File.Mutex); |
| 690 | assert(File.RebuildInProgress); |
| 691 | File.RebuildInProgress = false; |
| 692 | |
| 693 | if (File.RebuildCounter == RequestRebuildCounter) { |
| 694 | // Our rebuild request was successful. |
| 695 | assert(futureIsReady(File.ASTFuture)); |
| 696 | assert(futureIsReady(File.PreambleFuture)); |
| 697 | } else { |
| 698 | // Our rebuild request was cancelled, because further reparse was requested. |
| 699 | assert(!futureIsReady(File.ASTFuture)); |
| 700 | assert(!futureIsReady(File.PreambleFuture)); |
| 701 | } |
| 702 | |
| 703 | Lock.unlock(); |
| 704 | File.RebuildCond.notify_all(); |
| 705 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 706 | |
| 707 | SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit, |
| 708 | const Position &Pos, |
| 709 | const FileEntry *FE) { |
| 710 | // The language server protocol uses zero-based line and column numbers. |
| 711 | // Clang uses one-based numbers. |
| 712 | |
| 713 | const ASTContext &AST = Unit.getASTContext(); |
| 714 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 715 | |
| 716 | SourceLocation InputLocation = |
| 717 | getMacroArgExpandedLocation(SourceMgr, FE, Pos); |
| 718 | if (Pos.character == 0) { |
| 719 | return InputLocation; |
| 720 | } |
| 721 | |
| 722 | // This handle cases where the position is in the middle of a token or right |
| 723 | // after the end of a token. In theory we could just use GetBeginningOfToken |
| 724 | // to find the start of the token at the input position, but this doesn't |
| 725 | // work when right after the end, i.e. foo|. |
| 726 | // So try to go back by one and see if we're still inside the an identifier |
| 727 | // token. If so, Take the beginning of this token. |
| 728 | // (It should be the same identifier because you can't have two adjacent |
| 729 | // identifiers without another token in between.) |
| 730 | SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation( |
| 731 | SourceMgr, FE, Position{Pos.line, Pos.character - 1}); |
| 732 | Token Result; |
| 733 | if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr, |
| 734 | AST.getLangOpts(), false)) { |
| 735 | // getRawToken failed, just use InputLocation. |
| 736 | return InputLocation; |
| 737 | } |
| 738 | |
| 739 | if (Result.is(tok::raw_identifier)) { |
| 740 | return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr, |
| 741 | AST.getLangOpts()); |
| 742 | } |
| 743 | |
| 744 | return InputLocation; |
| 745 | } |