blob: 7d9f59d613b4b08dcde0e16c5df778b139cd100c [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),
52 CStyledHeaderToCxx({{"assert.h", "cassert"},
53 {"complex.h", "ccomplex"},
54 {"ctype.h", "cctype"},
55 {"errno.h", "cerrno"},
56 {"float.h", "cfloat"},
57 {"inttypes.h", "cinttypes"},
58 {"iso646.h", "ciso646"},
59 {"limits.h", "climits"},
60 {"locale.h", "clocale"},
61 {"math.h", "cmath"},
62 {"setjmp.h", "csetjmp"},
63 {"signal.h", "csignal"},
64 {"stdarg.h", "cstdarg"},
65 {"stddef.h", "cstddef"},
66 {"stdint.h", "cstdint"},
67 {"stdio.h", "cstdio"},
68 {"stdlib.h", "cstdlib"},
69 {"string.h", "cstring"},
70 {"time.h", "ctime"},
71 {"wchar.h", "cwchar"},
72 {"wctype.h", "cwctype"}}) {
73 // Add C++ 11 headers.
74 if (LangOpts.CPlusPlus11) {
75 for (const auto &it : std::vector<std::pair<std::string, std::string>>(
76 {{"fenv.h", "cfenv"},
77 {"stdalign.h", "cstdalign"},
78 {"stdbool.h", "cstdbool"},
79 {"tgmath.h", "ctgmath"},
80 {"uchar.h", "cuchar"}})) {
81 CStyledHeaderToCxx.insert(it);
82 }
83 }
84}
85
86void IncludeModernizePPCallbacks::InclusionDirective(
87 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
88 bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
89 StringRef SearchPath, StringRef RelativePath, const Module *Imported) {
90 // FIXME: Take care of library symbols from the global namespace.
91 //
92 // Reasonable options for the check:
93 //
94 // 1. Insert std prefix for every such symbol occurance.
95 // 2. Insert `using namespace std;` to the beginning of TU.
96 // 3. Do nothing and let the user deal with the migration himself.
97 if (CStyledHeaderToCxx.count(FileName) != 0) {
98 std::string Replacement =
99 (llvm::Twine("<") + CStyledHeaderToCxx[FileName] + ">").str();
100 Check.diag(FilenameRange.getBegin(),
101 "inclusion of deprecated C++ header '%0'; consider using '%1' instead")
102 << FileName << CStyledHeaderToCxx[FileName]
103 << FixItHint::CreateReplacement(FilenameRange.getAsRange(),
104 Replacement);
105 }
106}
107
108} // namespace modernize
109} // namespace tidy
110} // namespace clang