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 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
18 
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"};
23 
getReplacementType(StringRef Type)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);
30 }
31 
registerMatchers(MatchFinder * Finder)32 void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) {
33   auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl");
34   auto IoStateType =
35       qualType(hasDeclaration(IoStateDecl), unless(elaboratedType()));
36 
37   Finder->addMatcher(typeLoc(loc(IoStateType)).bind("TypeLoc"), this);
38 }
39 
check(const MatchFinder::MatchResult & Result)40 void DeprecatedIosBaseAliasesCheck::check(
41     const MatchFinder::MatchResult &Result) {
42   SourceManager &SM = *Result.SourceManager;
43 
44   const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("TypeDecl");
45   StringRef TypeName = Typedef->getName();
46   auto Replacement = getReplacementType(TypeName);
47 
48   const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("TypeLoc");
49   SourceLocation IoStateLoc = TL->getBeginLoc();
50 
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;
57   }
58 
59   SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
60 
61   if (Replacement) {
62     auto FixName = *Replacement;
63     auto Builder = diag(IoStateLoc, "'std::ios_base::%0' is deprecated; use "
64                                     "'std::ios_base::%1' instead")
65                    << TypeName << FixName;
66 
67     if (Fix)
68       Builder << FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc),
69                                               FixName);
70   } else
71     diag(IoStateLoc, "'std::ios_base::%0' is deprecated") << TypeName;
72 }
73 
74 } // namespace modernize
75 } // namespace tidy
76 } // namespace clang
77