DefinitionsInHeadersCheck.cpp
5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===--- DefinitionsInHeadersCheck.cpp - clang-tidy------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DefinitionsInHeadersCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace misc {
namespace {
AST_MATCHER_P(NamedDecl, usesHeaderFileExtension,
utils::HeaderFileExtensionsSet, HeaderFileExtensions) {
return utils::isExpansionLocInHeaderFile(
Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
HeaderFileExtensions);
}
} // namespace
DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
UseHeaderFileExtension(Options.get("UseHeaderFileExtension", true)),
RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
"HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
HeaderFileExtensions, ',')) {
// FIXME: Find a more suitable way to handle invalid configuration
// options.
llvm::errs() << "Invalid header file extension: "
<< RawStringHeaderFileExtensions << "\n";
}
}
void DefinitionsInHeadersCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "UseHeaderFileExtension", UseHeaderFileExtension);
Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
}
void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus)
return;
auto DefinitionMatcher =
anyOf(functionDecl(isDefinition(), unless(isDeleted())),
varDecl(isDefinition()));
if (UseHeaderFileExtension) {
Finder->addMatcher(namedDecl(DefinitionMatcher,
usesHeaderFileExtension(HeaderFileExtensions))
.bind("name-decl"),
this);
} else {
Finder->addMatcher(
namedDecl(DefinitionMatcher,
anyOf(usesHeaderFileExtension(HeaderFileExtensions),
unless(isExpansionInMainFile())))
.bind("name-decl"),
this);
}
}
void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
// Don't run the check in failing TUs.
if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
return;
// C++ [basic.def.odr] p6:
// There can be more than one definition of a class type, enumeration type,
// inline function with external linkage, class template, non-static function
// template, static data member of a class template, member function of a
// class template, or template specialization for which some template
// parameters are not specifiedin a program provided that each definition
// appears in a different translation unit, and provided the definitions
// satisfy the following requirements.
const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("name-decl");
assert(ND);
if (ND->isInvalidDecl())
return;
// Internal linkage variable definitions are ignored for now:
// const int a = 1;
// static int b = 1;
//
// Although these might also cause ODR violations, we can be less certain and
// should try to keep the false-positive rate down.
//
// FIXME: Should declarations in anonymous namespaces get the same treatment
// as static / const declarations?
if (!ND->hasExternalFormalLinkage() && !ND->isInAnonymousNamespace())
return;
if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
// Inline functions are allowed.
if (FD->isInlined())
return;
// Function templates are allowed.
if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
return;
// Ignore instantiated functions.
if (FD->isTemplateInstantiation())
return;
// Member function of a class template and member function of a nested class
// in a class template are allowed.
if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
const auto *DC = MD->getDeclContext();
while (DC->isRecord()) {
if (const auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
if (isa<ClassTemplatePartialSpecializationDecl>(RD))
return;
if (RD->getDescribedClassTemplate())
return;
}
DC = DC->getParent();
}
}
bool IsFullSpec = FD->getTemplateSpecializationKind() != TSK_Undeclared;
diag(FD->getLocation(),
"%select{function|full function template specialization}0 %1 defined "
"in a header file; function definitions in header files can lead to "
"ODR violations")
<< IsFullSpec << FD;
diag(FD->getLocation(), /*FixDescription=*/"make as 'inline'",
DiagnosticIDs::Note)
<< FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline ");
} else if (const auto *VD = dyn_cast<VarDecl>(ND)) {
// Static data members of a class template are allowed.
if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember())
return;
// Ignore instantiated static data members of classes.
if (isTemplateInstantiation(VD->getTemplateSpecializationKind()))
return;
// Ignore variable definition within function scope.
if (VD->hasLocalStorage() || VD->isStaticLocal())
return;
// Ignore inline variables.
if (VD->isInline())
return;
diag(VD->getLocation(),
"variable %0 defined in a header file; "
"variable definitions in header files can lead to ODR violations")
<< VD;
}
}
} // namespace misc
} // namespace tidy
} // namespace clang