Skip to content

Commit 20e94ae

Browse files
committed
Reworked switch to be generated using TableGen
1 parent 3f3b45c commit 20e94ae

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
@@ -737,6 +737,9 @@ class Attr {
737737
// our existing general parsing we need to have a separate flag that
738738
// opts an attribute into strict parsing of attribute parameters
739739
bit StrictEnumParameters = 0;
740+
// Set to true for attributes which have Sema checks which requires the type
741+
// to be deduced.
742+
bit IsTypeDependent = 0;
740743
// Lists language options, one of which is required to be true for the
741744
// attribute to be applicable. If empty, no language options are required.
742745
list<LangOpt> LangOpts = [];
@@ -1408,6 +1411,7 @@ def Cleanup : InheritableAttr {
14081411
let Args = [DeclArgument<Function, "FunctionDecl">];
14091412
let Subjects = SubjectList<[LocalVar]>;
14101413
let Documentation = [CleanupDocs];
1414+
bit IsTypeDependent = 1;
14111415
// FIXME: DeclArgument should be reworked to also store the
14121416
// Expr instead of adding attr specific hacks like the following.
14131417
// 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
@@ -4769,6 +4769,8 @@ class Sema final : public SemaBase {
47694769
// linkage or not.
47704770
static bool mightHaveNonExternalLinkage(const DeclaratorDecl *FD);
47714771

4772+
#include "clang/Sema/AttrIsTypeDependent.inc"
4773+
47724774
///@}
47734775

47744776
//

clang/lib/Sema/SemaDecl.cpp

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

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

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5061,6 +5061,32 @@ void EmitClangAttrParsedAttrKinds(const RecordKeeper &Records,
50615061
<< "}\n";
50625062
}
50635063

5064+
// Emits Sema calls for type dependent attributes
5065+
void EmitClangAttrIsTypeDependent(const RecordKeeper &Records,
5066+
raw_ostream &OS) {
5067+
emitSourceFileHeader("Attribute is type dependent", OS, Records);
5068+
5069+
std::set<StringRef> Seen;
5070+
for (const auto *A : Records.getAllDerivedDefinitions("Attr")) {
5071+
const Record &Attr = *A;
5072+
if (Attr.getValueAsBit("IsTypeDependent")) {
5073+
Seen.insert(Attr.getName());
5074+
}
5075+
}
5076+
5077+
OS << "void checkAttrIsTypeDependent(Expr *E, Decl *D, const Attr *A) {\n";
5078+
OS << " switch (A->getKind()) {\n";
5079+
for (const StringRef &SeenAttr : Seen) {
5080+
OS << " case attr::" << SeenAttr << ":\n";
5081+
OS << " ActOn" << SeenAttr << "Attr(E, D, A);\n";
5082+
OS << " break;\n";
5083+
}
5084+
OS << " default:\n";
5085+
OS << " break;\n";
5086+
OS << " }\n";
5087+
OS << "}\n";
5088+
}
5089+
50645090
// Emits the code to dump an attribute.
50655091
void EmitClangAttrTextNodeDump(const RecordKeeper &Records, raw_ostream &OS) {
50665092
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)