2016-02-24 21:36:34 +08:00
|
|
|
//===--- DeprecatedHeadersCheck.cpp - clang-tidy---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-24 21:36:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DeprecatedHeadersCheck.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2016-02-24 21:36:34 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class IncludeModernizePPCallbacks : public PPCallbacks {
|
|
|
|
public:
|
|
|
|
explicit IncludeModernizePPCallbacks(ClangTidyCheck &Check,
|
|
|
|
LangOptions LangOpts);
|
|
|
|
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
2018-05-11 03:13:14 +08:00
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override;
|
2016-02-24 21:36:34 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyCheck &Check;
|
|
|
|
LangOptions LangOpts;
|
|
|
|
llvm::StringMap<std::string> CStyledHeaderToCxx;
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
llvm::StringSet<> DeleteHeaders;
|
2016-02-24 21:36:34 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-23 02:58:12 +08:00
|
|
|
void DeprecatedHeadersCheck::registerPPCallbacks(
|
|
|
|
const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
|
|
|
|
PP->addPPCallbacks(
|
2019-08-15 07:52:23 +08:00
|
|
|
::std::make_unique<IncludeModernizePPCallbacks>(*this, getLangOpts()));
|
2016-02-24 21:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(ClangTidyCheck &Check,
|
|
|
|
LangOptions LangOpts)
|
2016-02-25 08:39:11 +08:00
|
|
|
: Check(Check), LangOpts(LangOpts) {
|
|
|
|
for (const auto &KeyValue :
|
|
|
|
std::vector<std::pair<llvm::StringRef, std::string>>(
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
{{"assert.h", "cassert"},
|
|
|
|
{"complex.h", "complex"},
|
|
|
|
{"ctype.h", "cctype"},
|
|
|
|
{"errno.h", "cerrno"},
|
|
|
|
{"float.h", "cfloat"},
|
|
|
|
{"limits.h", "climits"},
|
|
|
|
{"locale.h", "clocale"},
|
|
|
|
{"math.h", "cmath"},
|
|
|
|
{"setjmp.h", "csetjmp"},
|
|
|
|
{"signal.h", "csignal"},
|
|
|
|
{"stdarg.h", "cstdarg"},
|
|
|
|
{"stddef.h", "cstddef"},
|
|
|
|
{"stdio.h", "cstdio"},
|
|
|
|
{"stdlib.h", "cstdlib"},
|
|
|
|
{"string.h", "cstring"},
|
|
|
|
{"time.h", "ctime"},
|
|
|
|
{"wchar.h", "cwchar"},
|
2016-02-25 08:39:11 +08:00
|
|
|
{"wctype.h", "cwctype"}})) {
|
|
|
|
CStyledHeaderToCxx.insert(KeyValue);
|
|
|
|
}
|
2016-02-24 21:36:34 +08:00
|
|
|
// Add C++ 11 headers.
|
|
|
|
if (LangOpts.CPlusPlus11) {
|
2016-02-25 07:48:24 +08:00
|
|
|
for (const auto &KeyValue :
|
2016-02-25 08:39:11 +08:00
|
|
|
std::vector<std::pair<llvm::StringRef, std::string>>(
|
2016-02-24 21:36:34 +08:00
|
|
|
{{"fenv.h", "cfenv"},
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
{"stdint.h", "cstdint"},
|
|
|
|
{"inttypes.h", "cinttypes"},
|
2016-02-24 21:36:34 +08:00
|
|
|
{"tgmath.h", "ctgmath"},
|
|
|
|
{"uchar.h", "cuchar"}})) {
|
2016-02-25 07:48:24 +08:00
|
|
|
CStyledHeaderToCxx.insert(KeyValue);
|
2016-02-24 21:36:34 +08:00
|
|
|
}
|
|
|
|
}
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
for (const auto &Key :
|
|
|
|
std::vector<std::string>({"stdalign.h", "stdbool.h", "iso646.h"})) {
|
|
|
|
DeleteHeaders.insert(Key);
|
|
|
|
}
|
2016-02-24 21:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IncludeModernizePPCallbacks::InclusionDirective(
|
|
|
|
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
|
|
|
|
bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
|
2018-05-11 03:13:14 +08:00
|
|
|
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) {
|
2016-02-24 21:36:34 +08:00
|
|
|
// FIXME: Take care of library symbols from the global namespace.
|
|
|
|
//
|
|
|
|
// Reasonable options for the check:
|
|
|
|
//
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
// 1. Insert std prefix for every such symbol occurrence.
|
2016-02-24 21:36:34 +08:00
|
|
|
// 2. Insert `using namespace std;` to the beginning of TU.
|
|
|
|
// 3. Do nothing and let the user deal with the migration himself.
|
|
|
|
if (CStyledHeaderToCxx.count(FileName) != 0) {
|
|
|
|
std::string Replacement =
|
|
|
|
(llvm::Twine("<") + CStyledHeaderToCxx[FileName] + ">").str();
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
Check.diag(FilenameRange.getBegin(), "inclusion of deprecated C++ header "
|
|
|
|
"'%0'; consider using '%1' instead")
|
2016-02-24 21:36:34 +08:00
|
|
|
<< FileName << CStyledHeaderToCxx[FileName]
|
|
|
|
<< FixItHint::CreateReplacement(FilenameRange.getAsRange(),
|
|
|
|
Replacement);
|
[clang-tidy] minor improvements in modernise-deprecated-headers check
This patch introduces a minor list of changes as proposed by Richard Smith in
the mailing list.
See original comments with an impact on the future check state below:
[comments.begin
> + {"complex.h", "ccomplex"},
It'd be better to convert this one to <complex>, or leave it alone.
<ccomplex> is an unnecessary wart.
(The contents of C++11's <complex.h> / <ccomplex> / <complex> (all of
which are identical) aren't comparable to C99's <complex.h>, so if
this was C++98 code using the C99 header, the code will be broken with
or without this transformation.)
> + {"iso646.h", "ciso646"},
Just delete #includes of this one. <ciso646> does nothing.
> + {"stdalign.h", "cstdalign"},
> + {"stdbool.h", "cstdbool"},
We should just delete these two includes. These headers do nothing in C++.
comments.end]
Reviewers: alexfh, aaron.ballman
Differential Revision: https://reviews.llvm.org/D17990
llvm-svn: 278254
2016-08-11 02:01:45 +08:00
|
|
|
} else if (DeleteHeaders.count(FileName) != 0) {
|
|
|
|
Check.diag(FilenameRange.getBegin(),
|
|
|
|
"including '%0' has no effect in C++; consider removing it")
|
|
|
|
<< FileName << FixItHint::CreateRemoval(
|
|
|
|
SourceRange(HashLoc, FilenameRange.getEnd()));
|
2016-02-24 21:36:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|