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 | 83ca8a2 | 2017-09-20 10:46:58 +0000 | [diff] [blame] | 12 | #include "Logger.h" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 13 | #include "Trace.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/CompilerInvocation.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/FrontendActions.h" |
Ilya Biryukov | 0f62ed2 | 2017-05-26 12:26:51 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 18 | #include "clang/Index/IndexDataConsumer.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 19 | #include "clang/Index/IndexingAction.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Lexer.h" |
| 21 | #include "clang/Lex/MacroInfo.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Serialization/ASTWriter.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 25 | #include "clang/Tooling/CompilationDatabase.h" |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ArrayRef.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/Support/CrashRecoveryContext.h" |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 30 | #include <algorithm> |
| 31 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 32 | using namespace clang::clangd; |
| 33 | using namespace clang; |
| 34 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 37 | bool compileCommandsAreEqual(const tooling::CompileCommand &LHS, |
| 38 | const tooling::CompileCommand &RHS) { |
| 39 | // We don't check for Output, it should not matter to clangd. |
| 40 | return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename && |
| 41 | llvm::makeArrayRef(LHS.CommandLine).equals(RHS.CommandLine); |
| 42 | } |
| 43 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 44 | template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) { |
| 45 | return Vec.capacity() * sizeof(T); |
| 46 | } |
| 47 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 48 | class DeclTrackingASTConsumer : public ASTConsumer { |
| 49 | public: |
| 50 | DeclTrackingASTConsumer(std::vector<const Decl *> &TopLevelDecls) |
| 51 | : TopLevelDecls(TopLevelDecls) {} |
| 52 | |
| 53 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 54 | for (const Decl *D : DG) { |
| 55 | // ObjCMethodDecl are not actually top-level decls. |
| 56 | if (isa<ObjCMethodDecl>(D)) |
| 57 | continue; |
| 58 | |
| 59 | TopLevelDecls.push_back(D); |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::vector<const Decl *> &TopLevelDecls; |
| 66 | }; |
| 67 | |
| 68 | class ClangdFrontendAction : public SyntaxOnlyAction { |
| 69 | public: |
| 70 | std::vector<const Decl *> takeTopLevelDecls() { |
| 71 | return std::move(TopLevelDecls); |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 76 | StringRef InFile) override { |
| 77 | return llvm::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | std::vector<const Decl *> TopLevelDecls; |
| 82 | }; |
| 83 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 84 | // Converts a half-open clang source range to an LSP range. |
| 85 | // Note that clang also uses closed source ranges, which this can't handle! |
| 86 | Range toRange(CharSourceRange R, const SourceManager &M) { |
| 87 | // Clang is 1-based, LSP uses 0-based indexes. |
| 88 | Position Begin; |
| 89 | Begin.line = static_cast<int>(M.getSpellingLineNumber(R.getBegin())) - 1; |
| 90 | Begin.character = |
| 91 | static_cast<int>(M.getSpellingColumnNumber(R.getBegin())) - 1; |
| 92 | |
| 93 | Position End; |
| 94 | End.line = static_cast<int>(M.getSpellingLineNumber(R.getEnd())) - 1; |
| 95 | End.character = static_cast<int>(M.getSpellingColumnNumber(R.getEnd())) - 1; |
| 96 | |
| 97 | return {Begin, End}; |
| 98 | } |
| 99 | |
| 100 | class InclusionLocationsCollector : public PPCallbacks { |
| 101 | public: |
| 102 | InclusionLocationsCollector(SourceManager &SourceMgr, |
| 103 | InclusionLocations &IncLocations) |
| 104 | : SourceMgr(SourceMgr), IncLocations(IncLocations) {} |
| 105 | |
| 106 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 107 | StringRef FileName, bool IsAngled, |
| 108 | CharSourceRange FilenameRange, const FileEntry *File, |
| 109 | StringRef SearchPath, StringRef RelativePath, |
| 110 | const Module *Imported) override { |
| 111 | auto SR = FilenameRange.getAsRange(); |
| 112 | if (SR.isInvalid() || !File || File->tryGetRealPathName().empty()) |
| 113 | return; |
| 114 | |
| 115 | if (SourceMgr.isInMainFile(SR.getBegin())) { |
| 116 | // Only inclusion directives in the main file make sense. The user cannot |
| 117 | // select directives not in the main file. |
| 118 | IncLocations.emplace_back(toRange(FilenameRange, SourceMgr), |
| 119 | File->tryGetRealPathName()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | SourceManager &SourceMgr; |
| 125 | InclusionLocations &IncLocations; |
| 126 | }; |
| 127 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 128 | class CppFilePreambleCallbacks : public PreambleCallbacks { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 129 | public: |
| 130 | std::vector<serialization::DeclID> takeTopLevelDeclIDs() { |
| 131 | return std::move(TopLevelDeclIDs); |
| 132 | } |
| 133 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 134 | InclusionLocations takeInclusionLocations() { |
| 135 | return std::move(IncLocations); |
| 136 | } |
| 137 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 138 | void AfterPCHEmitted(ASTWriter &Writer) override { |
| 139 | TopLevelDeclIDs.reserve(TopLevelDecls.size()); |
| 140 | for (Decl *D : TopLevelDecls) { |
| 141 | // Invalid top-level decls may not have been serialized. |
| 142 | if (D->isInvalidDecl()) |
| 143 | continue; |
| 144 | TopLevelDeclIDs.push_back(Writer.getDeclID(D)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void HandleTopLevelDecl(DeclGroupRef DG) override { |
| 149 | for (Decl *D : DG) { |
| 150 | if (isa<ObjCMethodDecl>(D)) |
| 151 | continue; |
| 152 | TopLevelDecls.push_back(D); |
| 153 | } |
| 154 | } |
| 155 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 156 | void BeforeExecute(CompilerInstance &CI) override { |
| 157 | SourceMgr = &CI.getSourceManager(); |
| 158 | } |
| 159 | |
| 160 | std::unique_ptr<PPCallbacks> createPPCallbacks() override { |
| 161 | assert(SourceMgr && "SourceMgr must be set at this point"); |
| 162 | return llvm::make_unique<InclusionLocationsCollector>(*SourceMgr, |
| 163 | IncLocations); |
| 164 | } |
| 165 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 166 | private: |
| 167 | std::vector<Decl *> TopLevelDecls; |
| 168 | std::vector<serialization::DeclID> TopLevelDeclIDs; |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 169 | InclusionLocations IncLocations; |
| 170 | SourceManager *SourceMgr = nullptr; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /// Convert from clang diagnostic level to LSP severity. |
| 174 | static int getSeverity(DiagnosticsEngine::Level L) { |
| 175 | switch (L) { |
| 176 | case DiagnosticsEngine::Remark: |
| 177 | return 4; |
| 178 | case DiagnosticsEngine::Note: |
| 179 | return 3; |
| 180 | case DiagnosticsEngine::Warning: |
| 181 | return 2; |
| 182 | case DiagnosticsEngine::Fatal: |
| 183 | case DiagnosticsEngine::Error: |
| 184 | return 1; |
| 185 | case DiagnosticsEngine::Ignored: |
| 186 | return 0; |
| 187 | } |
| 188 | llvm_unreachable("Unknown diagnostic level!"); |
| 189 | } |
| 190 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 191 | // Checks whether a location is within a half-open range. |
| 192 | // Note that clang also uses closed source ranges, which this can't handle! |
| 193 | bool locationInRange(SourceLocation L, CharSourceRange R, |
| 194 | const SourceManager &M) { |
| 195 | assert(R.isCharRange()); |
| 196 | if (!R.isValid() || M.getFileID(R.getBegin()) != M.getFileID(R.getEnd()) || |
| 197 | M.getFileID(R.getBegin()) != M.getFileID(L)) |
| 198 | return false; |
| 199 | return L != R.getEnd() && M.isPointWithin(L, R.getBegin(), R.getEnd()); |
| 200 | } |
| 201 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 202 | // Clang diags have a location (shown as ^) and 0 or more ranges (~~~~). |
| 203 | // LSP needs a single range. |
| 204 | Range diagnosticRange(const clang::Diagnostic &D, const LangOptions &L) { |
| 205 | auto &M = D.getSourceManager(); |
| 206 | auto Loc = M.getFileLoc(D.getLocation()); |
| 207 | // Accept the first range that contains the location. |
| 208 | for (const auto &CR : D.getRanges()) { |
| 209 | auto R = Lexer::makeFileCharRange(CR, M, L); |
| 210 | if (locationInRange(Loc, R, M)) |
| 211 | return toRange(R, M); |
| 212 | } |
| 213 | // The range may be given as a fixit hint instead. |
| 214 | for (const auto &F : D.getFixItHints()) { |
| 215 | auto R = Lexer::makeFileCharRange(F.RemoveRange, M, L); |
| 216 | if (locationInRange(Loc, R, M)) |
| 217 | return toRange(R, M); |
| 218 | } |
| 219 | // If no suitable range is found, just use the token at the location. |
| 220 | auto R = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(Loc), M, L); |
| 221 | if (!R.isValid()) // Fall back to location only, let the editor deal with it. |
| 222 | R = CharSourceRange::getCharRange(Loc); |
| 223 | return toRange(R, M); |
| 224 | } |
| 225 | |
| 226 | TextEdit toTextEdit(const FixItHint &FixIt, const SourceManager &M, |
| 227 | const LangOptions &L) { |
| 228 | TextEdit Result; |
| 229 | Result.range = toRange(Lexer::makeFileCharRange(FixIt.RemoveRange, M, L), M); |
| 230 | Result.newText = FixIt.CodeToInsert; |
| 231 | return Result; |
| 232 | } |
| 233 | |
| 234 | llvm::Optional<DiagWithFixIts> toClangdDiag(const clang::Diagnostic &D, |
| 235 | DiagnosticsEngine::Level Level, |
| 236 | const LangOptions &LangOpts) { |
| 237 | if (!D.hasSourceManager() || !D.getLocation().isValid() || |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 238 | !D.getSourceManager().isInMainFile(D.getLocation())) { |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 239 | IgnoreDiagnostics::log(Level, D); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 240 | return llvm::None; |
Ilya Biryukov | d14dc49 | 2018-02-01 19:06:45 +0000 | [diff] [blame] | 241 | } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 242 | |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 243 | SmallString<64> Message; |
| 244 | D.FormatDiagnostic(Message); |
| 245 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 246 | DiagWithFixIts Result; |
| 247 | Result.Diag.range = diagnosticRange(D, LangOpts); |
| 248 | Result.Diag.severity = getSeverity(Level); |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 249 | Result.Diag.message = Message.str(); |
| 250 | for (const FixItHint &Fix : D.getFixItHints()) |
| 251 | Result.FixIts.push_back(toTextEdit(Fix, D.getSourceManager(), LangOpts)); |
| 252 | return std::move(Result); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | class StoreDiagsConsumer : public DiagnosticConsumer { |
| 256 | public: |
| 257 | StoreDiagsConsumer(std::vector<DiagWithFixIts> &Output) : Output(Output) {} |
| 258 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 259 | // Track language options in case we need to expand token ranges. |
| 260 | void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override { |
| 261 | LangOpts = Opts; |
| 262 | } |
| 263 | |
| 264 | void EndSourceFile() override { LangOpts = llvm::None; } |
| 265 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 266 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 267 | const clang::Diagnostic &Info) override { |
| 268 | DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); |
| 269 | |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 270 | if (LangOpts) |
| 271 | if (auto D = toClangdDiag(Info, DiagLevel, *LangOpts)) |
| 272 | Output.push_back(std::move(*D)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private: |
| 276 | std::vector<DiagWithFixIts> &Output; |
Sam McCall | 8111d3b | 2017-12-13 08:48:42 +0000 | [diff] [blame] | 277 | llvm::Optional<LangOptions> LangOpts; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 280 | } // namespace |
| 281 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 282 | void clangd::dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) { |
| 283 | AST.getASTContext().getTranslationUnitDecl()->dump(OS, true); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 284 | } |
Ilya Biryukov | f01af68 | 2017-05-23 13:42:59 +0000 | [diff] [blame] | 285 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 286 | llvm::Optional<ParsedAST> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 287 | ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 288 | std::shared_ptr<const PreambleData> Preamble, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 289 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 290 | std::shared_ptr<PCHContainerOperations> PCHs, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 291 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 292 | std::vector<DiagWithFixIts> ASTDiags; |
| 293 | StoreDiagsConsumer UnitDiagsConsumer(/*ref*/ ASTDiags); |
| 294 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 295 | const PrecompiledPreamble *PreamblePCH = |
| 296 | Preamble ? &Preamble->Preamble : nullptr; |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 297 | auto Clang = prepareCompilerInstance( |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 298 | std::move(CI), PreamblePCH, std::move(Buffer), std::move(PCHs), |
Benjamin Kramer | 5349eed | 2017-10-28 17:32:56 +0000 | [diff] [blame] | 299 | std::move(VFS), /*ref*/ UnitDiagsConsumer); |
Ilya Biryukov | cec6335 | 2018-01-29 14:30:28 +0000 | [diff] [blame] | 300 | if (!Clang) |
| 301 | return llvm::None; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 302 | |
| 303 | // Recover resources if we crash before exiting this method. |
| 304 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
| 305 | Clang.get()); |
| 306 | |
| 307 | auto Action = llvm::make_unique<ClangdFrontendAction>(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 308 | const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0]; |
| 309 | if (!Action->BeginSourceFile(*Clang, MainInput)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 310 | log("BeginSourceFile() failed when building AST for " + |
| 311 | MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 312 | return llvm::None; |
| 313 | } |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 314 | |
| 315 | InclusionLocations IncLocations; |
| 316 | // Copy over the includes from the preamble, then combine with the |
| 317 | // non-preamble includes below. |
| 318 | if (Preamble) |
| 319 | IncLocations = Preamble->IncLocations; |
| 320 | |
| 321 | Clang->getPreprocessor().addPPCallbacks( |
| 322 | llvm::make_unique<InclusionLocationsCollector>(Clang->getSourceManager(), |
| 323 | IncLocations)); |
| 324 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 325 | if (!Action->Execute()) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 326 | log("Execute() failed when building AST for " + MainInput.getFile()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 327 | |
| 328 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 329 | // has a longer lifetime. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 330 | Clang->getDiagnostics().setClient(new IgnoreDiagnostics); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 331 | |
| 332 | std::vector<const Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 333 | return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action), |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 334 | std::move(ParsedDecls), std::move(ASTDiags), |
| 335 | std::move(IncLocations)); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 338 | namespace { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 339 | |
| 340 | SourceLocation getMacroArgExpandedLocation(const SourceManager &Mgr, |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 341 | const FileEntry *FE, Position Pos) { |
| 342 | SourceLocation InputLoc = |
| 343 | Mgr.translateFileLineCol(FE, Pos.line + 1, Pos.character + 1); |
| 344 | return Mgr.getMacroArgExpandedLocation(InputLoc); |
| 345 | } |
| 346 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 347 | } // namespace |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 348 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 349 | void ParsedAST::ensurePreambleDeclsDeserialized() { |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 350 | if (PreambleDeclsDeserialized || !Preamble) |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 351 | return; |
| 352 | |
| 353 | std::vector<const Decl *> Resolved; |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 354 | Resolved.reserve(Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 355 | |
| 356 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 357 | for (serialization::DeclID TopLevelDecl : Preamble->TopLevelDeclIDs) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 358 | // Resolve the declaration ID to an actual declaration, possibly |
| 359 | // deserializing the declaration in the process. |
| 360 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
| 361 | Resolved.push_back(D); |
| 362 | } |
| 363 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 364 | TopLevelDecls.reserve(TopLevelDecls.size() + |
| 365 | Preamble->TopLevelDeclIDs.size()); |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 366 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 367 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 368 | PreambleDeclsDeserialized = true; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 371 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 372 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 373 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 374 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 375 | ParsedAST::~ParsedAST() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 376 | if (Action) { |
| 377 | Action->EndSourceFile(); |
| 378 | } |
| 379 | } |
| 380 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 381 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 382 | |
| 383 | const ASTContext &ParsedAST::getASTContext() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 384 | return Clang->getASTContext(); |
| 385 | } |
| 386 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 387 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 388 | |
Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 389 | std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() { |
| 390 | return Clang->getPreprocessorPtr(); |
| 391 | } |
| 392 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 393 | const Preprocessor &ParsedAST::getPreprocessor() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 394 | return Clang->getPreprocessor(); |
| 395 | } |
| 396 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 397 | ArrayRef<const Decl *> ParsedAST::getTopLevelDecls() { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 398 | ensurePreambleDeclsDeserialized(); |
| 399 | return TopLevelDecls; |
| 400 | } |
| 401 | |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 402 | const std::vector<DiagWithFixIts> &ParsedAST::getDiagnostics() const { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 403 | return Diags; |
| 404 | } |
| 405 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 406 | std::size_t ParsedAST::getUsedBytes() const { |
| 407 | auto &AST = getASTContext(); |
| 408 | // FIXME(ibiryukov): we do not account for the dynamically allocated part of |
| 409 | // SmallVector<FixIt> inside each Diag. |
| 410 | return AST.getASTAllocatedMemory() + AST.getSideTableAllocatedMemory() + |
| 411 | ::getUsedBytes(TopLevelDecls) + ::getUsedBytes(Diags); |
| 412 | } |
| 413 | |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 414 | const InclusionLocations &ParsedAST::getInclusionLocations() const { |
| 415 | return IncLocations; |
| 416 | } |
| 417 | |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 418 | PreambleData::PreambleData(PrecompiledPreamble Preamble, |
| 419 | std::vector<serialization::DeclID> TopLevelDeclIDs, |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 420 | std::vector<DiagWithFixIts> Diags, |
| 421 | InclusionLocations IncLocations) |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 422 | : Preamble(std::move(Preamble)), |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 423 | TopLevelDeclIDs(std::move(TopLevelDeclIDs)), Diags(std::move(Diags)), |
| 424 | IncLocations(std::move(IncLocations)) {} |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 425 | |
| 426 | ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble, |
| 427 | std::unique_ptr<CompilerInstance> Clang, |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 428 | std::unique_ptr<FrontendAction> Action, |
| 429 | std::vector<const Decl *> TopLevelDecls, |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 430 | std::vector<DiagWithFixIts> Diags, |
| 431 | InclusionLocations IncLocations) |
Ilya Biryukov | 2660cc9 | 2017-11-24 13:04:21 +0000 | [diff] [blame] | 432 | : Preamble(std::move(Preamble)), Clang(std::move(Clang)), |
| 433 | Action(std::move(Action)), Diags(std::move(Diags)), |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 434 | TopLevelDecls(std::move(TopLevelDecls)), PreambleDeclsDeserialized(false), |
| 435 | IncLocations(std::move(IncLocations)) { |
Ilya Biryukov | 04db368 | 2017-07-21 13:29:29 +0000 | [diff] [blame] | 436 | assert(this->Clang); |
| 437 | assert(this->Action); |
| 438 | } |
| 439 | |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 440 | CppFile::CppFile(PathRef FileName, bool StorePreamblesInMemory, |
Eric Liu | bfac8f7 | 2017-12-19 18:00:37 +0000 | [diff] [blame] | 441 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 442 | ASTParsedCallback ASTCallback) |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 443 | : FileName(FileName), StorePreamblesInMemory(StorePreamblesInMemory), |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 444 | PCHs(std::move(PCHs)), ASTCallback(std::move(ASTCallback)) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 445 | log("Created CppFile for " + FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | llvm::Optional<std::vector<DiagWithFixIts>> |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 449 | CppFile::rebuild(ParseInputs &&Inputs) { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 450 | log("Rebuilding file " + FileName + " with command [" + |
| 451 | Inputs.CompileCommand.Directory + "] " + |
| 452 | llvm::join(Inputs.CompileCommand.CommandLine, " ")); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 453 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 454 | std::vector<const char *> ArgStrs; |
| 455 | for (const auto &S : Inputs.CompileCommand.CommandLine) |
| 456 | ArgStrs.push_back(S.c_str()); |
| 457 | |
Ilya Biryukov | a9cf311 | 2018-02-13 17:15:06 +0000 | [diff] [blame] | 458 | if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { |
| 459 | log("Couldn't set working directory"); |
| 460 | // We run parsing anyway, our lit-tests rely on results for non-existing |
| 461 | // working dirs. |
| 462 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 463 | |
| 464 | // Prepare CompilerInvocation. |
| 465 | std::unique_ptr<CompilerInvocation> CI; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 466 | { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 467 | // FIXME(ibiryukov): store diagnostics from CommandLine when we start |
| 468 | // reporting them. |
| 469 | IgnoreDiagnostics IgnoreDiagnostics; |
| 470 | IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 471 | CompilerInstance::createDiagnostics(new DiagnosticOptions, |
| 472 | &IgnoreDiagnostics, false); |
| 473 | CI = createInvocationFromCommandLine(ArgStrs, CommandLineDiagsEngine, |
| 474 | Inputs.FS); |
Ilya Biryukov | b6ad25c | 2018-02-09 13:51:57 +0000 | [diff] [blame] | 475 | if (!CI) { |
| 476 | log("Could not build CompilerInvocation for file " + FileName); |
| 477 | AST = llvm::None; |
| 478 | Preamble = nullptr; |
| 479 | return llvm::None; |
| 480 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 481 | // createInvocationFromCommandLine sets DisableFree. |
| 482 | CI->getFrontendOpts().DisableFree = false; |
| 483 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 484 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 485 | std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = |
| 486 | llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 487 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 488 | // Compute updated Preamble. |
| 489 | std::shared_ptr<const PreambleData> NewPreamble = |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 490 | rebuildPreamble(*CI, Inputs.CompileCommand, Inputs.FS, *ContentsBuffer); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 491 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 492 | // Remove current AST to avoid wasting memory. |
| 493 | AST = llvm::None; |
| 494 | // Compute updated AST. |
| 495 | llvm::Optional<ParsedAST> NewAST; |
| 496 | { |
| 497 | trace::Span Tracer("Build"); |
| 498 | SPAN_ATTACH(Tracer, "File", FileName); |
| 499 | NewAST = ParsedAST::Build(std::move(CI), NewPreamble, |
| 500 | std::move(ContentsBuffer), PCHs, Inputs.FS); |
| 501 | } |
Ilya Biryukov | 82b59ae | 2018-01-23 15:07:52 +0000 | [diff] [blame] | 502 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 503 | std::vector<DiagWithFixIts> Diagnostics; |
| 504 | if (NewAST) { |
| 505 | // Collect diagnostics from both the preamble and the AST. |
| 506 | if (NewPreamble) |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 507 | Diagnostics.insert(Diagnostics.begin(), NewPreamble->Diags.begin(), |
| 508 | NewPreamble->Diags.end()); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 509 | Diagnostics.insert(Diagnostics.end(), NewAST->getDiagnostics().begin(), |
| 510 | NewAST->getDiagnostics().end()); |
| 511 | } |
Ilya Biryukov | bdbf49a | 2018-02-15 16:24:34 +0000 | [diff] [blame] | 512 | if (ASTCallback && NewAST) { |
| 513 | trace::Span Tracer("Running ASTCallback"); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 514 | ASTCallback(FileName, NewAST.getPointer()); |
Ilya Biryukov | bdbf49a | 2018-02-15 16:24:34 +0000 | [diff] [blame] | 515 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 516 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 517 | // Write the results of rebuild into class fields. |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 518 | Command = std::move(Inputs.CompileCommand); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 519 | Preamble = std::move(NewPreamble); |
| 520 | AST = std::move(NewAST); |
| 521 | return Diagnostics; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 524 | const std::shared_ptr<const PreambleData> &CppFile::getPreamble() const { |
| 525 | return Preamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 528 | ParsedAST *CppFile::getAST() const { |
| 529 | // We could add mutable to AST instead of const_cast here, but that would also |
| 530 | // allow writing to AST from const methods. |
| 531 | return AST ? const_cast<ParsedAST *>(AST.getPointer()) : nullptr; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 534 | std::size_t CppFile::getUsedBytes() const { |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 535 | std::size_t Total = 0; |
| 536 | if (AST) |
| 537 | Total += AST->getUsedBytes(); |
| 538 | if (StorePreamblesInMemory && Preamble) |
| 539 | Total += Preamble->Preamble.getSize(); |
| 540 | return Total; |
Ilya Biryukov | df84234 | 2018-01-25 14:32:21 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 543 | std::shared_ptr<const PreambleData> |
| 544 | CppFile::rebuildPreamble(CompilerInvocation &CI, |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 545 | const tooling::CompileCommand &Command, |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 546 | IntrusiveRefCntPtr<vfs::FileSystem> FS, |
| 547 | llvm::MemoryBuffer &ContentsBuffer) const { |
| 548 | const auto &OldPreamble = this->Preamble; |
| 549 | auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), &ContentsBuffer, 0); |
Ilya Biryukov | 7fac6e9 | 2018-02-19 18:18:49 +0000 | [diff] [blame] | 550 | if (OldPreamble && compileCommandsAreEqual(this->Command, Command) && |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 551 | OldPreamble->Preamble.CanReuse(CI, &ContentsBuffer, Bounds, FS.get())) { |
| 552 | log("Reusing preamble for file " + Twine(FileName)); |
| 553 | return OldPreamble; |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 554 | } |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 555 | log("Preamble for file " + Twine(FileName) + |
| 556 | " cannot be reused. Attempting to rebuild it."); |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 557 | |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 558 | trace::Span Tracer("Preamble"); |
| 559 | SPAN_ATTACH(Tracer, "File", FileName); |
| 560 | std::vector<DiagWithFixIts> PreambleDiags; |
| 561 | StoreDiagsConsumer PreambleDiagnosticsConsumer(/*ref*/ PreambleDiags); |
| 562 | IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 563 | CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(), |
| 564 | &PreambleDiagnosticsConsumer, false); |
| 565 | |
| 566 | // Skip function bodies when building the preamble to speed up building |
| 567 | // the preamble and make it smaller. |
| 568 | assert(!CI.getFrontendOpts().SkipFunctionBodies); |
| 569 | CI.getFrontendOpts().SkipFunctionBodies = true; |
| 570 | |
| 571 | CppFilePreambleCallbacks SerializedDeclsCollector; |
| 572 | auto BuiltPreamble = PrecompiledPreamble::Build( |
| 573 | CI, &ContentsBuffer, Bounds, *PreambleDiagsEngine, FS, PCHs, |
| 574 | /*StoreInMemory=*/StorePreamblesInMemory, SerializedDeclsCollector); |
| 575 | |
| 576 | // When building the AST for the main file, we do want the function |
| 577 | // bodies. |
| 578 | CI.getFrontendOpts().SkipFunctionBodies = false; |
| 579 | |
| 580 | if (BuiltPreamble) { |
| 581 | log("Built preamble of size " + Twine(BuiltPreamble->getSize()) + |
| 582 | " for file " + Twine(FileName)); |
| 583 | |
| 584 | return std::make_shared<PreambleData>( |
| 585 | std::move(*BuiltPreamble), |
| 586 | SerializedDeclsCollector.takeTopLevelDeclIDs(), |
Marc-Andre Laperle | 63a1098 | 2018-02-21 02:39:08 +0000 | [diff] [blame] | 587 | std::move(PreambleDiags), |
| 588 | SerializedDeclsCollector.takeInclusionLocations()); |
Ilya Biryukov | 44ba9e0 | 2018-02-09 10:17:23 +0000 | [diff] [blame] | 589 | } else { |
| 590 | log("Could not build a preamble for file " + Twine(FileName)); |
| 591 | return nullptr; |
| 592 | } |
Ilya Biryukov | 02d5870 | 2017-08-01 15:51:38 +0000 | [diff] [blame] | 593 | } |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 594 | |
| 595 | SourceLocation clangd::getBeginningOfIdentifier(ParsedAST &Unit, |
| 596 | const Position &Pos, |
| 597 | const FileEntry *FE) { |
| 598 | // The language server protocol uses zero-based line and column numbers. |
| 599 | // Clang uses one-based numbers. |
| 600 | |
| 601 | const ASTContext &AST = Unit.getASTContext(); |
| 602 | const SourceManager &SourceMgr = AST.getSourceManager(); |
| 603 | |
| 604 | SourceLocation InputLocation = |
| 605 | getMacroArgExpandedLocation(SourceMgr, FE, Pos); |
| 606 | if (Pos.character == 0) { |
| 607 | return InputLocation; |
| 608 | } |
| 609 | |
| 610 | // This handle cases where the position is in the middle of a token or right |
| 611 | // after the end of a token. In theory we could just use GetBeginningOfToken |
| 612 | // to find the start of the token at the input position, but this doesn't |
| 613 | // work when right after the end, i.e. foo|. |
Benjamin Kramer | 5eb6a28 | 2018-02-06 20:08:23 +0000 | [diff] [blame] | 614 | // 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] | 615 | // token. If so, Take the beginning of this token. |
| 616 | // (It should be the same identifier because you can't have two adjacent |
| 617 | // identifiers without another token in between.) |
Ilya Biryukov | 7beea3a | 2018-02-14 10:52:04 +0000 | [diff] [blame] | 618 | Position PosCharBehind = Pos; |
| 619 | --PosCharBehind.character; |
| 620 | |
| 621 | SourceLocation PeekBeforeLocation = |
| 622 | getMacroArgExpandedLocation(SourceMgr, FE, PosCharBehind); |
Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 623 | Token Result; |
| 624 | if (Lexer::getRawToken(PeekBeforeLocation, Result, SourceMgr, |
| 625 | AST.getLangOpts(), false)) { |
| 626 | // getRawToken failed, just use InputLocation. |
| 627 | return InputLocation; |
| 628 | } |
| 629 | |
| 630 | if (Result.is(tok::raw_identifier)) { |
| 631 | return Lexer::GetBeginningOfToken(PeekBeforeLocation, SourceMgr, |
| 632 | AST.getLangOpts()); |
| 633 | } |
| 634 | |
| 635 | return InputLocation; |
| 636 | } |