[Index] Expose USR generation for types
authorIlya Biryukov <ibiryukov@google.com>
Mon, 26 Nov 2018 15:24:48 +0000 (15:24 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Mon, 26 Nov 2018 15:24:48 +0000 (15:24 +0000)
Summary: Used in clangd.

Reviewers: sammccall, ioeric

Reviewed By: sammccall

Subscribers: kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D52275

llvm-svn: 347558

clang/include/clang/Index/USRGeneration.h
clang/lib/Index/USRGeneration.cpp

index 0bb7123..f1389ec 100644 (file)
 #include "llvm/ADT/StringRef.h"
 
 namespace clang {
+class ASTContext;
 class Decl;
 class MacroDefinitionRecord;
 class Module;
 class SourceLocation;
 class SourceManager;
+class QualType;
 
 namespace index {
 
@@ -71,6 +73,11 @@ bool generateUSRForMacro(const MacroDefinitionRecord *MD,
 bool generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
                          const SourceManager &SM, SmallVectorImpl<char> &Buf);
 
+/// Generates a USR for a type.
+///
+/// \return true on error, false on success.
+bool generateUSRForType(QualType T, ASTContext &Ctx, SmallVectorImpl<char> &Buf);
+
 /// Generate a USR for a module, including the USR prefix.
 /// \returns true on error, false on success.
 bool generateFullUSRForModule(const Module *Mod, raw_ostream &OS);
@@ -87,6 +94,7 @@ bool generateUSRFragmentForModule(const Module *Mod, raw_ostream &OS);
 /// \returns true on error, false on success.
 bool generateUSRFragmentForModuleName(StringRef ModName, raw_ostream &OS);
 
+
 } // namespace index
 } // namespace clang
 
index b75aa25..2c3c186 100644 (file)
@@ -1105,6 +1105,17 @@ bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
   return false;
 }
 
+bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx,
+                                      SmallVectorImpl<char> &Buf) {
+  if (T.isNull())
+    return true;
+  T = T.getCanonicalType();
+
+  USRGenerator UG(&Ctx, Buf);
+  UG.VisitType(T);
+  return UG.ignoreResults();
+}
+
 bool clang::index::generateFullUSRForModule(const Module *Mod,
                                             raw_ostream &OS) {
   if (!Mod->Parent)