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