summaryrefslogtreecommitdiffstats
path: root/clang/tools/libclang/CIndexUSRs.cpp
diff options
context:
space:
mode:
authorTed Kremenek <kremenek@apple.com>2010-11-16 08:15:38 +0000
committerTed Kremenek <kremenek@apple.com>2010-11-16 08:15:38 +0000
commit4ff4654893dc48a5b906fb1c0fd36208a0052f60 (patch)
treed50189e2c6f5d25d8126dba90844302d4a4f7d7b /clang/tools/libclang/CIndexUSRs.cpp
parent915542850b7bd6d8b9e3ff6535f38ce8f73bb8dd (diff)
downloadbcm5719-llvm-4ff4654893dc48a5b906fb1c0fd36208a0052f60.tar.gz
bcm5719-llvm-4ff4654893dc48a5b906fb1c0fd36208a0052f60.zip
Rework USR generation from CXCursors to use CXStringBufs
for the backing of generated USRs. This optmizes for the case when a client generates a sequence of USRs in sequence, disposing of them soon after generating them. By using a string buffer, we recycle malloc'ed memory instead of constantly malloc'ing and copying strings. llvm-svn: 119338
Diffstat (limited to 'clang/tools/libclang/CIndexUSRs.cpp')
-rw-r--r--clang/tools/libclang/CIndexUSRs.cpp62
1 files changed, 42 insertions, 20 deletions
diff --git a/clang/tools/libclang/CIndexUSRs.cpp b/clang/tools/libclang/CIndexUSRs.cpp
index adc77d16b62..8a1d4233c1a 100644
--- a/clang/tools/libclang/CIndexUSRs.cpp
+++ b/clang/tools/libclang/CIndexUSRs.cpp
@@ -30,7 +30,8 @@ using namespace clang::cxstring;
namespace {
class USRGenerator : public DeclVisitor<USRGenerator> {
- llvm::SmallString<1024> Buf;
+ llvm::OwningPtr<llvm::SmallString<128> > OwnedBuf;
+ llvm::SmallVectorImpl<char> &Buf;
llvm::raw_svector_ostream Out;
bool IgnoreResults;
ASTUnit *AU;
@@ -39,11 +40,13 @@ class USRGenerator : public DeclVisitor<USRGenerator> {
llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
public:
- USRGenerator(const CXCursor *C = 0)
- : Out(Buf),
- IgnoreResults(false),
- AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
- generatedLoc(false)
+ USRGenerator(const CXCursor *C = 0, llvm::SmallVectorImpl<char> *extBuf = 0)
+ : OwnedBuf(extBuf ? 0 : new llvm::SmallString<128>()),
+ Buf(extBuf ? *extBuf : *OwnedBuf.get()),
+ Out(Buf),
+ IgnoreResults(false),
+ AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
+ generatedLoc(false)
{
// Add the USR space prefix.
Out << "c:";
@@ -794,19 +797,27 @@ static CXString getDeclCursorUSR(const CXCursor &C) {
break;
}
- USRGenerator UG(&C);
- UG->Visit(D);
-
- if (UG->ignoreResults())
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ if (!TU)
return createCXString("");
-#if 0
- // For development testing.
- assert(UG.str().size() > 2);
-#endif
+ CXStringBuf *buf = cxstring::getCXStringBuf(TU);
+ if (!buf)
+ return createCXString("");
+
+ {
+ USRGenerator UG(&C, &buf->Data);
+ UG->Visit(D);
- // Return a copy of the string that must be disposed by the caller.
- return createCXString(UG.str(), true);
+ if (UG->ignoreResults()) {
+ disposeCXStringBuf(buf);
+ return createCXString("");
+ }
+ }
+ // Return the C-string, but don't make a copy since it is already in
+ // the string buffer.
+ buf->Data.push_back('\0');
+ return createCXString(buf);
}
extern "C" {
@@ -818,10 +829,21 @@ CXString clang_getCursorUSR(CXCursor C) {
return getDeclCursorUSR(C);
if (K == CXCursor_MacroDefinition) {
- USRGenerator UG(&C);
- UG << "macro@"
- << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
- return createCXString(UG.str(), true);
+ CXTranslationUnit TU = cxcursor::getCursorTU(C);
+ if (!TU)
+ return createCXString("");
+
+ CXStringBuf *buf = cxstring::getCXStringBuf(TU);
+ if (!buf)
+ return createCXString("");
+
+ {
+ USRGenerator UG(&C, &buf->Data);
+ UG << "macro@"
+ << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
+ }
+ buf->Data.push_back('\0');
+ return createCXString(buf);
}
return createCXString("");
OpenPOWER on IntegriCloud