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