Skip to content

Commit 5b05ef6

Browse files
committed
review: unify error reporting of having an invalid string literal argument
1 parent 8c693f0 commit 5b05ef6

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,68 +5330,73 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
53305330
return;
53315331
}
53325332

5333-
if (!isTokenStringLiteral()) {
5333+
auto ProcessStringLiteral = [this]() -> std::optional<StringLiteral *> {
5334+
if (!isTokenStringLiteral())
5335+
return std::nullopt;
5336+
5337+
ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
5338+
if (StringResult.isInvalid())
5339+
return std::nullopt;
5340+
5341+
if (auto Lit = dyn_cast<StringLiteral>(StringResult.get()))
5342+
return Lit;
5343+
5344+
return std::nullopt;
5345+
};
5346+
5347+
auto StrLiteral = ProcessStringLiteral();
5348+
if (!StrLiteral.has_value()) {
53345349
Diag(Tok, diag::err_expected_string_literal)
53355350
<< /*in attributes...*/ 4 << RootSignatureIdent->getName();
53365351
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
53375352
if (Tok.is(tok::r_paren))
53385353
T.consumeClose();
53395354
return;
53405355
}
5356+
5357+
// Construct our identifier
5358+
StringRef Signature = StrLiteral.value()->getString();
5359+
auto Hash = llvm::hash_value(Signature);
5360+
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
5361+
IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr));
5362+
5363+
LookupResult R(Actions, DeclIdent, SourceLocation(),
5364+
Sema::LookupOrdinaryName);
5365+
// Check if we have already found a decl of the same name, if we haven't
5366+
// then parse the root signature string and construct the in-memory elements
5367+
if (!Actions.LookupQualifiedName(R, Actions.CurContext)) {
5368+
// Invoke the root signature parser to construct the in-memory constructs
5369+
hlsl::RootSignatureLexer Lexer(Signature, RootSignatureLoc);
5370+
SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
5371+
hlsl::RootSignatureParser Parser(Elements, Lexer, PP);
5372+
if (Parser.parse()) {
5373+
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5374+
if (Tok.is(tok::r_paren))
5375+
T.consumeClose();
5376+
return;
5377+
}
53415378

5342-
ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
5343-
if (StringResult.isInvalid()) {
5344-
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5345-
if (Tok.is(tok::r_paren))
5346-
T.consumeClose();
5347-
return;
5348-
}
5379+
// Allocate the root elements onto ASTContext
5380+
unsigned N = Elements.size();
5381+
auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
5382+
::new (Actions.getASTContext()) llvm::hlsl::rootsig::RootElement[N],
5383+
N);
5384+
for (unsigned I = 0; I < N; ++I)
5385+
RootElements[I] = Elements[I];
53495386

5350-
ArgsVector Args;
5351-
if (auto Lit = dyn_cast<StringLiteral>(StringResult.get())) {
5352-
// Construct our identifier
5353-
StringRef Signature = Lit->getString();
5354-
auto Hash = llvm::hash_value(Signature);
5355-
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
5356-
IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr));
5357-
5358-
LookupResult R(Actions, DeclIdent, SourceLocation(),
5359-
Sema::LookupOrdinaryName);
5360-
// Check if we have already found a decl of the same name, if we haven't
5361-
// then parse the root signature string and construct the in-memory elements
5362-
if (!Actions.LookupQualifiedName(R, Actions.CurContext)) {
5363-
// Invoke the root signature parser to construct the in-memory constructs
5364-
hlsl::RootSignatureLexer Lexer(Signature, RootSignatureLoc);
5365-
SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
5366-
hlsl::RootSignatureParser Parser(Elements, Lexer, PP);
5367-
if (Parser.parse()) {
5368-
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5369-
if (Tok.is(tok::r_paren))
5370-
T.consumeClose();
5371-
return;
5372-
}
5387+
// Create the Root Signature
5388+
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
5389+
Actions.getASTContext(), /*DeclContext=*/Actions.CurContext,
5390+
RootSignatureLoc, DeclIdent, RootElements);
5391+
SignatureDecl->setImplicit();
5392+
Actions.PushOnScopeChains(SignatureDecl, getCurScope());
5393+
}
53735394

5374-
// Allocate the root elements onto ASTContext
5375-
unsigned N = Elements.size();
5376-
auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
5377-
::new (Actions.getASTContext()) llvm::hlsl::rootsig::RootElement[N],
5378-
N);
5379-
for (unsigned I = 0; I < N; ++I)
5380-
RootElements[I] = Elements[I];
5381-
5382-
// Create the Root Signature
5383-
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
5384-
Actions.getASTContext(), /*DeclContext=*/Actions.CurContext,
5385-
RootSignatureLoc, DeclIdent, RootElements);
5386-
SignatureDecl->setImplicit();
5387-
Actions.PushOnScopeChains(SignatureDecl, getCurScope());
5388-
}
5395+
// Create the arg for the ParsedAttr
5396+
IdentifierLoc *ILoc = ::new (Actions.getASTContext())
5397+
IdentifierLoc(RootSignatureLoc, DeclIdent);
53895398

5390-
// Create the arg for the ParsedAttr
5391-
IdentifierLoc *ILoc = ::new (Actions.getASTContext())
5392-
IdentifierLoc(RootSignatureLoc, DeclIdent);
5393-
Args.push_back(ILoc);
5394-
}
5399+
ArgsVector Args = { ILoc };
53955400

53965401
if (!T.consumeClose())
53975402
Attrs.addNew(RootSignatureIdent,

0 commit comments

Comments
 (0)