Author: hokein Date: Tue Aug 7 01:57:52 2018 New Revision: 339116 URL: http://llvm.org/viewvc/llvm-project?rev=339116&view=rev Log: [clangd] Share getSymbolID implementation.
Summary: And remove all duplicated implementation. Reviewers: ioeric Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D50375 Modified: clang-tools-extra/trunk/clangd/AST.cpp clang-tools-extra/trunk/clangd/AST.h clang-tools-extra/trunk/clangd/CodeComplete.cpp clang-tools-extra/trunk/clangd/XRefs.cpp clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp Modified: clang-tools-extra/trunk/clangd/AST.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.cpp?rev=339116&r1=339115&r2=339116&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/AST.cpp (original) +++ clang-tools-extra/trunk/clangd/AST.cpp Tue Aug 7 01:57:52 2018 @@ -12,6 +12,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" #include "clang/Basic/SourceManager.h" +#include "clang/Index/USRGeneration.h" namespace clang { namespace clangd { @@ -53,5 +54,12 @@ std::string printQualifiedName(const Nam return QName; } +llvm::Optional<SymbolID> getSymbolID(const Decl *D) { + llvm::SmallString<128> USR; + if (index::generateUSRForDecl(D, USR)) + return None; + return SymbolID(USR); +} + } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/clangd/AST.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/AST.h?rev=339116&r1=339115&r2=339116&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/AST.h (original) +++ clang-tools-extra/trunk/clangd/AST.h Tue Aug 7 01:57:52 2018 @@ -16,6 +16,7 @@ #include "clang/AST/Decl.h" #include "clang/Basic/SourceLocation.h" +#include "index/Index.h" namespace clang { class SourceManager; @@ -33,6 +34,9 @@ SourceLocation findNameLoc(const clang:: /// like inline namespaces. std::string printQualifiedName(const NamedDecl &ND); +/// Gets the symbol ID for a declaration, if possible. +llvm::Optional<SymbolID> getSymbolID(const Decl *D); + } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=339116&r1=339115&r2=339116&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original) +++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Tue Aug 7 01:57:52 2018 @@ -396,10 +396,7 @@ llvm::Optional<SymbolID> getSymbolID(con switch (R.Kind) { case CodeCompletionResult::RK_Declaration: case CodeCompletionResult::RK_Pattern: { - llvm::SmallString<128> USR; - if (/*Ignore=*/clang::index::generateUSRForDecl(R.Declaration, USR)) - return None; - return SymbolID(USR); + return clang::clangd::getSymbolID(R.Declaration); } case CodeCompletionResult::RK_Macro: // FIXME: Macros do have USRs, but the CCR doesn't contain enough info. Modified: clang-tools-extra/trunk/clangd/XRefs.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/XRefs.cpp?rev=339116&r1=339115&r2=339116&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/XRefs.cpp (original) +++ clang-tools-extra/trunk/clangd/XRefs.cpp Tue Aug 7 01:57:52 2018 @@ -202,15 +202,6 @@ makeLocation(ParsedAST &AST, const Sourc return L; } -// Get the symbol ID for a declaration, if possible. -llvm::Optional<SymbolID> getSymbolID(const Decl *D) { - llvm::SmallString<128> USR; - if (index::generateUSRForDecl(D, USR)) { - return None; - } - return SymbolID(USR); -} - } // namespace std::vector<Location> findDefinitions(ParsedAST &AST, Position Pos, Modified: clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp?rev=339116&r1=339115&r2=339116&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp (original) +++ clang-tools-extra/trunk/clangd/index/SymbolCollector.cpp Tue Aug 7 01:57:52 2018 @@ -320,21 +320,20 @@ bool SymbolCollector::handleDeclOccurenc if (!shouldCollectSymbol(*ND, *ASTCtx, Opts)) return true; - llvm::SmallString<128> USR; - if (index::generateUSRForDecl(ND, USR)) + auto ID = getSymbolID(ND); + if (!ID) return true; - SymbolID ID(USR); const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD); - const Symbol *BasicSymbol = Symbols.find(ID); + const Symbol *BasicSymbol = Symbols.find(*ID); if (!BasicSymbol) // Regardless of role, ND is the canonical declaration. - BasicSymbol = addDeclaration(*ND, std::move(ID)); + BasicSymbol = addDeclaration(*ND, std::move(*ID)); else if (isPreferredDeclaration(OriginalDecl, Roles)) // If OriginalDecl is preferred, replace the existing canonical // declaration (e.g. a class forward declaration). There should be at most // one duplicate as we expect to see only one preferred declaration per // TU, because in practice they are definitions. - BasicSymbol = addDeclaration(OriginalDecl, std::move(ID)); + BasicSymbol = addDeclaration(OriginalDecl, std::move(*ID)); if (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) addDefinition(OriginalDecl, *BasicSymbol); @@ -423,9 +422,9 @@ void SymbolCollector::finish() { } }; for (const NamedDecl *ND : ReferencedDecls) { - llvm::SmallString<128> USR; - if (!index::generateUSRForDecl(ND, USR)) - IncRef(SymbolID(USR)); + if (auto ID = getSymbolID(ND)) { + IncRef(*ID); + } } if (Opts.CollectMacro) { assert(PP); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits