blob: aa3d8245f607d2802210bb0a433bbc3c3f56d460 [file] [log] [blame]
Alexander Kornienkoe4a75fd2016-02-24 13:36:34 +00001//===--- DeprecatedHeadersCheck.cpp - clang-tidy---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DeprecatedHeadersCheck.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
16#include <vector>
17
18namespace clang {
19namespace tidy {
20namespace modernize {
21
22namespace {
23class IncludeModernizePPCallbacks : public PPCallbacks {
24public:
25 explicit IncludeModernizePPCallbacks(ClangTidyCheck &Check,
26 LangOptions LangOpts);
27
28 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
29 StringRef FileName, bool IsAngled,
30 CharSourceRange FilenameRange, const FileEntry *File,
31 StringRef SearchPath, StringRef RelativePath,
32 const Module *Imported) override;
33
34private:
35 ClangTidyCheck &Check;
36 LangOptions LangOpts;
37 llvm::StringMap<std::string> CStyledHeaderToCxx;
38};
39} // namespace
40
41void DeprecatedHeadersCheck::registerPPCallbacks(CompilerInstance &Compiler) {
42 if (this->getLangOpts().CPlusPlus) {
43 Compiler.getPreprocessor().addPPCallbacks(
44 ::llvm::make_unique<IncludeModernizePPCallbacks>(*this,
45 this->getLangOpts()));
46 }
47}
48
49IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(ClangTidyCheck &Check,
50 LangOptions LangOpts)
51 : Check(Check), LangOpts(LangOpts),
Alexander Kornienko4432e122016-02-24 23:48:24 +000052 CStyledHeaderToCxx{{"assert.h", "cassert"}, {"complex.h", "ccomplex"},
53 {"ctype.h", "cctype"}, {"errno.h", "cerrno"},
54 {"float.h", "cfloat"}, {"inttypes.h", "cinttypes"},
55 {"iso646.h", "ciso646"}, {"limits.h", "climits"},
56 {"locale.h", "clocale"}, {"math.h", "cmath"},
57 {"setjmp.h", "csetjmp"}, {"signal.h", "csignal"},
58 {"stdarg.h", "cstdarg"}, {"stddef.h", "cstddef"},
59 {"stdint.h", "cstdint"}, {"stdio.h", "cstdio"},
60 {"stdlib.h", "cstdlib"}, {"string.h", "cstring"},
61 {"time.h", "ctime"}, {"wchar.h", "cwchar"},
62 {"wctype.h", "cwctype"}} {
Alexander Kornienkoe4a75fd2016-02-24 13:36:34 +000063 // Add C++ 11 headers.
64 if (LangOpts.CPlusPlus11) {
Alexander Kornienko4432e122016-02-24 23:48:24 +000065 for (const auto &KeyValue :
66 std::vector<std::pair<std::string, std::string>>(
Alexander Kornienkoe4a75fd2016-02-24 13:36:34 +000067 {{"fenv.h", "cfenv"},
68 {"stdalign.h", "cstdalign"},
69 {"stdbool.h", "cstdbool"},
70 {"tgmath.h", "ctgmath"},
71 {"uchar.h", "cuchar"}})) {
Alexander Kornienko4432e122016-02-24 23:48:24 +000072 CStyledHeaderToCxx.insert(KeyValue);
Alexander Kornienkoe4a75fd2016-02-24 13:36:34 +000073 }
74 }
75}
76
77void IncludeModernizePPCallbacks::InclusionDirective(
78 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
79 bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
80 StringRef SearchPath, StringRef RelativePath, const Module *Imported) {
81 // FIXME: Take care of library symbols from the global namespace.
82 //
83 // Reasonable options for the check:
84 //
85 // 1. Insert std prefix for every such symbol occurance.
86 // 2. Insert `using namespace std;` to the beginning of TU.
87 // 3. Do nothing and let the user deal with the migration himself.
88 if (CStyledHeaderToCxx.count(FileName) != 0) {
89 std::string Replacement =
90 (llvm::Twine("<") + CStyledHeaderToCxx[FileName] + ">").str();
91 Check.diag(FilenameRange.getBegin(),
92 "inclusion of deprecated C++ header '%0'; consider using '%1' instead")
93 << FileName << CStyledHeaderToCxx[FileName]
94 << FixItHint::CreateReplacement(FilenameRange.getAsRange(),
95 Replacement);
96 }
97}
98
99} // namespace modernize
100} // namespace tidy
101} // namespace clang