blob: 7cd7b6f1cd9e0689ef233c643fd1ac28c3c70338 [file] [log] [blame]
Guy Benyei11169dd2012-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"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000017#include "CXTranslationUnit.h"
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000018#include "clang/Frontend/ASTUnit.h"
Argyrios Kyrtzidis15a2fcc2013-08-17 00:40:41 +000019#include "clang/Index/USRGeneration.h"
Guy Benyei11169dd2012-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 Kyrtzidis5234b492013-08-21 00:49:25 +000025using namespace clang::index;
Guy Benyei11169dd2012-12-18 14:30:41 +000026
27//===----------------------------------------------------------------------===//
Guy Benyei11169dd2012-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 Kyrtzidis5234b492013-08-21 00:49:25 +000036 return generateUSRForDecl(D, Buf);
Guy Benyei11169dd2012-12-18 14:30:41 +000037}
38
Guy Benyei11169dd2012-12-18 14:30:41 +000039CXString clang_getCursorUSR(CXCursor C) {
40 const CXCursorKind &K = clang_getCursorKind(C);
41
42 if (clang_isDeclaration(K)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +000043 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei11169dd2012-12-18 14:30:41 +000044 if (!D)
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000045 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000046
47 CXTranslationUnit TU = cxcursor::getCursorTU(C);
48 if (!TU)
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000049 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000050
Dmitri Gribenko74895212013-02-03 13:52:47 +000051 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei11169dd2012-12-18 14:30:41 +000052 if (!buf)
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000053 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000054
55 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
56 if (Ignore) {
Dmitri Gribenkob95b3f12013-01-26 22:44:19 +000057 buf->dispose();
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000058 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000059 }
60
61 // Return the C-string, but don't make a copy since it is already in
62 // the string buffer.
63 buf->Data.push_back('\0');
64 return createCXString(buf);
65 }
66
67 if (K == CXCursor_MacroDefinition) {
68 CXTranslationUnit TU = cxcursor::getCursorTU(C);
69 if (!TU)
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000070 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000071
Dmitri Gribenko74895212013-02-03 13:52:47 +000072 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei11169dd2012-12-18 14:30:41 +000073 if (!buf)
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000074 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000075
Dmitri Gribenko237769e2014-03-28 22:21:26 +000076 bool Ignore = generateUSRForMacro(cxcursor::getCursorMacroDefinition(C),
77 cxtu::getASTUnit(TU)->getSourceManager(),
78 buf->Data);
79 if (Ignore) {
80 buf->dispose();
81 return cxstring::createEmpty();
82 }
83
84 // Return the C-string, but don't make a copy since it is already in
85 // the string buffer.
Guy Benyei11169dd2012-12-18 14:30:41 +000086 buf->Data.push_back('\0');
87 return createCXString(buf);
88 }
89
Dmitri Gribenko36a6dd02013-02-01 14:21:22 +000090 return cxstring::createEmpty();
Guy Benyei11169dd2012-12-18 14:30:41 +000091}
92
93CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +000094 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000095 llvm::raw_svector_ostream OS(Buf);
96 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000097 generateUSRForObjCIvar(name, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000098 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +000099}
100
101CXString clang_constructUSR_ObjCMethod(const char *name,
102 unsigned isInstanceMethod,
103 CXString classUSR) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +0000104 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000105 llvm::raw_svector_ostream OS(Buf);
106 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000107 generateUSRForObjCMethod(name, isInstanceMethod, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000108 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000109}
110
111CXString clang_constructUSR_ObjCClass(const char *name) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +0000112 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000113 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000114 generateUSRForObjCClass(name, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000115 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000116}
117
118CXString clang_constructUSR_ObjCProtocol(const char *name) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +0000119 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000120 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000121 generateUSRForObjCProtocol(name, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000122 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000123}
124
125CXString clang_constructUSR_ObjCCategory(const char *class_name,
126 const char *category_name) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +0000127 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000128 llvm::raw_svector_ostream OS(Buf);
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000129 generateUSRForObjCCategory(class_name, category_name, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000130 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000131}
132
133CXString clang_constructUSR_ObjCProperty(const char *property,
134 CXString classUSR) {
Argyrios Kyrtzidis7aa49d82013-08-21 01:51:19 +0000135 SmallString<128> Buf(getUSRSpacePrefix());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000136 llvm::raw_svector_ostream OS(Buf);
137 OS << extractUSRSuffix(clang_getCString(classUSR));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000138 generateUSRForObjCProperty(property, /*isClassProp=*/false, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000139 return cxstring::createDup(OS.str());
Guy Benyei11169dd2012-12-18 14:30:41 +0000140}