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