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 const llvm::SmallVector<StringRef, 5> DeprecatedTypes = {
20     {"::std::ios_base::io_state"},
21     {"::std::ios_base::open_mode"},
22     {"::std::ios_base::seek_dir"},
23     {"::std::ios_base::streamoff"},
24     {"::std::ios_base::streampos"}};
25 
26 static const llvm::StringMap<StringRef> ReplacementTypes = {
27     {"io_state", "iostate"},
28     {"open_mode", "openmode"},
29     {"seek_dir", "seekdir"}};
30 
registerMatchers(MatchFinder * Finder)31 void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) {
32   // Only register the matchers for C++; the functionality currently does not
33   // provide any benefit to other languages, despite being benign.
34   if (!getLangOpts().CPlusPlus)
35     return;
36 
37   auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl");
38   auto IoStateType =
39       qualType(hasDeclaration(IoStateDecl), unless(elaboratedType()));
40 
41   Finder->addMatcher(typeLoc(loc(IoStateType)).bind("TypeLoc"), this);
42 }
43 
check(const MatchFinder::MatchResult & Result)44 void DeprecatedIosBaseAliasesCheck::check(
45     const MatchFinder::MatchResult &Result) {
46   SourceManager &SM = *Result.SourceManager;
47 
48   const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("TypeDecl");
49   StringRef TypeName = Typedef->getName();
50   bool HasReplacement = ReplacementTypes.count(TypeName);
51 
52   const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("TypeLoc");
53   SourceLocation IoStateLoc = TL->getBeginLoc();
54 
55   // Do not generate fixits for matches depending on template arguments and
56   // macro expansions.
57   bool Fix = HasReplacement && !TL->getType()->isDependentType();
58   if (IoStateLoc.isMacroID()) {
59     IoStateLoc = SM.getSpellingLoc(IoStateLoc);
60     Fix = false;
61   }
62 
63   SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
64 
65   if (HasReplacement) {
66     auto FixName = ReplacementTypes.lookup(TypeName);
67     auto Builder = diag(IoStateLoc, "'std::ios_base::%0' is deprecated; use "
68                                     "'std::ios_base::%1' instead")
69                    << TypeName << FixName;
70 
71     if (Fix)
72       Builder << FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc),
73                                               FixName);
74   } else
75     diag(IoStateLoc, "'std::ios_base::%0' is deprecated") << TypeName;
76 }
77 
78 } // namespace modernize
79 } // namespace tidy
80 } // namespace clang
81