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"
24 class IncludeModernizePPCallbacks :
public PPCallbacks {
26 explicit IncludeModernizePPCallbacks(ClangTidyCheck &
Check,
29 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
31 CharSourceRange FilenameRange,
const FileEntry *
File,
32 StringRef SearchPath, StringRef RelativePath,
33 const Module *Imported)
override;
45 Compiler.getPreprocessor().addPPCallbacks(
46 ::llvm::make_unique<IncludeModernizePPCallbacks>(*
this,
53 : Check(Check), LangOpts(LangOpts) {
54 for (
const auto &KeyValue :
55 std::vector<std::pair<llvm::StringRef, std::string>>(
56 {{
"assert.h",
"cassert"},
57 {
"complex.h",
"complex"},
58 {
"ctype.h",
"cctype"},
59 {
"errno.h",
"cerrno"},
60 {
"float.h",
"cfloat"},
61 {
"limits.h",
"climits"},
62 {
"locale.h",
"clocale"},
64 {
"setjmp.h",
"csetjmp"},
65 {
"signal.h",
"csignal"},
66 {
"stdarg.h",
"cstdarg"},
67 {
"stddef.h",
"cstddef"},
68 {
"stdio.h",
"cstdio"},
69 {
"stdlib.h",
"cstdlib"},
70 {
"string.h",
"cstring"},
72 {
"wchar.h",
"cwchar"},
73 {
"wctype.h",
"cwctype"}})) {
77 if (LangOpts.CPlusPlus11) {
78 for (
const auto &KeyValue :
79 std::vector<std::pair<llvm::StringRef, std::string>>(
81 {
"stdint.h",
"cstdint"},
82 {
"inttypes.h",
"cinttypes"},
83 {
"tgmath.h",
"ctgmath"},
84 {
"uchar.h",
"cuchar"}})) {
88 for (
const auto &Key :
89 std::vector<std::string>({
"stdalign.h",
"stdbool.h",
"iso646.h"})) {
94 void IncludeModernizePPCallbacks::InclusionDirective(
95 SourceLocation HashLoc,
const Token &IncludeTok, StringRef FileName,
96 bool IsAngled, CharSourceRange FilenameRange,
const FileEntry *
File,
97 StringRef SearchPath, StringRef RelativePath,
const Module *Imported) {
106 std::string Replacement =
108 Check.
diag(FilenameRange.getBegin(),
"inclusion of deprecated C++ header "
109 "'%0'; consider using '%1' instead")
111 << FixItHint::CreateReplacement(FilenameRange.getAsRange(),
114 Check.
diag(FilenameRange.getBegin(),
115 "including '%0' has no effect in C++; consider removing it")
116 << FileName << FixItHint::CreateRemoval(
117 SourceRange(HashLoc, FilenameRange.getEnd()));
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.