| 1 | //===--- DeprecatedHeadersCheck.cpp - clang-tidy---------------------------===// |
| 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 | |
| 9 | #include "DeprecatedHeadersCheck.h" |
| 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 <algorithm> |
| 18 | #include <vector> |
| 19 | |
| 20 | using IncludeMarker = |
| 21 | clang::tidy::modernize::DeprecatedHeadersCheck::IncludeMarker; |
| 22 | namespace clang::tidy::modernize { |
| 23 | namespace { |
| 24 | |
| 25 | class IncludeModernizePPCallbacks : public PPCallbacks { |
| 26 | public: |
| 27 | explicit IncludeModernizePPCallbacks( |
| 28 | std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts, |
| 29 | const SourceManager &SM, 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 *Imported, |
| 36 | SrcMgr::CharacteristicKind FileType) override; |
| 37 | |
| 38 | private: |
| 39 | std::vector<IncludeMarker> &IncludesToBeProcessed; |
| 40 | LangOptions LangOpts; |
| 41 | llvm::StringMap<std::string> CStyledHeaderToCxx; |
| 42 | llvm::StringSet<> DeleteHeaders; |
| 43 | const SourceManager &SM; |
| 44 | bool CheckHeaderFile; |
| 45 | }; |
| 46 | |
| 47 | class ExternCRefutationVisitor |
| 48 | : public RecursiveASTVisitor<ExternCRefutationVisitor> { |
| 49 | std::vector<IncludeMarker> &IncludesToBeProcessed; |
| 50 | const SourceManager &SM; |
| 51 | |
| 52 | public: |
| 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() != LinkageSpecDecl::lang_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 | |
| 77 | DeprecatedHeadersCheck::DeprecatedHeadersCheck(StringRef Name, |
| 78 | ClangTidyContext *Context) |
| 79 | : ClangTidyCheck(Name, Context), |
| 80 | CheckHeaderFile(Options.get("CheckHeaderFile", false)) {} |
| 81 | |
| 82 | void DeprecatedHeadersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 83 | Options.store(Opts, "CheckHeaderFile", CheckHeaderFile); |
| 84 | } |
| 85 | |
| 86 | void DeprecatedHeadersCheck::registerPPCallbacks( |
| 87 | const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
| 88 | PP->addPPCallbacks(std::make_unique<IncludeModernizePPCallbacks>( |
| 89 | IncludesToBeProcessed, getLangOpts(), PP->getSourceManager(), |
| 90 | CheckHeaderFile)); |
| 91 | } |
| 92 | void DeprecatedHeadersCheck::registerMatchers( |
| 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 | |
| 103 | void DeprecatedHeadersCheck::onEndOfTranslationUnit() { |
| 104 | IncludesToBeProcessed.clear(); |
| 105 | } |
| 106 | |
| 107 | void DeprecatedHeadersCheck::check( |
| 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 | |
| 133 | IncludeModernizePPCallbacks::IncludeModernizePPCallbacks( |
| 134 | std::vector<IncludeMarker> &IncludesToBeProcessed, LangOptions LangOpts, |
| 135 | const SourceManager &SM, bool CheckHeaderFile) |
| 136 | : IncludesToBeProcessed(IncludesToBeProcessed), LangOpts(LangOpts), SM(SM), |
| 137 | CheckHeaderFile(CheckHeaderFile) { |
| 138 | for (const auto &KeyValue : |
| 139 | std::vector<std::pair<llvm::StringRef, std::string>>( |
| 140 | {{"assert.h", "cassert"}, |
| 141 | {"complex.h", "complex"}, |
| 142 | {"ctype.h", "cctype"}, |
| 143 | {"errno.h", "cerrno"}, |
| 144 | {"float.h", "cfloat"}, |
| 145 | {"limits.h", "climits"}, |
| 146 | {"locale.h", "clocale"}, |
| 147 | {"math.h", "cmath"}, |
| 148 | {"setjmp.h", "csetjmp"}, |
| 149 | {"signal.h", "csignal"}, |
| 150 | {"stdarg.h", "cstdarg"}, |
| 151 | {"stddef.h", "cstddef"}, |
| 152 | {"stdio.h", "cstdio"}, |
| 153 | {"stdlib.h", "cstdlib"}, |
| 154 | {"string.h", "cstring"}, |
| 155 | {"time.h", "ctime"}, |
| 156 | {"wchar.h", "cwchar"}, |
| 157 | {"wctype.h", "cwctype"}})) { |
| 158 | CStyledHeaderToCxx.insert(KeyValue); |
| 159 | } |
| 160 | // Add C++ 11 headers. |
| 161 | if (LangOpts.CPlusPlus11) { |
| 162 | for (const auto &KeyValue : |
| 163 | std::vector<std::pair<llvm::StringRef, std::string>>( |
| 164 | {{"fenv.h", "cfenv"}, |
| 165 | {"stdint.h", "cstdint"}, |
| 166 | {"inttypes.h", "cinttypes"}, |
| 167 | {"tgmath.h", "ctgmath"}, |
| 168 | {"uchar.h", "cuchar"}})) { |
| 169 | CStyledHeaderToCxx.insert(KeyValue); |
| 170 | } |
| 171 | } |
| 172 | for (const auto &Key : |
| 173 | std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) { |
| 174 | DeleteHeaders.insert(Key); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void IncludeModernizePPCallbacks::InclusionDirective( |
| 179 | SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, |
| 180 | bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, |
| 181 | StringRef SearchPath, StringRef RelativePath, const Module *Imported, |
| 182 | SrcMgr::CharacteristicKind FileType) { |
| 183 | |
| 184 | // If we don't want to warn for non-main file reports and this is one, skip |
| 185 | // it. |
| 186 | if (!CheckHeaderFile && !SM.isInMainFile(HashLoc)) |
| 187 | return; |
| 188 | |
| 189 | // Ignore system headers. |
| 190 | if (SM.isInSystemHeader(HashLoc)) |
| 191 | return; |
| 192 | |
| 193 | // FIXME: Take care of library symbols from the global namespace. |
| 194 | // |
| 195 | // Reasonable options for the check: |
| 196 | // |
| 197 | // 1. Insert std prefix for every such symbol occurrence. |
| 198 | // 2. Insert `using namespace std;` to the beginning of TU. |
| 199 | // 3. Do nothing and let the user deal with the migration himself. |
| 200 | SourceLocation DiagLoc = FilenameRange.getBegin(); |
| 201 | if (CStyledHeaderToCxx.count(FileName) != 0) { |
| 202 | IncludesToBeProcessed.push_back( |
| 203 | IncludeMarker{CStyledHeaderToCxx[FileName], FileName, |
| 204 | FilenameRange.getAsRange(), DiagLoc}); |
| 205 | } else if (DeleteHeaders.count(FileName) != 0) { |
| 206 | IncludesToBeProcessed.push_back( |
| 207 | IncludeMarker{std::string{}, FileName, |
| 208 | SourceRange{HashLoc, FilenameRange.getEnd()}, DiagLoc}); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } // namespace clang::tidy::modernize |