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