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