Coverage Report

Created: 2020-02-15 09:57

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