Skip to content

Commit 118bf03

Browse files
committed
review: unify error reporting of having an invalid string literal argument
1 parent 27728c2 commit 118bf03

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
@@ -5228,68 +5228,73 @@ void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
52285228
return;
52295229
}
52305230

5231-
if (!isTokenStringLiteral()) {
5231+
auto ProcessStringLiteral = [this]() -> std::optional<StringLiteral *> {
5232+
if (!isTokenStringLiteral())
5233+
return std::nullopt;
5234+
5235+
ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
5236+
if (StringResult.isInvalid())
5237+
return std::nullopt;
5238+
5239+
if (auto Lit = dyn_cast<StringLiteral>(StringResult.get()))
5240+
return Lit;
5241+
5242+
return std::nullopt;
5243+
};
5244+
5245+
auto StrLiteral = ProcessStringLiteral();
5246+
if (!StrLiteral.has_value()) {
52325247
Diag(Tok, diag::err_expected_string_literal)
52335248
<< /*in attributes...*/ 4 << RootSignatureIdent->getName();
52345249
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
52355250
if (Tok.is(tok::r_paren))
52365251
T.consumeClose();
52375252
return;
52385253
}
5254+
5255+
// Construct our identifier
5256+
StringRef Signature = StrLiteral.value()->getString();
5257+
auto Hash = llvm::hash_value(Signature);
5258+
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
5259+
IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr));
5260+
5261+
LookupResult R(Actions, DeclIdent, SourceLocation(),
5262+
Sema::LookupOrdinaryName);
5263+
// Check if we have already found a decl of the same name, if we haven't
5264+
// then parse the root signature string and construct the in-memory elements
5265+
if (!Actions.LookupQualifiedName(R, Actions.CurContext)) {
5266+
// Invoke the root signature parser to construct the in-memory constructs
5267+
hlsl::RootSignatureLexer Lexer(Signature, RootSignatureLoc);
5268+
SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
5269+
hlsl::RootSignatureParser Parser(Elements, Lexer, PP);
5270+
if (Parser.parse()) {
5271+
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5272+
if (Tok.is(tok::r_paren))
5273+
T.consumeClose();
5274+
return;
5275+
}
52395276

5240-
ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
5241-
if (StringResult.isInvalid()) {
5242-
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5243-
if (Tok.is(tok::r_paren))
5244-
T.consumeClose();
5245-
return;
5246-
}
5277+
// Allocate the root elements onto ASTContext
5278+
unsigned N = Elements.size();
5279+
auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
5280+
::new (Actions.getASTContext()) llvm::hlsl::rootsig::RootElement[N],
5281+
N);
5282+
for (unsigned I = 0; I < N; ++I)
5283+
RootElements[I] = Elements[I];
52475284

5248-
ArgsVector Args;
5249-
if (auto Lit = dyn_cast<StringLiteral>(StringResult.get())) {
5250-
// Construct our identifier
5251-
StringRef Signature = Lit->getString();
5252-
auto Hash = llvm::hash_value(Signature);
5253-
std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(Hash);
5254-
IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(IdStr));
5255-
5256-
LookupResult R(Actions, DeclIdent, SourceLocation(),
5257-
Sema::LookupOrdinaryName);
5258-
// Check if we have already found a decl of the same name, if we haven't
5259-
// then parse the root signature string and construct the in-memory elements
5260-
if (!Actions.LookupQualifiedName(R, Actions.CurContext)) {
5261-
// Invoke the root signature parser to construct the in-memory constructs
5262-
hlsl::RootSignatureLexer Lexer(Signature, RootSignatureLoc);
5263-
SmallVector<llvm::hlsl::rootsig::RootElement> Elements;
5264-
hlsl::RootSignatureParser Parser(Elements, Lexer, PP);
5265-
if (Parser.parse()) {
5266-
SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
5267-
if (Tok.is(tok::r_paren))
5268-
T.consumeClose();
5269-
return;
5270-
}
5285+
// Create the Root Signature
5286+
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
5287+
Actions.getASTContext(), /*DeclContext=*/Actions.CurContext,
5288+
RootSignatureLoc, DeclIdent, RootElements);
5289+
SignatureDecl->setImplicit();
5290+
Actions.PushOnScopeChains(SignatureDecl, getCurScope());
5291+
}
52715292

5272-
// Allocate the root elements onto ASTContext
5273-
unsigned N = Elements.size();
5274-
auto RootElements = MutableArrayRef<llvm::hlsl::rootsig::RootElement>(
5275-
::new (Actions.getASTContext()) llvm::hlsl::rootsig::RootElement[N],
5276-
N);
5277-
for (unsigned I = 0; I < N; ++I)
5278-
RootElements[I] = Elements[I];
5279-
5280-
// Create the Root Signature
5281-
auto *SignatureDecl = HLSLRootSignatureDecl::Create(
5282-
Actions.getASTContext(), /*DeclContext=*/Actions.CurContext,
5283-
RootSignatureLoc, DeclIdent, RootElements);
5284-
SignatureDecl->setImplicit();
5285-
Actions.PushOnScopeChains(SignatureDecl, getCurScope());
5286-
}
5293+
// Create the arg for the ParsedAttr
5294+
IdentifierLoc *ILoc = ::new (Actions.getASTContext())
5295+
IdentifierLoc(RootSignatureLoc, DeclIdent);
52875296

5288-
// Create the arg for the ParsedAttr
5289-
IdentifierLoc *ILoc = ::new (Actions.getASTContext())
5290-
IdentifierLoc(RootSignatureLoc, DeclIdent);
5291-
Args.push_back(ILoc);
5292-
}
5297+
ArgsVector Args = { ILoc };
52935298

52945299
if (!T.consumeClose())
52955300
Attrs.addNew(RootSignatureIdent,

0 commit comments

Comments
 (0)