1d0794365SJonas Toth //===--- DeprecatedIosBaseAliasesCheck.cpp - clang-tidy--------------------===//
2d0794365SJonas Toth //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d0794365SJonas Toth //
7d0794365SJonas Toth //===----------------------------------------------------------------------===//
8d0794365SJonas Toth
9d0794365SJonas Toth #include "DeprecatedIosBaseAliasesCheck.h"
10d0794365SJonas Toth #include "clang/AST/ASTContext.h"
11d0794365SJonas Toth #include "clang/ASTMatchers/ASTMatchFinder.h"
1271f55735SKazu Hirata #include <optional>
13d0794365SJonas Toth
14d0794365SJonas Toth using namespace clang::ast_matchers;
15d0794365SJonas Toth
16*7d2ea6c4SCarlos Galvez namespace clang::tidy::modernize {
17d0794365SJonas Toth
185fc5c7dbSBenjamin Kramer static constexpr std::array<StringRef, 5> DeprecatedTypes = {
195fc5c7dbSBenjamin Kramer "::std::ios_base::io_state", "::std::ios_base::open_mode",
205fc5c7dbSBenjamin Kramer "::std::ios_base::seek_dir", "::std::ios_base::streamoff",
215fc5c7dbSBenjamin Kramer "::std::ios_base::streampos"};
22d0794365SJonas Toth
getReplacementType(StringRef Type)23f71ffd3bSKazu Hirata static std::optional<const char *> getReplacementType(StringRef Type) {
24f71ffd3bSKazu Hirata return llvm::StringSwitch<std::optional<const char *>>(Type)
25c7581815SBenjamin Kramer .Case("io_state", "iostate")
26c7581815SBenjamin Kramer .Case("open_mode", "openmode")
27c7581815SBenjamin Kramer .Case("seek_dir", "seekdir")
28cd8702efSKazu Hirata .Default(std::nullopt);
29c7581815SBenjamin Kramer }
30d0794365SJonas Toth
registerMatchers(MatchFinder * Finder)31d0794365SJonas Toth void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) {
32d0794365SJonas Toth auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl");
33d0794365SJonas Toth auto IoStateType =
34d0794365SJonas Toth qualType(hasDeclaration(IoStateDecl), unless(elaboratedType()));
35d0794365SJonas Toth
36d0794365SJonas Toth Finder->addMatcher(typeLoc(loc(IoStateType)).bind("TypeLoc"), this);
37d0794365SJonas Toth }
38d0794365SJonas Toth
check(const MatchFinder::MatchResult & Result)39d0794365SJonas Toth void DeprecatedIosBaseAliasesCheck::check(
40d0794365SJonas Toth const MatchFinder::MatchResult &Result) {
41d0794365SJonas Toth SourceManager &SM = *Result.SourceManager;
42d0794365SJonas Toth
43d0794365SJonas Toth const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("TypeDecl");
44d0794365SJonas Toth StringRef TypeName = Typedef->getName();
45c7581815SBenjamin Kramer auto Replacement = getReplacementType(TypeName);
46d0794365SJonas Toth
47d0794365SJonas Toth const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("TypeLoc");
48d0794365SJonas Toth SourceLocation IoStateLoc = TL->getBeginLoc();
49d0794365SJonas Toth
50d0794365SJonas Toth // Do not generate fixits for matches depending on template arguments and
51d0794365SJonas Toth // macro expansions.
52c7581815SBenjamin Kramer bool Fix = Replacement && !TL->getType()->isDependentType();
53d0794365SJonas Toth if (IoStateLoc.isMacroID()) {
54d0794365SJonas Toth IoStateLoc = SM.getSpellingLoc(IoStateLoc);
55d0794365SJonas Toth Fix = false;
56d0794365SJonas Toth }
57d0794365SJonas Toth
58d0794365SJonas Toth SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1);
59d0794365SJonas Toth
60c7581815SBenjamin Kramer if (Replacement) {
61ab2d3ce4SAlexander Kornienko const char *FixName = *Replacement;
62d0794365SJonas Toth auto Builder = diag(IoStateLoc, "'std::ios_base::%0' is deprecated; use "
63d0794365SJonas Toth "'std::ios_base::%1' instead")
64d0794365SJonas Toth << TypeName << FixName;
65d0794365SJonas Toth
66d0794365SJonas Toth if (Fix)
67d0794365SJonas Toth Builder << FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc),
68d0794365SJonas Toth FixName);
69d0794365SJonas Toth } else
70d0794365SJonas Toth diag(IoStateLoc, "'std::ios_base::%0' is deprecated") << TypeName;
71d0794365SJonas Toth }
72d0794365SJonas Toth
73*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::modernize
74