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