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