1 //===--- ForwardDeclarationNamespaceCheck.cpp - clang-tidy ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ForwardDeclarationNamespaceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
17 using namespace clang::ast_matchers
;
19 namespace clang::tidy::bugprone
{
21 void ForwardDeclarationNamespaceCheck::registerMatchers(MatchFinder
*Finder
) {
22 // Match all class declarations/definitions *EXCEPT*
23 // 1. implicit classes, e.g. `class A {};` has implicit `class A` inside `A`.
24 // 2. nested classes declared/defined inside another class.
25 // 3. template class declaration, template instantiation or
26 // specialization (NOTE: extern specialization is filtered out by
27 // `unless(hasAncestor(cxxRecordDecl()))`).
28 auto IsInSpecialization
= hasAncestor(
29 decl(anyOf(cxxRecordDecl(isExplicitTemplateSpecialization()),
30 functionDecl(isExplicitTemplateSpecialization()))));
33 hasParent(decl(anyOf(namespaceDecl(), translationUnitDecl()))),
34 unless(isImplicit()), unless(hasAncestor(cxxRecordDecl())),
35 unless(isInstantiated()), unless(IsInSpecialization
),
36 unless(classTemplateSpecializationDecl()))
40 // Match all friend declarations. Classes used in friend declarations are not
41 // marked as referenced in AST. We need to record all record classes used in
42 // friend declarations.
43 Finder
->addMatcher(friendDecl().bind("friend_decl"), this);
46 void ForwardDeclarationNamespaceCheck::check(
47 const MatchFinder::MatchResult
&Result
) {
48 if (const auto *RecordDecl
=
49 Result
.Nodes
.getNodeAs
<CXXRecordDecl
>("record_decl")) {
50 StringRef DeclName
= RecordDecl
->getName();
51 if (RecordDecl
->isThisDeclarationADefinition()) {
52 DeclNameToDefinitions
[DeclName
].push_back(RecordDecl
);
54 // If a declaration has no definition, the definition could be in another
55 // namespace (a wrong namespace).
56 // NOTE: even a declaration does have definition, we still need it to
57 // compare with other declarations.
58 DeclNameToDeclarations
[DeclName
].push_back(RecordDecl
);
61 const auto *Decl
= Result
.Nodes
.getNodeAs
<FriendDecl
>("friend_decl");
62 assert(Decl
&& "Decl is neither record_decl nor friend decl!");
64 // Classes used in friend declarations are not marked referenced in AST,
65 // so we need to check classes used in friend declarations manually to
66 // reduce the rate of false positive.
70 // struct B { friend A; };
72 // `A` will not be marked as "referenced" in the AST.
73 if (const TypeSourceInfo
*Tsi
= Decl
->getFriendType()) {
74 QualType Desugared
= Tsi
->getType().getDesugaredType(*Result
.Context
);
75 FriendTypes
.insert(Desugared
.getTypePtr());
80 static bool haveSameNamespaceOrTranslationUnit(const CXXRecordDecl
*Decl1
,
81 const CXXRecordDecl
*Decl2
) {
82 const DeclContext
*ParentDecl1
= Decl1
->getLexicalParent();
83 const DeclContext
*ParentDecl2
= Decl2
->getLexicalParent();
85 // Since we only matched declarations whose parent is Namespace or
86 // TranslationUnit declaration, the parent should be either a translation unit
88 if (ParentDecl1
->getDeclKind() == Decl::TranslationUnit
||
89 ParentDecl2
->getDeclKind() == Decl::TranslationUnit
) {
90 return ParentDecl1
== ParentDecl2
;
92 assert(ParentDecl1
->getDeclKind() == Decl::Namespace
&&
93 "ParentDecl1 declaration must be a namespace");
94 assert(ParentDecl2
->getDeclKind() == Decl::Namespace
&&
95 "ParentDecl2 declaration must be a namespace");
96 auto *Ns1
= NamespaceDecl::castFromDeclContext(ParentDecl1
);
97 auto *Ns2
= NamespaceDecl::castFromDeclContext(ParentDecl2
);
98 return Ns1
->getOriginalNamespace() == Ns2
->getOriginalNamespace();
101 static std::string
getNameOfNamespace(const CXXRecordDecl
*Decl
) {
102 const auto *ParentDecl
= Decl
->getLexicalParent();
103 if (ParentDecl
->getDeclKind() == Decl::TranslationUnit
) {
106 const auto *NsDecl
= cast
<NamespaceDecl
>(ParentDecl
);
108 llvm::raw_string_ostream
OStream(Ns
);
109 NsDecl
->printQualifiedName(OStream
);
111 return Ns
.empty() ? "(global)" : Ns
;
114 void ForwardDeclarationNamespaceCheck::onEndOfTranslationUnit() {
115 // Iterate each group of declarations by name.
116 for (const auto &KeyValuePair
: DeclNameToDeclarations
) {
117 const auto &Declarations
= KeyValuePair
.second
;
118 // If more than 1 declaration exists, we check if all are in the same
120 for (const auto *CurDecl
: Declarations
) {
121 if (CurDecl
->hasDefinition() || CurDecl
->isReferenced()) {
122 continue; // Skip forward declarations that are used/referenced.
124 if (FriendTypes
.contains(CurDecl
->getTypeForDecl())) {
125 continue; // Skip forward declarations referenced as friend.
127 if (CurDecl
->getLocation().isMacroID() ||
128 CurDecl
->getLocation().isInvalid()) {
131 // Compare with all other declarations with the same name.
132 for (const auto *Decl
: Declarations
) {
133 if (Decl
== CurDecl
) {
134 continue; // Don't compare with self.
136 if (!CurDecl
->hasDefinition() &&
137 !haveSameNamespaceOrTranslationUnit(CurDecl
, Decl
)) {
138 diag(CurDecl
->getLocation(),
139 "declaration %0 is never referenced, but a declaration with "
140 "the same name found in another namespace '%1'")
141 << CurDecl
<< getNameOfNamespace(Decl
);
142 diag(Decl
->getLocation(), "a declaration of %0 is found here",
145 break; // FIXME: We only generate one warning for each declaration.
148 // Check if a definition in another namespace exists.
149 const auto DeclName
= CurDecl
->getName();
150 if (!DeclNameToDefinitions
.contains(DeclName
)) {
151 continue; // No definition in this translation unit, we can skip it.
153 // Make a warning for each definition with the same name (in other
155 const auto &Definitions
= DeclNameToDefinitions
[DeclName
];
156 for (const auto *Def
: Definitions
) {
157 diag(CurDecl
->getLocation(),
158 "no definition found for %0, but a definition with "
159 "the same name %1 found in another namespace '%2'")
160 << CurDecl
<< Def
<< getNameOfNamespace(Def
);
161 diag(Def
->getLocation(), "a definition of %0 is found here",
169 } // namespace clang::tidy::bugprone