| 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" |
| Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 11 | #include "../AST.h" |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 12 | #include "../CodeCompletionStrings.h" |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 13 | #include "../Logger.h" |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 14 | #include "../SourceCode.h" |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 15 | #include "../URI.h" |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 16 | #include "CanonicalIncludes.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 19 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Index/IndexSymbol.h" |
| 22 | #include "clang/Index/USRGeneration.h" |
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/Path.h" |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace clangd { |
| 29 | |
| 30 | namespace { |
| Ilya Biryukov | f118d51 | 2018-04-14 16:27:35 +0000 | [diff] [blame] | 31 | /// If \p ND is a template specialization, returns the described template. |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 32 | /// Otherwise, returns \p ND. |
| 33 | const NamedDecl &getTemplateOrThis(const NamedDecl &ND) { |
| Ilya Biryukov | f118d51 | 2018-04-14 16:27:35 +0000 | [diff] [blame] | 34 | if (auto T = ND.getDescribedTemplate()) |
| 35 | return *T; |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 36 | return ND; |
| 37 | } |
| 38 | |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 39 | // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the |
| 40 | // current working directory of the given SourceManager if the Path is not an |
| 41 | // absolute path. If failed, this resolves relative paths against \p FallbackDir |
| 42 | // to get an absolute path. Then, this tries creating an URI for the absolute |
| 43 | // path with schemes specified in \p Opts. This returns an URI with the first |
| 44 | // working scheme, if there is any; otherwise, this returns None. |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 45 | // |
| 46 | // The Path can be a path relative to the build directory, or retrieved from |
| 47 | // the SourceManager. |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 48 | llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path, |
| 49 | const SymbolCollector::Options &Opts) { |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 50 | llvm::SmallString<128> AbsolutePath(Path); |
| 51 | if (std::error_code EC = |
| 52 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute( |
| 53 | AbsolutePath)) |
| Eric Liu | 20defe1 | 2018-05-15 16:22:43 +0000 | [diff] [blame] | 54 | log("Warning: could not make absolute file: " + EC.message()); |
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 55 | if (llvm::sys::path::is_absolute(AbsolutePath)) { |
| 56 | // Handle the symbolic link path case where the current working directory |
| 57 | // (getCurrentWorkingDirectory) is a symlink./ We always want to the real |
| 58 | // file path (instead of the symlink path) for the C++ symbols. |
| 59 | // |
| 60 | // Consider the following example: |
| 61 | // |
| 62 | // src dir: /project/src/foo.h |
| 63 | // current working directory (symlink): /tmp/build -> /project/src/ |
| 64 | // |
| 65 | // The file path of Symbol is "/project/src/foo.h" instead of |
| 66 | // "/tmp/build/foo.h" |
| 67 | if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory( |
| 68 | llvm::sys::path::parent_path(AbsolutePath.str()))) { |
| 69 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); |
| 70 | SmallString<128> AbsoluteFilename; |
| 71 | llvm::sys::path::append(AbsoluteFilename, DirName, |
| 72 | llvm::sys::path::filename(AbsolutePath.str())); |
| 73 | AbsolutePath = AbsoluteFilename; |
| 74 | } |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 75 | } else if (!Opts.FallbackDir.empty()) { |
| 76 | llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath); |
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 77 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 78 | } |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 79 | |
| 80 | std::string ErrMsg; |
| 81 | for (const auto &Scheme : Opts.URISchemes) { |
| 82 | auto U = URI::create(AbsolutePath, Scheme); |
| 83 | if (U) |
| 84 | return U->toString(); |
| 85 | ErrMsg += llvm::toString(U.takeError()) + "\n"; |
| 86 | } |
| 87 | log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " + |
| 88 | ErrMsg); |
| 89 | return llvm::None; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 90 | } |
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 91 | |
| Eric Liu | d67ec24 | 2018-05-16 12:12:30 +0000 | [diff] [blame] | 92 | // All proto generated headers should start with this line. |
| 93 | static const char *PROTO_HEADER_COMMENT = |
| 94 | "// Generated by the protocol buffer compiler. DO NOT EDIT!"; |
| 95 | |
| 96 | // Checks whether the decl is a private symbol in a header generated by |
| 97 | // protobuf compiler. |
| 98 | // To identify whether a proto header is actually generated by proto compiler, |
| 99 | // we check whether it starts with PROTO_HEADER_COMMENT. |
| 100 | // FIXME: make filtering extensible when there are more use cases for symbol |
| 101 | // filters. |
| 102 | bool isPrivateProtoDecl(const NamedDecl &ND) { |
| 103 | const auto &SM = ND.getASTContext().getSourceManager(); |
| 104 | auto Loc = findNameLoc(&ND); |
| 105 | auto FileName = SM.getFilename(Loc); |
| 106 | if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h")) |
| 107 | return false; |
| 108 | auto FID = SM.getFileID(Loc); |
| 109 | // Double check that this is an actual protobuf header. |
| 110 | if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT)) |
| 111 | return false; |
| 112 | |
| 113 | // ND without identifier can be operators. |
| 114 | if (ND.getIdentifier() == nullptr) |
| 115 | return false; |
| 116 | auto Name = ND.getIdentifier()->getName(); |
| 117 | if (!Name.contains('_')) |
| 118 | return false; |
| 119 | // Nested proto entities (e.g. Message::Nested) have top-level decls |
| 120 | // that shouldn't be used (Message_Nested). Ignore them completely. |
| 121 | // The nested entities are dangling type aliases, we may want to reconsider |
| 122 | // including them in the future. |
| 123 | // For enum constants, SOME_ENUM_CONSTANT is not private and should be |
| 124 | // indexed. Outer_INNER is private. This heuristic relies on naming style, it |
| 125 | // will include OUTER_INNER and exclude some_enum_constant. |
| 126 | // FIXME: the heuristic relies on naming style (i.e. no underscore in |
| 127 | // user-defined names) and can be improved. |
| 128 | return (ND.getKind() != Decl::EnumConstant) || |
| 129 | std::any_of(Name.begin(), Name.end(), islower); |
| 130 | } |
| 131 | |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 132 | bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx, |
| 133 | const SymbolCollector::Options &Opts) { |
| 134 | using namespace clang::ast_matchers; |
| 135 | if (ND->isImplicit()) |
| 136 | return true; |
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 137 | // Skip anonymous declarations, e.g (anonymous enum/class/struct). |
| 138 | if (ND->getDeclName().isEmpty()) |
| 139 | return true; |
| 140 | |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 141 | // FIXME: figure out a way to handle internal linkage symbols (e.g. static |
| 142 | // variables, function) defined in the .cc files. Also we skip the symbols |
| 143 | // in anonymous namespace as the qualifier names of these symbols are like |
| 144 | // `foo::<anonymous>::bar`, which need a special handling. |
| 145 | // In real world projects, we have a relatively large set of header files |
| 146 | // that define static variables (like "static const int A = 1;"), we still |
| 147 | // want to collect these symbols, although they cause potential ODR |
| 148 | // violations. |
| 149 | if (ND->isInAnonymousNamespace()) |
| 150 | return true; |
| 151 | |
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 152 | // We only want: |
| 153 | // * symbols in namespaces or translation unit scopes (e.g. no class |
| 154 | // members) |
| 155 | // * enum constants in unscoped enum decl (e.g. "red" in "enum {red};") |
| Eric Liu | cf17738 | 2018-02-02 10:31:42 +0000 | [diff] [blame] | 156 | auto InTopLevelScope = hasDeclContext( |
| 157 | anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl())); |
| Sam McCall | 824913b | 2018-03-09 13:25:29 +0000 | [diff] [blame] | 158 | // Don't index template specializations. |
| 159 | auto IsSpecialization = |
| 160 | anyOf(functionDecl(isExplicitTemplateSpecialization()), |
| 161 | cxxRecordDecl(isExplicitTemplateSpecialization()), |
| 162 | varDecl(isExplicitTemplateSpecialization())); |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 163 | if (match(decl(allOf(unless(isExpansionInMainFile()), |
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 164 | anyOf(InTopLevelScope, |
| 165 | hasDeclContext(enumDecl(InTopLevelScope, |
| Sam McCall | 824913b | 2018-03-09 13:25:29 +0000 | [diff] [blame] | 166 | unless(isScoped())))), |
| 167 | unless(IsSpecialization))), |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 168 | *ND, *ASTCtx) |
| 169 | .empty()) |
| 170 | return true; |
| 171 | |
| Eric Liu | d67ec24 | 2018-05-16 12:12:30 +0000 | [diff] [blame] | 172 | // Avoid indexing internal symbols in protobuf generated headers. |
| 173 | if (isPrivateProtoDecl(*ND)) |
| 174 | return true; |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 178 | // We only collect #include paths for symbols that are suitable for global code |
| 179 | // completion, except for namespaces since #include path for a namespace is hard |
| 180 | // to define. |
| 181 | bool shouldCollectIncludePath(index::SymbolKind Kind) { |
| 182 | using SK = index::SymbolKind; |
| 183 | switch (Kind) { |
| 184 | case SK::Macro: |
| 185 | case SK::Enum: |
| 186 | case SK::Struct: |
| 187 | case SK::Class: |
| 188 | case SK::Union: |
| 189 | case SK::TypeAlias: |
| 190 | case SK::Using: |
| 191 | case SK::Function: |
| 192 | case SK::Variable: |
| 193 | case SK::EnumConstant: |
| 194 | return true; |
| 195 | default: |
| 196 | return false; |
| 197 | } |
| 198 | } |
| 199 | |
| Eric Liu | 02ce01f | 2018-02-22 10:14:05 +0000 | [diff] [blame] | 200 | /// Gets a canonical include (URI of the header or <header> or "header") for |
| 201 | /// header of \p Loc. |
| 202 | /// Returns None if fails to get include header for \p Loc. |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 203 | llvm::Optional<std::string> |
| Eric Liu | b96363d | 2018-03-01 18:06:40 +0000 | [diff] [blame] | 204 | getIncludeHeader(llvm::StringRef QName, const SourceManager &SM, |
| 205 | SourceLocation Loc, const SymbolCollector::Options &Opts) { |
| Eric Liu | 3cee95e | 2018-05-24 14:40:24 +0000 | [diff] [blame] | 206 | std::vector<std::string> Headers; |
| 207 | // Collect the #include stack. |
| 208 | while (true) { |
| 209 | if (!Loc.isValid()) |
| 210 | break; |
| 211 | auto FilePath = SM.getFilename(Loc); |
| 212 | if (FilePath.empty()) |
| 213 | break; |
| 214 | Headers.push_back(FilePath); |
| 215 | if (SM.isInMainFile(Loc)) |
| 216 | break; |
| 217 | Loc = SM.getIncludeLoc(SM.getFileID(Loc)); |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 218 | } |
| Eric Liu | 3cee95e | 2018-05-24 14:40:24 +0000 | [diff] [blame] | 219 | if (Headers.empty()) |
| 220 | return llvm::None; |
| 221 | llvm::StringRef Header = Headers[0]; |
| 222 | if (Opts.Includes) { |
| 223 | Header = Opts.Includes->mapHeader(Headers, QName); |
| 224 | if (Header.startswith("<") || Header.startswith("\"")) |
| 225 | return Header.str(); |
| 226 | } |
| 227 | return toURI(SM, Header, Opts); |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| Haojian Wu | b018906 | 2018-01-31 12:56:51 +0000 | [diff] [blame] | 230 | // Return the symbol location of the given declaration `D`. |
| 231 | // |
| 232 | // For symbols defined inside macros: |
| 233 | // * use expansion location, if the symbol is formed via macro concatenation. |
| 234 | // * use spelling location, otherwise. |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 235 | llvm::Optional<SymbolLocation> getSymbolLocation( |
| 236 | const NamedDecl &D, SourceManager &SM, const SymbolCollector::Options &Opts, |
| 237 | const clang::LangOptions &LangOpts, std::string &FileURIStorage) { |
| Haojian Wu | 5f10026 | 2018-03-09 14:00:34 +0000 | [diff] [blame] | 238 | SourceLocation NameLoc = findNameLoc(&D); |
| 239 | auto U = toURI(SM, SM.getFilename(NameLoc), Opts); |
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 240 | if (!U) |
| 241 | return llvm::None; |
| 242 | FileURIStorage = std::move(*U); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 243 | SymbolLocation Result; |
| 244 | Result.FileURI = FileURIStorage; |
| Haojian Wu | 545c02a | 2018-04-13 08:30:39 +0000 | [diff] [blame] | 245 | auto TokenLength = clang::Lexer::MeasureTokenLength(NameLoc, SM, LangOpts); |
| 246 | |
| 247 | auto CreatePosition = [&SM](SourceLocation Loc) { |
| Haojian Wu | c534001 | 2018-04-30 11:40:02 +0000 | [diff] [blame] | 248 | auto LSPLoc = sourceLocToPosition(SM, Loc); |
| Haojian Wu | 545c02a | 2018-04-13 08:30:39 +0000 | [diff] [blame] | 249 | SymbolLocation::Position Pos; |
| Haojian Wu | c534001 | 2018-04-30 11:40:02 +0000 | [diff] [blame] | 250 | Pos.Line = LSPLoc.line; |
| 251 | Pos.Column = LSPLoc.character; |
| Haojian Wu | 545c02a | 2018-04-13 08:30:39 +0000 | [diff] [blame] | 252 | return Pos; |
| 253 | }; |
| 254 | |
| 255 | Result.Start = CreatePosition(NameLoc); |
| 256 | auto EndLoc = NameLoc.getLocWithOffset(TokenLength); |
| 257 | Result.End = CreatePosition(EndLoc); |
| 258 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 259 | return std::move(Result); |
| Haojian Wu | b018906 | 2018-01-31 12:56:51 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 262 | // Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union) |
| 263 | // in a header file, in which case clangd would prefer to use ND as a canonical |
| 264 | // declaration. |
| 265 | // 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] | 266 | // the first seen declaration as canonical declaration is not a good enough |
| Eric Liu | cf8601b | 2018-02-28 09:33:15 +0000 | [diff] [blame] | 267 | // heuristic. |
| 268 | bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) { |
| 269 | using namespace clang::ast_matchers; |
| 270 | return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) && |
| 271 | llvm::isa<TagDecl>(&ND) && |
| 272 | match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty(); |
| 273 | } |
| 274 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 275 | } // namespace |
| 276 | |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 277 | SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {} |
| 278 | |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 279 | void SymbolCollector::initialize(ASTContext &Ctx) { |
| 280 | ASTCtx = &Ctx; |
| 281 | CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
| 282 | CompletionTUInfo = |
| 283 | llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator); |
| 284 | } |
| 285 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 286 | // Always return true to continue indexing. |
| 287 | bool SymbolCollector::handleDeclOccurence( |
| 288 | const Decl *D, index::SymbolRoleSet Roles, |
| Sam McCall | b9d5711 | 2018-04-09 14:28:52 +0000 | [diff] [blame] | 289 | ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc, |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 290 | index::IndexDataConsumer::ASTNodeInfo ASTNode) { |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 291 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 292 | assert(CompletionAllocator && CompletionTUInfo); |
| 293 | const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D); |
| 294 | if (!ND) |
| 295 | return true; |
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 296 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 297 | // Mark D as referenced if this is a reference coming from the main file. |
| 298 | // 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] | 299 | auto &SM = ASTCtx->getSourceManager(); |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 300 | if (Opts.CountReferences && |
| 301 | (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) && |
| Sam McCall | b9d5711 | 2018-04-09 14:28:52 +0000 | [diff] [blame] | 302 | SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID()) |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 303 | ReferencedDecls.insert(ND); |
| 304 | |
| 305 | // Don't continue indexing if this is a mere reference. |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 306 | if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) || |
| 307 | Roles & static_cast<unsigned>(index::SymbolRole::Definition))) |
| 308 | return true; |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 309 | if (shouldFilterDecl(ND, ASTCtx, Opts)) |
| 310 | return true; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 311 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 312 | llvm::SmallString<128> USR; |
| 313 | if (index::generateUSRForDecl(ND, USR)) |
| 314 | return true; |
| 315 | SymbolID ID(USR); |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 316 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 317 | const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD); |
| 318 | const Symbol *BasicSymbol = Symbols.find(ID); |
| 319 | if (!BasicSymbol) // Regardless of role, ND is the canonical declaration. |
| 320 | BasicSymbol = addDeclaration(*ND, std::move(ID)); |
| 321 | else if (isPreferredDeclaration(OriginalDecl, Roles)) |
| 322 | // If OriginalDecl is preferred, replace the existing canonical |
| 323 | // declaration (e.g. a class forward declaration). There should be at most |
| 324 | // one duplicate as we expect to see only one preferred declaration per |
| 325 | // TU, because in practice they are definitions. |
| 326 | BasicSymbol = addDeclaration(OriginalDecl, std::move(ID)); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 327 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 328 | if (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) |
| 329 | addDefinition(OriginalDecl, *BasicSymbol); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
| Sam McCall | 93f99bf | 2018-03-12 14:49:09 +0000 | [diff] [blame] | 333 | void SymbolCollector::finish() { |
| 334 | // At the end of the TU, add 1 to the refcount of the ReferencedDecls. |
| 335 | for (const auto *ND : ReferencedDecls) { |
| 336 | llvm::SmallString<128> USR; |
| 337 | if (!index::generateUSRForDecl(ND, USR)) |
| 338 | if (const auto *S = Symbols.find(SymbolID(USR))) { |
| 339 | Symbol Inc = *S; |
| 340 | ++Inc.References; |
| 341 | Symbols.insert(Inc); |
| 342 | } |
| 343 | } |
| 344 | ReferencedDecls.clear(); |
| 345 | } |
| 346 | |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 347 | const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, |
| 348 | SymbolID ID) { |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 349 | auto &Ctx = ND.getASTContext(); |
| 350 | auto &SM = Ctx.getSourceManager(); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 351 | |
| 352 | std::string QName; |
| 353 | llvm::raw_string_ostream OS(QName); |
| 354 | PrintingPolicy Policy(ASTCtx->getLangOpts()); |
| 355 | // Note that inline namespaces are treated as transparent scopes. This |
| 356 | // reflects the way they're most commonly used for lookup. Ideally we'd |
| 357 | // include them, but at query time it's hard to find all the inline |
| 358 | // namespaces to query: the preamble doesn't have a dedicated list. |
| 359 | Policy.SuppressUnwrittenScope = true; |
| 360 | ND.printQualifiedName(OS, Policy); |
| 361 | OS.flush(); |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 362 | assert(!StringRef(QName).startswith("::")); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 363 | |
| 364 | Symbol S; |
| 365 | S.ID = std::move(ID); |
| 366 | std::tie(S.Scope, S.Name) = splitQualifiedName(QName); |
| 367 | S.SymInfo = index::getSymbolInfo(&ND); |
| 368 | std::string FileURI; |
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 369 | if (auto DeclLoc = |
| 370 | getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 371 | S.CanonicalDeclaration = *DeclLoc; |
| 372 | |
| 373 | // Add completion info. |
| 374 | // FIXME: we may want to choose a different redecl, or combine from several. |
| 375 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| Ilya Biryukov | cf124bd | 2018-04-13 11:03:07 +0000 | [diff] [blame] | 376 | // We use the primary template, as clang does during code completion. |
| 377 | CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 378 | const auto *CCS = SymbolCompletion.CreateCodeCompletionString( |
| 379 | *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, |
| 380 | *CompletionTUInfo, |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 381 | /*IncludeBriefComments*/ false); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 382 | std::string Label; |
| 383 | std::string SnippetInsertText; |
| 384 | std::string IgnoredLabel; |
| 385 | std::string PlainInsertText; |
| 386 | getLabelAndInsertText(*CCS, &Label, &SnippetInsertText, |
| 387 | /*EnableSnippets=*/true); |
| 388 | getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText, |
| 389 | /*EnableSnippets=*/false); |
| 390 | std::string FilterText = getFilterText(*CCS); |
| Ilya Biryukov | 4371450 | 2018-05-16 12:32:44 +0000 | [diff] [blame] | 391 | std::string Documentation = |
| Ilya Biryukov | be0eb8f | 2018-05-24 14:49:23 +0000 | [diff] [blame^] | 392 | formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion, |
| 393 | /*CommentsFromHeaders=*/true)); |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 394 | std::string CompletionDetail = getDetail(*CCS); |
| 395 | |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 396 | std::string Include; |
| 397 | if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) { |
| 398 | // Use the expansion location to get the #include header since this is |
| 399 | // where the symbol is exposed. |
| Eric Liu | b96363d | 2018-03-01 18:06:40 +0000 | [diff] [blame] | 400 | if (auto Header = getIncludeHeader( |
| 401 | QName, SM, SM.getExpansionLoc(ND.getLocation()), Opts)) |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 402 | Include = std::move(*Header); |
| 403 | } |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 404 | S.CompletionFilterText = FilterText; |
| 405 | S.CompletionLabel = Label; |
| 406 | S.CompletionPlainInsertText = PlainInsertText; |
| 407 | S.CompletionSnippetInsertText = SnippetInsertText; |
| 408 | Symbol::Details Detail; |
| 409 | Detail.Documentation = Documentation; |
| 410 | Detail.CompletionDetail = CompletionDetail; |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 411 | Detail.IncludeHeader = Include; |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 412 | S.Detail = &Detail; |
| 413 | |
| 414 | Symbols.insert(S); |
| 415 | return Symbols.find(S.ID); |
| 416 | } |
| 417 | |
| 418 | void SymbolCollector::addDefinition(const NamedDecl &ND, |
| 419 | const Symbol &DeclSym) { |
| 420 | if (DeclSym.Definition) |
| 421 | return; |
| 422 | // If we saw some forward declaration, we end up copying the symbol. |
| 423 | // This is not ideal, but avoids duplicating the "is this a definition" check |
| 424 | // in clang::index. We should only see one definition. |
| 425 | Symbol S = DeclSym; |
| 426 | std::string FileURI; |
| 427 | if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(), |
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 428 | Opts, ASTCtx->getLangOpts(), FileURI)) |
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 429 | S.Definition = *DefLoc; |
| 430 | Symbols.insert(S); |
| 431 | } |
| 432 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 433 | } // namespace clangd |
| 434 | } // namespace clang |