blob: d7b65852844eddad5fba76663237e83268fa2582 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===- 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 "CXString.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070017#include "CXTranslationUnit.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070018#include "clang/Frontend/ASTUnit.h"
Argyrios Kyrtzidis2845a672013-08-17 00:40:41 +000019#include "clang/Index/USRGeneration.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000020#include "clang/Lex/PreprocessingRecord.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +000025using namespace clang::index;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026
27//===----------------------------------------------------------------------===//
Guy Benyei7f92f2d2012-12-18 14:30:41 +000028// API hooks.
29//===----------------------------------------------------------------------===//
30
31static inline StringRef extractUSRSuffix(StringRef s) {
32 return s.startswith("c:") ? s.substr(2) : "";
33}
34
35bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +000036 return generateUSRForDecl(D, Buf);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000037}
38
39extern "C" {
40
41CXString clang_getCursorUSR(CXCursor C) {
42 const CXCursorKind &K = clang_getCursorKind(C);
43
44 if (clang_isDeclaration(K)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +000045 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000046 if (!D)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000047 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000048
49 CXTranslationUnit TU = cxcursor::getCursorTU(C);
50 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000051 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000052
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +000053 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000054 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000055 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000056
57 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
58 if (Ignore) {
Dmitri Gribenko9c48d162013-01-26 22:44:19 +000059 buf->dispose();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000060 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000061 }
62
63 // Return the C-string, but don't make a copy since it is already in
64 // the string buffer.
65 buf->Data.push_back('\0');
66 return createCXString(buf);
67 }
68
69 if (K == CXCursor_MacroDefinition) {
70 CXTranslationUnit TU = cxcursor::getCursorTU(C);
71 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000072 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000073
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +000074 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +000075 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000076 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000077
Stephen Hines651f13c2014-04-23 16:59:28 -070078 bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C),
79 cxtu::getASTUnit(TU)->getSourceManager(),
80 buf->Data);
81 if (Ignore) {
82 buf->dispose();
83 return cxstring::createEmpty();
84 }
85
86 // Return the C-string, but don't make a copy since it is already in
87 // the string buffer.
Guy Benyei7f92f2d2012-12-18 14:30:41 +000088 buf->Data.push_back('\0');
89 return createCXString(buf);
90 }
91
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +000092 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +000093}
94
95CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +000096 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +000097 llvm::raw_svector_ostream OS(Buf);
98 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +000099 generateUSRForObjCIvar(name, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000100 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000101}
102
103CXString clang_constructUSR_ObjCMethod(const char *name,
104 unsigned isInstanceMethod,
105 CXString classUSR) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +0000106 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000107 llvm::raw_svector_ostream OS(Buf);
108 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +0000109 generateUSRForObjCMethod(name, isInstanceMethod, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000110 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000111}
112
113CXString clang_constructUSR_ObjCClass(const char *name) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +0000114 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000115 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +0000116 generateUSRForObjCClass(name, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000117 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000118}
119
120CXString clang_constructUSR_ObjCProtocol(const char *name) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +0000121 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000122 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +0000123 generateUSRForObjCProtocol(name, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000124 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000125}
126
127CXString clang_constructUSR_ObjCCategory(const char *class_name,
128 const char *category_name) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +0000129 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000130 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +0000131 generateUSRForObjCCategory(class_name, category_name, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000132 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000133}
134
135CXString clang_constructUSR_ObjCProperty(const char *property,
136 CXString classUSR) {
Argyrios Kyrtzidis3f4ec312013-08-21 01:51:19 +0000137 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000138 llvm::raw_svector_ostream OS(Buf);
139 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidise8458342013-08-21 00:49:25 +0000140 generateUSRForObjCProperty(property, OS);
Argyrios Kyrtzidis12161962013-08-16 18:17:55 +0000141 return cxstring::createDup(OS.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000142}
143
144} // end extern "C"