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