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