clang-tools  15.0.0git
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 
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 
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 
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 
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  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;
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
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::clangd::check
bool check(llvm::StringRef File, llvm::Optional< Range > LineRange, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:273
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))
clang::ast_matchers
Definition: AbseilMatcher.h:14
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::tidy::modernize::DeprecatedTypes
static constexpr std::array< StringRef, 5 > DeprecatedTypes
Definition: DeprecatedIosBaseAliasesCheck.cpp:19
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
DeprecatedIosBaseAliasesCheck.h
clang::tidy::modernize::getReplacementType
static llvm::Optional< const char * > getReplacementType(StringRef Type)
Definition: DeprecatedIosBaseAliasesCheck.cpp:24