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