| 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 | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 11 | #include "../CodeCompletionStrings.h" | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 12 | #include "../Logger.h" | 
|  | 13 | #include "../URI.h" | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 14 | #include "CanonicalIncludes.h" | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 16 | #include "clang/ASTMatchers/ASTMatchFinder.h" | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" | 
|  | 18 | #include "clang/Index/IndexSymbol.h" | 
|  | 19 | #include "clang/Index/USRGeneration.h" | 
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" | 
|  | 22 | #include "llvm/Support/Path.h" | 
|  | 23 |  | 
|  | 24 | namespace clang { | 
|  | 25 | namespace clangd { | 
|  | 26 |  | 
|  | 27 | namespace { | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 28 | // Returns a URI of \p Path. Firstly, this makes the \p Path absolute using the | 
|  | 29 | // current working directory of the given SourceManager if the Path is not an | 
|  | 30 | // absolute path. If failed, this resolves relative paths against \p FallbackDir | 
|  | 31 | // to get an absolute path. Then, this tries creating an URI for the absolute | 
|  | 32 | // path with schemes specified in \p Opts. This returns an URI with the first | 
|  | 33 | // working scheme, if there is any; otherwise, this returns None. | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 34 | // | 
|  | 35 | // The Path can be a path relative to the build directory, or retrieved from | 
|  | 36 | // the SourceManager. | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 37 | llvm::Optional<std::string> toURI(const SourceManager &SM, StringRef Path, | 
|  | 38 | const SymbolCollector::Options &Opts) { | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 39 | llvm::SmallString<128> AbsolutePath(Path); | 
|  | 40 | if (std::error_code EC = | 
|  | 41 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute( | 
|  | 42 | AbsolutePath)) | 
|  | 43 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() | 
|  | 44 | << '\n'; | 
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 45 | if (llvm::sys::path::is_absolute(AbsolutePath)) { | 
|  | 46 | // Handle the symbolic link path case where the current working directory | 
|  | 47 | // (getCurrentWorkingDirectory) is a symlink./ We always want to the real | 
|  | 48 | // file path (instead of the symlink path) for the  C++ symbols. | 
|  | 49 | // | 
|  | 50 | // Consider the following example: | 
|  | 51 | // | 
|  | 52 | //   src dir: /project/src/foo.h | 
|  | 53 | //   current working directory (symlink): /tmp/build -> /project/src/ | 
|  | 54 | // | 
|  | 55 | // The file path of Symbol is "/project/src/foo.h" instead of | 
|  | 56 | // "/tmp/build/foo.h" | 
|  | 57 | if (const DirectoryEntry *Dir = SM.getFileManager().getDirectory( | 
|  | 58 | llvm::sys::path::parent_path(AbsolutePath.str()))) { | 
|  | 59 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); | 
|  | 60 | SmallString<128> AbsoluteFilename; | 
|  | 61 | llvm::sys::path::append(AbsoluteFilename, DirName, | 
|  | 62 | llvm::sys::path::filename(AbsolutePath.str())); | 
|  | 63 | AbsolutePath = AbsoluteFilename; | 
|  | 64 | } | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 65 | } else if (!Opts.FallbackDir.empty()) { | 
|  | 66 | llvm::sys::fs::make_absolute(Opts.FallbackDir, AbsolutePath); | 
| Eric Liu | 278e2d1 | 2018-01-29 15:13:29 +0000 | [diff] [blame] | 67 | llvm::sys::path::remove_dots(AbsolutePath, /*remove_dot_dot=*/true); | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 68 | } | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 69 |  | 
|  | 70 | std::string ErrMsg; | 
|  | 71 | for (const auto &Scheme : Opts.URISchemes) { | 
|  | 72 | auto U = URI::create(AbsolutePath, Scheme); | 
|  | 73 | if (U) | 
|  | 74 | return U->toString(); | 
|  | 75 | ErrMsg += llvm::toString(U.takeError()) + "\n"; | 
|  | 76 | } | 
|  | 77 | log(llvm::Twine("Failed to create an URI for file ") + AbsolutePath + ": " + | 
|  | 78 | ErrMsg); | 
|  | 79 | return llvm::None; | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 80 | } | 
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 81 |  | 
| Sam McCall | 8b2faee | 2018-01-19 22:18:21 +0000 | [diff] [blame] | 82 | // "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no qualifier. | 
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 83 | std::pair<llvm::StringRef, llvm::StringRef> | 
|  | 84 | splitQualifiedName(llvm::StringRef QName) { | 
|  | 85 | assert(!QName.startswith("::") && "Qualified names should not start with ::"); | 
|  | 86 | size_t Pos = QName.rfind("::"); | 
|  | 87 | if (Pos == llvm::StringRef::npos) | 
|  | 88 | return {StringRef(), QName}; | 
| Sam McCall | 8b2faee | 2018-01-19 22:18:21 +0000 | [diff] [blame] | 89 | return {QName.substr(0, Pos + 2), QName.substr(Pos + 2)}; | 
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 92 | bool shouldFilterDecl(const NamedDecl *ND, ASTContext *ASTCtx, | 
|  | 93 | const SymbolCollector::Options &Opts) { | 
|  | 94 | using namespace clang::ast_matchers; | 
|  | 95 | if (ND->isImplicit()) | 
|  | 96 | return true; | 
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 97 | // Skip anonymous declarations, e.g (anonymous enum/class/struct). | 
|  | 98 | if (ND->getDeclName().isEmpty()) | 
|  | 99 | return true; | 
|  | 100 |  | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 101 | // FIXME: figure out a way to handle internal linkage symbols (e.g. static | 
|  | 102 | // variables, function) defined in the .cc files. Also we skip the symbols | 
|  | 103 | // in anonymous namespace as the qualifier names of these symbols are like | 
|  | 104 | // `foo::<anonymous>::bar`, which need a special handling. | 
|  | 105 | // In real world projects, we have a relatively large set of header files | 
|  | 106 | // that define static variables (like "static const int A = 1;"), we still | 
|  | 107 | // want to collect these symbols, although they cause potential ODR | 
|  | 108 | // violations. | 
|  | 109 | if (ND->isInAnonymousNamespace()) | 
|  | 110 | return true; | 
|  | 111 |  | 
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 112 | // We only want: | 
|  | 113 | //   * symbols in namespaces or translation unit scopes (e.g. no class | 
|  | 114 | //     members) | 
|  | 115 | //   * enum constants in unscoped enum decl (e.g. "red" in "enum {red};") | 
| Eric Liu | cf17738 | 2018-02-02 10:31:42 +0000 | [diff] [blame] | 116 | auto InTopLevelScope = hasDeclContext( | 
|  | 117 | anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl())); | 
| Haojian Wu | 9873fdd | 2018-01-19 09:35:55 +0000 | [diff] [blame] | 118 | if (match(decl(allOf(Opts.IndexMainFiles | 
|  | 119 | ? decl() | 
|  | 120 | : decl(unless(isExpansionInMainFile())), | 
|  | 121 | anyOf(InTopLevelScope, | 
|  | 122 | hasDeclContext(enumDecl(InTopLevelScope, | 
|  | 123 | unless(isScoped())))))), | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 124 | *ND, *ASTCtx) | 
|  | 125 | .empty()) | 
|  | 126 | return true; | 
|  | 127 |  | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 131 | // We only collect #include paths for symbols that are suitable for global code | 
|  | 132 | // completion, except for namespaces since #include path for a namespace is hard | 
|  | 133 | // to define. | 
|  | 134 | bool shouldCollectIncludePath(index::SymbolKind Kind) { | 
|  | 135 | using SK = index::SymbolKind; | 
|  | 136 | switch (Kind) { | 
|  | 137 | case SK::Macro: | 
|  | 138 | case SK::Enum: | 
|  | 139 | case SK::Struct: | 
|  | 140 | case SK::Class: | 
|  | 141 | case SK::Union: | 
|  | 142 | case SK::TypeAlias: | 
|  | 143 | case SK::Using: | 
|  | 144 | case SK::Function: | 
|  | 145 | case SK::Variable: | 
|  | 146 | case SK::EnumConstant: | 
|  | 147 | return true; | 
|  | 148 | default: | 
|  | 149 | return false; | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
| Eric Liu | 02ce01f | 2018-02-22 10:14:05 +0000 | [diff] [blame^] | 153 | /// Gets a canonical include (URI of the header or <header>  or "header") for | 
|  | 154 | /// header of \p Loc. | 
|  | 155 | /// Returns None if fails to get include header for \p Loc. | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 156 | /// FIXME: we should handle .inc files whose symbols are expected be exported by | 
|  | 157 | /// their containing headers. | 
|  | 158 | llvm::Optional<std::string> | 
|  | 159 | getIncludeHeader(const SourceManager &SM, SourceLocation Loc, | 
|  | 160 | const SymbolCollector::Options &Opts) { | 
|  | 161 | llvm::StringRef FilePath = SM.getFilename(Loc); | 
|  | 162 | if (FilePath.empty()) | 
|  | 163 | return llvm::None; | 
|  | 164 | if (Opts.Includes) { | 
|  | 165 | llvm::StringRef Mapped = Opts.Includes->mapHeader(FilePath); | 
|  | 166 | if (Mapped != FilePath) | 
|  | 167 | return (Mapped.startswith("<") || Mapped.startswith("\"")) | 
|  | 168 | ? Mapped.str() | 
|  | 169 | : ("\"" + Mapped + "\"").str(); | 
|  | 170 | } | 
| Eric Liu | 02ce01f | 2018-02-22 10:14:05 +0000 | [diff] [blame^] | 171 |  | 
|  | 172 | return toURI(SM, SM.getFilename(Loc), Opts); | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Haojian Wu | b018906 | 2018-01-31 12:56:51 +0000 | [diff] [blame] | 175 | // Return the symbol location of the given declaration `D`. | 
|  | 176 | // | 
|  | 177 | // For symbols defined inside macros: | 
|  | 178 | //   * use expansion location, if the symbol is formed via macro concatenation. | 
|  | 179 | //   * use spelling location, otherwise. | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 180 | llvm::Optional<SymbolLocation> | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 181 | getSymbolLocation(const NamedDecl &D, SourceManager &SM, | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 182 | const SymbolCollector::Options &Opts, | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 183 | const clang::LangOptions& LangOpts, | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 184 | std::string &FileURIStorage) { | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 185 | SourceLocation SpellingLoc = SM.getSpellingLoc(D.getLocation()); | 
|  | 186 | if (D.getLocation().isMacroID()) { | 
|  | 187 | std::string PrintLoc = SpellingLoc.printToString(SM); | 
| Haojian Wu | 3b8e00c | 2018-02-06 09:50:35 +0000 | [diff] [blame] | 188 | if (llvm::StringRef(PrintLoc).startswith("<scratch") || | 
|  | 189 | llvm::StringRef(PrintLoc).startswith("<command line>")) { | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 190 | // We use the expansion location for the following symbols, as spelling | 
|  | 191 | // locations of these symbols are not interesting to us: | 
|  | 192 | //   * symbols formed via macro concatenation, the spelling location will | 
|  | 193 | //     be "<scratch space>" | 
|  | 194 | //   * symbols controlled and defined by a compile command-line option | 
|  | 195 | //     `-DName=foo`, the spelling location will be "<command line>". | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 196 | SpellingLoc = SM.getExpansionRange(D.getLocation()).first; | 
| Haojian Wu | b018906 | 2018-01-31 12:56:51 +0000 | [diff] [blame] | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 200 | auto U = toURI(SM, SM.getFilename(SpellingLoc), Opts); | 
| Eric Liu | 7f24765 | 2018-02-06 16:10:35 +0000 | [diff] [blame] | 201 | if (!U) | 
|  | 202 | return llvm::None; | 
|  | 203 | FileURIStorage = std::move(*U); | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 204 | SymbolLocation Result; | 
|  | 205 | Result.FileURI = FileURIStorage; | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 206 | Result.StartOffset = SM.getFileOffset(SpellingLoc); | 
|  | 207 | Result.EndOffset = Result.StartOffset + clang::Lexer::MeasureTokenLength( | 
|  | 208 | SpellingLoc, SM, LangOpts); | 
| 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 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 212 | } // namespace | 
|  | 213 |  | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 214 | SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {} | 
|  | 215 |  | 
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 216 | void SymbolCollector::initialize(ASTContext &Ctx) { | 
|  | 217 | ASTCtx = &Ctx; | 
|  | 218 | CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); | 
|  | 219 | CompletionTUInfo = | 
|  | 220 | llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator); | 
|  | 221 | } | 
|  | 222 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 223 | // Always return true to continue indexing. | 
|  | 224 | bool SymbolCollector::handleDeclOccurence( | 
|  | 225 | const Decl *D, index::SymbolRoleSet Roles, | 
|  | 226 | ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset, | 
|  | 227 | index::IndexDataConsumer::ASTNodeInfo ASTNode) { | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 228 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); | 
|  | 229 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 230 | // FIXME: collect all symbol references. | 
|  | 231 | if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) || | 
|  | 232 | Roles & static_cast<unsigned>(index::SymbolRole::Definition))) | 
|  | 233 | return true; | 
|  | 234 |  | 
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 235 | assert(CompletionAllocator && CompletionTUInfo); | 
|  | 236 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 237 | if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) { | 
| Eric Liu | 9af958f | 2018-01-10 14:57:58 +0000 | [diff] [blame] | 238 | if (shouldFilterDecl(ND, ASTCtx, Opts)) | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 239 | return true; | 
| Benjamin Kramer | 50a967d | 2017-12-28 14:47:01 +0000 | [diff] [blame] | 240 | llvm::SmallString<128> USR; | 
|  | 241 | if (index::generateUSRForDecl(ND, USR)) | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 242 | return true; | 
|  | 243 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 244 | auto ID = SymbolID(USR); | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 245 | const Symbol* BasicSymbol = Symbols.find(ID); | 
|  | 246 | if (!BasicSymbol) // Regardless of role, ND is the canonical declaration. | 
|  | 247 | BasicSymbol = addDeclaration(*ND, std::move(ID)); | 
|  | 248 | if (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) | 
|  | 249 | addDefinition(*cast<NamedDecl>(ASTNode.OrigD), *BasicSymbol); | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 250 | } | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 251 | return true; | 
|  | 252 | } | 
|  | 253 |  | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 254 | const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, | 
|  | 255 | SymbolID ID) { | 
|  | 256 | auto &SM = ND.getASTContext().getSourceManager(); | 
|  | 257 |  | 
|  | 258 | std::string QName; | 
|  | 259 | llvm::raw_string_ostream OS(QName); | 
|  | 260 | PrintingPolicy Policy(ASTCtx->getLangOpts()); | 
|  | 261 | // Note that inline namespaces are treated as transparent scopes. This | 
|  | 262 | // reflects the way they're most commonly used for lookup. Ideally we'd | 
|  | 263 | // include them, but at query time it's hard to find all the inline | 
|  | 264 | // namespaces to query: the preamble doesn't have a dedicated list. | 
|  | 265 | Policy.SuppressUnwrittenScope = true; | 
|  | 266 | ND.printQualifiedName(OS, Policy); | 
|  | 267 | OS.flush(); | 
|  | 268 |  | 
|  | 269 | Symbol S; | 
|  | 270 | S.ID = std::move(ID); | 
|  | 271 | std::tie(S.Scope, S.Name) = splitQualifiedName(QName); | 
|  | 272 | S.SymInfo = index::getSymbolInfo(&ND); | 
|  | 273 | std::string FileURI; | 
|  | 274 | // FIXME: we may want a different "canonical" heuristic than clang chooses. | 
|  | 275 | // Clang seems to choose the first, which may not have the most information. | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 276 | if (auto DeclLoc = | 
|  | 277 | getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI)) | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 278 | S.CanonicalDeclaration = *DeclLoc; | 
|  | 279 |  | 
|  | 280 | // Add completion info. | 
|  | 281 | // FIXME: we may want to choose a different redecl, or combine from several. | 
|  | 282 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); | 
|  | 283 | CodeCompletionResult SymbolCompletion(&ND, 0); | 
|  | 284 | const auto *CCS = SymbolCompletion.CreateCodeCompletionString( | 
|  | 285 | *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, | 
|  | 286 | *CompletionTUInfo, | 
|  | 287 | /*IncludeBriefComments*/ true); | 
|  | 288 | std::string Label; | 
|  | 289 | std::string SnippetInsertText; | 
|  | 290 | std::string IgnoredLabel; | 
|  | 291 | std::string PlainInsertText; | 
|  | 292 | getLabelAndInsertText(*CCS, &Label, &SnippetInsertText, | 
|  | 293 | /*EnableSnippets=*/true); | 
|  | 294 | getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText, | 
|  | 295 | /*EnableSnippets=*/false); | 
|  | 296 | std::string FilterText = getFilterText(*CCS); | 
|  | 297 | std::string Documentation = getDocumentation(*CCS); | 
|  | 298 | std::string CompletionDetail = getDetail(*CCS); | 
|  | 299 |  | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 300 | std::string Include; | 
|  | 301 | if (Opts.CollectIncludePath && shouldCollectIncludePath(S.SymInfo.Kind)) { | 
|  | 302 | // Use the expansion location to get the #include header since this is | 
|  | 303 | // where the symbol is exposed. | 
|  | 304 | if (auto Header = | 
|  | 305 | getIncludeHeader(SM, SM.getExpansionLoc(ND.getLocation()), Opts)) | 
|  | 306 | Include = std::move(*Header); | 
|  | 307 | } | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 308 | S.CompletionFilterText = FilterText; | 
|  | 309 | S.CompletionLabel = Label; | 
|  | 310 | S.CompletionPlainInsertText = PlainInsertText; | 
|  | 311 | S.CompletionSnippetInsertText = SnippetInsertText; | 
|  | 312 | Symbol::Details Detail; | 
|  | 313 | Detail.Documentation = Documentation; | 
|  | 314 | Detail.CompletionDetail = CompletionDetail; | 
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 315 | Detail.IncludeHeader = Include; | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 316 | S.Detail = &Detail; | 
|  | 317 |  | 
|  | 318 | Symbols.insert(S); | 
|  | 319 | return Symbols.find(S.ID); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | void SymbolCollector::addDefinition(const NamedDecl &ND, | 
|  | 323 | const Symbol &DeclSym) { | 
|  | 324 | if (DeclSym.Definition) | 
|  | 325 | return; | 
|  | 326 | // If we saw some forward declaration, we end up copying the symbol. | 
|  | 327 | // This is not ideal, but avoids duplicating the "is this a definition" check | 
|  | 328 | // in clang::index. We should only see one definition. | 
|  | 329 | Symbol S = DeclSym; | 
|  | 330 | std::string FileURI; | 
|  | 331 | if (auto DefLoc = getSymbolLocation(ND, ND.getASTContext().getSourceManager(), | 
| Haojian Wu | dc02a3d | 2018-02-13 09:53:50 +0000 | [diff] [blame] | 332 | Opts, ASTCtx->getLangOpts(), FileURI)) | 
| Sam McCall | 6003951 | 2018-02-09 14:42:01 +0000 | [diff] [blame] | 333 | S.Definition = *DefLoc; | 
|  | 334 | Symbols.insert(S); | 
|  | 335 | } | 
|  | 336 |  | 
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 337 | } // namespace clangd | 
|  | 338 | } // namespace clang |