clang-tools 10.0.0

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
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
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
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
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::modernize::ReplacementTypes
static const llvm::StringMap< StringRef > ReplacementTypes
Definition: DeprecatedIosBaseAliasesCheck.cpp:26
clang::tidy::modernize::DeprecatedTypes
static const llvm::SmallVector< StringRef, 5 > DeprecatedTypes
Definition: DeprecatedIosBaseAliasesCheck.cpp:19
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
DeprecatedIosBaseAliasesCheck.h
Fix
static cl::opt< bool > Fix("fix", cl::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))