aboutsummaryrefslogtreecommitdiffstats
path: root/tools/syz-declextract/clangtool/declextract.cpp
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2025-04-14 08:03:20 +0200
committerDmitry Vyukov <dvyukov@google.com>2025-04-15 08:30:57 +0000
commit93db339b43e883b4f87a3b0617e2f8a3f8eb313b (patch)
tree62049346be7e8564c06ec42186a57f3dfd1d0819 /tools/syz-declextract/clangtool/declextract.cpp
parenteb2144e822c56abb85860646d22d7ce4656fcdb1 (diff)
tools/syz-declextract: extract enums declared with a typedef
Diffstat (limited to 'tools/syz-declextract/clangtool/declextract.cpp')
-rw-r--r--tools/syz-declextract/clangtool/declextract.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/tools/syz-declextract/clangtool/declextract.cpp b/tools/syz-declextract/clangtool/declextract.cpp
index 3b9769069..f1f1ad0a7 100644
--- a/tools/syz-declextract/clangtool/declextract.cpp
+++ b/tools/syz-declextract/clangtool/declextract.cpp
@@ -124,7 +124,7 @@ private:
void run(const MatchFinder::MatchResult& Result, MatchFunc Action);
template <typename T> const T* getResult(StringRef ID) const;
FieldType extractRecord(QualType QT, const RecordType* Typ, const std::string& BackupName);
- std::string extractEnum(const EnumDecl* Decl);
+ std::string extractEnum(QualType QT, const EnumDecl* Decl);
void emitConst(const std::string& Name, int64_t Val, SourceLocation Loc);
std::string getDeclName(const Expr* Expr);
const ValueDecl* getValueDecl(const Expr* Expr);
@@ -223,7 +223,7 @@ FieldType Extractor::genType(QualType QT, const std::string& BackupName) {
return IntType{.ByteSize = sizeofType(T), .Name = TypeName(QT), .Base = QualType(T, 0).getAsString()};
}
if (auto* Typ = llvm::dyn_cast<EnumType>(T)) {
- return IntType{.ByteSize = sizeofType(T), .Enum = extractEnum(Typ->getDecl())};
+ return IntType{.ByteSize = sizeofType(T), .Enum = extractEnum(QT, Typ->getDecl())};
}
if (llvm::isa<FunctionProtoType>(T)) {
return PtrType{.Elem = TodoType(), .IsConst = true};
@@ -330,8 +330,22 @@ FieldType Extractor::extractRecord(QualType QT, const RecordType* Typ, const std
return Name;
}
-std::string Extractor::extractEnum(const EnumDecl* Decl) {
- const std::string& Name = Decl->getNameAsString();
+std::string Extractor::extractEnum(QualType QT, const EnumDecl* Decl) {
+ std::string Name = Decl->getNameAsString();
+ if (Name.empty()) {
+ // This is an unnamed enum declared with a typedef:
+ // typedef enum {...} enum_name;
+ auto Elaborated = dyn_cast<ElaboratedType>(QT.getTypePtr());
+ if (Elaborated) {
+ auto Typedef = dyn_cast<TypedefType>(Elaborated->getNamedType().getTypePtr());
+ if (Typedef)
+ Name = Typedef->getDecl()->getNameAsString();
+ }
+ if (Name.empty()) {
+ QT.dump();
+ llvm::report_fatal_error("enum with empty name");
+ }
+ }
if (EnumDedup[Name])
return Name;
EnumDedup[Name] = true;