Skip to content

Commit 6250313

Browse files
authored
[clang] Fix compile-time regression from attribute arg checking change (#101768)
In 2acf77f code was added to use the 'full' name including syntax and scope. Instead of building up a large string for each name, add syntax and scope checks to the value expression in tablegen. There is already code to generate expressions for target specific attributes. This change refactors and adds to that code to include syntax and scope checks. The tablegen avoids generating the complicated expression unless there are two attributes using the same name, otherwise the case values will be as simple as before. Removes the currently unused attributeHasStrictIdentifierArgAtIndex function and the related tablegen.
1 parent 15d4a84 commit 6250313

File tree

5 files changed

+247
-270
lines changed

5 files changed

+247
-270
lines changed

clang/include/clang/Basic/AttributeCommonInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ class AttributeCommonInfo {
191191
/// __gnu__::__attr__ will be normalized to gnu::attr).
192192
std::string getNormalizedFullName() const;
193193

194-
/// Generate a normalized full name, with syntax, scope and name.
195-
static std::string
196-
normalizeFullNameWithSyntax(const IdentifierInfo *Name,
197-
const IdentifierInfo *Scope,
198-
AttributeCommonInfo::Syntax SyntaxUsed);
199-
200194
bool isDeclspecAttribute() const { return SyntaxUsed == AS_Declspec; }
201195
bool isMicrosoftAttribute() const { return SyntaxUsed == AS_Microsoft; }
202196

clang/lib/Basic/Attributes.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,6 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
153153
normalizeName(getAttrName(), getScopeName(), getSyntax()));
154154
}
155155

156-
static StringRef getSyntaxName(AttributeCommonInfo::Syntax SyntaxUsed) {
157-
switch (SyntaxUsed) {
158-
case AttributeCommonInfo::AS_GNU:
159-
return "GNU";
160-
case AttributeCommonInfo::AS_CXX11:
161-
return "CXX11";
162-
case AttributeCommonInfo::AS_C23:
163-
return "C23";
164-
case AttributeCommonInfo::AS_Declspec:
165-
return "Declspec";
166-
case AttributeCommonInfo::AS_Microsoft:
167-
return "Microsoft";
168-
case AttributeCommonInfo::AS_Keyword:
169-
return "Keyword";
170-
case AttributeCommonInfo::AS_Pragma:
171-
return "Pragma";
172-
case AttributeCommonInfo::AS_ContextSensitiveKeyword:
173-
return "ContextSensitiveKeyword";
174-
case AttributeCommonInfo::AS_HLSLAnnotation:
175-
return "HLSLAnnotation";
176-
case AttributeCommonInfo::AS_Implicit:
177-
return "Implicit";
178-
}
179-
llvm_unreachable("Invalid attribute syntax");
180-
}
181-
182-
std::string AttributeCommonInfo::normalizeFullNameWithSyntax(
183-
const IdentifierInfo *Name, const IdentifierInfo *ScopeName,
184-
Syntax SyntaxUsed) {
185-
return (Twine(getSyntaxName(SyntaxUsed)) +
186-
"::" + normalizeName(Name, ScopeName, SyntaxUsed))
187-
.str();
188-
}
189-
190156
unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
191157
// Both variables will be used in tablegen generated
192158
// attribute spell list index matching code.

clang/lib/Parse/ParseDecl.cpp

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -314,27 +314,24 @@ void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
314314
}
315315

316316
/// Determine whether the given attribute has an identifier argument.
317-
static bool attributeHasIdentifierArg(const IdentifierInfo &II,
317+
static bool attributeHasIdentifierArg(const llvm::Triple &T,
318+
const IdentifierInfo &II,
318319
ParsedAttr::Syntax Syntax,
319320
IdentifierInfo *ScopeName) {
320-
std::string FullName =
321-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
322321
#define CLANG_ATTR_IDENTIFIER_ARG_LIST
323-
return llvm::StringSwitch<bool>(FullName)
322+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
324323
#include "clang/Parse/AttrParserStringSwitches.inc"
325324
.Default(false);
326325
#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
327326
}
328327

329-
/// Determine whether the given attribute has an identifier argument.
328+
/// Determine whether the given attribute has string arguments.
330329
static ParsedAttributeArgumentsProperties
331330
attributeStringLiteralListArg(const llvm::Triple &T, const IdentifierInfo &II,
332331
ParsedAttr::Syntax Syntax,
333332
IdentifierInfo *ScopeName) {
334-
std::string FullName =
335-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
336333
#define CLANG_ATTR_STRING_LITERAL_ARG_LIST
337-
return llvm::StringSwitch<uint32_t>(FullName)
334+
return llvm::StringSwitch<uint32_t>(normalizeAttrName(II.getName()))
338335
#include "clang/Parse/AttrParserStringSwitches.inc"
339336
.Default(0);
340337
#undef CLANG_ATTR_STRING_LITERAL_ARG_LIST
@@ -344,10 +341,8 @@ attributeStringLiteralListArg(const llvm::Triple &T, const IdentifierInfo &II,
344341
static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II,
345342
ParsedAttr::Syntax Syntax,
346343
IdentifierInfo *ScopeName) {
347-
std::string FullName =
348-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
349344
#define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
350-
return llvm::StringSwitch<bool>(FullName)
345+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
351346
#include "clang/Parse/AttrParserStringSwitches.inc"
352347
.Default(false);
353348
#undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
@@ -357,10 +352,8 @@ static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II,
357352
static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II,
358353
ParsedAttr::Syntax Syntax,
359354
IdentifierInfo *ScopeName) {
360-
std::string FullName =
361-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
362355
#define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
363-
return llvm::StringSwitch<bool>(FullName)
356+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
364357
#include "clang/Parse/AttrParserStringSwitches.inc"
365358
.Default(false);
366359
#undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
@@ -370,10 +363,8 @@ static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II,
370363
static bool attributeAcceptsExprPack(const IdentifierInfo &II,
371364
ParsedAttr::Syntax Syntax,
372365
IdentifierInfo *ScopeName) {
373-
std::string FullName =
374-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
375366
#define CLANG_ATTR_ACCEPTS_EXPR_PACK
376-
return llvm::StringSwitch<bool>(FullName)
367+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
377368
#include "clang/Parse/AttrParserStringSwitches.inc"
378369
.Default(false);
379370
#undef CLANG_ATTR_ACCEPTS_EXPR_PACK
@@ -383,53 +374,31 @@ static bool attributeAcceptsExprPack(const IdentifierInfo &II,
383374
static bool attributeIsTypeArgAttr(const IdentifierInfo &II,
384375
ParsedAttr::Syntax Syntax,
385376
IdentifierInfo *ScopeName) {
386-
std::string FullName =
387-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
388377
#define CLANG_ATTR_TYPE_ARG_LIST
389-
return llvm::StringSwitch<bool>(FullName)
378+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
390379
#include "clang/Parse/AttrParserStringSwitches.inc"
391380
.Default(false);
392381
#undef CLANG_ATTR_TYPE_ARG_LIST
393382
}
394383

395-
/// Determine whether the given attribute takes identifier arguments.
384+
/// Determine whether the given attribute takes a strict identifier argument.
396385
static bool attributeHasStrictIdentifierArgs(const IdentifierInfo &II,
397386
ParsedAttr::Syntax Syntax,
398387
IdentifierInfo *ScopeName) {
399-
std::string FullName =
400-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
401-
#define CLANG_ATTR_STRICT_IDENTIFIER_ARG_AT_INDEX_LIST
402-
return (llvm::StringSwitch<uint64_t>(FullName)
403-
#include "clang/Parse/AttrParserStringSwitches.inc"
404-
.Default(0)) != 0;
405-
#undef CLANG_ATTR_STRICT_IDENTIFIER_ARG_AT_INDEX_LIST
406-
}
407-
408-
/// Determine whether the given attribute takes an identifier argument at a
409-
/// specific index
410-
static bool attributeHasStrictIdentifierArgAtIndex(const IdentifierInfo &II,
411-
ParsedAttr::Syntax Syntax,
412-
IdentifierInfo *ScopeName,
413-
size_t argIndex) {
414-
std::string FullName =
415-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
416-
#define CLANG_ATTR_STRICT_IDENTIFIER_ARG_AT_INDEX_LIST
417-
return (llvm::StringSwitch<uint64_t>(FullName)
388+
#define CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST
389+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
418390
#include "clang/Parse/AttrParserStringSwitches.inc"
419-
.Default(0)) &
420-
(1ull << argIndex);
421-
#undef CLANG_ATTR_STRICT_IDENTIFIER_ARG_AT_INDEX_LIST
391+
.Default(false);
392+
#undef CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST
422393
}
423394

424395
/// Determine whether the given attribute requires parsing its arguments
425396
/// in an unevaluated context or not.
426397
static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II,
427398
ParsedAttr::Syntax Syntax,
428399
IdentifierInfo *ScopeName) {
429-
std::string FullName =
430-
AttributeCommonInfo::normalizeFullNameWithSyntax(&II, ScopeName, Syntax);
431400
#define CLANG_ATTR_ARG_CONTEXT_LIST
432-
return llvm::StringSwitch<bool>(FullName)
401+
return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
433402
#include "clang/Parse/AttrParserStringSwitches.inc"
434403
.Default(false);
435404
#undef CLANG_ATTR_ARG_CONTEXT_LIST
@@ -575,7 +544,8 @@ unsigned Parser::ParseAttributeArgsCommon(
575544
// If this attribute wants an 'identifier' argument, make it so.
576545
bool IsIdentifierArg =
577546
AttributeHasVariadicIdentifierArg ||
578-
attributeHasIdentifierArg(*AttrName, Form.getSyntax(), ScopeName);
547+
attributeHasIdentifierArg(getTargetInfo().getTriple(), *AttrName,
548+
Form.getSyntax(), ScopeName);
579549
ParsedAttr::Kind AttrKind =
580550
ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
581551

@@ -619,13 +589,6 @@ unsigned Parser::ParseAttributeArgsCommon(
619589
if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
620590
Tok.setKind(tok::identifier);
621591

622-
if (Tok.is(tok::identifier) &&
623-
attributeHasStrictIdentifierArgAtIndex(
624-
*AttrName, Form.getSyntax(), ScopeName, ArgExprs.size())) {
625-
ArgExprs.push_back(ParseIdentifierLoc());
626-
continue;
627-
}
628-
629592
ExprResult ArgExpr;
630593
if (Tok.is(tok::identifier)) {
631594
ArgExprs.push_back(ParseIdentifierLoc());

0 commit comments

Comments
 (0)