UNSUPPORT test on 64-bit AIX too
[llvm-project.git] / clang-tools-extra / clang-tidy / modernize / DeprecatedIosBaseAliasesCheck.cpp
blobd6216000fe20fbdbfdf70fc56e2db6ec295966b7
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 //===----------------------------------------------------------------------===//
9 #include "DeprecatedIosBaseAliasesCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers;
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
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")
29 .Default(llvm::None);
32 void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) {
33 auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl");
34 auto IoStateType =
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
52 // macro expansions.
53 bool Fix = Replacement && !TL->getType()->isDependentType();
54 if (IoStateLoc.isMacroID()) {
55 IoStateLoc = SM.getSpellingLoc(IoStateLoc);
56 Fix = false;
59 SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
61 if (Replacement) {
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;
67 if (Fix)
68 Builder << FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc),
69 FixName);
70 } else
71 diag(IoStateLoc, "'std::ios_base::%0' is deprecated") << TypeName;
74 } // namespace modernize
75 } // namespace tidy
76 } // namespace clang