Skip to content

Commit a7ac571

Browse files
committed
Reworked switch to be generated using TableGen
1 parent cb8afb6 commit a7ac571

File tree

8 files changed

+54
-7
lines changed

8 files changed

+54
-7
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ class Attr {
741741
// our existing general parsing we need to have a separate flag that
742742
// opts an attribute into strict parsing of attribute parameters
743743
bit StrictEnumParameters = 0;
744+
// Set to true for attributes which have Sema checks which requires the type
745+
// to be deduced.
746+
bit IsTypeDependent = 0;
744747
// Lists language options, one of which is required to be true for the
745748
// attribute to be applicable. If empty, no language options are required.
746749
list<LangOpt> LangOpts = [];
@@ -1400,6 +1403,7 @@ def Cleanup : InheritableAttr {
14001403
let Args = [DeclArgument<Function, "FunctionDecl">];
14011404
let Subjects = SubjectList<[LocalVar]>;
14021405
let Documentation = [CleanupDocs];
1406+
bit IsTypeDependent = 1;
14031407
// FIXME: DeclArgument should be reworked to also store the
14041408
// Expr instead of adding attr specific hacks like the following.
14051409
// See the discussion in https://github.com/llvm/llvm-project/pull/14023.

clang/include/clang/Sema/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ clang_tablegen(AttrParsedAttrKinds.inc -gen-clang-attr-parsed-attr-kinds
88
SOURCE ../Basic/Attr.td
99
TARGET ClangAttrParsedAttrKinds)
1010

11+
clang_tablegen(AttrIsTypeDependent.inc -gen-clang-attr-is-type-dependent
12+
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
13+
SOURCE ../Basic/Attr.td
14+
TARGET ClangAttrIsTypeDependent)
15+
1116
clang_tablegen(AttrSpellingListIndex.inc -gen-clang-attr-spelling-index
1217
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
1318
SOURCE ../Basic/Attr.td

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,8 @@ class Sema final : public SemaBase {
47644764
// linkage or not.
47654765
static bool mightHaveNonExternalLinkage(const DeclaratorDecl *FD);
47664766

4767+
#include "clang/Sema/AttrIsTypeDependent.inc"
4768+
47674769
///@}
47684770

47694771
//

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,13 +3360,7 @@ void Sema::CheckAttributesOnDeducedType(Expr *E, Decl *D) {
33603360
return;
33613361

33623362
for (const Attr *A : D->getAttrs()) {
3363-
switch (A->getKind()) {
3364-
case attr::Cleanup:
3365-
ActOnCleanupAttr(E, D, A);
3366-
break;
3367-
default:
3368-
continue;
3369-
}
3363+
checkAttrIsTypeDependent(E, D, A);
33703364
}
33713365
}
33723366

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5045,6 +5045,32 @@ void EmitClangAttrParsedAttrKinds(const RecordKeeper &Records,
50455045
<< "}\n";
50465046
}
50475047

5048+
// Emits Sema calls for type dependent attributes
5049+
void EmitClangAttrIsTypeDependent(const RecordKeeper &Records,
5050+
raw_ostream &OS) {
5051+
emitSourceFileHeader("Attribute is type dependent", OS, Records);
5052+
5053+
std::set<StringRef> Seen;
5054+
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) {
5055+
const Record &Attr = *A;
5056+
if (Attr.getValueAsBit("IsTypeDependent")) {
5057+
Seen.insert(Attr.getName());
5058+
}
5059+
}
5060+
5061+
OS << "void checkAttrIsTypeDependent(Expr *E, Decl *D, const Attr *A) {\n";
5062+
OS << " switch (A->getKind()) {\n";
5063+
for (const StringRef &SeenAttr : Seen) {
5064+
OS << " case attr::" << SeenAttr << ":\n";
5065+
OS << " ActOn" << SeenAttr << "Attr(E, D, A);\n";
5066+
OS << " break;\n";
5067+
}
5068+
OS << " default:\n";
5069+
OS << " break;\n";
5070+
OS << " }\n";
5071+
OS << "}\n";
5072+
}
5073+
50485074
// Emits the code to dump an attribute.
50495075
void EmitClangAttrTextNodeDump(const RecordKeeper &Records, raw_ostream &OS) {
50505076
emitSourceFileHeader("Attribute text node dumper", OS, Records);

clang/utils/TableGen/TableGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum ActionType {
4343
GenClangAttrParsedAttrList,
4444
GenClangAttrParsedAttrImpl,
4545
GenClangAttrParsedAttrKinds,
46+
GenClangAttrIsTypeDependent,
4647
GenClangAttrTextNodeDump,
4748
GenClangAttrNodeTraverse,
4849
GenClangBasicReader,
@@ -179,6 +180,9 @@ cl::opt<ActionType> Action(
179180
clEnumValN(GenClangAttrParsedAttrKinds,
180181
"gen-clang-attr-parsed-attr-kinds",
181182
"Generate a clang parsed attribute kinds"),
183+
clEnumValN(GenClangAttrIsTypeDependent,
184+
"gen-clang-attr-is-type-dependent",
185+
"Generate clang is type dependent attribute code"),
182186
clEnumValN(GenClangAttrTextNodeDump, "gen-clang-attr-text-node-dump",
183187
"Generate clang attribute text node dumper"),
184188
clEnumValN(GenClangAttrNodeTraverse, "gen-clang-attr-node-traverse",
@@ -423,6 +427,9 @@ bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) {
423427
case GenClangAttrParsedAttrKinds:
424428
EmitClangAttrParsedAttrKinds(Records, OS);
425429
break;
430+
case GenClangAttrIsTypeDependent:
431+
EmitClangAttrIsTypeDependent(Records, OS);
432+
break;
426433
case GenClangAttrTextNodeDump:
427434
EmitClangAttrTextNodeDump(Records, OS);
428435
break;

clang/utils/TableGen/TableGenBackends.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void EmitClangAttrParsedAttrImpl(const llvm::RecordKeeper &Records,
8282
llvm::raw_ostream &OS);
8383
void EmitClangAttrParsedAttrKinds(const llvm::RecordKeeper &Records,
8484
llvm::raw_ostream &OS);
85+
void EmitClangAttrIsTypeDependent(const llvm::RecordKeeper &Records,
86+
llvm::raw_ostream &OS);
8587
void EmitClangAttrTextNodeDump(const llvm::RecordKeeper &Records,
8688
llvm::raw_ostream &OS);
8789
void EmitClangAttrNodeTraverse(const llvm::RecordKeeper &Records,

llvm/docs/TableGen/BackEnds.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,13 @@ ClangAttrParsedAttrKinds
355355
``AttributeList::getKind`` function, mapping a string (and syntax) to a parsed
356356
attribute ``AttributeList::Kind`` enumeration.
357357

358+
ClangAttrIsTypeDependent
359+
------------------------
360+
361+
**Purpose**: Creates ``AttrIsTypeDependent.inc``, which is used to implement the
362+
``Sema::CheckAttributesOnDeducedType`` function, mapping an attribute kind to a
363+
Sema function if it exists.
364+
358365
ClangAttrDump
359366
-------------
360367

0 commit comments

Comments
 (0)