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