xref: /minix3/external/bsd/llvm/dist/clang/tools/libclang/CIndexUSRs.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the generation and use of USRs from CXEntities.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "CIndexer.h"
15f4a2713aSLionel Sambuc #include "CXCursor.h"
16f4a2713aSLionel Sambuc #include "CXString.h"
17*0a6a1f1dSLionel Sambuc #include "CXTranslationUnit.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Frontend/ASTUnit.h"
19f4a2713aSLionel Sambuc #include "clang/Index/USRGeneration.h"
20f4a2713aSLionel Sambuc #include "clang/Lex/PreprocessingRecord.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace clang::index;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
28f4a2713aSLionel Sambuc // API hooks.
29f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
30f4a2713aSLionel Sambuc 
extractUSRSuffix(StringRef s)31f4a2713aSLionel Sambuc static inline StringRef extractUSRSuffix(StringRef s) {
32f4a2713aSLionel Sambuc   return s.startswith("c:") ? s.substr(2) : "";
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
getDeclCursorUSR(const Decl * D,SmallVectorImpl<char> & Buf)35f4a2713aSLionel Sambuc bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
36f4a2713aSLionel Sambuc   return generateUSRForDecl(D, Buf);
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc extern "C" {
40f4a2713aSLionel Sambuc 
clang_getCursorUSR(CXCursor C)41f4a2713aSLionel Sambuc CXString clang_getCursorUSR(CXCursor C) {
42f4a2713aSLionel Sambuc   const CXCursorKind &K = clang_getCursorKind(C);
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   if (clang_isDeclaration(K)) {
45f4a2713aSLionel Sambuc     const Decl *D = cxcursor::getCursorDecl(C);
46f4a2713aSLionel Sambuc     if (!D)
47f4a2713aSLionel Sambuc       return cxstring::createEmpty();
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc     CXTranslationUnit TU = cxcursor::getCursorTU(C);
50f4a2713aSLionel Sambuc     if (!TU)
51f4a2713aSLionel Sambuc       return cxstring::createEmpty();
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc     cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
54f4a2713aSLionel Sambuc     if (!buf)
55f4a2713aSLionel Sambuc       return cxstring::createEmpty();
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc     bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
58f4a2713aSLionel Sambuc     if (Ignore) {
59f4a2713aSLionel Sambuc       buf->dispose();
60f4a2713aSLionel Sambuc       return cxstring::createEmpty();
61f4a2713aSLionel Sambuc     }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc     // Return the C-string, but don't make a copy since it is already in
64f4a2713aSLionel Sambuc     // the string buffer.
65f4a2713aSLionel Sambuc     buf->Data.push_back('\0');
66f4a2713aSLionel Sambuc     return createCXString(buf);
67f4a2713aSLionel Sambuc   }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc   if (K == CXCursor_MacroDefinition) {
70f4a2713aSLionel Sambuc     CXTranslationUnit TU = cxcursor::getCursorTU(C);
71f4a2713aSLionel Sambuc     if (!TU)
72f4a2713aSLionel Sambuc       return cxstring::createEmpty();
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc     cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
75f4a2713aSLionel Sambuc     if (!buf)
76f4a2713aSLionel Sambuc       return cxstring::createEmpty();
77f4a2713aSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc     bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C),
79*0a6a1f1dSLionel Sambuc                                       cxtu::getASTUnit(TU)->getSourceManager(),
80*0a6a1f1dSLionel Sambuc                                       buf->Data);
81*0a6a1f1dSLionel Sambuc     if (Ignore) {
82*0a6a1f1dSLionel Sambuc       buf->dispose();
83*0a6a1f1dSLionel Sambuc       return cxstring::createEmpty();
84*0a6a1f1dSLionel Sambuc     }
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc     // Return the C-string, but don't make a copy since it is already in
87*0a6a1f1dSLionel Sambuc     // the string buffer.
88f4a2713aSLionel Sambuc     buf->Data.push_back('\0');
89f4a2713aSLionel Sambuc     return createCXString(buf);
90f4a2713aSLionel Sambuc   }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc   return cxstring::createEmpty();
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCIvar(const char * name,CXString classUSR)95f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
96f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
97f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
98f4a2713aSLionel Sambuc   OS << extractUSRSuffix(clang_getCString(classUSR));
99f4a2713aSLionel Sambuc   generateUSRForObjCIvar(name, OS);
100f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCMethod(const char * name,unsigned isInstanceMethod,CXString classUSR)103f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCMethod(const char *name,
104f4a2713aSLionel Sambuc                                        unsigned isInstanceMethod,
105f4a2713aSLionel Sambuc                                        CXString classUSR) {
106f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
107f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
108f4a2713aSLionel Sambuc   OS << extractUSRSuffix(clang_getCString(classUSR));
109f4a2713aSLionel Sambuc   generateUSRForObjCMethod(name, isInstanceMethod, OS);
110f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCClass(const char * name)113f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCClass(const char *name) {
114f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
115f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
116f4a2713aSLionel Sambuc   generateUSRForObjCClass(name, OS);
117f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCProtocol(const char * name)120f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCProtocol(const char *name) {
121f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
122f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
123f4a2713aSLionel Sambuc   generateUSRForObjCProtocol(name, OS);
124f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCCategory(const char * class_name,const char * category_name)127f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCCategory(const char *class_name,
128f4a2713aSLionel Sambuc                                          const char *category_name) {
129f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
130f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
131f4a2713aSLionel Sambuc   generateUSRForObjCCategory(class_name, category_name, OS);
132f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc 
clang_constructUSR_ObjCProperty(const char * property,CXString classUSR)135f4a2713aSLionel Sambuc CXString clang_constructUSR_ObjCProperty(const char *property,
136f4a2713aSLionel Sambuc                                          CXString classUSR) {
137f4a2713aSLionel Sambuc   SmallString<128> Buf(getUSRSpacePrefix());
138f4a2713aSLionel Sambuc   llvm::raw_svector_ostream OS(Buf);
139f4a2713aSLionel Sambuc   OS << extractUSRSuffix(clang_getCString(classUSR));
140f4a2713aSLionel Sambuc   generateUSRForObjCProperty(property, OS);
141f4a2713aSLionel Sambuc   return cxstring::createDup(OS.str());
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc } // end extern "C"
145