| 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" |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 12 | #include "clang/AST/DeclCXX.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
| 14 | #include "clang/Index/IndexSymbol.h" |
| 15 | #include "clang/Index/USRGeneration.h" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
| 17 | #include "llvm/Support/Path.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace clangd { |
| 21 | |
| 22 | namespace { |
| 23 | // Make the Path absolute using the current working directory of the given |
| 24 | // SourceManager if the Path is not an absolute path. |
| 25 | // |
| 26 | // The Path can be a path relative to the build directory, or retrieved from |
| 27 | // the SourceManager. |
| 28 | std::string makeAbsolutePath(const SourceManager &SM, StringRef Path) { |
| 29 | llvm::SmallString<128> AbsolutePath(Path); |
| 30 | if (std::error_code EC = |
| 31 | SM.getFileManager().getVirtualFileSystem()->makeAbsolute( |
| 32 | AbsolutePath)) |
| 33 | llvm::errs() << "Warning: could not make absolute file: '" << EC.message() |
| 34 | << '\n'; |
| 35 | // Handle the symbolic link path case where the current working directory |
| 36 | // (getCurrentWorkingDirectory) is a symlink./ We always want to the real |
| 37 | // file path (instead of the symlink path) for the C++ symbols. |
| 38 | // |
| 39 | // Consider the following example: |
| 40 | // |
| 41 | // src dir: /project/src/foo.h |
| 42 | // current working directory (symlink): /tmp/build -> /project/src/ |
| 43 | // |
| 44 | // The file path of Symbol is "/project/src/foo.h" instead of |
| 45 | // "/tmp/build/foo.h" |
| 46 | const DirectoryEntry *Dir = SM.getFileManager().getDirectory( |
| 47 | llvm::sys::path::parent_path(AbsolutePath.str())); |
| 48 | if (Dir) { |
| 49 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); |
| Benjamin Kramer | 50a967d | 2017-12-28 14:47:01 +0000 | [diff] [blame] | 50 | SmallString<128> AbsoluteFilename; |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 51 | llvm::sys::path::append(AbsoluteFilename, DirName, |
| 52 | llvm::sys::path::filename(AbsolutePath.str())); |
| Benjamin Kramer | 50a967d | 2017-12-28 14:47:01 +0000 | [diff] [blame] | 53 | return AbsoluteFilename.str(); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 54 | } |
| 55 | return AbsolutePath.str(); |
| 56 | } |
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 57 | |
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 58 | // "a::b::c", return {"a::b", "c"}. Scope is empty if it doesn't exist. |
| 59 | std::pair<llvm::StringRef, llvm::StringRef> |
| 60 | splitQualifiedName(llvm::StringRef QName) { |
| 61 | assert(!QName.startswith("::") && "Qualified names should not start with ::"); |
| 62 | size_t Pos = QName.rfind("::"); |
| 63 | if (Pos == llvm::StringRef::npos) |
| 64 | return {StringRef(), QName}; |
| 65 | return {QName.substr(0, Pos), QName.substr(Pos + 2)}; |
| 66 | } |
| 67 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 70 | void SymbolCollector::initialize(ASTContext &Ctx) { |
| 71 | ASTCtx = &Ctx; |
| 72 | CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
| 73 | CompletionTUInfo = |
| 74 | llvm::make_unique<CodeCompletionTUInfo>(CompletionAllocator); |
| 75 | } |
| 76 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 77 | // Always return true to continue indexing. |
| 78 | bool SymbolCollector::handleDeclOccurence( |
| 79 | const Decl *D, index::SymbolRoleSet Roles, |
| 80 | ArrayRef<index::SymbolRelation> Relations, FileID FID, unsigned Offset, |
| 81 | index::IndexDataConsumer::ASTNodeInfo ASTNode) { |
| 82 | // FIXME: collect all symbol references. |
| 83 | if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) || |
| 84 | Roles & static_cast<unsigned>(index::SymbolRole::Definition))) |
| 85 | return true; |
| 86 | |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 87 | assert(CompletionAllocator && CompletionTUInfo); |
| 88 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 89 | if (const NamedDecl *ND = llvm::dyn_cast<NamedDecl>(D)) { |
| Haojian Wu | fe22b74 | 2018-01-09 10:44:09 +0000 | [diff] [blame] | 90 | // FIXME: figure out a way to handle internal linkage symbols (e.g. static |
| 91 | // variables, function) defined in the .cc files. Also we skip the symbols |
| 92 | // in anonymous namespace as the qualifier names of these symbols are like |
| 93 | // `foo::<anonymous>::bar`, which need a special handling. |
| 94 | // In real world projects, we have a relatively large set of header files |
| 95 | // that define static variables (like "static const int A = 1;"), we still |
| 96 | // want to collect these symbols, although they cause potential ODR |
| 97 | // violations. |
| 98 | if (ND->isInAnonymousNamespace()) |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 99 | return true; |
| 100 | |
| Benjamin Kramer | 50a967d | 2017-12-28 14:47:01 +0000 | [diff] [blame] | 101 | llvm::SmallString<128> USR; |
| 102 | if (index::generateUSRForDecl(ND, USR)) |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 103 | return true; |
| 104 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 105 | auto ID = SymbolID(USR); |
| Sam McCall | 4b9bbb3 | 2017-12-23 19:38:03 +0000 | [diff] [blame] | 106 | if (Symbols.find(ID) != nullptr) |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 107 | return true; |
| 108 | |
| 109 | auto &SM = ND->getASTContext().getSourceManager(); |
| Benjamin Kramer | 6452efd | 2017-12-21 17:51:35 +0000 | [diff] [blame] | 110 | std::string FilePath = |
| 111 | makeAbsolutePath(SM, SM.getFilename(D->getLocation())); |
| 112 | SymbolLocation Location = {FilePath, SM.getFileOffset(D->getLocStart()), |
| 113 | SM.getFileOffset(D->getLocEnd())}; |
| Eric Liu | 4feda80 | 2017-12-19 11:37:40 +0000 | [diff] [blame] | 114 | std::string QName = ND->getQualifiedNameAsString(); |
| 115 | auto ScopeAndName = splitQualifiedName(QName); |
| Sam McCall | 98a7318 | 2017-12-22 08:12:39 +0000 | [diff] [blame] | 116 | |
| 117 | Symbol S; |
| 118 | S.ID = std::move(ID); |
| 119 | S.Scope = ScopeAndName.first; |
| 120 | S.Name = ScopeAndName.second; |
| 121 | S.SymInfo = index::getSymbolInfo(D); |
| 122 | S.CanonicalDeclaration = Location; |
| Eric Liu | 76f6b44 | 2018-01-09 17:32:00 +0000 | [diff] [blame] | 123 | |
| 124 | // Add completion info. |
| 125 | assert(ASTCtx && PP.get() && "ASTContext and Preprocessor must be set."); |
| 126 | CodeCompletionResult SymbolCompletion(ND, 0); |
| 127 | const auto *CCS = SymbolCompletion.CreateCodeCompletionString( |
| 128 | *ASTCtx, *PP, CodeCompletionContext::CCC_Name, *CompletionAllocator, |
| 129 | *CompletionTUInfo, |
| 130 | /*IncludeBriefComments*/ true); |
| 131 | std::string Label; |
| 132 | std::string SnippetInsertText; |
| 133 | std::string IgnoredLabel; |
| 134 | std::string PlainInsertText; |
| 135 | getLabelAndInsertText(*CCS, &Label, &SnippetInsertText, |
| 136 | /*EnableSnippets=*/true); |
| 137 | getLabelAndInsertText(*CCS, &IgnoredLabel, &PlainInsertText, |
| 138 | /*EnableSnippets=*/false); |
| 139 | std::string FilterText = getFilterText(*CCS); |
| 140 | std::string Documentation = getDocumentation(*CCS); |
| 141 | std::string CompletionDetail = getDetail(*CCS); |
| 142 | |
| 143 | S.CompletionFilterText = FilterText; |
| 144 | S.CompletionLabel = Label; |
| 145 | S.CompletionPlainInsertText = PlainInsertText; |
| 146 | S.CompletionSnippetInsertText = SnippetInsertText; |
| 147 | Symbol::Details Detail; |
| 148 | Detail.Documentation = Documentation; |
| 149 | Detail.CompletionDetail = CompletionDetail; |
| 150 | S.Detail = &Detail; |
| 151 | |
| Sam McCall | 98a7318 | 2017-12-22 08:12:39 +0000 | [diff] [blame] | 152 | Symbols.insert(S); |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| Haojian Wu | 4c1394d | 2017-12-12 15:42:10 +0000 | [diff] [blame] | 158 | } // namespace clangd |
| 159 | } // namespace clang |