1 //===--- DeprecatedIosBaseAliasesCheck.cpp - clang-tidy--------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DeprecatedIosBaseAliasesCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
19 static constexpr std::array
<StringRef
, 5> DeprecatedTypes
= {
20 "::std::ios_base::io_state", "::std::ios_base::open_mode",
21 "::std::ios_base::seek_dir", "::std::ios_base::streamoff",
22 "::std::ios_base::streampos"};
24 static llvm::Optional
<const char *> getReplacementType(StringRef Type
) {
25 return llvm::StringSwitch
<llvm::Optional
<const char *>>(Type
)
26 .Case("io_state", "iostate")
27 .Case("open_mode", "openmode")
28 .Case("seek_dir", "seekdir")
32 void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder
*Finder
) {
33 auto IoStateDecl
= typedefDecl(hasAnyName(DeprecatedTypes
)).bind("TypeDecl");
35 qualType(hasDeclaration(IoStateDecl
), unless(elaboratedType()));
37 Finder
->addMatcher(typeLoc(loc(IoStateType
)).bind("TypeLoc"), this);
40 void DeprecatedIosBaseAliasesCheck::check(
41 const MatchFinder::MatchResult
&Result
) {
42 SourceManager
&SM
= *Result
.SourceManager
;
44 const auto *Typedef
= Result
.Nodes
.getNodeAs
<TypedefDecl
>("TypeDecl");
45 StringRef TypeName
= Typedef
->getName();
46 auto Replacement
= getReplacementType(TypeName
);
48 const auto *TL
= Result
.Nodes
.getNodeAs
<TypeLoc
>("TypeLoc");
49 SourceLocation IoStateLoc
= TL
->getBeginLoc();
51 // Do not generate fixits for matches depending on template arguments and
53 bool Fix
= Replacement
&& !TL
->getType()->isDependentType();
54 if (IoStateLoc
.isMacroID()) {
55 IoStateLoc
= SM
.getSpellingLoc(IoStateLoc
);
59 SourceLocation EndLoc
= IoStateLoc
.getLocWithOffset(TypeName
.size() - 1);
62 const char *FixName
= *Replacement
;
63 auto Builder
= diag(IoStateLoc
, "'std::ios_base::%0' is deprecated; use "
64 "'std::ios_base::%1' instead")
65 << TypeName
<< FixName
;
68 Builder
<< FixItHint::CreateReplacement(SourceRange(IoStateLoc
, EndLoc
),
71 diag(IoStateLoc
, "'std::ios_base::%0' is deprecated") << TypeName
;
74 } // namespace modernize