| 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(); |
| Ilya Biryukov | b63c35e | 2019-12-10 10:08:39 +0100 | [diff] [blame] | 85 | auto Loc = nameLocation(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. |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame^] | 248 | bool SymbolCollector::handleDeclOccurrence( |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 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); |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame^] | 255 | // Indexing API puts canonical decl into D, which might not have a valid |
| Kadir Cetinkaya | bb6cd82 | 2019-04-15 14:38:46 +0000 | [diff] [blame] | 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(); |
| 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 | decdbc1 | 2019-12-09 12:00:42 +0100 | [diff] [blame] | 281 | SM.getFileID(SM.getSpellingLoc(Loc)) == 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. |
| Haojian Wu | decdbc1 | 2019-12-09 12:00:42 +0100 | [diff] [blame] | 314 | // Unlike other fields, e.g. Symbols (which use spelling locations), we use |
| 315 | // file locations for references (as it aligns the behavior of clangd's |
| 316 | // AST-based xref). |
| 317 | // FIXME: we should try to use the file locations for other fields. |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 318 | if (CollectRef && !IsMainFileOnly && !isa<NamespaceDecl>(ND) && |
| Haojian Wu | decdbc1 | 2019-12-09 12:00:42 +0100 | [diff] [blame] | 319 | (Opts.RefsInHeaders || |
| 320 | SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID())) |
| 321 | DeclRefs[ND].emplace_back(SM.getFileLoc(Loc), Roles); |
| Haojian Wu | e83cacc | 2018-10-15 11:46:26 +0000 | [diff] [blame] | 322 | // Don't continue indexing if this is a mere reference. |
| 323 | if (IsOnlyRef) |
| 324 | return true; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 325 | |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame] | 326 | // FIXME: ObjCPropertyDecl are not properly indexed here: |
| 327 | // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is |
| 328 | // not a NamedDecl. |
| 329 | auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD); |
| 330 | if (!OriginalDecl) |
| 331 | return true; |
| 332 | |
| Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 333 | const Symbol *BasicSymbol = Symbols.find(*ID); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 334 | if (!BasicSymbol) // Regardless of role, ND is the canonical declaration. |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 335 | BasicSymbol = addDeclaration(*ND, std::move(*ID), IsMainFileOnly); |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame] | 336 | else if (isPreferredDeclaration(*OriginalDecl, Roles)) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 337 | // If OriginalDecl is preferred, replace the existing canonical |
| 338 | // declaration (e.g. a class forward declaration). There should be at most |
| 339 | // one duplicate as we expect to see only one preferred declaration per |
| 340 | // TU, because in practice they are definitions. |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame] | 341 | BasicSymbol = addDeclaration(*OriginalDecl, std::move(*ID), IsMainFileOnly); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 342 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 343 | if (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) |
| Ilya Biryukov | 4e0c400 | 2019-01-23 10:35:12 +0000 | [diff] [blame] | 344 | addDefinition(*OriginalDecl, *BasicSymbol); |
| Nathan Ridge | 73e6f47 | 2019-06-04 04:25:44 +0000 | [diff] [blame] | 345 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame^] | 349 | bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name, |
| 350 | const MacroInfo *MI, |
| 351 | index::SymbolRoleSet Roles, |
| 352 | SourceLocation Loc) { |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 353 | assert(PP.get()); |
| 354 | |
| 355 | const auto &SM = PP->getSourceManager(); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 356 | auto DefLoc = MI->getDefinitionLoc(); |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 357 | auto SpellingLoc = SM.getSpellingLoc(Loc); |
| 358 | bool IsMainFileSymbol = SM.isInMainFile(SM.getExpansionLoc(DefLoc)); |
| Haojian Wu | 7b6f874 | 2019-01-28 14:11:49 +0000 | [diff] [blame] | 359 | |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 360 | // Builtin macros don't have useful locations and aren't needed in completion. |
| 361 | if (MI->isBuiltinMacro()) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 362 | return true; |
| 363 | |
| Haojian Wu | 7b6f874 | 2019-01-28 14:11:49 +0000 | [diff] [blame] | 364 | // Also avoid storing predefined macros like __DBL_MIN__. |
| 365 | if (SM.isWrittenInBuiltinFile(DefLoc)) |
| 366 | return true; |
| 367 | |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 368 | auto ID = getSymbolID(Name->getName(), MI, SM); |
| 369 | if (!ID) |
| 370 | return true; |
| 371 | |
| 372 | // Do not store references to main-file macros. |
| 373 | if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileSymbol && |
| 374 | (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID())) |
| 375 | MacroRefs[*ID].push_back({Loc, Roles}); |
| 376 | |
| 377 | // Collect symbols. |
| 378 | if (!Opts.CollectMacro) |
| 379 | return true; |
| 380 | |
| 381 | // Skip main-file macros if we are not collecting them. |
| 382 | if (IsMainFileSymbol && !Opts.CollectMainFileSymbols) |
| 383 | return false; |
| 384 | |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 385 | // Mark the macro as referenced if this is a reference coming from the main |
| 386 | // file. The macro may not be an interesting symbol, but it's cheaper to check |
| 387 | // at the end. |
| 388 | if (Opts.CountReferences && |
| 389 | (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) && |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 390 | SM.getFileID(SpellingLoc) == SM.getMainFileID()) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 391 | ReferencedMacros.insert(Name); |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 392 | |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 393 | // Don't continue indexing if this is a mere reference. |
| 394 | // FIXME: remove macro with ID if it is undefined. |
| 395 | if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) || |
| 396 | Roles & static_cast<unsigned>(index::SymbolRole::Definition))) |
| 397 | return true; |
| 398 | |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 399 | // Only collect one instance in case there are multiple. |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 400 | if (Symbols.find(*ID) != nullptr) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 401 | return true; |
| 402 | |
| 403 | Symbol S; |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 404 | S.ID = std::move(*ID); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 405 | S.Name = Name->getName(); |
| Haojian Wu | 7b6f874 | 2019-01-28 14:11:49 +0000 | [diff] [blame] | 406 | if (!IsMainFileSymbol) { |
| 407 | S.Flags |= Symbol::IndexedForCodeCompletion; |
| 408 | S.Flags |= Symbol::VisibleOutsideFile; |
| 409 | } |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 410 | S.SymInfo = index::getSymbolInfoForMacro(*MI); |
| 411 | std::string FileURI; |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 412 | // FIXME: use the result to filter out symbols. |
| Ilya Biryukov | 30c86b6 | 2019-08-20 08:54:30 +0000 | [diff] [blame] | 413 | shouldIndexFile(SM.getFileID(Loc)); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 414 | if (auto DeclLoc = |
| 415 | getTokenLocation(DefLoc, SM, Opts, PP->getLangOpts(), FileURI)) |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 416 | S.CanonicalDeclaration = *DeclLoc; |
| 417 | |
| 418 | CodeCompletionResult SymbolCompletion(Name); |
| 419 | const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro( |
| 420 | *PP, *CompletionAllocator, *CompletionTUInfo); |
| 421 | std::string Signature; |
| 422 | std::string SnippetSuffix; |
| 423 | getSignature(*CCS, &Signature, &SnippetSuffix); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 424 | S.Signature = Signature; |
| 425 | S.CompletionSnippetSuffix = SnippetSuffix; |
| Eric Liu | 83f63e4 | 2018-09-03 10:18:21 +0000 | [diff] [blame] | 426 | |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 427 | IndexedMacros.insert(Name); |
| 428 | setIncludeLocation(S, DefLoc); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 429 | Symbols.insert(S); |
| 430 | return true; |
| 431 | } |
| 432 | |
| Nathan Ridge | 73e6f47 | 2019-06-04 04:25:44 +0000 | [diff] [blame] | 433 | void SymbolCollector::processRelations( |
| 434 | const NamedDecl &ND, const SymbolID &ID, |
| 435 | ArrayRef<index::SymbolRelation> Relations) { |
| 436 | // Store subtype relations. |
| 437 | if (!dyn_cast<TagDecl>(&ND)) |
| 438 | return; |
| 439 | |
| 440 | for (const auto &R : Relations) { |
| 441 | if (!shouldIndexRelation(R)) |
| 442 | continue; |
| 443 | |
| 444 | const Decl *Object = R.RelatedSymbol; |
| 445 | |
| 446 | auto ObjectID = getSymbolID(Object); |
| 447 | if (!ObjectID) |
| 448 | continue; |
| 449 | |
| 450 | // Record the relation. |
| 451 | // TODO: There may be cases where the object decl is not indexed for some |
| 452 | // reason. Those cases should probably be removed in due course, but for |
| 453 | // now there are two possible ways to handle it: |
| 454 | // (A) Avoid storing the relation in such cases. |
| 455 | // (B) Store it anyways. Clients will likely lookup() the SymbolID |
| 456 | // in the index and find nothing, but that's a situation they |
| 457 | // probably need to handle for other reasons anyways. |
| 458 | // We currently do (B) because it's simpler. |
| Haojian Wu | c8e3f43 | 2019-10-17 14:08:28 +0000 | [diff] [blame] | 459 | this->Relations.insert(Relation{ID, RelationKind::BaseOf, *ObjectID}); |
| Nathan Ridge | 73e6f47 | 2019-06-04 04:25:44 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| Nathan Ridge | b2f45ac | 2019-05-30 23:54:43 +0000 | [diff] [blame] | 463 | void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation Loc) { |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 464 | if (Opts.CollectIncludePath) |
| 465 | if (shouldCollectIncludePath(S.SymInfo.Kind)) |
| 466 | // Use the expansion location to get the #include header since this is |
| 467 | // where the symbol is exposed. |
| 468 | IncludeFiles[S.ID] = |
| 469 | PP->getSourceManager().getDecomposedExpansionLoc(Loc).first; |
| 470 | } |
| 471 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 472 | void SymbolCollector::finish() { |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 473 | // At the end of the TU, add 1 to the refcount of all referenced symbols. |
| 474 | auto IncRef = [this](const SymbolID &ID) { |
| 475 | if (const auto *S = Symbols.find(ID)) { |
| 476 | Symbol Inc = *S; |
| 477 | ++Inc.References; |
| 478 | Symbols.insert(Inc); |
| 479 | } |
| 480 | }; |
| 481 | for (const NamedDecl *ND : ReferencedDecls) { |
| Haojian Wu | c6ddb46 | 2018-08-07 08:57:52 +0000 | [diff] [blame] | 482 | if (auto ID = getSymbolID(ND)) { |
| 483 | IncRef(*ID); |
| 484 | } |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 485 | } |
| 486 | if (Opts.CollectMacro) { |
| 487 | assert(PP); |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 488 | // First, drop header guards. We can't identify these until EOF. |
| 489 | for (const IdentifierInfo *II : IndexedMacros) { |
| 490 | if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo()) |
| Utkarsh Saxena | 02ec6ff | 2019-11-11 12:38:17 +0100 | [diff] [blame] | 491 | if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager())) |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 492 | if (MI->isUsedForHeaderGuard()) |
| 493 | Symbols.erase(*ID); |
| 494 | } |
| 495 | // Now increment refcounts. |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 496 | for (const IdentifierInfo *II : ReferencedMacros) { |
| Eric Liu | a62c9d6 | 2018-07-09 18:54:51 +0000 | [diff] [blame] | 497 | if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo()) |
| Utkarsh Saxena | 02ec6ff | 2019-11-11 12:38:17 +0100 | [diff] [blame] | 498 | if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager())) |
| Eric Liu | d25f121 | 2018-09-06 09:59:37 +0000 | [diff] [blame] | 499 | IncRef(*ID); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 500 | } |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 501 | } |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 502 | // Fill in IncludeHeaders. |
| 503 | // 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] | 504 | // Symbols in slabs aren' mutable, so insert() has to walk all the strings |
| 505 | // :-( |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 506 | llvm::SmallString<256> QName; |
| 507 | for (const auto &Entry : IncludeFiles) |
| 508 | if (const Symbol *S = Symbols.find(Entry.first)) { |
| 509 | QName = S->Scope; |
| 510 | QName.append(S->Name); |
| 511 | if (auto Header = getIncludeHeader(QName, Entry.second)) { |
| 512 | Symbol NewSym = *S; |
| 513 | NewSym.IncludeHeaders.push_back({*Header, 1}); |
| 514 | Symbols.insert(NewSym); |
| 515 | } |
| 516 | } |
| 517 | |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 518 | const auto &SM = ASTCtx->getSourceManager(); |
| Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 519 | llvm::DenseMap<FileID, std::string> URICache; |
| 520 | auto GetURI = [&](FileID FID) -> llvm::Optional<std::string> { |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 521 | auto Found = URICache.find(FID); |
| 522 | if (Found == URICache.end()) { |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 523 | if (auto *FileEntry = SM.getFileEntryForID(FID)) { |
| 524 | auto FileURI = toURI(SM, FileEntry->getName(), Opts); |
| Kadir Cetinkaya | dd67793 | 2018-12-19 10:46:21 +0000 | [diff] [blame] | 525 | Found = URICache.insert({FID, FileURI}).first; |
| Haojian Wu | c014d86 | 2018-10-17 08:54:48 +0000 | [diff] [blame] | 526 | } else { |
| Kirill Bobyrev | 3b9715c | 2019-12-16 10:33:56 +0100 | [diff] [blame^] | 527 | // Ignore cases where we can not find a corresponding file entry for |
| 528 | // given location, e.g. symbols formed via macro concatenation. |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 529 | return None; |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | return Found->second; |
| 533 | }; |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 534 | auto CollectRef = |
| 535 | [&](SymbolID ID, |
| 536 | const std::pair<SourceLocation, index::SymbolRoleSet> &LocAndRole) { |
| 537 | auto FileID = SM.getFileID(LocAndRole.first); |
| 538 | // FIXME: use the result to filter out references. |
| 539 | shouldIndexFile(FileID); |
| 540 | if (auto FileURI = GetURI(FileID)) { |
| 541 | auto Range = |
| 542 | getTokenRange(LocAndRole.first, SM, ASTCtx->getLangOpts()); |
| 543 | Ref R; |
| 544 | R.Location.Start = Range.first; |
| 545 | R.Location.End = Range.second; |
| 546 | R.Location.FileURI = FileURI->c_str(); |
| 547 | R.Kind = toRefKind(LocAndRole.second); |
| 548 | Refs.insert(ID, R); |
| 549 | } |
| 550 | }; |
| 551 | // Populate Refs slab from MacroRefs. |
| 552 | for (const auto &IDAndRefs : MacroRefs) { |
| 553 | for (const auto &LocAndRole : IDAndRefs.second) |
| 554 | CollectRef(IDAndRefs.first, LocAndRole); |
| 555 | } |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 556 | // Populate Refs slab from DeclRefs. |
| Haojian Wu | 7dd4950 | 2018-10-17 08:38:36 +0000 | [diff] [blame] | 557 | if (auto MainFileURI = GetURI(SM.getMainFileID())) { |
| Sam McCall | b013831 | 2018-09-04 14:39:56 +0000 | [diff] [blame] | 558 | for (const auto &It : DeclRefs) { |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 559 | if (auto ID = getSymbolID(It.first)) { |
| Utkarsh Saxena | 9347655 | 2019-11-20 15:18:54 +0100 | [diff] [blame] | 560 | for (const auto &LocAndRole : It.second) |
| 561 | CollectRef(*ID, LocAndRole); |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| Haojian Wu | d81e314 | 2018-08-31 12:54:13 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 566 | ReferencedDecls.clear(); |
| Eric Liu | 48db19e | 2018-07-09 15:31:07 +0000 | [diff] [blame] | 567 | ReferencedMacros.clear(); |
| Sam McCall | b013831 | 2018-09-04 14:39:56 +0000 | [diff] [blame] | 568 | DeclRefs.clear(); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 569 | FilesToIndexCache.clear(); |
| Sam McCall | a96efb6 | 2019-04-17 18:33:07 +0000 | [diff] [blame] | 570 | HeaderIsSelfContainedCache.clear(); |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 571 | IncludeFiles.clear(); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| Kadir Cetinkaya | 8665802 | 2019-03-19 09:27:04 +0000 | [diff] [blame] | 574 | const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID, |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 575 | bool IsMainFileOnly) { |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 576 | auto &Ctx = ND.getASTContext(); |
| 577 | auto &SM = Ctx.getSourceManager(); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 578 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 579 | Symbol S; |
| 580 | S.ID = std::move(ID); |
| Eric Liu | 7ad1696 | 2018-06-22 10:46:59 +0000 | [diff] [blame] | 581 | std::string QName = printQualifiedName(ND); |
| Sam McCall | 032db94 | 2018-06-22 06:41:43 +0000 | [diff] [blame] | 582 | // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo: |
| 583 | // for consistency with CodeCompletionString and a clean name/signature split. |
| Kadir Cetinkaya | 79063de | 2019-04-12 10:09:24 +0000 | [diff] [blame] | 584 | std::tie(S.Scope, S.Name) = splitQualifiedName(QName); |
| 585 | std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND); |
| 586 | S.TemplateSpecializationArgs = TemplateSpecializationArgs; |
| Marc-Andre Laperle | 945b5a3 | 2018-06-05 14:01:40 +0000 | [diff] [blame] | 587 | |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 588 | // We collect main-file symbols, but do not use them for code completion. |
| 589 | if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx)) |
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 590 | S.Flags |= Symbol::IndexedForCodeCompletion; |
| Eric Liu | 4859738 | 2018-10-18 12:23:05 +0000 | [diff] [blame] | 591 | if (isImplementationDetail(&ND)) |
| 592 | S.Flags |= Symbol::ImplementationDetail; |
| Sam McCall | 0e93b07 | 2019-01-14 10:01:17 +0000 | [diff] [blame] | 593 | if (!IsMainFileOnly) |
| 594 | S.Flags |= Symbol::VisibleOutsideFile; |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 595 | S.SymInfo = index::getSymbolInfo(&ND); |
| 596 | std::string FileURI; |
| Ilya Biryukov | b63c35e | 2019-12-10 10:08:39 +0100 | [diff] [blame] | 597 | auto Loc = nameLocation(ND, SM); |
| Kadir Cetinkaya | bb6cd82 | 2019-04-15 14:38:46 +0000 | [diff] [blame] | 598 | assert(Loc.isValid() && "Invalid source location for NamedDecl"); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 599 | // FIXME: use the result to filter out symbols. |
| Ilya Biryukov | 30c86b6 | 2019-08-20 08:54:30 +0000 | [diff] [blame] | 600 | shouldIndexFile(SM.getFileID(Loc)); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 601 | if (auto DeclLoc = |
| 602 | getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 603 | S.CanonicalDeclaration = *DeclLoc; |
| 604 | |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 605 | S.Origin = Opts.Origin; |
| 606 | if (ND.getAvailability() == AR_Deprecated) |
| 607 | S.Flags |= Symbol::Deprecated; |
| 608 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 609 | // Add completion info. |
| 610 | // FIXME: we may want to choose a different redecl, or combine from several. |
| 611 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 612 | // We use the primary template, as clang does during code completion. |
| 613 | CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 614 | const auto *CCS = SymbolCompletion.CreateCodeCompletionString( |
| Kadir Cetinkaya | b915790 | 2018-10-24 15:24:29 +0000 | [diff] [blame] | 615 | *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator, |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 616 | *CompletionTUInfo, |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 617 | /*IncludeBriefComments*/ false); |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 618 | std::string Documentation = |
| Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame] | 619 | formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion, |
| 620 | /*CommentsFromHeaders=*/true)); |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 621 | if (!(S.Flags & Symbol::IndexedForCodeCompletion)) { |
| Haojian Wu | da79dcc | 2019-02-25 16:00:00 +0000 | [diff] [blame] | 622 | if (Opts.StoreAllDocumentation) |
| 623 | S.Documentation = Documentation; |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 624 | Symbols.insert(S); |
| 625 | return Symbols.find(S.ID); |
| 626 | } |
| Haojian Wu | da79dcc | 2019-02-25 16:00:00 +0000 | [diff] [blame] | 627 | S.Documentation = Documentation; |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 628 | std::string Signature; |
| 629 | std::string SnippetSuffix; |
| 630 | getSignature(*CCS, &Signature, &SnippetSuffix); |
| 631 | S.Signature = Signature; |
| 632 | S.CompletionSnippetSuffix = SnippetSuffix; |
| Sam McCall | a68951e | 2018-06-22 16:11:35 +0000 | [diff] [blame] | 633 | std::string ReturnType = getReturnType(*CCS); |
| Haojian Wu | 8f85b9f | 2019-01-10 09:22:40 +0000 | [diff] [blame] | 634 | S.ReturnType = ReturnType; |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 635 | |
| Ilya Biryukov | 4d3d82e | 2018-11-26 15:52:16 +0000 | [diff] [blame] | 636 | llvm::Optional<OpaqueType> TypeStorage; |
| Ilya Biryukov | a21392b | 2018-11-26 15:29:14 +0000 | [diff] [blame] | 637 | if (S.Flags & Symbol::IndexedForCodeCompletion) { |
| Ilya Biryukov | 4d3d82e | 2018-11-26 15:52:16 +0000 | [diff] [blame] | 638 | TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion); |
| 639 | if (TypeStorage) |
| 640 | S.Type = TypeStorage->raw(); |
| Ilya Biryukov | a21392b | 2018-11-26 15:29:14 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 643 | Symbols.insert(S); |
| Sam McCall | ec02653 | 2019-05-03 13:17:29 +0000 | [diff] [blame] | 644 | setIncludeLocation(S, ND.getLocation()); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 645 | return Symbols.find(S.ID); |
| 646 | } |
| 647 | |
| 648 | void SymbolCollector::addDefinition(const NamedDecl &ND, |
| 649 | const Symbol &DeclSym) { |
| 650 | if (DeclSym.Definition) |
| 651 | return; |
| 652 | // If we saw some forward declaration, we end up copying the symbol. |
| 653 | // This is not ideal, but avoids duplicating the "is this a definition" check |
| 654 | // in clang::index. We should only see one definition. |
| 655 | Symbol S = DeclSym; |
| 656 | std::string FileURI; |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 657 | const auto &SM = ND.getASTContext().getSourceManager(); |
| Ilya Biryukov | b63c35e | 2019-12-10 10:08:39 +0100 | [diff] [blame] | 658 | auto Loc = nameLocation(ND, SM); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 659 | // FIXME: use the result to filter out symbols. |
| Ilya Biryukov | 30c86b6 | 2019-08-20 08:54:30 +0000 | [diff] [blame] | 660 | shouldIndexFile(SM.getFileID(Loc)); |
| Eric Liu | ad588af | 2018-11-06 10:55:21 +0000 | [diff] [blame] | 661 | if (auto DefLoc = |
| 662 | getTokenLocation(Loc, SM, Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 663 | S.Definition = *DefLoc; |
| 664 | Symbols.insert(S); |
| 665 | } |
| 666 | |
| Sam McCall | a96efb6 | 2019-04-17 18:33:07 +0000 | [diff] [blame] | 667 | /// Gets a canonical include (URI of the header or <header> or "header") for |
| 668 | /// header of \p FID (which should usually be the *expansion* file). |
| 669 | /// Returns None if includes should not be inserted for this file. |
| 670 | llvm::Optional<std::string> |
| 671 | SymbolCollector::getIncludeHeader(llvm::StringRef QName, FileID FID) { |
| 672 | const SourceManager &SM = ASTCtx->getSourceManager(); |
| 673 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 674 | if (!FE || FE->getName().empty()) |
| 675 | return llvm::None; |
| 676 | llvm::StringRef Filename = FE->getName(); |
| 677 | // If a file is mapped by canonical headers, use that mapping, regardless |
| 678 | // of whether it's an otherwise-good header (header guards etc). |
| 679 | if (Opts.Includes) { |
| 680 | llvm::StringRef Canonical = Opts.Includes->mapHeader(Filename, QName); |
| 681 | // If we had a mapping, always use it. |
| 682 | if (Canonical.startswith("<") || Canonical.startswith("\"")) |
| 683 | return Canonical.str(); |
| 684 | if (Canonical != Filename) |
| 685 | return toURI(SM, Canonical, Opts); |
| 686 | } |
| 687 | if (!isSelfContainedHeader(FID)) { |
| 688 | // A .inc or .def file is often included into a real header to define |
| 689 | // symbols (e.g. LLVM tablegen files). |
| 690 | if (Filename.endswith(".inc") || Filename.endswith(".def")) |
| 691 | return getIncludeHeader(QName, SM.getFileID(SM.getIncludeLoc(FID))); |
| 692 | // Conservatively refuse to insert #includes to files without guards. |
| 693 | return llvm::None; |
| 694 | } |
| 695 | // Standard case: just insert the file itself. |
| 696 | return toURI(SM, Filename, Opts); |
| 697 | } |
| 698 | |
| 699 | bool SymbolCollector::isSelfContainedHeader(FileID FID) { |
| 700 | // The real computation (which will be memoized). |
| 701 | auto Compute = [&] { |
| 702 | const SourceManager &SM = ASTCtx->getSourceManager(); |
| 703 | const FileEntry *FE = SM.getFileEntryForID(FID); |
| 704 | if (!FE) |
| 705 | return false; |
| 706 | if (!PP->getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE)) |
| 707 | return false; |
| 708 | // This pattern indicates that a header can't be used without |
| 709 | // particular preprocessor state, usually set up by another header. |
| Sam McCall | e3559ee | 2019-04-25 17:47:07 +0000 | [diff] [blame] | 710 | if (isDontIncludeMeHeader(SM.getBufferData(FID))) |
| Sam McCall | a96efb6 | 2019-04-17 18:33:07 +0000 | [diff] [blame] | 711 | return false; |
| 712 | return true; |
| 713 | }; |
| 714 | |
| 715 | auto R = HeaderIsSelfContainedCache.try_emplace(FID, false); |
| 716 | if (R.second) |
| 717 | R.first->second = Compute(); |
| 718 | return R.first->second; |
| 719 | } |
| 720 | |
| Sam McCall | e3559ee | 2019-04-25 17:47:07 +0000 | [diff] [blame] | 721 | // Is Line an #if or #ifdef directive? |
| 722 | static bool isIf(llvm::StringRef Line) { |
| 723 | Line = Line.ltrim(); |
| 724 | if (!Line.consume_front("#")) |
| 725 | return false; |
| 726 | Line = Line.ltrim(); |
| 727 | return Line.startswith("if"); |
| 728 | } |
| 729 | // Is Line an #error directive mentioning includes? |
| 730 | static bool isErrorAboutInclude(llvm::StringRef Line) { |
| 731 | Line = Line.ltrim(); |
| 732 | if (!Line.consume_front("#")) |
| 733 | return false; |
| 734 | Line = Line.ltrim(); |
| Nathan Ridge | b2f45ac | 2019-05-30 23:54:43 +0000 | [diff] [blame] | 735 | if (!Line.startswith("error")) |
| Sam McCall | e3559ee | 2019-04-25 17:47:07 +0000 | [diff] [blame] | 736 | return false; |
| 737 | return Line.contains_lower("includ"); // Matches "include" or "including". |
| 738 | } |
| 739 | |
| 740 | bool SymbolCollector::isDontIncludeMeHeader(llvm::StringRef Content) { |
| 741 | llvm::StringRef Line; |
| 742 | // Only sniff up to 100 lines or 10KB. |
| Nathan Ridge | b2f45ac | 2019-05-30 23:54:43 +0000 | [diff] [blame] | 743 | Content = Content.take_front(100 * 100); |
| Sam McCall | e3559ee | 2019-04-25 17:47:07 +0000 | [diff] [blame] | 744 | for (unsigned I = 0; I < 100 && !Content.empty(); ++I) { |
| 745 | std::tie(Line, Content) = Content.split('\n'); |
| 746 | if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first)) |
| 747 | return true; |
| 748 | } |
| 749 | return false; |
| 750 | } |
| 751 | |
| Ilya Biryukov | 30c86b6 | 2019-08-20 08:54:30 +0000 | [diff] [blame] | 752 | bool SymbolCollector::shouldIndexFile(FileID FID) { |
| 753 | if (!Opts.FileFilter) |
| 754 | return true; |
| 755 | auto I = FilesToIndexCache.try_emplace(FID); |
| 756 | if (I.second) |
| 757 | I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID); |
| 758 | return I.first->second; |
| 759 | } |
| 760 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 761 | } // namespace clangd |
| 762 | } // namespace clang |