| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 1 | //===--- SymbolCollector.cpp -------------------------------------*- C++-*-===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "SymbolCollector.h" |
| Eric Liu | f768868 | 2018-09-07 09:40:36 +0000 | [diff] [blame] | 10 | #include "AST.h" |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 11 | #include "CanonicalIncludes.h" |
| Eric Liu | f768868 | 2018-09-07 09:40:36 +0000 | [diff] [blame] | 12 | #include "CodeComplete.h" |
| 13 | #include "CodeCompletionStrings.h" |
| 14 | #include "Logger.h" |
| 15 | #include "SourceCode.h" |
| 16 | #include "URI.h" |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/DeclBase.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceLocation.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Specifiers.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 24 | #include "clang/Index/IndexSymbol.h" |
| 25 | #include "clang/Index/USRGeneration.h" |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Casting.h" |
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | #include "llvm/Support/Path.h" |
| 30 | |
| 31 | namespace clang { |
| 32 | namespace clangd { |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 33 | namespace { |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 34 | |
| Ilya Biryukov | f118d51 | 2018-04-14 16:27:35 +0000 | [diff] [blame] | 35 | /// If \p ND is a template specialization, returns the described template. |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 36 | /// Otherwise, returns \p ND. |
| 37 | const NamedDecl &getTemplateOrThis(const NamedDecl &ND) { |
| Ilya Biryukov | f118d51 | 2018-04-14 16:27:35 +0000 | [diff] [blame] | 38 | if (auto T = ND.getDescribedTemplate()) |
| 39 | return *T; |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 40 | return ND; |
| 41 | } |
| 42 | |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 43 | // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the |
| 44 | // current working directory of the given SourceManager if the Path is not an |
| 45 | // absolute path. If failed, this resolves relative paths against \p FallbackDir |
| 46 | // to get an absolute path. Then, this tries creating an URI for the absolute |
| 47 | // path with schemes specified in \p Opts. This returns an URI with the first |
| 48 | // working scheme, if there is any; otherwise, this returns None. |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 49 | // |
| 50 | // The Path can be a path relative to the build directory, or retrieved from |
| 51 | // the SourceManager. |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 52 | std::string toURI(const SourceManager &SM, llvm::StringRef Path, |
| 53 | const SymbolCollector::Options &Opts) { |
| 54 | llvm::SmallString<128> AbsolutePath(Path); |
| 55 | if (auto CanonPath = |
| 56 | getCanonicalPath(SM.getFileManager().getFile(Path), SM)) { |
| 57 | AbsolutePath = *CanonPath; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 58 | } |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 59 | // We don't perform is_absolute check in an else branch because makeAbsolute |
| 60 | // might return a relative path on some InMemoryFileSystems. |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 61 | if (!llvm::sys::path::is_absolute(AbsolutePath) && !Opts.FallbackDir.empty()) |
| 62 | llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath); |
| 63 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); |
| Eric Liu | c0ac4bb | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 64 | return URI::create(AbsolutePath).toString(); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 65 | } |
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 66 | |
| Eric Liu | d67ec24 | 2018-05-16 12:12:30 +0000 | [diff] [blame] | 67 | // All proto generated headers should start with this line. |
| 68 | static const char *PROTO_HEADER_COMMENT = |
| 69 | "// Generated by the protocol buffer compiler. DO NOT EDIT!"; |
| 70 | |
| 71 | // Checks whether the decl is a private symbol in a header generated by |
| 72 | // protobuf compiler. |
| 73 | // To identify whether a proto header is actually generated by proto compiler, |
| 74 | // we check whether it starts with PROTO_HEADER_COMMENT. |
| 75 | // FIXME: make filtering extensible when there are more use cases for symbol |
| 76 | // filters. |
| 77 | bool isPrivateProtoDecl(const NamedDecl &ND) { |
| 78 | const auto &SM = ND.getASTContext().getSourceManager(); |
| 79 | auto Loc = findNameLoc(&ND); |
| 80 | auto FileName = SM.getFilename(Loc); |
| 81 | if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h")) |
| 82 | return false; |
| 83 | auto FID = SM.getFileID(Loc); |
| 84 | // Double check that this is an actual protobuf header. |
| 85 | if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT)) |
| 86 | return false; |
| 87 | |
| 88 | // ND without identifier can be operators. |
| 89 | if (ND.getIdentifier() == nullptr) |
| 90 | return false; |
| 91 | auto Name = ND.getIdentifier()->getName(); |
| 92 | if (!Name.contains('_')) |
| 93 | return false; |
| 94 | // Nested proto entities (e.g. Message::Nested) have top-level decls |
| 95 | // that shouldn't be used (Message_Nested). Ignore them completely. |
| 96 | // The nested entities are dangling type aliases, we may want to reconsider |
| 97 | // including them in the future. |
| 98 | // For enum constants, SOME_ENUM_CONSTANT is not private and should be |
| 99 | // indexed. Outer_INNER is private. This heuristic relies on naming style, it |
| 100 | // will include OUTER_INNER and exclude some_enum_constant. |
| 101 | // FIXME: the heuristic relies on naming style (i.e. no underscore in |
| 102 | // user-defined names) and can be improved. |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 103 | return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower); |
| Eric Liu | d67ec24 | 2018-05-16 12:12:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 106 | // We only collect #include paths for symbols that are suitable for global code |
| 107 | // completion, except for namespaces since #include path for a namespace is hard |
| 108 | // to define. |
| 109 | bool shouldCollectIncludePath(index::SymbolKind Kind) { |
| 110 | using SK = index::SymbolKind; |
| 111 | switch (Kind) { |
| 112 | case SK::Macro: |
| 113 | case SK::Enum: |
| 114 | case SK::Struct: |
| 115 | case SK::Class: |
| 116 | case SK::Union: |
| 117 | case SK::TypeAlias: |
| 118 | case SK::Using: |
| 119 | case SK::Function: |
| 120 | case SK::Variable: |
| 121 | case SK::EnumConstant: |
| 122 | return true; |
| 123 | default: |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| Eric Liu | 02ce01f | 2018-02-22 10:14:05 +0000 | [diff] [blame] | 128 | /// Gets a canonical include (URI of the header or <header> or "header") for |
| 129 | /// header of \p Loc. |
| 130 | /// Returns None if fails to get include header for \p Loc. |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 131 | llvm::Optional<std::string> |
| 132 | getIncludeHeader(llvm::StringRef QName, const SourceManager &SM, |
| 133 | SourceLocation Loc, const SymbolCollector::Options &Opts) { |
| Eric Liu | 3cee95e | 2018-05-24 14:40:24 +0000 | [diff] [blame] | 134 | std::vector<std::string> Headers; |
| 135 | // Collect the #include stack. |
| 136 | while (true) { |
| 137 | if (!Loc.isValid()) |
| 138 | break; |
| 139 | auto FilePath = SM.getFilename(Loc); |
| 140 | if (FilePath.empty()) |
| 141 | break; |
| 142 | Headers.push_back(FilePath); |
| 143 | if (SM.isInMainFile(Loc)) |
| 144 | break; |
| 145 | Loc = SM.getIncludeLoc(SM.getFileID(Loc)); |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 146 | } |
| Eric Liu | 3cee95e | 2018-05-24 14:40:24 +0000 | [diff] [blame] | 147 | if (Headers.empty()) |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 148 | return None; |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 149 | llvm::StringRef Header = Headers[0]; |
| Eric Liu | 3cee95e | 2018-05-24 14:40:24 +0000 | [diff] [blame] | 150 | if (Opts.Includes) { |
| 151 | Header = Opts.Includes->mapHeader(Headers, QName); |
| 152 | if (Header.startswith("<") || Header.startswith("\"")) |
| 153 | return Header.str(); |
| 154 | } |
| 155 | return toURI(SM, Header, Opts); |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 158 | // Return the symbol range of the token at \p TokLoc. |
| 159 | std::pair<SymbolLocation::Position, SymbolLocation::Position> |
| 160 | getTokenRange(SourceLocation TokLoc, const SourceManager &SM, |
| 161 | const LangOptions &LangOpts) { |
| 162 | auto CreatePosition = [&SM](SourceLocation Loc) { |
| 163 | auto LSPLoc = sourceLocToPosition(SM, Loc); |
| 164 | SymbolLocation::Position Pos; |
| Haojian Wu | b515fab | 2018-10-18 10:43:50 +0000 | [diff] [blame] | 165 | Pos.setLine(LSPLoc.line); |
| 166 | Pos.setColumn(LSPLoc.character); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 167 | return Pos; |
| 168 | }; |
| 169 | |
| 170 | auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts); |
| 171 | return {CreatePosition(TokLoc), |
| 172 | CreatePosition(TokLoc.getLocWithOffset(TokenLength))}; |
| 173 | } |
| 174 | |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 175 | bool shouldIndexFile(const SourceManager &SM, FileID FID, |
| 176 | const SymbolCollector::Options &Opts, |
| 177 | llvm::DenseMap<FileID, bool> *FilesToIndexCache) { |
| 178 | if (!Opts.FileFilter) |
| 179 | return true; |
| 180 | auto I = FilesToIndexCache->try_emplace(FID); |
| 181 | if (I.second) |
| 182 | I.first->second = Opts.FileFilter(SM, FID); |
| 183 | return I.first->second; |
| 184 | } |
| 185 | |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 186 | // Return the symbol location of the token at \p TokLoc. |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 187 | llvm::Optional<SymbolLocation> |
| 188 | getTokenLocation(SourceLocation TokLoc, const SourceManager &SM, |
| 189 | const SymbolCollector::Options &Opts, |
| 190 | const clang::LangOptions &LangOpts, |
| 191 | std::string &FileURIStorage) { |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 192 | auto Path = SM.getFilename(TokLoc); |
| 193 | if (Path.empty()) |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 194 | return None; |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 195 | FileURIStorage = toURI(SM, Path, Opts); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 196 | SymbolLocation Result; |
| Haojian Wu | ee54a2b | 2018-11-14 11:55:45 +0000 | [diff] [blame] | 197 | Result.FileURI = FileURIStorage.c_str(); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 198 | auto Range = getTokenRange(TokLoc, SM, LangOpts); |
| 199 | Result.Start = Range.first; |
| 200 | Result.End = Range.second; |
| Haojian Wu | 545c02a | 2018-04-13 08:30:39 +0000 | [diff] [blame] | 201 | |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 202 | return Result; |
| Haojian Wu | b018906 | 2018-01-31 12:56:51 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 205 | // Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union) |
| 206 | // in a header file, in which case clangd would prefer to use ND as a canonical |
| 207 | // declaration. |
| 208 | // FIXME: handle symbol types that are not TagDecl (e.g. functions), if using |
| Fangrui Song | 943e12e | 2018-03-29 20:03:16 +0000 | [diff] [blame] | 209 | // the first seen declaration as canonical declaration is not a good enough |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 210 | // heuristic. |
| 211 | bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) { |
| Sam McCall | 5fb9746 | 2018-10-05 14:03:04 +0000 | [diff] [blame] | 212 | const auto& SM = ND.getASTContext().getSourceManager(); |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 213 | return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) && |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 214 | isa<TagDecl>(&ND) && |
| Sam McCall | 5fb9746 | 2018-10-05 14:03:04 +0000 | [diff] [blame] | 215 | !SM.isWrittenInMainFile(SM.getExpansionLoc(ND.getLocation())); |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| Sam McCall | b013831 | 2018-09-04 14:39:56 +0000 | [diff] [blame] | 218 | RefKind toRefKind(index::SymbolRoleSet Roles) { |
| 219 | return static_cast<RefKind>(static_cast<unsigned>(RefKind::All) & Roles); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 222 | template <class T> bool explicitTemplateSpecialization(const NamedDecl &ND) { |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 223 | if (const auto *TD = dyn_cast<T>(&ND)) |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 224 | if (TD->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 225 | return true; |
| 226 | return false; |
| 227 | } |
| 228 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 229 | } // namespace |
| 230 | |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 231 | SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {} |
| 232 | |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 233 | void SymbolCollector::initialize(ASTContext &Ctx) { |
| 234 | ASTCtx = &Ctx; |
| 235 | CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
| 236 | CompletionTUInfo = |
| 237 | llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator); |
| 238 | } |
| 239 | |
| Eric Liu | 8763e48 | 2018-06-21 12:12:26 +0000 | [diff] [blame] | 240 | bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND, |
| Haojian Wu | 7800dbe | 2018-12-03 13:16:04 +0000 | [diff] [blame] | 241 | const ASTContext &ASTCtx, |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 242 | const Options &Opts, |
| 243 | bool IsMainFileOnly) { |
| Eric Liu | 8763e48 | 2018-06-21 12:12:26 +0000 | [diff] [blame] | 244 | if (ND.isImplicit()) |
| 245 | return false; |
| 246 | // Skip anonymous declarations, e.g (anonymous enum/class/struct). |
| 247 | if (ND.getDeclName().isEmpty()) |
| 248 | return false; |
| 249 | |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 250 | // Skip main-file symbols if we are not collecting them. |
| 251 | if (IsMainFileOnly && !Opts.CollectMainFileSymbols) |
| 252 | return false; |
| 253 | |
| 254 | // Skip symbols in anonymous namespaces in header files. |
| 255 | if (!IsMainFileOnly && ND.isInAnonymousNamespace()) |
| Eric Liu | 8763e48 | 2018-06-21 12:12:26 +0000 | [diff] [blame] | 256 | return false; |
| 257 | |
| 258 | // We want most things but not "local" symbols such as symbols inside |
| 259 | // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl. |
| 260 | // FIXME: Need a matcher for ExportDecl in order to include symbols declared |
| 261 | // within an export. |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 262 | const auto *DeclCtx = ND.getDeclContext(); |
| 263 | switch (DeclCtx->getDeclKind()) { |
| 264 | case Decl::TranslationUnit: |
| 265 | case Decl::Namespace: |
| 266 | case Decl::LinkageSpec: |
| 267 | case Decl::Enum: |
| 268 | case Decl::ObjCProtocol: |
| 269 | case Decl::ObjCInterface: |
| 270 | case Decl::ObjCCategory: |
| 271 | case Decl::ObjCCategoryImpl: |
| 272 | case Decl::ObjCImplementation: |
| 273 | break; |
| 274 | default: |
| 275 | // Record has a few derivations (e.g. CXXRecord, Class specialization), it's |
| 276 | // easier to cast. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 277 | if (!isa<RecordDecl>(DeclCtx)) |
| Eric Liu | a57afd0 | 2018-09-17 07:43:49 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | if (explicitTemplateSpecialization<FunctionDecl>(ND) || |
| 281 | explicitTemplateSpecialization<CXXRecordDecl>(ND) || |
| 282 | explicitTemplateSpecialization<VarDecl>(ND)) |
| Eric Liu | 8763e48 | 2018-06-21 12:12:26 +0000 | [diff] [blame] | 283 | return false; |
| 284 | |
| 285 | // Avoid indexing internal symbols in protobuf generated headers. |
| 286 | if (isPrivateProtoDecl(ND)) |
| 287 | return false; |
| 288 | return true; |
| 289 | } |
| 290 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 291 | // Always return true to continue indexing. |
| 292 | bool SymbolCollector::handleDeclOccurence( |
| 293 | const Decl *D, index::SymbolRoleSet Roles, |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 294 | llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc, |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 295 | index::IndexDataConsumer::ASTNodeInfo ASTNode) { |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 296 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 297 | assert(CompletionAllocator && CompletionTUInfo); |
| Eric Liu | 77d1811 | 2018-06-04 11:31:55 +0000 | [diff] [blame] | 298 | assert(ASTNode.OrigD); |
| 299 | // If OrigD is an declaration associated with a friend declaration and it's |
| 300 | // not a definition, skip it. Note that OrigD is the occurrence that the |
| 301 | // collector is currently visiting. |
| 302 | if ((ASTNode.OrigD->getFriendObjectKind() != |
| 303 | Decl::FriendObjectKind::FOK_None) && |
| 304 | !(Roles & static_cast<unsigned>(index::SymbolRole::Definition))) |
| 305 | return true; |
| 306 | // A declaration created for a friend declaration should not be used as the |
| 307 | // canonical declaration in the index. Use OrigD instead, unless we've already |
| 308 | // picked a replacement for D |
| 309 | if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None) |
| 310 | D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second; |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 311 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 312 | if (!ND) |
| 313 | return true; |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 314 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 315 | // Mark D as referenced if this is a reference coming from the main file. |
| 316 | // D may not be an interesting symbol, but it's cheaper to check at the end. |
| Sam McCall | b9d5711 | 2018-04-09 14:28:52 +0000 | [diff] [blame] | 317 | auto &SM = ASTCtx->getSourceManager(); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 318 | auto SpellingLoc = SM.getSpellingLoc(Loc); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 319 | if (Opts.CountReferences && |
| 320 | (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) && |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 321 | SM.getFileID(SpellingLoc) == SM.getMainFileID()) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 322 | ReferencedDecls.insert(ND); |
| 323 | |
| Haojian Wu | e83cacc | 2018-10-15 11:46:26 +0000 | [diff] [blame] | 324 | bool CollectRef = static_cast<unsigned>(Opts.RefFilter) & Roles; |
| 325 | bool IsOnlyRef = |
| 326 | !(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) | |
| 327 | static_cast<unsigned>(index::SymbolRole::Definition))); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 328 | |
| Haojian Wu | e83cacc | 2018-10-15 11:46:26 +0000 | [diff] [blame] | 329 | if (IsOnlyRef && !CollectRef) |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 330 | return true; |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 331 | |
| 332 | // ND is the canonical (i.e. first) declaration. If it's in the main file, |
| 333 | // then no public declaration was visible, so assume it's main-file only. |
| 334 | bool IsMainFileOnly = SM.isWrittenInMainFile(SM.getExpansionLoc( |
| 335 | ND->getBeginLoc())); |
| 336 | if (!shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly)) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 337 | return true; |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 338 | // Do not store references to main-file symbols. |
| 339 | if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) && |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 340 | (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID())) |
| Haojian Wu | e83cacc | 2018-10-15 11:46:26 +0000 | [diff] [blame] | 341 | DeclRefs[ND].emplace_back(SpellingLoc, Roles); |
| 342 | // Don't continue indexing if this is a mere reference. |
| 343 | if (IsOnlyRef) |
| 344 | return true; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 345 | |
| Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 346 | auto ID = getSymbolID(ND); |
| 347 | if (!ID) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 348 | return true; |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 349 | |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame^] | 350 | // FIXME: ObjCPropertyDecl are not properly indexed here: |
| 351 | // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is |
| 352 | // not a NamedDecl. |
| 353 | auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD); |
| 354 | if (!OriginalDecl) |
| 355 | return true; |
| 356 | |
| Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 357 | const Symbol *BasicSymbol = Symbols.find(*ID); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 358 | if (!BasicSymbol) // Regardless of role, ND is the canonical declaration. |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 359 | BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly); |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame^] | 360 | else if (isPreferredDeclaration(*OriginalDecl, Roles)) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 361 | // If OriginalDecl is preferred, replace the existing canonical |
| 362 | // declaration (e.g. a class forward declaration). There should be at most |
| 363 | // one duplicate as we expect to see only one preferred declaration per |
| 364 | // TU, because in practice they are definitions. |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame^] | 365 | BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 366 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 367 | if (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame^] | 368 | addDefinition(*OriginalDecl, *BasicSymbol); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 372 | bool SymbolCollector::handleMacroOccurence(const IdentifierInfo *Name, |
| 373 | const MacroInfo *MI, |
| 374 | index::SymbolRoleSet Roles, |
| 375 | SourceLocation Loc) { |
| 376 | if (!Opts.CollectMacro) |
| 377 | return true; |
| 378 | assert(PP.get()); |
| 379 | |
| 380 | const auto &SM = PP->getSourceManager(); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 381 | auto DefLoc = MI->getDefinitionLoc(); |
| 382 | if (SM.isInMainFile(SM.getExpansionLoc(DefLoc))) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 383 | return true; |
| 384 | // Header guards are not interesting in index. Builtin macros don't have |
| 385 | // useful locations and are not needed for code completions. |
| 386 | if (MI->isUsedForHeaderGuard() || MI->isBuiltinMacro()) |
| 387 | return true; |
| 388 | |
| 389 | // Mark the macro as referenced if this is a reference coming from the main |
| 390 | // file. The macro may not be an interesting symbol, but it's cheaper to check |
| 391 | // at the end. |
| 392 | if (Opts.CountReferences && |
| 393 | (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) && |
| 394 | SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID()) |
| 395 | ReferencedMacros.insert(Name); |
| 396 | // Don't continue indexing if this is a mere reference. |
| 397 | // FIXME: remove macro with ID if it is undefined. |
| 398 | if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) || |
| 399 | Roles & static_cast<unsigned>(index::SymbolRole::Definition))) |
| 400 | return true; |
| 401 | |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 402 | auto ID = getSymbolID(*Name, MI, SM); |
| 403 | if (!ID) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 404 | return true; |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 405 | |
| 406 | // Only collect one instance in case there are multiple. |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 407 | if (Symbols.find(*ID) != nullptr) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 408 | return true; |
| 409 | |
| 410 | Symbol S; |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 411 | S.ID = std::move(*ID); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 412 | S.Name = Name->getName(); |
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 413 | S.Flags |= Symbol::IndexedForCodeCompletion; |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 414 | S.SymInfo = index::getSymbolInfoForMacro(*MI); |
| 415 | std::string FileURI; |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 416 | // FIXME: use the result to filter out symbols. |
| 417 | shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache); |
| 418 | if (auto DeclLoc = |
| 419 | getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI)) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 420 | S.CanonicalDeclaration = *DeclLoc; |
| 421 | |
| 422 | CodeCompletionResult SymbolCompletion(Name); |
| 423 | const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro( |
| 424 | *PP, *CompletionAllocator, *CompletionTUInfo); |
| 425 | std::string Signature; |
| 426 | std::string SnippetSuffix; |
| 427 | getSignature(*CCS, &Signature, &SnippetSuffix); |
| 428 | |
| 429 | std::string Include; |
| 430 | if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) { |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 431 | if (auto Header = getIncludeHeader(Name->getName(), SM, |
| 432 | SM.getExpansionLoc(DefLoc), Opts)) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 433 | Include = std::move(*Header); |
| 434 | } |
| 435 | S.Signature = Signature; |
| 436 | S.CompletionSnippetSuffix = SnippetSuffix; |
| Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 437 | if (!Include.empty()) |
| 438 | S.IncludeHeaders.emplace_back(Include, 1); |
| 439 | |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 440 | Symbols.insert(S); |
| 441 | return true; |
| 442 | } |
| 443 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 444 | void SymbolCollector::finish() { |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 445 | // At the end of the TU, add 1 to the refcount of all referenced symbols. |
| 446 | auto IncRef = [this](const SymbolID &ID) { |
| 447 | if (const auto *S = Symbols.find(ID)) { |
| 448 | Symbol Inc = *S; |
| 449 | ++Inc.References; |
| 450 | Symbols.insert(Inc); |
| 451 | } |
| 452 | }; |
| 453 | for (const NamedDecl *ND : ReferencedDecls) { |
| Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 454 | if (auto ID = getSymbolID(ND)) { |
| 455 | IncRef(*ID); |
| 456 | } |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 457 | } |
| 458 | if (Opts.CollectMacro) { |
| 459 | assert(PP); |
| 460 | for (const IdentifierInfo *II : ReferencedMacros) { |
| Eric Liu | a62c9d6 | 2018-07-09 18:54:51 +0000 | [diff] [blame] | 461 | if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo()) |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 462 | if (auto ID = getSymbolID(*II, MI, PP->getSourceManager())) |
| 463 | IncRef(*ID); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 464 | } |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 465 | } |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 466 | |
| 467 | const auto &SM = ASTCtx->getSourceManager(); |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 468 | llvm::DenseMap<FileID, std::string> URICache; |
| 469 | auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> { |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 470 | auto Found = URICache.find(FID); |
| 471 | if (Found == URICache.end()) { |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 472 | if (auto *FileEntry = SM.getFileEntryForID(FID)) { |
| 473 | auto FileURI = toURI(SM, FileEntry->getName(), Opts); |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 474 | Found = URICache.insert({FID, FileURI}).first; |
| Haojian Wu | c014d86 | 2018-10-17 08:54:48 +0000 | [diff] [blame] | 475 | } else { |
| 476 | // Ignore cases where we can not find a corresponding file entry |
| 477 | // for the loc, thoses are not interesting, e.g. symbols formed |
| 478 | // via macro concatenation. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 479 | return None; |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | return Found->second; |
| 483 | }; |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 484 | |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 485 | if (auto MainFileURI = GetURI(SM.getMainFileID())) { |
| Sam McCall | b013831 | 2018-09-04 14:39:56 +0000 | [diff] [blame] | 486 | for (const auto &It : DeclRefs) { |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 487 | if (auto ID = getSymbolID(It.first)) { |
| Haojian Wu | e83cacc | 2018-10-15 11:46:26 +0000 | [diff] [blame] | 488 | for (const auto &LocAndRole : It.second) { |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 489 | auto FileID = SM.getFileID(LocAndRole.first); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 490 | // FIXME: use the result to filter out references. |
| 491 | shouldIndexFile(SM, FileID, Opts, &FilesToIndexCache); |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 492 | if (auto FileURI = GetURI(FileID)) { |
| 493 | auto Range = |
| 494 | getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts()); |
| 495 | Ref R; |
| 496 | R.Location.Start = Range.first; |
| 497 | R.Location.End = Range.second; |
| Haojian Wu | ee54a2b | 2018-11-14 11:55:45 +0000 | [diff] [blame] | 498 | R.Location.FileURI = FileURI->c_str(); |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 499 | R.Kind = toRefKind(LocAndRole.second); |
| 500 | Refs.insert(*ID, R); |
| 501 | } |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 507 | ReferencedDecls.clear(); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 508 | ReferencedMacros.clear(); |
| Sam McCall | b013831 | 2018-09-04 14:39:56 +0000 | [diff] [blame] | 509 | DeclRefs.clear(); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 510 | FilesToIndexCache.clear(); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 513 | const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 514 | SymbolID ID, |
| 515 | bool IsMainFileOnly) { |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 516 | auto &Ctx = ND.getASTContext(); |
| 517 | auto &SM = Ctx.getSourceManager(); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 518 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 519 | Symbol S; |
| 520 | S.ID = std::move(ID); |
| Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 521 | std::string QName = printQualifiedName(ND); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 522 | std::tie(S.Scope, S.Name) = splitQualifiedName(QName); |
| Sam McCall | 032db94 | 2018-06-22 06:41:43 +0000 | [diff] [blame] | 523 | // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo: |
| 524 | // for consistency with CodeCompletionString and a clean name/signature split. |
| Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 525 | |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 526 | // We collect main-file symbols, but do not use them for code completion. |
| 527 | if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx)) |
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 528 | S.Flags |= Symbol::IndexedForCodeCompletion; |
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 529 | if (isImplementationDetail(&ND)) |
| 530 | S.Flags |= Symbol::ImplementationDetail; |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 531 | if (!IsMainFileOnly) |
| 532 | S.Flags |= Symbol::VisibleOutsideFile; |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 533 | S.SymInfo = index::getSymbolInfo(&ND); |
| 534 | std::string FileURI; |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 535 | auto Loc = findNameLoc(&ND); |
| 536 | // FIXME: use the result to filter out symbols. |
| 537 | shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache); |
| 538 | if (auto DeclLoc = |
| 539 | getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 540 | S.CanonicalDeclaration = *DeclLoc; |
| 541 | |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 542 | S.Origin = Opts.Origin; |
| 543 | if (ND.getAvailability() == AR_Deprecated) |
| 544 | S.Flags |= Symbol::Deprecated; |
| 545 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 546 | // Add completion info. |
| 547 | // FIXME: we may want to choose a different redecl, or combine from several. |
| 548 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 549 | // We use the primary template, as clang does during code completion. |
| 550 | CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 551 | const auto *CCS = SymbolCompletion.CreateCodeCompletionString( |
| Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 552 | *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator, |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 553 | *CompletionTUInfo, |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 554 | /*IncludeBriefComments*/ false); |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 555 | std::string Documentation = |
| Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 556 | formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion, |
| 557 | /*CommentsFromHeaders=*/true)); |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 558 | // For symbols not indexed for completion (class members), we also store their |
| 559 | // docs in the index, because Sema doesn't load the docs from the preamble, we |
| 560 | // rely on the index to get the docs. |
| 561 | // FIXME: this can be optimized by only storing the docs in dynamic index -- |
| 562 | // dynamic index should index these symbols when Sema completes a member |
| 563 | // completion. |
| 564 | S.Documentation = Documentation; |
| 565 | if (!(S.Flags & Symbol::IndexedForCodeCompletion)) { |
| 566 | Symbols.insert(S); |
| 567 | return Symbols.find(S.ID); |
| 568 | } |
| 569 | |
| 570 | std::string Signature; |
| 571 | std::string SnippetSuffix; |
| 572 | getSignature(*CCS, &Signature, &SnippetSuffix); |
| 573 | S.Signature = Signature; |
| 574 | S.CompletionSnippetSuffix = SnippetSuffix; |
| Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 575 | std::string ReturnType = getReturnType(*CCS); |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 576 | S.ReturnType = ReturnType; |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 577 | |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 578 | std::string Include; |
| 579 | if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) { |
| 580 | // Use the expansion location to get the #include header since this is |
| 581 | // where the symbol is exposed. |
| Eric Liu | b96363d | 2018-03-01 18:06:40 +0000 | [diff] [blame] | 582 | if (auto Header = getIncludeHeader( |
| 583 | QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts)) |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 584 | Include = std::move(*Header); |
| 585 | } |
| Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 586 | if (!Include.empty()) |
| 587 | S.IncludeHeaders.emplace_back(Include, 1); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 588 | |
| Ilya Biryukov | 4d3d82e | 2018-11-26 15:52:16 +0000 | [diff] [blame] | 589 | llvm::Optional<OpaqueType> TypeStorage; |
| Ilya Biryukov | a21392b | 2018-11-26 15:29:14 +0000 | [diff] [blame] | 590 | if (S.Flags & Symbol::IndexedForCodeCompletion) { |
| Ilya Biryukov | 4d3d82e | 2018-11-26 15:52:16 +0000 | [diff] [blame] | 591 | TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion); |
| 592 | if (TypeStorage) |
| 593 | S.Type = TypeStorage->raw(); |
| Ilya Biryukov | a21392b | 2018-11-26 15:29:14 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 596 | Symbols.insert(S); |
| 597 | return Symbols.find(S.ID); |
| 598 | } |
| 599 | |
| 600 | void SymbolCollector::addDefinition(const NamedDecl &ND, |
| 601 | const Symbol &DeclSym) { |
| 602 | if (DeclSym.Definition) |
| 603 | return; |
| 604 | // If we saw some forward declaration, we end up copying the symbol. |
| 605 | // This is not ideal, but avoids duplicating the "is this a definition" check |
| 606 | // in clang::index. We should only see one definition. |
| 607 | Symbol S = DeclSym; |
| 608 | std::string FileURI; |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 609 | auto Loc = findNameLoc(&ND); |
| 610 | const auto &SM = ND.getASTContext().getSourceManager(); |
| 611 | // FIXME: use the result to filter out symbols. |
| 612 | shouldIndexFile(SM, SM.getFileID(Loc), Opts, &FilesToIndexCache); |
| 613 | if (auto DefLoc = |
| 614 | getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 615 | S.Definition = *DefLoc; |
| 616 | Symbols.insert(S); |
| 617 | } |
| 618 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 619 | } // namespace clangd |
| 620 | } // namespace clang |