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