clang-tools
20.0.0git
llvm-project
clang-tools-extra
clang-tidy
modernize
DeprecatedIosBaseAliasesCheck.cpp
Go to the documentation of this file.
1
//===--- DeprecatedIosBaseAliasesCheck.cpp - clang-tidy--------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "
DeprecatedIosBaseAliasesCheck.h
"
10
#include "clang/AST/ASTContext.h"
11
#include "clang/ASTMatchers/ASTMatchFinder.h"
12
#include <optional>
13
14
using namespace
clang::ast_matchers
;
15
16
namespace
clang::tidy::modernize
{
17
18
static
constexpr
std::array<StringRef, 5>
DeprecatedTypes
= {
19
"::std::ios_base::io_state"
,
"::std::ios_base::open_mode"
,
20
"::std::ios_base::seek_dir"
,
"::std::ios_base::streamoff"
,
21
"::std::ios_base::streampos"
};
22
23
static
std::optional<const char *>
getReplacementType
(StringRef
Type
) {
24
return
llvm::StringSwitch<std::optional<const char *>>(
Type
)
25
.Case(
"io_state"
,
"iostate"
)
26
.Case(
"open_mode"
,
"openmode"
)
27
.Case(
"seek_dir"
,
"seekdir"
)
28
.Default(std::nullopt);
29
}
30
31
void
DeprecatedIosBaseAliasesCheck::registerMatchers
(MatchFinder *Finder) {
32
auto
IoStateDecl = typedefDecl(hasAnyName(
DeprecatedTypes
)).bind(
"TypeDecl"
);
33
auto
IoStateType =
34
qualType(hasDeclaration(IoStateDecl), unless(elaboratedType()));
35
36
Finder->addMatcher(typeLoc(loc(IoStateType)).bind(
"TypeLoc"
),
this
);
37
}
38
39
void
DeprecatedIosBaseAliasesCheck::check
(
40
const
MatchFinder::MatchResult &Result) {
41
SourceManager &SM = *Result.SourceManager;
42
43
const
auto
*Typedef = Result.Nodes.getNodeAs<TypedefDecl>(
"TypeDecl"
);
44
StringRef TypeName = Typedef->getName();
45
auto
Replacement =
getReplacementType
(TypeName);
46
47
const
auto
*TL = Result.Nodes.getNodeAs<TypeLoc>(
"TypeLoc"
);
48
SourceLocation IoStateLoc = TL->getBeginLoc();
49
50
// Do not generate fixits for matches depending on template arguments and
51
// macro expansions.
52
bool
Fix
= Replacement && !TL->getType()->isDependentType();
53
if
(IoStateLoc.isMacroID()) {
54
IoStateLoc = SM.getSpellingLoc(IoStateLoc);
55
Fix
=
false
;
56
}
57
58
SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
59
60
if
(Replacement) {
61
const
char
*FixName = *Replacement;
62
auto
Builder
=
diag
(IoStateLoc,
"'std::ios_base::%0' is deprecated; use "
63
"'std::ios_base::%1' instead"
)
64
<< TypeName << FixName;
65
66
if
(
Fix
)
67
Builder
<< FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc),
68
FixName);
69
}
else
70
diag
(IoStateLoc,
"'std::ios_base::%0' is deprecated"
) << TypeName;
71
}
72
73
}
// namespace clang::tidy::modernize
Fix
static cl::opt< bool > Fix("fix", desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
Builder
CodeCompletionBuilder Builder
Definition:
CodeCompletionStringsTests.cpp:40
DeprecatedIosBaseAliasesCheck.h
Type
NodeType Type
Definition:
HTMLGenerator.cpp:76
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition:
ClangTidyCheck.cpp:25
clang::tidy::modernize::DeprecatedIosBaseAliasesCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition:
DeprecatedIosBaseAliasesCheck.cpp:39
clang::tidy::modernize::DeprecatedIosBaseAliasesCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition:
DeprecatedIosBaseAliasesCheck.cpp:31
clang::ast_matchers
Definition:
AbseilMatcher.h:13
clang::tidy::modernize
Definition:
AvoidBindCheck.cpp:32
clang::tidy::modernize::getReplacementType
static std::optional< const char * > getReplacementType(StringRef Type)
Definition:
DeprecatedIosBaseAliasesCheck.cpp:23
clang::tidy::modernize::DeprecatedTypes
static constexpr std::array< StringRef, 5 > DeprecatedTypes
Definition:
DeprecatedIosBaseAliasesCheck.cpp:18
Generated on Tue Oct 8 2024 10:31:03 for clang-tools by
1.9.6