-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Replace deprecated std::ios_base aliases
This check warns the uses of the deprecated member types of std::ios_base and replaces those that have a non-deprecated equivalent. Patch by andobence! Reviewd by: alexfh Revision ID: https://reviews.llvm.org/D51332 llvm-svn: 343848
- Loading branch information
Showing
8 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //===--- DeprecatedIosBaseAliasesCheck.cpp - clang-tidy--------------------===// | ||
| // | ||
| // The LLVM Compiler Infrastructure | ||
| // | ||
| // This file is distributed under the University of Illinois Open Source | ||
| // License. See LICENSE.TXT for details. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "DeprecatedIosBaseAliasesCheck.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
|
||
| using namespace clang::ast_matchers; | ||
|
|
||
| namespace clang { | ||
| namespace tidy { | ||
| namespace modernize { | ||
|
|
||
| static const std::array<StringRef, 5> DeprecatedTypes = { | ||
| "::std::ios_base::io_state", "::std::ios_base::open_mode", | ||
| "::std::ios_base::seek_dir", "::std::ios_base::streamoff", | ||
| "::std::ios_base::streampos", | ||
| }; | ||
|
|
||
| static const llvm::StringMap<StringRef> ReplacementTypes = { | ||
| {"io_state", "iostate"}, | ||
| {"open_mode", "openmode"}, | ||
| {"seek_dir", "seekdir"}}; | ||
|
|
||
| void DeprecatedIosBaseAliasesCheck::registerMatchers(MatchFinder *Finder) { | ||
| // Only register the matchers for C++; the functionality currently does not | ||
| // provide any benefit to other languages, despite being benign. | ||
| if (!getLangOpts().CPlusPlus) | ||
| return; | ||
|
|
||
| auto IoStateDecl = typedefDecl(hasAnyName(DeprecatedTypes)).bind("TypeDecl"); | ||
| auto IoStateType = | ||
| qualType(hasDeclaration(IoStateDecl), unless(elaboratedType())); | ||
|
|
||
| Finder->addMatcher(typeLoc(loc(IoStateType)).bind("TypeLoc"), this); | ||
| } | ||
|
|
||
| void DeprecatedIosBaseAliasesCheck::check( | ||
| const MatchFinder::MatchResult &Result) { | ||
| SourceManager &SM = *Result.SourceManager; | ||
|
|
||
| const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("TypeDecl"); | ||
| StringRef TypeName = Typedef->getName(); | ||
| bool HasReplacement = ReplacementTypes.count(TypeName); | ||
|
|
||
| const auto *TL = Result.Nodes.getNodeAs<TypeLoc>("TypeLoc"); | ||
| SourceLocation IoStateLoc = TL->getBeginLoc(); | ||
|
|
||
| // Do not generate fixits for matches depending on template arguments and | ||
| // macro expansions. | ||
| bool Fix = HasReplacement && !TL->getType()->isDependentType(); | ||
| if (IoStateLoc.isMacroID()) { | ||
| IoStateLoc = SM.getSpellingLoc(IoStateLoc); | ||
| Fix = false; | ||
| } | ||
|
|
||
| SourceLocation EndLoc = IoStateLoc.getLocWithOffset(TypeName.size() - 1); | ||
|
|
||
| if (HasReplacement) { | ||
| auto FixName = ReplacementTypes.lookup(TypeName); | ||
| auto Builder = diag(IoStateLoc, "'std::ios_base::%0' is deprecated; use " | ||
| "'std::ios_base::%1' instead") | ||
| << TypeName << FixName; | ||
|
|
||
| if (Fix) | ||
| Builder << FixItHint::CreateReplacement(SourceRange(IoStateLoc, EndLoc), | ||
| FixName); | ||
| } else | ||
| diag(IoStateLoc, "'std::ios_base::%0' is deprecated") << TypeName; | ||
| } | ||
|
|
||
| } // namespace modernize | ||
| } // namespace tidy | ||
| } // namespace clang |
36 changes: 36 additions & 0 deletions
36
clang-tools-extra/clang-tidy/modernize/DeprecatedIosBaseAliasesCheck.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //===--- DeprecatedIosBaseAliasesCheck.h - clang-tidy------------*- C++ -*-===// | ||
| // | ||
| // The LLVM Compiler Infrastructure | ||
| // | ||
| // This file is distributed under the University of Illinois Open Source | ||
| // License. See LICENSE.TXT for details. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_DEPRECATEDIOSBASEALIASESCHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_DEPRECATEDIOSBASEALIASESCHECK_H | ||
|
|
||
| #include "../ClangTidy.h" | ||
|
|
||
| namespace clang { | ||
| namespace tidy { | ||
| namespace modernize { | ||
|
|
||
| /// This check warns the uses of the deprecated member types of ``std::ios_base`` | ||
| /// and replaces those that have a non-deprecated equivalent. | ||
| /// | ||
| /// For the user-facing documentation see: | ||
| /// http://clang.llvm.org/extra/clang-tidy/checks/modernize-deprecated-ios-base-aliases.html | ||
| class DeprecatedIosBaseAliasesCheck : public ClangTidyCheck { | ||
| public: | ||
| DeprecatedIosBaseAliasesCheck(StringRef Name, ClangTidyContext *Context) | ||
| : ClangTidyCheck(Name, Context) {} | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
| }; | ||
|
|
||
| } // namespace modernize | ||
| } // namespace tidy | ||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_DEPRECATEDIOSBASEALIASESCHECK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
clang-tools-extra/docs/clang-tidy/checks/modernize-deprecated-ios-base-aliases.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .. title:: clang-tidy - modernize-deprecated-ios-base-aliases | ||
|
|
||
| modernize-deprecated-ios-base-aliases | ||
| ===================================== | ||
|
|
||
| This check warns the uses of the deprecated member types of ``std::ios_base`` | ||
| and replaces those that have a non-deprecated equivalent. | ||
|
|
||
| =================================== =========================== | ||
| Deprecated member type Replacement | ||
| =================================== =========================== | ||
| ``std::ios_base::io_state`` ``std::ios_base::iostate`` | ||
| ``std::ios_base::open_mode`` ``std::ios_base::openmode`` | ||
| ``std::ios_base::seek_dir`` ``std::ios_base::seekdir`` | ||
| ``std::ios_base::streamoff`` | ||
| ``std::ios_base::streampos`` | ||
| =================================== =========================== |
Oops, something went wrong.