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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//===--- DeprecatedHeadersCheck.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 "DeprecatedHeadersCheck.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include <algorithm>
#include <vector>
using IncludeMarker =
clang::tidy::modernize::DeprecatedHeadersCheck::IncludeMarker;
namespace clang::tidy::modernize {
namespace {
class IncludeModernizePPCallbacks : public PPCallbacks {
public:
explicit IncludeModernizePPCallbacks(
std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
const SourceManager &SM, bool CheckHeaderFile);
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
OptionalFileEntryRef File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
private:
std::vector<IncludeMarker> &IncludesToBeProcessed;
LangOptions LangOpts;
llvm::StringMap<std::string> CStyledHeaderToCxx;
llvm::StringSet<> DeleteHeaders;
const SourceManager &SM;
bool CheckHeaderFile;
};
class ExternCRefutationVisitor
: public RecursiveASTVisitor<ExternCRefutationVisitor> {
std::vector<IncludeMarker> &IncludesToBeProcessed;
const SourceManager &SM;
public:
ExternCRefutationVisitor(std::vector<IncludeMarker> &IncludesToBeProcessed,
SourceManager &SM)
: IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {}
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool shouldVisitLambdaBody() const { return false; }
bool VisitLinkageSpecDecl(LinkageSpecDecl *LinkSpecDecl) const {
if (LinkSpecDecl->getLanguage() != LinkageSpecDecl::lang_c ||
!LinkSpecDecl->hasBraces())
return true;
auto ExternCBlockBegin = LinkSpecDecl->getBeginLoc();
auto ExternCBlockEnd = LinkSpecDecl->getEndLoc();
auto IsWrapped = [=, &SM = SM](const IncludeMarker &Marker) -> bool {
return SM.isBeforeInTranslationUnit(ExternCBlockBegin, Marker.DiagLoc) &&
SM.isBeforeInTranslationUnit(Marker.DiagLoc, ExternCBlockEnd);
};
llvm::erase_if(IncludesToBeProcessed, IsWrapped);
return true;
}
};
} // namespace
DeprecatedHeadersCheck::DeprecatedHeadersCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
CheckHeaderFile(Options.get("CheckHeaderFile", false)) {}
void DeprecatedHeadersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "CheckHeaderFile", CheckHeaderFile);
}
void DeprecatedHeadersCheck::registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
PP->addPPCallbacks(std::make_unique<IncludeModernizePPCallbacks>(
IncludesToBeProcessed, getLangOpts(), PP->getSourceManager(),
CheckHeaderFile));
}
void DeprecatedHeadersCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
// Even though the checker operates on a "preprocessor" level, we still need
// to act on a "TranslationUnit" to acquire the AST where we can walk each
// Decl and look for `extern "C"` blocks where we will suppress the report we
// collected during the preprocessing phase.
// The `onStartOfTranslationUnit()` won't suffice, since we need some handle
// to the `ASTContext`.
Finder->addMatcher(ast_matchers::translationUnitDecl().bind("TU"), this);
}
void DeprecatedHeadersCheck::onEndOfTranslationUnit() {
IncludesToBeProcessed.clear();
}
void DeprecatedHeadersCheck::check(
const ast_matchers::MatchFinder::MatchResult &Result) {
SourceManager &SM = Result.Context->getSourceManager();
// Suppress includes wrapped by `extern "C" { ... }` blocks.
ExternCRefutationVisitor Visitor(IncludesToBeProcessed, SM);
Visitor.TraverseAST(*Result.Context);
// Emit all the remaining reports.
for (const IncludeMarker &Marker : IncludesToBeProcessed) {
if (Marker.Replacement.empty()) {
diag(Marker.DiagLoc,
"including '%0' has no effect in C++; consider removing it")
<< Marker.FileName
<< FixItHint::CreateRemoval(Marker.ReplacementRange);
} else {
diag(Marker.DiagLoc, "inclusion of deprecated C++ header "
"'%0'; consider using '%1' instead")
<< Marker.FileName << Marker.Replacement
<< FixItHint::CreateReplacement(
Marker.ReplacementRange,
(llvm::Twine("<") + Marker.Replacement + ">").str());
}
}
}
IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts,
const SourceManager &SM, bool CheckHeaderFile)
: IncludesToBeProcessed(IncludesToBeProcessed), LangOpts(LangOpts), SM(SM),
CheckHeaderFile(CheckHeaderFile) {
for (const auto &KeyValue :
std::vector<std::pair<llvm::StringRef, std::string>>(
{{"assert.h", "cassert"},
{"complex.h", "complex"},
{"ctype.h", "cctype"},
{"errno.h", "cerrno"},
{"float.h", "cfloat"},
{"limits.h", "climits"},
{"locale.h", "clocale"},
{"math.h", "cmath"},
{"setjmp.h", "csetjmp"},
{"signal.h", "csignal"},
{"stdarg.h", "cstdarg"},
{"stddef.h", "cstddef"},
{"stdio.h", "cstdio"},
{"stdlib.h", "cstdlib"},
{"string.h", "cstring"},
{"time.h", "ctime"},
{"wchar.h", "cwchar"},
{"wctype.h", "cwctype"}})) {
CStyledHeaderToCxx.insert(KeyValue);
}
// Add C++ 11 headers.
if (LangOpts.CPlusPlus11) {
for (const auto &KeyValue :
std::vector<std::pair<llvm::StringRef, std::string>>(
{{"fenv.h", "cfenv"},
{"stdint.h", "cstdint"},
{"inttypes.h", "cinttypes"},
{"tgmath.h", "ctgmath"},
{"uchar.h", "cuchar"}})) {
CStyledHeaderToCxx.insert(KeyValue);
}
}
for (const auto &Key :
std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) {
DeleteHeaders.insert(Key);
}
}
void IncludeModernizePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
// If we don't want to warn for non-main file reports and this is one, skip
// it.
if (!CheckHeaderFile && !SM.isInMainFile(HashLoc))
return;
// Ignore system headers.
if (SM.isInSystemHeader(HashLoc))
return;
// FIXME: Take care of library symbols from the global namespace.
//
// Reasonable options for the check:
//
// 1. Insert std prefix for every such symbol occurrence.
// 2. Insert `using namespace std;` to the beginning of TU.
// 3. Do nothing and let the user deal with the migration himself.
SourceLocation DiagLoc = FilenameRange.getBegin();
if (CStyledHeaderToCxx.count(FileName) != 0) {
IncludesToBeProcessed.push_back(
IncludeMarker{CStyledHeaderToCxx[FileName], FileName,
FilenameRange.getAsRange(), DiagLoc});
} else if (DeleteHeaders.count(FileName) != 0) {
IncludesToBeProcessed.push_back(
IncludeMarker{std::string{}, FileName,
SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
}
}
} // namespace clang::tidy::modernize
|