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