Fix a leak in the generated code for attributes with strings.

Storing std::strings in attributes simply doesn't work, we never call
the destructor. Use an array of StringRefs instead of std::strings and
copy the data into memory taken from the ASTContext.

llvm-svn: 260831
This commit is contained in:
Benjamin Kramer 2016-02-13 18:11:49 +00:00
parent bcdb0f2ede
commit 1b5820133f
2 changed files with 20 additions and 7 deletions

View File

@ -4952,7 +4952,7 @@ static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
return;
std::vector<std::string> Sanitizers;
std::vector<StringRef> Sanitizers;
for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) {
StringRef SanitizerName;
@ -4976,8 +4976,8 @@ static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
const AttributeList &Attr) {
StringRef AttrName = Attr.getName()->getName();
normalizeName(AttrName);
std::string SanitizerName =
llvm::StringSwitch<std::string>(AttrName)
StringRef SanitizerName =
llvm::StringSwitch<StringRef>(AttrName)
.Case("no_address_safety_analysis", "address")
.Case("no_sanitize_address", "address")
.Case("no_sanitize_thread", "thread")

View File

@ -81,7 +81,7 @@ static std::string ReadPCHRecord(StringRef type) {
.Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
.Case("Expr *", "ReadExpr(F)")
.Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
.Case("std::string", "ReadString(Record, Idx)")
.Case("StringRef", "ReadString(Record, Idx)")
.Default("Record[Idx++]");
}
@ -95,7 +95,7 @@ static std::string WritePCHRecord(StringRef type, StringRef name) {
.Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
.Case("IdentifierInfo *",
"AddIdentifierRef(" + std::string(name) + ", Record);\n")
.Case("std::string", "AddString(" + std::string(name) + ", Record);\n")
.Case("StringRef", "AddString(" + std::string(name) + ", Record);\n")
.Default("Record.push_back(" + std::string(name) + ");\n");
}
@ -528,7 +528,9 @@ namespace {
: Argument(Arg, Attr), Type(T), ArgName(getLowerName().str() + "_"),
ArgSizeName(ArgName + "Size"), RangeName(getLowerName()) {}
std::string getType() const { return Type; }
const std::string &getType() const { return Type; }
const std::string &getArgName() const { return ArgName; }
const std::string &getArgSizeName() const { return ArgSizeName; }
bool isVariadic() const override { return true; }
void writeAccessors(raw_ostream &OS) const override {
@ -993,8 +995,19 @@ namespace {
class VariadicStringArgument : public VariadicArgument {
public:
VariadicStringArgument(const Record &Arg, StringRef Attr)
: VariadicArgument(Arg, Attr, "std::string")
: VariadicArgument(Arg, Attr, "StringRef")
{}
void writeCtorBody(raw_ostream &OS) const override {
OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
" ++I) {\n"
" StringRef Ref = " << getUpperName() << "[I];\n"
" if (!Ref.empty()) {\n"
" char *Mem = new (Ctx, 1) char[Ref.size()];\n"
" std::memcpy(Mem, Ref.data(), Ref.size());\n"
" " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
" }\n"
" }";
}
void writeValueImpl(raw_ostream &OS) const override {
OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
}