[llvm-branch-commits] [clang] [clang] NFC: Provide inline definitions for {get, cast}TagDecl and friends (PR #155051)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Aug 23 13:14:02 PDT 2025


================
@@ -23,6 +23,55 @@
 
 namespace clang {
 
+inline CXXRecordDecl *Type::getAsCXXRecordDecl() const {
+  const auto *TT = dyn_cast<TagType>(CanonicalType);
+  if (!isa_and_present<RecordType, InjectedClassNameType>(TT))
+    return nullptr;
+  auto *TD = TT->getOriginalDecl();
+  if (!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD))
+    return nullptr;
----------------
Sirraide wrote:

I see; it took me a bit to make sense of all the boolean operations here; is there a performance reason why we can’t just replace
```c++
auto *TD = TT->getOriginalDecl();
if (!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD))
  return nullptr;
return cast<CXXRecordDecl>(TD)->getDefinitionOrSelf();
```
with
```c++
auto *TD = dyn_cast<CXXRecordDecl>(TT->getOriginalDecl());
return TD ? TD->getDefinitionOrSelf() : nullptr;
```
i.e. is `isa<InjectedClassNameType>(TT)` faster than a `dyn_cast<CXXRecordDecl>` here?

Alternatively, if that’s not possible, I think replacing
```c++
!isa<InjectedClassNameType>(TT) && !isa<CXXRecordDecl>(TD
```
with
```c++
isa<RecordType>(TT) && !isa<CXXRecordDecl>(TD
```
would make this a bit more readable (at least to me). The way it’s written right now it just took me a while to make sense of it but maybe that’s just me...

https://github.com/llvm/llvm-project/pull/155051


More information about the llvm-branch-commits mailing list