clang-tools 22.0.0git
DeprecatedHeadersCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/RecursiveASTVisitor.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringSet.h"
16
17#include <vector>
18
21namespace clang::tidy::modernize {
22namespace {
23
24class IncludeModernizePPCallbacks : public PPCallbacks {
25public:
26 explicit IncludeModernizePPCallbacks(
27 std::vector<IncludeMarker> &IncludesToBeProcessed,
28 const LangOptions &LangOpts, const SourceManager &SM,
29 bool CheckHeaderFile);
30
31 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
32 StringRef FileName, bool IsAngled,
33 CharSourceRange FilenameRange,
34 OptionalFileEntryRef File, StringRef SearchPath,
35 StringRef RelativePath, const Module *SuggestedModule,
36 bool ModuleImported,
37 SrcMgr::CharacteristicKind FileType) override;
38
39private:
40 std::vector<IncludeMarker> &IncludesToBeProcessed;
41 llvm::StringMap<StringRef> CStyledHeaderToCxx;
42 llvm::StringSet<> DeleteHeaders;
43 const SourceManager &SM;
44 bool CheckHeaderFile;
45};
46
47class ExternCRefutationVisitor
48 : public RecursiveASTVisitor<ExternCRefutationVisitor> {
49 std::vector<IncludeMarker> &IncludesToBeProcessed;
50 const SourceManager &SM;
51
52public:
53 ExternCRefutationVisitor(std::vector<IncludeMarker> &IncludesToBeProcessed,
54 SourceManager &SM)
55 : IncludesToBeProcessed(IncludesToBeProcessed), SM(SM) {}
56 bool shouldWalkTypesOfTypeLocs() const { return false; }
57 bool shouldVisitLambdaBody() const { return false; }
58
59 bool VisitLinkageSpecDecl(LinkageSpecDecl *LinkSpecDecl) const {
60 if (LinkSpecDecl->getLanguage() != LinkageSpecLanguageIDs::C ||
61 !LinkSpecDecl->hasBraces())
62 return true;
63
64 auto ExternCBlockBegin = LinkSpecDecl->getBeginLoc();
65 auto ExternCBlockEnd = LinkSpecDecl->getEndLoc();
66 auto IsWrapped = [=, &SM = SM](const IncludeMarker &Marker) -> bool {
67 return SM.isBeforeInTranslationUnit(ExternCBlockBegin, Marker.DiagLoc) &&
68 SM.isBeforeInTranslationUnit(Marker.DiagLoc, ExternCBlockEnd);
69 };
70
71 llvm::erase_if(IncludesToBeProcessed, IsWrapped);
72 return true;
73 }
74};
75} // namespace
76
78 ClangTidyContext *Context)
79 : ClangTidyCheck(Name, Context),
80 CheckHeaderFile(Options.get("CheckHeaderFile", false)) {}
81
83 Options.store(Opts, "CheckHeaderFile", CheckHeaderFile);
84}
85
87 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
88 PP->addPPCallbacks(std::make_unique<IncludeModernizePPCallbacks>(
89 IncludesToBeProcessed, getLangOpts(), PP->getSourceManager(),
90 CheckHeaderFile));
91}
93 ast_matchers::MatchFinder *Finder) {
94 // Even though the checker operates on a "preprocessor" level, we still need
95 // to act on a "TranslationUnit" to acquire the AST where we can walk each
96 // Decl and look for `extern "C"` blocks where we will suppress the report we
97 // collected during the preprocessing phase.
98 // The `onStartOfTranslationUnit()` won't suffice, since we need some handle
99 // to the `ASTContext`.
100 Finder->addMatcher(ast_matchers::translationUnitDecl().bind("TU"), this);
101}
102
104 IncludesToBeProcessed.clear();
105}
106
108 const ast_matchers::MatchFinder::MatchResult &Result) {
109 SourceManager &SM = Result.Context->getSourceManager();
110
111 // Suppress includes wrapped by `extern "C" { ... }` blocks.
112 ExternCRefutationVisitor Visitor(IncludesToBeProcessed, SM);
113 Visitor.TraverseAST(*Result.Context);
114
115 // Emit all the remaining reports.
116 for (const IncludeMarker &Marker : IncludesToBeProcessed) {
117 if (Marker.Replacement.empty()) {
118 diag(Marker.DiagLoc,
119 "including '%0' has no effect in C++; consider removing it")
120 << Marker.FileName
121 << FixItHint::CreateRemoval(Marker.ReplacementRange);
122 } else {
123 diag(Marker.DiagLoc, "inclusion of deprecated C++ header "
124 "'%0'; consider using '%1' instead")
125 << Marker.FileName << Marker.Replacement
126 << FixItHint::CreateReplacement(
127 Marker.ReplacementRange,
128 (llvm::Twine("<") + Marker.Replacement + ">").str());
129 }
130 }
131}
132
133IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
134 std::vector<IncludeMarker> &IncludesToBeProcessed,
135 const LangOptions &LangOpts, const SourceManager &SM, bool CheckHeaderFile)
136 : IncludesToBeProcessed(IncludesToBeProcessed), SM(SM),
137 CheckHeaderFile(CheckHeaderFile) {
138
139 static constexpr std::pair<StringRef, StringRef> CXX98Headers[] = {
140 {"assert.h", "cassert"}, {"complex.h", "complex"},
141 {"ctype.h", "cctype"}, {"errno.h", "cerrno"},
142 {"float.h", "cfloat"}, {"limits.h", "climits"},
143 {"locale.h", "clocale"}, {"math.h", "cmath"},
144 {"setjmp.h", "csetjmp"}, {"signal.h", "csignal"},
145 {"stdarg.h", "cstdarg"}, {"stddef.h", "cstddef"},
146 {"stdio.h", "cstdio"}, {"stdlib.h", "cstdlib"},
147 {"string.h", "cstring"}, {"time.h", "ctime"},
148 {"wchar.h", "cwchar"}, {"wctype.h", "cwctype"},
149 };
150 CStyledHeaderToCxx.insert(std::begin(CXX98Headers), std::end(CXX98Headers));
151
152 static constexpr std::pair<StringRef, StringRef> CXX11Headers[] = {
153 {"fenv.h", "cfenv"}, {"stdint.h", "cstdint"},
154 {"inttypes.h", "cinttypes"}, {"tgmath.h", "ctgmath"},
155 {"uchar.h", "cuchar"},
156 };
157 if (LangOpts.CPlusPlus11)
158 CStyledHeaderToCxx.insert(std::begin(CXX11Headers), std::end(CXX11Headers));
159
160 static constexpr StringRef HeadersToDelete[] = {"stdalign.h", "stdbool.h",
161 "iso646.h"};
162 DeleteHeaders.insert_range(HeadersToDelete);
163}
164
165void IncludeModernizePPCallbacks::InclusionDirective(
166 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
167 bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
168 StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
169 bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
170
171 // If we don't want to warn for non-main file reports and this is one, skip
172 // it.
173 if (!CheckHeaderFile && !SM.isInMainFile(HashLoc))
174 return;
175
176 // Ignore system headers.
177 if (SM.isInSystemHeader(HashLoc))
178 return;
179
180 // FIXME: Take care of library symbols from the global namespace.
181 //
182 // Reasonable options for the check:
183 //
184 // 1. Insert std prefix for every such symbol occurrence.
185 // 2. Insert `using namespace std;` to the beginning of TU.
186 // 3. Do nothing and let the user deal with the migration himself.
187 SourceLocation DiagLoc = FilenameRange.getBegin();
188 if (auto It = CStyledHeaderToCxx.find(FileName);
189 It != CStyledHeaderToCxx.end()) {
190 IncludesToBeProcessed.emplace_back(IncludeMarker{
191 It->second, FileName, FilenameRange.getAsRange(), DiagLoc});
192 } else if (DeleteHeaders.contains(FileName)) {
193 IncludesToBeProcessed.emplace_back(
194 // NOLINTNEXTLINE(modernize-use-emplace) - false-positive
195 IncludeMarker{StringRef{}, FileName,
196 SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc});
197 }
198}
199
200} // namespace clang::tidy::modernize
clang::tidy::modernize::DeprecatedHeadersCheck::IncludeMarker IncludeMarker
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
DeprecatedHeadersCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
llvm::StringMap< ClangTidyValue > OptionMap