CIndexUSRs.cpp revision 8e67219a7cb0cf7bfa432f8c30da9c52999737ce
1//===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
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// This file implements the generation and use of USRs from CXEntities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIndexer.h"
15#include "CXCursor.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Lex/PreprocessingRecord.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
23using namespace clang::cxstring;
24
25//===----------------------------------------------------------------------===//
26// USR generation.
27//===----------------------------------------------------------------------===//
28
29namespace {
30class USRGenerator : public DeclVisitor<USRGenerator> {
31  llvm::raw_ostream &Out;
32  bool IgnoreResults;
33  ASTUnit *AU;
34  bool generatedLoc;
35public:
36  USRGenerator(ASTUnit *au, llvm::raw_ostream &out)
37    : Out(out), IgnoreResults(false), AU(au), generatedLoc(false) {}
38
39  bool ignoreResults() const { return IgnoreResults; }
40
41  // Visitation methods from generating USRs from AST elements.
42  void VisitDeclContext(DeclContext *D);
43  void VisitFieldDecl(FieldDecl *D);
44  void VisitFunctionDecl(FunctionDecl *D);
45  void VisitNamedDecl(NamedDecl *D);
46  void VisitNamespaceDecl(NamespaceDecl *D);
47  void VisitObjCClassDecl(ObjCClassDecl *CD);
48  void VisitObjCContainerDecl(ObjCContainerDecl *CD);
49  void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
50  void VisitObjCMethodDecl(ObjCMethodDecl *MD);
51  void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
52  void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
53  void VisitTagDecl(TagDecl *D);
54  void VisitTypedefDecl(TypedefDecl *D);
55  void VisitVarDecl(VarDecl *D);
56  void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
57    IgnoreResults = true;
58    return;
59  }
60
61  /// Generate the string component containing the location of the
62  ///  declaration.
63  bool GenLoc(const Decl *D);
64
65  /// String generation methods used both by the visitation methods
66  /// and from other clients that want to directly generate USRs.  These
67  /// methods do not construct complete USRs (which incorporate the parents
68  /// of an AST element), but only the fragments concerning the AST element
69  /// itself.
70
71  /// Generate a USR fragment for a named declaration.  This does
72  /// not include the USR component for the parent.
73  void GenNamedDecl(llvm::StringRef name);
74
75  /// Generate a USR for an Objective-C class.
76  void GenObjCClass(llvm::StringRef cls);
77  /// Generate a USR for an Objective-C class category.
78  void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
79  /// Generate a USR fragment for an Objective-C instance variable.  The
80  /// complete USR can be created by concatenating the USR for the
81  /// encompassing class with this USR fragment.
82  void GenObjCIvar(llvm::StringRef ivar);
83  /// Generate a USR fragment for an Objective-C method.
84  void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
85  /// Generate a USR fragment for an Objective-C property.
86  void GenObjCProperty(llvm::StringRef prop);
87  /// Generate a USR for an Objective-C protocol.
88  void GenObjCProtocol(llvm::StringRef prot);
89
90  void VisitType(QualType T);
91};
92
93class StringUSRGenerator {
94private:
95  llvm::SmallString<1024> StrBuf;
96  llvm::raw_svector_ostream Out;
97  USRGenerator UG;
98public:
99  StringUSRGenerator(const CXCursor *C = 0)
100    : Out(StrBuf), UG(C ? cxcursor::getCursorASTUnit(*C) : 0, Out) {
101    // Add the USR space prefix.
102    Out << "c:";
103  }
104
105  llvm::StringRef str() {
106    return Out.str();
107  }
108
109  USRGenerator* operator->() { return &UG; }
110
111  template <typename T>
112  llvm::raw_svector_ostream &operator<<(const T &x) {
113    Out << x;
114    return Out;
115  }
116};
117
118} // end anonymous namespace
119
120//===----------------------------------------------------------------------===//
121// Generating USRs from ASTS.
122//===----------------------------------------------------------------------===//
123
124static bool InAnonymousNamespace(const Decl *D) {
125  if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
126    return ND->isAnonymousNamespace();
127  return false;
128}
129
130static inline bool ShouldGenerateLocation(const NamedDecl *D) {
131  return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
132}
133
134void USRGenerator::VisitDeclContext(DeclContext *DC) {
135  if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
136    Visit(D);
137}
138
139void USRGenerator::VisitFieldDecl(FieldDecl *D) {
140  const std::string &s = D->getNameAsString();
141  if (s.empty()) {
142    // Bit fields can be anonymous.
143    IgnoreResults = true;
144    return;
145  }
146  VisitDeclContext(D->getDeclContext());
147  Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@") << s;
148}
149
150void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
151  if (ShouldGenerateLocation(D) && GenLoc(D))
152    return;
153
154  VisitDeclContext(D->getDeclContext());
155  Out << "@F@" << D->getNameAsString();
156
157  ASTContext &Ctx = AU->getASTContext();
158  if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
159    return;
160
161  // Mangle in type information for the arguments.
162  for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
163       I != E; ++I) {
164    Out << '#';
165    if (ParmVarDecl *PD = *I)
166      VisitType(PD->getType());
167  }
168  if (D->isVariadic())
169    Out << '.';
170}
171
172void USRGenerator::VisitNamedDecl(NamedDecl *D) {
173  VisitDeclContext(D->getDeclContext());
174  const std::string &s = D->getNameAsString();
175  // The string can be empty if the declaration has no name; e.g., it is
176  // the ParmDecl with no name for declaration of a function pointer type, e.g.:
177  //  	void  (*f)(void *);
178  // In this case, don't generate a USR.
179  if (s.empty())
180    IgnoreResults = true;
181  else
182    GenNamedDecl(s);
183}
184
185void USRGenerator::VisitVarDecl(VarDecl *D) {
186  // VarDecls can be declared 'extern' within a function or method body,
187  // but their enclosing DeclContext is the function, not the TU.  We need
188  // to check the storage class to correctly generate the USR.
189  if (ShouldGenerateLocation(D) && GenLoc(D))
190    return;
191
192  VisitDeclContext(D->getDeclContext());
193
194  // Variables always have simple names.
195  llvm::StringRef s = D->getName();
196
197  // The string can be empty if the declaration has no name; e.g., it is
198  // the ParmDecl with no name for declaration of a function pointer type, e.g.:
199  //  	void  (*f)(void *);
200  // In this case, don't generate a USR.
201  if (s.empty())
202    IgnoreResults = true;
203  else
204    GenNamedDecl(s);
205}
206
207void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
208  if (D->isAnonymousNamespace()) {
209    Out << "@aN";
210    return;
211  }
212
213  VisitDeclContext(D->getDeclContext());
214  if (!IgnoreResults)
215    Out << "@N@" << D->getName();
216}
217
218void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
219  Visit(cast<Decl>(D->getDeclContext()));
220  GenObjCMethod(DeclarationName(D->getSelector()).getAsString(),
221                D->isInstanceMethod());
222}
223
224void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
225  // FIXME: @class declarations can refer to multiple classes.  We need
226  //  to be able to traverse these.
227  IgnoreResults = true;
228}
229
230void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
231  // FIXME: @protocol declarations can refer to multiple protocols.  We need
232  //  to be able to traverse these.
233  IgnoreResults = true;
234}
235
236void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
237  switch (D->getKind()) {
238    default:
239      assert(false && "Invalid ObjC container.");
240    case Decl::ObjCInterface:
241    case Decl::ObjCImplementation:
242      GenObjCClass(D->getName());
243      break;
244    case Decl::ObjCCategory: {
245      ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
246      ObjCInterfaceDecl *ID = CD->getClassInterface();
247      if (!ID) {
248        // Handle invalid code where the @interface might not
249        // have been specified.
250        // FIXME: We should be able to generate this USR even if the
251        // @interface isn't available.
252        IgnoreResults = true;
253        return;
254      }
255      GenObjCCategory(ID->getName(), CD->getName());
256      break;
257    }
258    case Decl::ObjCCategoryImpl: {
259      ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
260      ObjCInterfaceDecl *ID = CD->getClassInterface();
261      if (!ID) {
262        // Handle invalid code where the @interface might not
263        // have been specified.
264        // FIXME: We should be able to generate this USR even if the
265        // @interface isn't available.
266        IgnoreResults = true;
267        return;
268      }
269      GenObjCCategory(ID->getName(), CD->getName());
270      break;
271    }
272    case Decl::ObjCProtocol:
273      GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
274      break;
275  }
276}
277
278void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
279  Visit(cast<Decl>(D->getDeclContext()));
280  GenObjCProperty(D->getName());
281}
282
283void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
284  if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
285    VisitObjCPropertyDecl(PD);
286    return;
287  }
288
289  IgnoreResults = true;
290}
291
292void USRGenerator::VisitTagDecl(TagDecl *D) {
293  // Add the location of the tag decl to handle resolution across
294  // translation units.
295  if (ShouldGenerateLocation(D) && GenLoc(D))
296    return;
297
298  D = D->getCanonicalDecl();
299  VisitDeclContext(D->getDeclContext());
300
301  switch (D->getTagKind()) {
302    case TagDecl::TK_struct: Out << "@S"; break;
303    case TagDecl::TK_class:  Out << "@C"; break;
304    case TagDecl::TK_union:  Out << "@U"; break;
305    case TagDecl::TK_enum:   Out << "@E"; break;
306  }
307
308  const std::string &s = D->getNameAsString();
309  const TypedefDecl *TD = 0;
310  if (s.empty()) {
311    TD = D->getTypedefForAnonDecl();
312    Out << (TD ? 'A' : 'a');
313  }
314
315  if (s.empty()) {
316    if (TD)
317      Out << '@' << TD;
318  }
319  else
320    Out << '@' << s;
321}
322
323void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
324  if (ShouldGenerateLocation(D) && GenLoc(D))
325    return;
326  DeclContext *DC = D->getDeclContext();
327  if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
328    Visit(DCN);
329  Out << "@T@";
330  Out << D->getName();
331}
332
333bool USRGenerator::GenLoc(const Decl *D) {
334  if (generatedLoc)
335    return IgnoreResults;
336  generatedLoc = true;
337
338  const SourceManager &SM = AU->getSourceManager();
339  SourceLocation L = D->getLocStart();
340  if (L.isInvalid()) {
341    IgnoreResults = true;
342    return true;
343  }
344  L = SM.getInstantiationLoc(L);
345  const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
346  const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
347  if (FE) {
348    llvm::sys::Path P(FE->getName());
349    Out << P.getLast();
350  }
351  else {
352    // This case really isn't interesting.
353    IgnoreResults = true;
354    return true;
355  }
356  Out << '@'
357      << SM.getLineNumber(Decomposed.first, Decomposed.second) << ':'
358      << SM.getColumnNumber(Decomposed.first, Decomposed.second);
359
360  return IgnoreResults;
361}
362
363void USRGenerator::VisitType(QualType T) {
364  // This method mangles in USR information for types.  It can possibly
365  // just reuse the naming-mangling logic used by codegen, although the
366  // requirements for USRs might not be the same.
367  do {
368    T = T.getTypePtr()->getCanonicalTypeInternal();
369    Qualifiers Q = T.getQualifiers();
370    if (Q.hasConst())
371      Out << '1';
372    if (Q.hasVolatile())
373      Out << '2';
374    if (Q.hasRestrict())
375      Out << '3';
376
377    // Mangle in ObjC GC qualifiers?
378
379    if (const PointerType *PT = T->getAs<PointerType>()) {
380      Out << '*';
381      T = PT->getPointeeType();
382      continue;
383    }
384    if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
385      Out << '&';
386      T = RT->getPointeeType();
387      continue;
388    }
389    if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
390      Out << 'F';
391      VisitType(FT->getResultType());
392      for (FunctionProtoType::arg_type_iterator
393            I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
394        VisitType(*I);
395      }
396      if (FT->isVariadic())
397        Out << '.';
398      return;
399    }
400    if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
401      Out << 'B';
402      T = BT->getPointeeType();
403      continue;
404    }
405    if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
406      unsigned char c = '\0';
407      switch (BT->getKind()) {
408        case BuiltinType::Void:
409          c = 'v'; break;
410        case BuiltinType::Bool:
411          c = 'b'; break;
412        case BuiltinType::Char_U:
413        case BuiltinType::UChar:
414          c = 'c'; break;
415        case BuiltinType::Char16:
416          c = 'q'; break;
417        case BuiltinType::Char32:
418          c = 'w'; break;
419        case BuiltinType::UShort:
420          c = 's'; break;
421        case BuiltinType::UInt:
422          c = 'i'; break;
423        case BuiltinType::ULong:
424          c = 'l'; break;
425        case BuiltinType::ULongLong:
426          c = 'k'; break;
427        case BuiltinType::UInt128:
428          c = 'j'; break;
429        case BuiltinType::Char_S:
430        case BuiltinType::SChar:
431          c = 'C'; break;
432        case BuiltinType::WChar:
433          c = 'W'; break;
434        case BuiltinType::Short:
435          c = 'S'; break;
436        case BuiltinType::Int:
437          c = 'I'; break;
438        case BuiltinType::Long:
439          c = 'L'; break;
440        case BuiltinType::LongLong:
441          c = 'K'; break;
442        case BuiltinType::Int128:
443          c = 'J'; break;
444        case BuiltinType::Float:
445          c = 'f'; break;
446        case BuiltinType::Double:
447          c = 'd'; break;
448        case BuiltinType::LongDouble:
449          c = 'D'; break;
450        case BuiltinType::NullPtr:
451          c = 'n'; break;
452        case BuiltinType::Overload:
453        case BuiltinType::Dependent:
454        case BuiltinType::UndeducedAuto:
455          IgnoreResults = true;
456          return;
457        case BuiltinType::ObjCId:
458          c = 'o'; break;
459        case BuiltinType::ObjCClass:
460          c = 'O'; break;
461        case BuiltinType::ObjCSel:
462          c = 'e'; break;
463      }
464      Out << c;
465      return;
466    }
467    if (const ComplexType *CT = T->getAs<ComplexType>()) {
468      Out << '<';
469      T = CT->getElementType();
470      continue;
471    }
472
473    // Unhandled type.
474    Out << ' ';
475    break;
476  } while (true);
477}
478
479//===----------------------------------------------------------------------===//
480// General purpose USR generation methods.
481//===----------------------------------------------------------------------===//
482
483void USRGenerator::GenNamedDecl(llvm::StringRef name) {
484  Out << "@" << name;
485}
486
487void USRGenerator::GenObjCClass(llvm::StringRef cls) {
488  Out << "objc(cs)" << cls;
489}
490
491void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
492  Out << "objc(cy)" << cls << '@' << cat;
493}
494
495void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
496  GenNamedDecl(ivar);
497}
498
499void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
500  Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
501}
502
503void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
504  Out << "(py)" << prop;
505}
506
507void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
508  Out << "objc(pl)" << prot;
509}
510
511//===----------------------------------------------------------------------===//
512// API hooks.
513//===----------------------------------------------------------------------===//
514
515static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
516  return s.startswith("c:") ? s.substr(2) : "";
517}
518
519static CXString getDeclCursorUSR(const CXCursor &C) {
520  Decl *D = cxcursor::getCursorDecl(C);
521
522  // Don't generate USRs for things with invalid locations.
523  if (!D || D->getLocStart().isInvalid())
524    return createCXString("");
525
526  // Check if the cursor has 'NoLinkage'.
527  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
528    switch (ND->getLinkage()) {
529      case ExternalLinkage:
530        // Generate USRs for all entities with external linkage.
531        break;
532      case NoLinkage:
533      case UniqueExternalLinkage:
534        // We allow enums, typedefs, and structs that have no linkage to
535        // have USRs that are anchored to the file they were defined in
536        // (e.g., the header).  This is a little gross, but in principal
537        // enums/anonymous structs/etc. defined in a common header file
538        // are referred to across multiple translation units.
539        if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
540            isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
541            isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
542          break;
543        // Fall-through.
544      case InternalLinkage:
545        if (isa<FunctionDecl>(ND))
546          break;
547    }
548
549  StringUSRGenerator SUG(&C);
550  SUG->Visit(D);
551
552  if (SUG->ignoreResults())
553    return createCXString("");
554
555  // For development testing.
556  // assert(SUG.str().size() > 2);
557
558    // Return a copy of the string that must be disposed by the caller.
559  return createCXString(SUG.str(), true);
560}
561
562extern "C" {
563
564CXString clang_getCursorUSR(CXCursor C) {
565  const CXCursorKind &K = clang_getCursorKind(C);
566
567  if (clang_isDeclaration(K))
568      return getDeclCursorUSR(C);
569
570  if (K == CXCursor_MacroDefinition) {
571    StringUSRGenerator SUG(&C);
572    SUG << "macro@"
573        << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
574    return createCXString(SUG.str(), true);
575  }
576
577  return createCXString("");
578}
579
580CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
581  StringUSRGenerator SUG;
582  SUG << extractUSRSuffix(clang_getCString(classUSR));
583  SUG->GenObjCIvar(name);
584  return createCXString(SUG.str(), true);
585}
586
587CXString clang_constructUSR_ObjCMethod(const char *name,
588                                       unsigned isInstanceMethod,
589                                       CXString classUSR) {
590  StringUSRGenerator SUG;
591  SUG << extractUSRSuffix(clang_getCString(classUSR));
592  SUG->GenObjCMethod(name, isInstanceMethod);
593  return createCXString(SUG.str(), true);
594}
595
596CXString clang_constructUSR_ObjCClass(const char *name) {
597  StringUSRGenerator SUG;
598  SUG->GenObjCClass(name);
599  return createCXString(SUG.str(), true);
600}
601
602CXString clang_constructUSR_ObjCProtocol(const char *name) {
603  StringUSRGenerator SUG;
604  SUG->GenObjCProtocol(name);
605  return createCXString(SUG.str(), true);
606}
607
608CXString clang_constructUSR_ObjCCategory(const char *class_name,
609                                         const char *category_name) {
610  StringUSRGenerator SUG;
611  SUG->GenObjCCategory(class_name, category_name);
612  return createCXString(SUG.str(), true);
613}
614
615CXString clang_constructUSR_ObjCProperty(const char *property,
616                                         CXString classUSR) {
617  StringUSRGenerator SUG;
618  SUG << extractUSRSuffix(clang_getCString(classUSR));
619  SUG->GenObjCProperty(property);
620  return createCXString(SUG.str(), true);
621}
622
623} // end extern "C"
624