Remove use of 'std::string' from Attr objects, using instead a byte
array allocated using the allocator in ASTContext. This addresses
these strings getting leaked when using a BumpPtrAllocator (in
ASTContext).
Fixes: <rdar://problem/7636765>
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95853 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/AttrImpl.cpp b/lib/AST/AttrImpl.cpp
index 42c099d..d835edf 100644
--- a/lib/AST/AttrImpl.cpp
+++ b/lib/AST/AttrImpl.cpp
@@ -11,11 +11,45 @@
//
//===----------------------------------------------------------------------===//
-
#include "clang/AST/Attr.h"
#include "clang/AST/ASTContext.h"
using namespace clang;
+void Attr::Destroy(ASTContext &C) {
+ if (Next) {
+ Next->Destroy(C);
+ Next = 0;
+ }
+ this->~Attr();
+ C.Deallocate((void*)this);
+}
+
+AttrWithString::AttrWithString(Attr::Kind AK, ASTContext &C, llvm::StringRef s)
+ : Attr(AK) {
+ assert(!s.empty());
+ StrLen = s.size();
+ Str = new (C) char[StrLen];
+ memcpy(const_cast<char*>(Str), s.data(), StrLen);
+}
+
+void AttrWithString::Destroy(ASTContext &C) {
+ C.Deallocate(const_cast<char*>(Str));
+ Attr::Destroy(C);
+}
+
+void AttrWithString::ReplaceString(ASTContext &C, llvm::StringRef newS) {
+ if (newS.size() > StrLen) {
+ C.Deallocate(const_cast<char*>(Str));
+ Str = new char[newS.size()];
+ }
+ StrLen = newS.size();
+ memcpy(const_cast<char*>(Str), newS.data(), StrLen);
+}
+
+void FormatAttr::setType(ASTContext &C, llvm::StringRef type) {
+ ReplaceString(C, type);
+}
+
#define DEF_SIMPLE_ATTR_CLONE(ATTR) \
Attr *ATTR##Attr::clone(ASTContext &C) const { \
return ::new (C) ATTR##Attr; \
@@ -66,15 +100,15 @@
}
Attr* AnnotateAttr::clone(ASTContext &C) const {
- return ::new (C) AnnotateAttr(Annotation);
+ return ::new (C) AnnotateAttr(C, getAnnotation());
}
Attr *AsmLabelAttr::clone(ASTContext &C) const {
- return ::new (C) AsmLabelAttr(Label);
+ return ::new (C) AsmLabelAttr(C, getLabel());
}
Attr *AliasAttr::clone(ASTContext &C) const {
- return ::new (C) AliasAttr(Aliasee);
+ return ::new (C) AliasAttr(C, getAliasee());
}
Attr *ConstructorAttr::clone(ASTContext &C) const {
@@ -94,7 +128,7 @@
}
Attr *SectionAttr::clone(ASTContext &C) const {
- return ::new (C) SectionAttr(Name);
+ return ::new (C) SectionAttr(C, getName());
}
Attr *NonNullAttr::clone(ASTContext &C) const {
@@ -102,7 +136,7 @@
}
Attr *FormatAttr::clone(ASTContext &C) const {
- return ::new (C) FormatAttr(Type, formatIdx, firstArg);
+ return ::new (C) FormatAttr(C, getType(), formatIdx, firstArg);
}
Attr *FormatArgAttr::clone(ASTContext &C) const {
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 4851636..5acb82f 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -29,15 +29,6 @@
using namespace clang;
-void Attr::Destroy(ASTContext &C) {
- if (Next) {
- Next->Destroy(C);
- Next = 0;
- }
- this->~Attr();
- C.Deallocate((void*)this);
-}
-
/// \brief Return the TypeLoc wrapper for the type source info.
TypeLoc TypeSourceInfo::getTypeLoc() const {
return TypeLoc(Ty, (void*)(this + 1));