Skip to content

Commit c4cc357

Browse files
zwuismizvekov
andauthored
[Clang][AST][NFC] (RecordDecl -> CXXRecordDecl)::isInjectedClassName (#148195)
Move `RecordDecl::isInjectedClassName` to `CXXRecordDecl::isInjectedClassName`. C language doesn't have the term "injected class name". Co-authored-by: Matheus Izvekov <mizvekov@gmail.com>
1 parent 7b43c6c commit c4cc357

File tree

10 files changed

+36
-28
lines changed

10 files changed

+36
-28
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ bool contextAllowsIndex(enum CodeCompletionContext::Kind K) {
870870
}
871871

872872
static bool isInjectedClass(const NamedDecl &D) {
873-
if (auto *R = dyn_cast_or_null<RecordDecl>(&D))
873+
if (auto *R = dyn_cast_or_null<CXXRecordDecl>(&D))
874874
if (R->isInjectedClassName())
875875
return true;
876876
return false;

clang-tools-extra/clangd/Quality.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static SymbolRelevanceSignals::AccessibleScope
258258
computeScope(const NamedDecl *D) {
259259
// Injected "Foo" within the class "Foo" has file scope, not class scope.
260260
const DeclContext *DC = D->getDeclContext();
261-
if (auto *R = dyn_cast_or_null<RecordDecl>(D))
261+
if (auto *R = dyn_cast_or_null<CXXRecordDecl>(D))
262262
if (R->isInjectedClassName())
263263
DC = DC->getParent();
264264
// Class constructor should have the same scope as the class.

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class HighlightingsBuilder {
597597
std::optional<HighlightingModifier> scopeModifier(const NamedDecl *D) {
598598
const DeclContext *DC = D->getDeclContext();
599599
// Injected "Foo" within the class "Foo" has file scope, not class scope.
600-
if (auto *R = dyn_cast_or_null<RecordDecl>(D))
600+
if (auto *R = dyn_cast_or_null<CXXRecordDecl>(D))
601601
if (R->isInjectedClassName())
602602
DC = DC->getParent();
603603
// Lambda captures are considered function scope, not class scope.

clang/include/clang/AST/Decl.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,21 +4420,6 @@ class RecordDecl : public TagDecl {
44204420

44214421
void reorderDecls(const SmallVectorImpl<Decl *> &Decls);
44224422

4423-
/// Determines whether this declaration represents the
4424-
/// injected class name.
4425-
///
4426-
/// The injected class name in C++ is the name of the class that
4427-
/// appears inside the class itself. For example:
4428-
///
4429-
/// \code
4430-
/// struct C {
4431-
/// // C is implicitly declared here as a synonym for the class name.
4432-
/// };
4433-
///
4434-
/// C::C c; // same as "C c;"
4435-
/// \endcode
4436-
bool isInjectedClassName() const;
4437-
44384423
/// Determine whether this record is a class describing a lambda
44394424
/// function object.
44404425
bool isLambda() const;

clang/include/clang/AST/DeclCXX.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,7 @@ class CXXRecordDecl : public RecordDecl {
546546
}
547547

548548
CXXRecordDecl *getMostRecentNonInjectedDecl() {
549-
CXXRecordDecl *Recent =
550-
static_cast<CXXRecordDecl *>(this)->getMostRecentDecl();
549+
CXXRecordDecl *Recent = getMostRecentDecl();
551550
while (Recent->isInjectedClassName()) {
552551
// FIXME: Does injected class name need to be in the redeclarations chain?
553552
assert(Recent->getPreviousDecl());
@@ -1889,6 +1888,21 @@ class CXXRecordDecl : public RecordDecl {
18891888
DL.IsGenericLambda = IsGeneric;
18901889
}
18911890

1891+
/// Determines whether this declaration represents the
1892+
/// injected class name.
1893+
///
1894+
/// The injected class name in C++ is the name of the class that
1895+
/// appears inside the class itself. For example:
1896+
///
1897+
/// \code
1898+
/// struct C {
1899+
/// // C is implicitly declared here as a synonym for the class name.
1900+
/// };
1901+
///
1902+
/// C::C c; // same as "C c;"
1903+
/// \endcode
1904+
bool isInjectedClassName() const;
1905+
18921906
// Determine whether this type is an Interface Like type for
18931907
// __interface inheritance purposes.
18941908
bool isInterfaceLike() const;

clang/lib/AST/Decl.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5136,11 +5136,6 @@ RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C,
51365136
return R;
51375137
}
51385138

5139-
bool RecordDecl::isInjectedClassName() const {
5140-
return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
5141-
cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
5142-
}
5143-
51445139
bool RecordDecl::isLambda() const {
51455140
if (auto RD = dyn_cast<CXXRecordDecl>(this))
51465141
return RD->isLambda();

clang/lib/AST/DeclCXX.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,16 @@ bool CXXRecordDecl::hasDeletedDestructor() const {
21492149
return false;
21502150
}
21512151

2152+
bool CXXRecordDecl::isInjectedClassName() const {
2153+
if (!isImplicit() || !getDeclName())
2154+
return false;
2155+
2156+
if (const auto *RD = dyn_cast<CXXRecordDecl>(getDeclContext()))
2157+
return RD->getDeclName() == getDeclName();
2158+
2159+
return false;
2160+
}
2161+
21522162
static bool isDeclContextInNamespace(const DeclContext *DC) {
21532163
while (!DC->isTranslationUnit()) {
21542164
if (DC->isNamespace())

clang/lib/Sema/SemaAccess.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,8 @@ static void diagnoseBadDirectAccess(Sema &S,
11291129
else if (TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D))
11301130
PrevDecl = TND->getPreviousDecl();
11311131
else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1132-
if (isa<RecordDecl>(D) && cast<RecordDecl>(D)->isInjectedClassName())
1132+
if (auto *RD = dyn_cast<CXXRecordDecl>(D);
1133+
RD && RD->isInjectedClassName())
11331134
break;
11341135
PrevDecl = TD->getPreviousDecl();
11351136
}

lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
288288
// Filter out decls that we can't complete later.
289289
if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
290290
return;
291-
RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
291+
auto *from_record_decl = dyn_cast<CXXRecordDecl>(from);
292292
// We don't need to complete injected class name decls.
293293
if (from_record_decl && from_record_decl->isInjectedClassName())
294294
return;

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,9 +2420,12 @@ void TypeSystemClang::DumpDeclHiearchy(clang::Decl *decl) {
24202420

24212421
clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
24222422
if (record_decl) {
2423+
bool is_injected_class_name =
2424+
llvm::isa<clang::CXXRecordDecl>(record_decl) &&
2425+
llvm::cast<CXXRecordDecl>(record_decl)->isInjectedClassName();
24232426
printf("%20s: %s%s\n", decl->getDeclKindName(),
24242427
record_decl->getDeclName().getAsString().c_str(),
2425-
record_decl->isInjectedClassName() ? " (injected class name)" : "");
2428+
is_injected_class_name ? " (injected class name)" : "");
24262429

24272430
} else {
24282431
clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);

0 commit comments

Comments
 (0)