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