From 9987cb60bac069f6d1af3fced71f8b6284ef87a1 Mon Sep 17 00:00:00 2001 From: Artur Pilipenko Date: Mon, 3 Aug 2015 14:31:49 +0000 Subject: [PATCH] Currently string attributes on function arguments/return values can be generated using LLVM API. However they are not supported in parser. So, the following scenario will fail: * generate function with string attribute using API, * dump it in LL format, * try to parse. Add parser support for string attributes to fix the issue. Reviewed By: reames, hfinkel Differential Revision: http://reviews.llvm.org/D11058 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243877 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLParser.cpp | 31 ++++++++++++++++++++++++------- lib/AsmParser/LLParser.h | 2 ++ test/Bitcode/attributes.ll | 10 ++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 3c561673bbd..e8b8e815e64 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -915,14 +915,8 @@ bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, } // Target-dependent attributes: case lltok::StringConstant: { - std::string Attr = Lex.getStrVal(); - Lex.Lex(); - std::string Val; - if (EatIfPresent(lltok::equal) && - ParseStringConstant(Val)) + if (ParseStringAttribute(B)) return true; - - B.addAttribute(Attr, Val); continue; } @@ -1229,6 +1223,19 @@ bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) { ParseToken(lltok::rparen, "expected ')' in address space"); } +/// ParseStringAttribute +/// := StringConstant +/// := StringConstant '=' StringConstant +bool LLParser::ParseStringAttribute(AttrBuilder &B) { + std::string Attr = Lex.getStrVal(); + Lex.Lex(); + std::string Val; + if (EatIfPresent(lltok::equal) && ParseStringConstant(Val)) + return true; + B.addAttribute(Attr, Val); + return false; +} + /// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes. bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) { bool HaveError = false; @@ -1240,6 +1247,11 @@ bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) { switch (Token) { default: // End of attributes. return HaveError; + case lltok::StringConstant: { + if (ParseStringAttribute(B)) + return true; + continue; + } case lltok::kw_align: { unsigned Alignment; if (ParseOptionalAlignment(Alignment)) @@ -1321,6 +1333,11 @@ bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) { switch (Token) { default: // End of attributes. return HaveError; + case lltok::StringConstant: { + if (ParseStringAttribute(B)) + return true; + continue; + } case lltok::kw_dereferenceable: { uint64_t Bytes; if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes)) diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h index a67c3f5b0db..acbf6f9e5b8 100644 --- a/lib/AsmParser/LLParser.h +++ b/lib/AsmParser/LLParser.h @@ -215,6 +215,8 @@ namespace llvm { return ParseUInt64(Val); } + bool ParseStringAttribute(AttrBuilder &B); + bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); bool parseOptionalUnnamedAddr(bool &UnnamedAddr) { diff --git a/test/Bitcode/attributes.ll b/test/Bitcode/attributes.ll index a0bc66642f7..0cf0745175e 100644 --- a/test/Bitcode/attributes.ll +++ b/test/Bitcode/attributes.ll @@ -262,6 +262,16 @@ define void @f44() argmemonly ret void; } +; CHECK: define "string_attribute" void @f45(i32 "string_attribute") +define "string_attribute" void @f45(i32 "string_attribute") { + ret void +} + +; CHECK: define "string_attribute_with_value"="value" void @f46(i32 "string_attribute_with_value"="value") +define "string_attribute_with_value"="value" void @f46(i32 "string_attribute_with_value"="value") { + ret void +} + ; CHECK: attributes #0 = { noreturn } ; CHECK: attributes #1 = { nounwind } ; CHECK: attributes #2 = { readnone } -- 2.34.1