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