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 | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 12 | #include "Diagnostics.h" |
Ilya Biryukov | 83ca8a2 | 2017-09-20 10:46:58 +0000 | [diff] [blame] | 13 | #include "Logger.h" |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 14 | #include "SourceCode.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 15 | #include "Trace.h" |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/Basic/LangOptions.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/CompilerInvocation.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendActions.h" |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/Utils.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 22 | #include "clang/Index/IndexDataConsumer.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 23 | #include "clang/Index/IndexingAction.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Lexer.h" |
| 25 | #include "clang/Lex/MacroInfo.h" |
| 26 | #include "clang/Lex/Preprocessor.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 27 | #include "clang/Sema/Sema.h" |
| 28 | #include "clang/Serialization/ASTWriter.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 29 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/ArrayRef.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/Support/CrashRecoveryContext.h" |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 36 | using namespace clang::clangd; |
| 37 | using namespace clang; |
| 38 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 41 | bool compileCommandsAreEqual(const tooling::CompileCommand &LHS, |
| 42 | const tooling::CompileCommand &RHS) { |
| 43 | // We don't check for Output, it should not matter to clangd. |
| 44 | return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename && |
| 45 | llvm::makeArrayRef(LHS.CommandLine).equals(RHS.CommandLine); |
| 46 | } |
| 47 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 48 | template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) { |
| 49 | return Vec.capacity() * sizeof(T); |
| 50 | } |
| 51 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 52 | class DeclTrackingASTConsumer : public ASTConsumer { |
| 53 | public: |
| 54 | DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls) |
| 55 | : TopLevelDecls(TopLevelDecls) {} |
| 56 | |
| 57 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 58 | for (const Decl *D : DG) { |
| 59 | // ObjCMethodDecl are not actually top-level decls. |
| 60 | if (isa<ObjCMethodDecl>(D)) |
| 61 | continue; |
| 62 | |
| 63 | TopLevelDecls.push_back(D); |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | std::vector<const Decl *> &TopLevelDecls; |
| 70 | }; |
| 71 | |
| 72 | class ClangdFrontendAction : public SyntaxOnlyAction { |
| 73 | public: |
| 74 | std::vector<const Decl *> takeTopLevelDecls() { |
| 75 | return std::move(TopLevelDecls); |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 80 | StringRef InFile) override { |
| 81 | return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | std::vector<const Decl *> TopLevelDecls; |
| 86 | }; |
| 87 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 88 | class CppFilePreambleCallbacks : public PreambleCallbacks { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 89 | public: |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 90 | CppFilePreambleCallbacks(PathRef File, PreambleParsedCallback ParsedCallback) |
| 91 | : File(File), ParsedCallback(ParsedCallback) {} |
| 92 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 93 | std::vector<Inclusion> takeInclusions() { return std::move(Inclusions); } |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 94 | |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 95 | void AfterExecute(CompilerInstance &CI) override { |
| 96 | if (!ParsedCallback) |
| 97 | return; |
| 98 | trace::Span Tracer("Running PreambleCallback"); |
| 99 | ParsedCallback(File, CI.getASTContext(), CI.getPreprocessorPtr()); |
| 100 | } |
| 101 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 102 | void BeforeExecute(CompilerInstance &CI) override { |
| 103 | SourceMgr = &CI.getSourceManager(); |
| 104 | } |
| 105 | |
| 106 | std::unique_ptr<PPCallbacks> createPPCallbacks() override { |
| 107 | assert(SourceMgr && "SourceMgr must be set at this point"); |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 108 | return collectInclusionsInMainFileCallback( |
| 109 | *SourceMgr, |
| 110 | [this](Inclusion Inc) { Inclusions.push_back(std::move(Inc)); }); |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 113 | private: |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 114 | PathRef File; |
| 115 | PreambleParsedCallback ParsedCallback; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 116 | std::vector<Inclusion> Inclusions; |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 117 | SourceManager *SourceMgr = nullptr; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 120 | } // namespace |
| 121 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 122 | void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { |
| 123 | AST.getASTContext().getTranslationUnitDecl()->dump(OS, true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 124 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 125 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 126 | llvm::Optional<ParsedAST> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 127 | ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 128 | std::shared_ptr<const PreambleData> Preamble, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 129 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 130 | std::shared_ptr<PCHContainerOperations> PCHs, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 131 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ilya Biryukov | 981a35d | 2018-05-28 12:11:37 +0000 | [diff] [blame] | 132 | assert(CI); |
| 133 | // Command-line parsing sets DisableFree to true by default, but we don't want |
| 134 | // to leak memory in clangd. |
| 135 | CI->getFrontendOpts().DisableFree = false; |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 136 | const PrecompiledPreamble *PreamblePCH = |
| 137 | Preamble ? &Preamble->Preamble : nullptr; |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 138 | |
| 139 | StoreDiags ASTDiags; |
| 140 | auto Clang = |
| 141 | prepareCompilerInstance(std::move(CI), PreamblePCH, std::move(Buffer), |
| 142 | std::move(PCHs), std::move(VFS), ASTDiags); |
Ilya Biryukov | cec6335 | 2018-01-29 14:30:28 +0000 | [diff] [blame] | 143 | if (!Clang) |
| 144 | return llvm::None; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 145 | |
| 146 | // Recover resources if we crash before exiting this method. |
| 147 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
| 148 | Clang.get()); |
| 149 | |
| 150 | auto Action = llvm::make_unique<ClangdFrontendAction>(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 151 | const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0]; |
| 152 | if (!Action->BeginSourceFile(*Clang, MainInput)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 153 | log("BeginSourceFile() failed when building AST for " + |
| 154 | MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 155 | return llvm::None; |
| 156 | } |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 157 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 158 | std::vector<Inclusion> Inclusions; |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 159 | // Copy over the includes from the preamble, then combine with the |
| 160 | // non-preamble includes below. |
| 161 | if (Preamble) |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 162 | Inclusions = Preamble->Inclusions; |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 163 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 164 | Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback( |
| 165 | Clang->getSourceManager(), |
| 166 | [&Inclusions](Inclusion Inc) { Inclusions.push_back(std::move(Inc)); })); |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 167 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 168 | if (!Action->Execute()) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 169 | log("Execute() failed when building AST for " + MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 170 | |
| 171 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 172 | // has a longer lifetime. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 173 | Clang->getDiagnostics().setClient(new IgnoreDiagnostics); |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 174 | // CompilerInstance won't run this callback, do it directly. |
| 175 | ASTDiags.EndSourceFile(); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 176 | |
| 177 | std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 178 | return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action), |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 179 | std::move(ParsedDecls), ASTDiags.take(), |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 180 | std::move(Inclusions)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 183 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 184 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 185 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 186 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 187 | ParsedAST::~ParsedAST() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 188 | if (Action) { |
| 189 | Action->EndSourceFile(); |
| 190 | } |
| 191 | } |
| 192 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 193 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 194 | |
| 195 | const ASTContext &ParsedAST::getASTContext() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 196 | return Clang->getASTContext(); |
| 197 | } |
| 198 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 199 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 200 | |
Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 201 | std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() { |
| 202 | return Clang->getPreprocessorPtr(); |
| 203 | } |
| 204 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 205 | const Preprocessor &ParsedAST::getPreprocessor() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 206 | return Clang->getPreprocessor(); |
| 207 | } |
| 208 | |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 209 | ArrayRef<const Decl *> ParsedAST::getLocalTopLevelDecls() { |
| 210 | return LocalTopLevelDecls; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 213 | const std::vector<Diag> &ParsedAST::getDiagnostics() const { return Diags; } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 214 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 215 | std::size_t ParsedAST::getUsedBytes() const { |
| 216 | auto &AST = getASTContext(); |
| 217 | // FIXME(ibiryukov): we do not account for the dynamically allocated part of |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 218 | // Message and Fixes inside each diagnostic. |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 219 | return AST.getASTAllocatedMemory() + AST.getSideTableAllocatedMemory() + |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 220 | ::getUsedBytes(LocalTopLevelDecls) + ::getUsedBytes(Diags); |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 223 | const std::vector<Inclusion> &ParsedAST::getInclusions() const { |
| 224 | return Inclusions; |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 227 | PreambleData::PreambleData(PrecompiledPreamble Preamble, |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 228 | std::vector<Diag> Diags, |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 229 | std::vector<Inclusion> Inclusions) |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 230 | : Preamble(std::move(Preamble)), Diags(std::move(Diags)), |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 231 | Inclusions(std::move(Inclusions)) {} |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 232 | |
| 233 | ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble, |
| 234 | std::unique_ptr<CompilerInstance> Clang, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 235 | std::unique_ptr<FrontendAction> Action, |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 236 | std::vector<const Decl *> LocalTopLevelDecls, |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 237 | std::vector<Diag> Diags, std::vector<Inclusion> Inclusions) |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 238 | : Preamble(std::move(Preamble)), Clang(std::move(Clang)), |
| 239 | Action(std::move(Action)), Diags(std::move(Diags)), |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 240 | LocalTopLevelDecls(std::move(LocalTopLevelDecls)), |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 241 | Inclusions(std::move(Inclusions)) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 242 | assert(this->Clang); |
| 243 | assert(this->Action); |
| 244 | } |
| 245 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 246 | CppFile::CppFile(PathRef FileName, bool StorePreamblesInMemory, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 247 | std::shared_ptr<PCHContainerOperations> PCHs, |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 248 | PreambleParsedCallback PreambleCallback) |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 249 | : FileName(FileName), StorePreamblesInMemory(StorePreamblesInMemory), |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 250 | PCHs(std::move(PCHs)), PreambleCallback(std::move(PreambleCallback)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 251 | log("Created CppFile for " + FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 254 | llvm::Optional<std::vector<Diag>> CppFile::rebuild(ParseInputs &&Inputs) { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 255 | log("Rebuilding file " + FileName + " with command [" + |
| 256 | Inputs.CompileCommand.Directory + "] " + |
| 257 | llvm::join(Inputs.CompileCommand.CommandLine, " ")); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 258 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 259 | std::vector<const char *> ArgStrs; |
| 260 | for (const auto &S : Inputs.CompileCommand.CommandLine) |
| 261 | ArgStrs.push_back(S.c_str()); |
| 262 | |
Ilya Biryukov | a9cf311 | 2018-02-13 17:15:06 +0000 | [diff] [blame] | 263 | if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { |
| 264 | log("Couldn't set working directory"); |
| 265 | // We run parsing anyway, our lit-tests rely on results for non-existing |
| 266 | // working dirs. |
| 267 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 268 | |
| 269 | // Prepare CompilerInvocation. |
| 270 | std::unique_ptr<CompilerInvocation> CI; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 271 | { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 272 | // FIXME(ibiryukov): store diagnostics from CommandLine when we start |
| 273 | // reporting them. |
| 274 | IgnoreDiagnostics IgnoreDiagnostics; |
| 275 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 276 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 277 | &IgnoreDiagnostics, false); |
| 278 | CI = createInvocationFromCommandLine(ArgStrs, CommandLineDiagsEngine, |
| 279 | Inputs.FS); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 280 | if (!CI) { |
| 281 | log("Could not build CompilerInvocation for file " + FileName); |
| 282 | AST = llvm::None; |
| 283 | Preamble = nullptr; |
| 284 | return llvm::None; |
| 285 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 286 | // createInvocationFromCommandLine sets DisableFree. |
| 287 | CI->getFrontendOpts().DisableFree = false; |
Ilya Biryukov | c22d344 | 2018-05-16 12:32:49 +0000 | [diff] [blame] | 288 | CI->getLangOpts()->CommentOpts.ParseAllComments = true; |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 289 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 290 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 291 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 292 | llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 293 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 294 | // Compute updated Preamble. |
| 295 | std::shared_ptr<const PreambleData> NewPreamble = |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 296 | rebuildPreamble(*CI, Inputs.CompileCommand, Inputs.FS, *ContentsBuffer); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 297 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 298 | // Remove current AST to avoid wasting memory. |
| 299 | AST = llvm::None; |
| 300 | // Compute updated AST. |
| 301 | llvm::Optional<ParsedAST> NewAST; |
| 302 | { |
| 303 | trace::Span Tracer("Build"); |
| 304 | SPAN_ATTACH(Tracer, "File", FileName); |
| 305 | NewAST = ParsedAST::Build(std::move(CI), NewPreamble, |
| 306 | std::move(ContentsBuffer), PCHs, Inputs.FS); |
| 307 | } |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 308 | |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 309 | std::vector<Diag> Diagnostics; |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 310 | if (NewAST) { |
| 311 | // Collect diagnostics from both the preamble and the AST. |
| 312 | if (NewPreamble) |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 313 | Diagnostics = NewPreamble->Diags; |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 314 | Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), |
| 315 | NewAST->getDiagnostics().end()); |
| 316 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 317 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 318 | // Write the results of rebuild into class fields. |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 319 | Command = std::move(Inputs.CompileCommand); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 320 | Preamble = std::move(NewPreamble); |
| 321 | AST = std::move(NewAST); |
| 322 | return Diagnostics; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 325 | const std::shared_ptr<const PreambleData> &CppFile::getPreamble() const { |
| 326 | return Preamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 329 | ParsedAST *CppFile::getAST() const { |
| 330 | // We could add mutable to AST instead of const_cast here, but that would also |
| 331 | // allow writing to AST from const methods. |
| 332 | return AST ? const_cast<ParsedAST *>(AST.getPointer()) : nullptr; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 335 | std::size_t CppFile::getUsedBytes() const { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 336 | std::size_t Total = 0; |
| 337 | if (AST) |
| 338 | Total += AST->getUsedBytes(); |
| 339 | if (StorePreamblesInMemory && Preamble) |
| 340 | Total += Preamble->Preamble.getSize(); |
| 341 | return Total; |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 344 | std::shared_ptr<const PreambleData> |
| 345 | CppFile::rebuildPreamble(CompilerInvocation &CI, |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 346 | const tooling::CompileCommand &Command, |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 347 | IntrusiveRefCntPtr<vfs::FileSystem> FS, |
| 348 | llvm::MemoryBuffer &ContentsBuffer) const { |
| 349 | const auto &OldPreamble = this->Preamble; |
| 350 | auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), &ContentsBuffer, 0); |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 351 | if (OldPreamble && compileCommandsAreEqual(this->Command, Command) && |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 352 | OldPreamble->Preamble.CanReuse(CI, &ContentsBuffer, Bounds, FS.get())) { |
| 353 | log("Reusing preamble for file " + Twine(FileName)); |
| 354 | return OldPreamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 355 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 356 | log("Preamble for file " + Twine(FileName) + |
| 357 | " cannot be reused. Attempting to rebuild it."); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 358 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 359 | trace::Span Tracer("Preamble"); |
| 360 | SPAN_ATTACH(Tracer, "File", FileName); |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 361 | StoreDiags PreambleDiagnostics; |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 362 | IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 363 | CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(), |
Ilya Biryukov | 71028b8 | 2018-03-12 15:28:22 +0000 | [diff] [blame] | 364 | &PreambleDiagnostics, false); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 365 | |
| 366 | // Skip function bodies when building the preamble to speed up building |
| 367 | // the preamble and make it smaller. |
| 368 | assert(!CI.getFrontendOpts().SkipFunctionBodies); |
| 369 | CI.getFrontendOpts().SkipFunctionBodies = true; |
| 370 | |
Ilya Biryukov | 6c5e99e | 2018-05-24 15:50:15 +0000 | [diff] [blame] | 371 | CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 372 | auto BuiltPreamble = PrecompiledPreamble::Build( |
| 373 | CI, &ContentsBuffer, Bounds, *PreambleDiagsEngine, FS, PCHs, |
| 374 | /*StoreInMemory=*/StorePreamblesInMemory, SerializedDeclsCollector); |
| 375 | |
| 376 | // When building the AST for the main file, we do want the function |
| 377 | // bodies. |
| 378 | CI.getFrontendOpts().SkipFunctionBodies = false; |
| 379 | |
| 380 | if (BuiltPreamble) { |
| 381 | log("Built preamble of size " + Twine(BuiltPreamble->getSize()) + |
| 382 | " for file " + Twine(FileName)); |
| 383 | |
| 384 | return std::make_shared<PreambleData>( |
Ilya Biryukov | da0256f | 2018-05-28 12:23:17 +0000 | [diff] [blame] | 385 | std::move(*BuiltPreamble), PreambleDiagnostics.take(), |
| 386 | SerializedDeclsCollector.takeInclusions()); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 387 | } else { |
| 388 | log("Could not build a preamble for file " + Twine(FileName)); |
| 389 | return nullptr; |
| 390 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 391 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 392 | |
| 393 | SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit, |
| 394 | const Position &Pos, |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 395 | const FileID FID) { |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 396 | const ASTContext &AST = Unit.getASTContext(); |
| 397 | const SourceManager &SourceMgr = AST.getSourceManager(); |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 398 | auto Offset = positionToOffset(SourceMgr.getBufferData(FID), Pos); |
| 399 | if (!Offset) { |
| 400 | log("getBeginningOfIdentifier: " + toString(Offset.takeError())); |
| 401 | return SourceLocation(); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 402 | } |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 403 | SourceLocation InputLoc = SourceMgr.getComposedLoc(FID, *Offset); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 404 | |
Sam McCall | a4962cc | 2018-04-27 11:59:28 +0000 | [diff] [blame] | 405 | // GetBeginningOfToken(pos) is almost what we want, but does the wrong thing |
| 406 | // if the cursor is at the end of the identifier. |
| 407 | // Instead, we lex at GetBeginningOfToken(pos - 1). The cases are: |
| 408 | // 1) at the beginning of an identifier, we'll be looking at something |
| 409 | // that isn't an identifier. |
| 410 | // 2) at the middle or end of an identifier, we get the identifier. |
| 411 | // 3) anywhere outside an identifier, we'll get some non-identifier thing. |
| 412 | // We can't actually distinguish cases 1 and 3, but returning the original |
| 413 | // location is correct for both! |
| 414 | if (*Offset == 0) // Case 1 or 3. |
| 415 | return SourceMgr.getMacroArgExpandedLocation(InputLoc); |
| 416 | SourceLocation Before = |
| 417 | SourceMgr.getMacroArgExpandedLocation(InputLoc.getLocWithOffset(-1)); |
| 418 | Before = Lexer::GetBeginningOfToken(Before, SourceMgr, AST.getLangOpts()); |
| 419 | Token Tok; |
| 420 | if (Before.isValid() && |
| 421 | !Lexer::getRawToken(Before, Tok, SourceMgr, AST.getLangOpts(), false) && |
| 422 | Tok.is(tok::raw_identifier)) |
| 423 | return Before; // Case 2. |
| 424 | return SourceMgr.getMacroArgExpandedLocation(InputLoc); // Case 1 or 3. |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 425 | } |