11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Lex/PPCallbacks.h"
13 #include "clang/Lex/Preprocessor.h"
14 #include "llvm/ADT/StringMap.h"
23 class IncludeModernizePPCallbacks :
public PPCallbacks {
25 explicit IncludeModernizePPCallbacks(ClangTidyCheck &
Check,
28 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
30 CharSourceRange FilenameRange,
const FileEntry *
File,
31 StringRef SearchPath, StringRef RelativePath,
32 const Module *Imported)
override;
43 Compiler.getPreprocessor().addPPCallbacks(
44 ::llvm::make_unique<IncludeModernizePPCallbacks>(*
this,
51 : Check(Check), LangOpts(LangOpts) {
52 for (
const auto &KeyValue :
53 std::vector<std::pair<llvm::StringRef, std::string>>(
54 {{
"assert.h",
"cassert"}, {
"complex.h",
"ccomplex"},
55 {
"ctype.h",
"cctype"}, {
"errno.h",
"cerrno"},
56 {
"float.h",
"cfloat"}, {
"inttypes.h",
"cinttypes"},
57 {
"iso646.h",
"ciso646"}, {
"limits.h",
"climits"},
58 {
"locale.h",
"clocale"}, {
"math.h",
"cmath"},
59 {
"setjmp.h",
"csetjmp"}, {
"signal.h",
"csignal"},
60 {
"stdarg.h",
"cstdarg"}, {
"stddef.h",
"cstddef"},
61 {
"stdint.h",
"cstdint"}, {
"stdio.h",
"cstdio"},
62 {
"stdlib.h",
"cstdlib"}, {
"string.h",
"cstring"},
63 {
"time.h",
"ctime"}, {
"wchar.h",
"cwchar"},
64 {
"wctype.h",
"cwctype"}})) {
68 if (LangOpts.CPlusPlus11) {
69 for (
const auto &KeyValue :
70 std::vector<std::pair<llvm::StringRef, std::string>>(
72 {
"stdalign.h",
"cstdalign"},
73 {
"stdbool.h",
"cstdbool"},
74 {
"tgmath.h",
"ctgmath"},
75 {
"uchar.h",
"cuchar"}})) {
81 void IncludeModernizePPCallbacks::InclusionDirective(
82 SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
83 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *
File,
84 StringRef SearchPath, StringRef RelativePath,
const Module *Imported) {
93 std::string Replacement =
95 Check.
diag(FilenameRange.getBegin(),
96 "inclusion of deprecated C++ header '%0'; consider using '%1' instead")
98 << FixItHint::CreateReplacement(FilenameRange.getAsRange(),
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
bool IsAngled
true if this was an include with angle brackets
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.