|
| 1 | +//===--- UnintendedCharOstreamOutputCheck.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 "UnintendedCharOstreamOutputCheck.h" |
| 10 | +#include "clang/AST/Type.h" |
| 11 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | +#include "clang/Basic/Diagnostic.h" |
| 14 | +#include "clang/Tooling/FixIt.h" |
| 15 | + |
| 16 | +using namespace clang::ast_matchers; |
| 17 | + |
| 18 | +namespace clang::tidy::bugprone { |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +// check if the type is unsigned char or signed char |
| 23 | +AST_MATCHER(Type, isNumericChar) { |
| 24 | + return Node.isSpecificBuiltinType(BuiltinType::SChar) || |
| 25 | + Node.isSpecificBuiltinType(BuiltinType::UChar); |
| 26 | +} |
| 27 | + |
| 28 | +// check if the type is char |
| 29 | +AST_MATCHER(Type, isChar) { |
| 30 | + return Node.isSpecificBuiltinType(BuiltinType::Char_S) || |
| 31 | + Node.isSpecificBuiltinType(BuiltinType::Char_U); |
| 32 | +} |
| 33 | + |
| 34 | +} // namespace |
| 35 | + |
| 36 | +UnintendedCharOstreamOutputCheck::UnintendedCharOstreamOutputCheck( |
| 37 | + StringRef Name, ClangTidyContext *Context) |
| 38 | + : ClangTidyCheck(Name, Context), CastTypeName(Options.get("CastTypeName")) { |
| 39 | +} |
| 40 | +void UnintendedCharOstreamOutputCheck::storeOptions( |
| 41 | + ClangTidyOptions::OptionMap &Opts) { |
| 42 | + if (CastTypeName.has_value()) |
| 43 | + Options.store(Opts, "CastTypeName", CastTypeName.value()); |
| 44 | +} |
| 45 | + |
| 46 | +void UnintendedCharOstreamOutputCheck::registerMatchers(MatchFinder *Finder) { |
| 47 | + auto BasicOstream = |
| 48 | + cxxRecordDecl(hasName("::std::basic_ostream"), |
| 49 | + // only basic_ostream<char, Traits> has overload operator<< |
| 50 | + // with char / unsigned char / signed char |
| 51 | + classTemplateSpecializationDecl( |
| 52 | + hasTemplateArgument(0, refersToType(isChar())))); |
| 53 | + Finder->addMatcher( |
| 54 | + cxxOperatorCallExpr( |
| 55 | + hasOverloadedOperatorName("<<"), |
| 56 | + hasLHS(hasType(hasUnqualifiedDesugaredType( |
| 57 | + recordType(hasDeclaration(cxxRecordDecl( |
| 58 | + anyOf(BasicOstream, isDerivedFrom(BasicOstream)))))))), |
| 59 | + hasRHS(hasType(hasUnqualifiedDesugaredType(isNumericChar())))) |
| 60 | + .bind("x"), |
| 61 | + this); |
| 62 | +} |
| 63 | + |
| 64 | +void UnintendedCharOstreamOutputCheck::check( |
| 65 | + const MatchFinder::MatchResult &Result) { |
| 66 | + const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("x"); |
| 67 | + const Expr *Value = Call->getArg(1); |
| 68 | + const SourceRange SourceRange = Value->getSourceRange(); |
| 69 | + |
| 70 | + DiagnosticBuilder Builder = |
| 71 | + diag(Call->getOperatorLoc(), |
| 72 | + "%0 passed to 'operator<<' outputs as character instead of integer. " |
| 73 | + "cast to 'unsigned int' to print numeric value or cast to 'char' to " |
| 74 | + "print as character") |
| 75 | + << Value->getType() << SourceRange; |
| 76 | + |
| 77 | + QualType T = Value->getType(); |
| 78 | + const Type *UnqualifiedDesugaredType = T->getUnqualifiedDesugaredType(); |
| 79 | + |
| 80 | + llvm::StringRef CastType = CastTypeName.value_or( |
| 81 | + UnqualifiedDesugaredType->isSpecificBuiltinType(BuiltinType::SChar) |
| 82 | + ? "int" |
| 83 | + : "unsigned int"); |
| 84 | + |
| 85 | + Builder << FixItHint::CreateReplacement( |
| 86 | + SourceRange, ("static_cast<" + CastType + ">(" + |
| 87 | + tooling::fixit::getText(*Value, *Result.Context) + ")") |
| 88 | + .str()); |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace clang::tidy::bugprone |
0 commit comments