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