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" |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 31 | #include <algorithm> |
| 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) { |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 181 | SmallString<64> Message; |
| 182 | D.FormatDiagnostic(Message); |
| 183 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 184 | if (!D.hasSourceManager() || !D.getLocation().isValid() || |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 185 | !D.getSourceManager().isInMainFile(D.getLocation())) { |
| 186 | |
| 187 | SmallString<64> Location; |
| 188 | if (D.hasSourceManager() && D.getLocation().isValid()) { |
| 189 | auto &SourceMgr = D.getSourceManager(); |
| 190 | auto Loc = SourceMgr.getFileLoc(D.getLocation()); |
| 191 | llvm::raw_svector_ostream OS(Location); |
| 192 | Loc.print(OS, SourceMgr); |
| 193 | } else { |
| 194 | Location = "<no-loc>"; |
| 195 | } |
| 196 | |
| 197 | log(llvm::formatv("Ignored diagnostic outside main file. {0}: {1}", |
| 198 | Location, Message)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 199 | return llvm::None; |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 200 | } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 201 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 202 | DiagWithFixIts Result; |
| 203 | Result.Diag.range = diagnosticRange(D, LangOpts); |
| 204 | Result.Diag.severity = getSeverity(Level); |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 205 | Result.Diag.message = Message.str(); |
| 206 | for (const FixItHint &Fix : D.getFixItHints()) |
| 207 | Result.FixIts.push_back(toTextEdit(Fix, D.getSourceManager(), LangOpts)); |
| 208 | return std::move(Result); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | class StoreDiagsConsumer : public DiagnosticConsumer { |
| 212 | public: |
| 213 | StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {} |
| 214 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 215 | // Track language options in case we need to expand token ranges. |
| 216 | void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override { |
| 217 | LangOpts = Opts; |
| 218 | } |
| 219 | |
| 220 | void EndSourceFile() override { LangOpts = llvm::None; } |
| 221 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 222 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 223 | const clang::Diagnostic &Info) override { |
| 224 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 225 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 226 | if (LangOpts) |
| 227 | if (auto D = toClangdDiag(Info, DiagLevel, *LangOpts)) |
| 228 | Output.push_back(std::move(*D)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private: |
| 232 | std::vector<DiagWithFixIts> &Output; |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 233 | llvm::Optional<LangOptions> LangOpts; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 236 | } // namespace |
| 237 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 238 | void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { |
| 239 | AST.getASTContext().getTranslationUnitDecl()->dump(OS, true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 240 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 241 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 242 | llvm::Optional<ParsedAST> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 243 | ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 244 | std::shared_ptr<const PreambleData> Preamble, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 245 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 246 | std::shared_ptr<PCHContainerOperations> PCHs, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 247 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 248 | std::vector<DiagWithFixIts> ASTDiags; |
| 249 | StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags); |
| 250 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 251 | const PrecompiledPreamble *PreamblePCH = |
| 252 | Preamble ? &Preamble->Preamble : nullptr; |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 253 | auto Clang = prepareCompilerInstance( |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 254 | std::move(CI), PreamblePCH, std::move(Buffer), std::move(PCHs), |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 255 | std::move(VFS), /*ref*/ UnitDiagsConsumer); |
Ilya Biryukov | cec6335 | 2018-01-29 14:30:28 +0000 | [diff] [blame] | 256 | if (!Clang) |
| 257 | return llvm::None; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 258 | |
| 259 | // Recover resources if we crash before exiting this method. |
| 260 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
| 261 | Clang.get()); |
| 262 | |
| 263 | auto Action = llvm::make_unique<ClangdFrontendAction>(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 264 | const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0]; |
| 265 | if (!Action->BeginSourceFile(*Clang, MainInput)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 266 | log("BeginSourceFile() failed when building AST for " + |
| 267 | MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 268 | return llvm::None; |
| 269 | } |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 270 | if (!Action->Execute()) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 271 | log("Execute() failed when building AST for " + MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 272 | |
| 273 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 274 | // has a longer lifetime. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 275 | Clang->getDiagnostics().setClient(new IgnoreDiagnostics); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 276 | |
| 277 | std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 278 | return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action), |
| 279 | std::move(ParsedDecls), std::move(ASTDiags)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 282 | namespace { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 283 | |
| 284 | SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 285 | const FileEntry *FE, Position Pos) { |
| 286 | SourceLocation InputLoc = |
| 287 | Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1); |
| 288 | return Mgr.getMacroArgExpandedLocation(InputLoc); |
| 289 | } |
| 290 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 291 | } // namespace |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 292 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 293 | void ParsedAST::ensurePreambleDeclsDeserialized() { |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 294 | if (PreambleDeclsDeserialized || !Preamble) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 295 | return; |
| 296 | |
| 297 | std::vector<const Decl *> Resolved; |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 298 | Resolved.reserve(Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 299 | |
| 300 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 301 | for (serialization::DeclID TopLevelDecl : Preamble->TopLevelDeclIDs) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 302 | // Resolve the declaration ID to an actual declaration, possibly |
| 303 | // deserializing the declaration in the process. |
| 304 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
| 305 | Resolved.push_back(D); |
| 306 | } |
| 307 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 308 | TopLevelDecls.reserve(TopLevelDecls.size() + |
| 309 | Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 310 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 311 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 312 | PreambleDeclsDeserialized = true; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 315 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 316 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 317 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 318 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 319 | ParsedAST::~ParsedAST() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 320 | if (Action) { |
| 321 | Action->EndSourceFile(); |
| 322 | } |
| 323 | } |
| 324 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 325 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 326 | |
| 327 | const ASTContext &ParsedAST::getASTContext() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 328 | return Clang->getASTContext(); |
| 329 | } |
| 330 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 331 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 332 | |
Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 333 | std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() { |
| 334 | return Clang->getPreprocessorPtr(); |
| 335 | } |
| 336 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 337 | const Preprocessor &ParsedAST::getPreprocessor() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 338 | return Clang->getPreprocessor(); |
| 339 | } |
| 340 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 341 | ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 342 | ensurePreambleDeclsDeserialized(); |
| 343 | return TopLevelDecls; |
| 344 | } |
| 345 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 346 | const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 347 | return Diags; |
| 348 | } |
| 349 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 350 | std::size_t ParsedAST::getUsedBytes() const { |
| 351 | auto &AST = getASTContext(); |
| 352 | // FIXME(ibiryukov): we do not account for the dynamically allocated part of |
| 353 | // SmallVector<FixIt> inside each Diag. |
| 354 | return AST.getASTAllocatedMemory() + AST.getSideTableAllocatedMemory() + |
| 355 | ::getUsedBytes(TopLevelDecls) + ::getUsedBytes(Diags); |
| 356 | } |
| 357 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 358 | PreambleData::PreambleData(PrecompiledPreamble Preamble, |
| 359 | std::vector<serialization::DeclID> TopLevelDeclIDs, |
| 360 | std::vector<DiagWithFixIts> Diags) |
| 361 | : Preamble(std::move(Preamble)), |
| 362 | TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)) {} |
| 363 | |
| 364 | ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble, |
| 365 | std::unique_ptr<CompilerInstance> Clang, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 366 | std::unique_ptr<FrontendAction> Action, |
| 367 | std::vector<const Decl *> TopLevelDecls, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 368 | std::vector<DiagWithFixIts> Diags) |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 369 | : Preamble(std::move(Preamble)), Clang(std::move(Clang)), |
| 370 | Action(std::move(Action)), Diags(std::move(Diags)), |
| 371 | TopLevelDecls(std::move(TopLevelDecls)), |
| 372 | PreambleDeclsDeserialized(false) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 373 | assert(this->Clang); |
| 374 | assert(this->Action); |
| 375 | } |
| 376 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 377 | CppFile::CppFile(PathRef FileName, bool StorePreamblesInMemory, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 378 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 379 | ASTParsedCallback ASTCallback) |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 380 | : FileName(FileName), StorePreamblesInMemory(StorePreamblesInMemory), |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 381 | PCHs(std::move(PCHs)), ASTCallback(std::move(ASTCallback)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 382 | log("Created CppFile for " + FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | llvm::Optional<std::vector<DiagWithFixIts>> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 386 | CppFile::rebuild(ParseInputs &&Inputs) { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 387 | log("Rebuilding file " + FileName + " with command [" + |
| 388 | Inputs.CompileCommand.Directory + "] " + |
| 389 | llvm::join(Inputs.CompileCommand.CommandLine, " ")); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 390 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 391 | std::vector<const char *> ArgStrs; |
| 392 | for (const auto &S : Inputs.CompileCommand.CommandLine) |
| 393 | ArgStrs.push_back(S.c_str()); |
| 394 | |
| 395 | Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory); |
| 396 | |
| 397 | // Prepare CompilerInvocation. |
| 398 | std::unique_ptr<CompilerInvocation> CI; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 399 | { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 400 | // FIXME(ibiryukov): store diagnostics from CommandLine when we start |
| 401 | // reporting them. |
| 402 | IgnoreDiagnostics IgnoreDiagnostics; |
| 403 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 404 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 405 | &IgnoreDiagnostics, false); |
| 406 | CI = createInvocationFromCommandLine(ArgStrs, CommandLineDiagsEngine, |
| 407 | Inputs.FS); |
| 408 | // createInvocationFromCommandLine sets DisableFree. |
| 409 | CI->getFrontendOpts().DisableFree = false; |
| 410 | } |
| 411 | assert(CI && "Couldn't create CompilerInvocation"); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 412 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 413 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 414 | llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 415 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 416 | // Compute updated Preamble. |
| 417 | std::shared_ptr<const PreambleData> NewPreamble = |
| 418 | rebuildPreamble(*CI, Inputs.FS, *ContentsBuffer); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 419 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 420 | // Remove current AST to avoid wasting memory. |
| 421 | AST = llvm::None; |
| 422 | // Compute updated AST. |
| 423 | llvm::Optional<ParsedAST> NewAST; |
| 424 | { |
| 425 | trace::Span Tracer("Build"); |
| 426 | SPAN_ATTACH(Tracer, "File", FileName); |
| 427 | NewAST = ParsedAST::Build(std::move(CI), NewPreamble, |
| 428 | std::move(ContentsBuffer), PCHs, Inputs.FS); |
| 429 | } |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 430 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 431 | std::vector<DiagWithFixIts> Diagnostics; |
| 432 | if (NewAST) { |
| 433 | // Collect diagnostics from both the preamble and the AST. |
| 434 | if (NewPreamble) |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 435 | Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(), |
| 436 | NewPreamble->Diags.end()); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 437 | Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), |
| 438 | NewAST->getDiagnostics().end()); |
| 439 | } |
| 440 | if (ASTCallback && NewAST) |
| 441 | ASTCallback(FileName, NewAST.getPointer()); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 442 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 443 | // Write the results of rebuild into class fields. |
| 444 | Preamble = std::move(NewPreamble); |
| 445 | AST = std::move(NewAST); |
| 446 | return Diagnostics; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 449 | const std::shared_ptr<const PreambleData> &CppFile::getPreamble() const { |
| 450 | return Preamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 453 | ParsedAST *CppFile::getAST() const { |
| 454 | // We could add mutable to AST instead of const_cast here, but that would also |
| 455 | // allow writing to AST from const methods. |
| 456 | return AST ? const_cast<ParsedAST *>(AST.getPointer()) : nullptr; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 459 | std::size_t CppFile::getUsedBytes() const { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 460 | std::size_t Total = 0; |
| 461 | if (AST) |
| 462 | Total += AST->getUsedBytes(); |
| 463 | if (StorePreamblesInMemory && Preamble) |
| 464 | Total += Preamble->Preamble.getSize(); |
| 465 | return Total; |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 468 | std::shared_ptr<const PreambleData> |
| 469 | CppFile::rebuildPreamble(CompilerInvocation &CI, |
| 470 | IntrusiveRefCntPtr<vfs::FileSystem> FS, |
| 471 | llvm::MemoryBuffer &ContentsBuffer) const { |
| 472 | const auto &OldPreamble = this->Preamble; |
| 473 | auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), &ContentsBuffer, 0); |
| 474 | if (OldPreamble && |
| 475 | OldPreamble->Preamble.CanReuse(CI, &ContentsBuffer, Bounds, FS.get())) { |
| 476 | log("Reusing preamble for file " + Twine(FileName)); |
| 477 | return OldPreamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 478 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 479 | log("Preamble for file " + Twine(FileName) + |
| 480 | " cannot be reused. Attempting to rebuild it."); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 481 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 482 | trace::Span Tracer("Preamble"); |
| 483 | SPAN_ATTACH(Tracer, "File", FileName); |
| 484 | std::vector<DiagWithFixIts> PreambleDiags; |
| 485 | StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags); |
| 486 | IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 487 | CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(), |
| 488 | &PreambleDiagnosticsConsumer, false); |
| 489 | |
| 490 | // Skip function bodies when building the preamble to speed up building |
| 491 | // the preamble and make it smaller. |
| 492 | assert(!CI.getFrontendOpts().SkipFunctionBodies); |
| 493 | CI.getFrontendOpts().SkipFunctionBodies = true; |
| 494 | |
| 495 | CppFilePreambleCallbacks SerializedDeclsCollector; |
| 496 | auto BuiltPreamble = PrecompiledPreamble::Build( |
| 497 | CI, &ContentsBuffer, Bounds, *PreambleDiagsEngine, FS, PCHs, |
| 498 | /*StoreInMemory=*/StorePreamblesInMemory, SerializedDeclsCollector); |
| 499 | |
| 500 | // When building the AST for the main file, we do want the function |
| 501 | // bodies. |
| 502 | CI.getFrontendOpts().SkipFunctionBodies = false; |
| 503 | |
| 504 | if (BuiltPreamble) { |
| 505 | log("Built preamble of size " + Twine(BuiltPreamble->getSize()) + |
| 506 | " for file " + Twine(FileName)); |
| 507 | |
| 508 | return std::make_shared<PreambleData>( |
| 509 | std::move(*BuiltPreamble), |
| 510 | SerializedDeclsCollector.takeTopLevelDeclIDs(), |
| 511 | std::move(PreambleDiags)); |
| 512 | } else { |
| 513 | log("Could not build a preamble for file " + Twine(FileName)); |
| 514 | return nullptr; |
| 515 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 516 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 517 | |
| 518 | SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit, |
| 519 | const Position &Pos, |
| 520 | const FileEntry *FE) { |
| 521 | // The language server protocol uses zero-based line and column numbers. |
| 522 | // Clang uses one-based numbers. |
| 523 | |
| 524 | const ASTContext &AST = Unit.getASTContext(); |
| 525 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 526 | |
| 527 | SourceLocation InputLocation = |
| 528 | getMacroArgExpandedLocation(SourceMgr, FE, Pos); |
| 529 | if (Pos.character == 0) { |
| 530 | return InputLocation; |
| 531 | } |
| 532 | |
| 533 | // This handle cases where the position is in the middle of a token or right |
| 534 | // after the end of a token. In theory we could just use GetBeginningOfToken |
| 535 | // to find the start of the token at the input position, but this doesn't |
| 536 | // work when right after the end, i.e. foo|. |
Benjamin Kramer | 5eb6a28 | 2018-02-06 20:08:23 +0000 | [diff] [blame] | 537 | // So try to go back by one and see if we're still inside an identifier |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 538 | // token. If so, Take the beginning of this token. |
| 539 | // (It should be the same identifier because you can't have two adjacent |
| 540 | // identifiers without another token in between.) |
| 541 | SourceLocation PeekBeforeLocation = getMacroArgExpandedLocation( |
| 542 | SourceMgr, FE, Position{Pos.line, Pos.character - 1}); |
| 543 | Token Result; |
| 544 | if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr, |
| 545 | AST.getLangOpts(), false)) { |
| 546 | // getRawToken failed, just use InputLocation. |
| 547 | return InputLocation; |
| 548 | } |
| 549 | |
| 550 | if (Result.is(tok::raw_identifier)) { |
| 551 | return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr, |
| 552 | AST.getLangOpts()); |
| 553 | } |
| 554 | |
| 555 | return InputLocation; |
| 556 | } |